mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
* fix(crypto): use authenticated encryption for IDP intent tokens IDP intent tokens were encrypted with AES-CFB which provides no integrity protection, making them malleable. Switch to A256GCM via the existing AuthAlgorithm interface (EncryptToken/DecryptToken), the same approach already used for OIDC and session tokens. The idpConfigEncryption key type is widened from EncryptionAlgorithm to AuthEncryptionAlgorithm so that data-at-rest encryption (IDP client secrets, LDAP bind passwords, SAML keys) continues to use the CFB path unmodified. Co-authored-by: Cursor <[email protected]> * fix(proto): raise IDP intent token max length to 500 for authenticated tokens The switch to authenticated A256GCM (JWE compact serialization) makes IDP intent tokens longer (~270 chars) than the previous AES-CFB base64 output. Raise the max_len/max_length validation and OpenAPI constraints on every idp_intent_token field from 200 to 500 so the new tokens are accepted: user v2/v2beta (idp + user_service), session v2/v2beta, and resources user v3alpha. Co-Authored-By: Claude Opus 4.8 <[email protected]> --------- Co-authored-by: Cursor <[email protected]> Co-authored-by: Claude Opus 4.8 <[email protected]>
176 lines
5.6 KiB
Go
176 lines
5.6 KiB
Go
package crypto
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"slices"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
const (
|
|
TypeEncryption CryptoType = iota
|
|
TypeHash // Depcrecated: use [passwap.Swapper] instead
|
|
)
|
|
|
|
//go:generate mockgen -typed -package crypto -destination ./crypto.mock.go . EncryptionAlgorithm,AuthEncryptionAlgorithm
|
|
|
|
// EncryptionAlgorithm is an interface that defines the methods for encrypting and decrypting values,
|
|
// used for sensitive data stored in the database.
|
|
// These methods are not intended to be used for encrypting data that is sent to the client,
|
|
// for this use the [AuthAlgorithm] instead.
|
|
type EncryptionAlgorithm interface {
|
|
Algorithm() string
|
|
EncryptionKeyID() string
|
|
DecryptionKeyIDs() []string
|
|
Encrypt(value []byte) ([]byte, error)
|
|
Decrypt(hashed []byte, keyID string) ([]byte, error)
|
|
|
|
// DecryptString decrypts the value using the key identified by keyID.
|
|
// When the decrypted value contains non-UTF8 characters an error is returned.
|
|
DecryptString(hashed []byte, keyID string) (string, error)
|
|
}
|
|
|
|
// AuthAlgorithm is an interface that defines the methods for encrypting and decrypting tokens,
|
|
// used for sensitive data sent to the client, such as access tokens and refresh tokens.
|
|
// Implementation must obfuscate and authenticate the token.
|
|
type AuthAlgorithm interface {
|
|
// EncryptToken encrypts the token using authenticated encryption with the current encryption key.
|
|
EncryptToken(data string) (string, error)
|
|
|
|
// DecryptToken authenticates and decrypts the token using the encryption key.
|
|
// When the decrypted value contains non-UTF8 characters an error is returned.
|
|
DecryptToken(token string) (string, error)
|
|
LegacyTokenEnabled() bool
|
|
}
|
|
|
|
type AuthEncryptionAlgorithm interface {
|
|
EncryptionAlgorithm
|
|
AuthAlgorithm
|
|
}
|
|
|
|
// CryptoValue is a struct that can be used to store encrypted values in a database.
|
|
// The struct is compatible with the [driver.Valuer] and database/sql.Scanner interfaces.
|
|
type CryptoValue struct {
|
|
CryptoType CryptoType
|
|
Algorithm string
|
|
KeyID string
|
|
Crypted []byte
|
|
}
|
|
|
|
func (c *CryptoValue) Value() (driver.Value, error) {
|
|
if c == nil {
|
|
return nil, nil
|
|
}
|
|
return json.Marshal(c)
|
|
}
|
|
|
|
func (c *CryptoValue) Scan(src any) error {
|
|
if b, ok := src.([]byte); ok {
|
|
return json.Unmarshal(b, c)
|
|
}
|
|
if s, ok := src.(string); ok {
|
|
return json.Unmarshal([]byte(s), c)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type CryptoType int
|
|
|
|
func Crypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
|
return Encrypt(value, alg)
|
|
}
|
|
|
|
func Encrypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
|
if alg == nil {
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "CRYPT-03ltQd", "input encryption algorithm cannot be nil")
|
|
}
|
|
encrypted, err := alg.Encrypt(value)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "CRYPT-qCD0JB", "error encrypting value")
|
|
}
|
|
return &CryptoValue{
|
|
CryptoType: TypeEncryption,
|
|
Algorithm: alg.Algorithm(),
|
|
KeyID: alg.EncryptionKeyID(),
|
|
Crypted: encrypted,
|
|
}, nil
|
|
}
|
|
|
|
func EncryptJSON(obj any, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
|
data, err := json.Marshal(obj)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "CRYPT-Ei6doF", "error encrypting value")
|
|
}
|
|
return Encrypt(data, alg)
|
|
}
|
|
|
|
func Decrypt(value *CryptoValue, alg EncryptionAlgorithm) ([]byte, error) {
|
|
if err := checkEncryptionAlgorithm(value, alg); err != nil {
|
|
return nil, err
|
|
}
|
|
return alg.Decrypt(value.Crypted, value.KeyID)
|
|
}
|
|
|
|
func DecryptJSON(value *CryptoValue, dst any, alg EncryptionAlgorithm) error {
|
|
data, err := Decrypt(value, alg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = json.Unmarshal(data, dst); err != nil {
|
|
return zerrors.ThrowInternal(err, "CRYPT-Jaik2R", "error decrypting value")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DecryptString decrypts the value using the key identified by keyID.
|
|
// When the decrypted value contains non-UTF8 characters an error is returned.
|
|
func DecryptString(value *CryptoValue, alg EncryptionAlgorithm) (string, error) {
|
|
if err := checkEncryptionAlgorithm(value, alg); err != nil {
|
|
return "", err
|
|
}
|
|
return alg.DecryptString(value.Crypted, value.KeyID)
|
|
}
|
|
|
|
func checkEncryptionAlgorithm(value *CryptoValue, alg EncryptionAlgorithm) error {
|
|
if value == nil {
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-mNsQwe", "input value cannot be nil")
|
|
}
|
|
if alg == nil {
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-paiRey", "input encryption algorithm cannot be nil")
|
|
}
|
|
if value.Algorithm != alg.Algorithm() {
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
|
|
}
|
|
if slices.Contains(alg.DecryptionKeyIDs(), value.KeyID) {
|
|
return nil
|
|
}
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
|
|
}
|
|
|
|
func CheckToken(alg AuthAlgorithm, token string, content string) error {
|
|
if token == "" {
|
|
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-Sfefs", "Errors.Intent.InvalidToken")
|
|
}
|
|
if alg == nil {
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-edCJsp", "input encryption algorithm cannot be nil")
|
|
}
|
|
decryptedToken, err := alg.DecryptToken(token)
|
|
if err != nil {
|
|
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Sf4gt", "Errors.Intent.InvalidToken")
|
|
}
|
|
if decryptedToken != content {
|
|
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-CRYPTO", "Errors.Intent.InvalidToken")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SecretOrEncodedHash returns the Crypted value from legacy [CryptoValue] if it is not nil.
|
|
// otherwise it will returns the encoded hash string.
|
|
func SecretOrEncodedHash(secret *CryptoValue, encoded string) string {
|
|
if secret != nil {
|
|
return string(secret.Crypted)
|
|
}
|
|
return encoded
|
|
}
|