mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 21:52:54 +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]>
135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package encryption
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
var (
|
|
defaultKeyIDs = []string{
|
|
"domainVerificationKey",
|
|
"idpConfigKey",
|
|
"oidcKey",
|
|
"samlKey",
|
|
"otpKey",
|
|
"smsKey",
|
|
"smtpKey",
|
|
"userKey",
|
|
"targetKey",
|
|
"csrfCookieKey",
|
|
"userAgentCookieKey",
|
|
}
|
|
)
|
|
|
|
type EncryptionKeyConfig struct {
|
|
DomainVerification *crypto.KeyConfig
|
|
IDPConfig *crypto.KeyConfig
|
|
OIDC *crypto.KeyConfig
|
|
SAML *crypto.KeyConfig
|
|
OTP *crypto.KeyConfig
|
|
SMS *crypto.KeyConfig
|
|
SMTP *crypto.KeyConfig
|
|
User *crypto.KeyConfig
|
|
Target *crypto.KeyConfig
|
|
CSRFCookieKeyID string
|
|
UserAgentCookieKeyID string
|
|
}
|
|
|
|
type EncryptionKeys struct {
|
|
DomainVerification crypto.EncryptionAlgorithm
|
|
IDPConfig crypto.AuthEncryptionAlgorithm
|
|
OIDC crypto.AuthEncryptionAlgorithm
|
|
SAML crypto.EncryptionAlgorithm
|
|
OTP crypto.EncryptionAlgorithm
|
|
SMS crypto.EncryptionAlgorithm
|
|
SMTP crypto.EncryptionAlgorithm
|
|
User crypto.EncryptionAlgorithm
|
|
Target crypto.EncryptionAlgorithm
|
|
CSRFCookieKey []byte
|
|
UserAgentCookieKey []byte
|
|
OIDCKey []byte
|
|
}
|
|
|
|
func EnsureEncryptionKeys(ctx context.Context, keyConfig *EncryptionKeyConfig, keyStorage crypto.KeyStorage) (keys *EncryptionKeys, err error) {
|
|
if err := VerifyDefaultKeys(ctx, keyStorage); err != nil {
|
|
return nil, err
|
|
}
|
|
keys = new(EncryptionKeys)
|
|
keys.DomainVerification, err = crypto.NewAESCrypto(keyConfig.DomainVerification, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.IDPConfig, err = crypto.NewAESCrypto(keyConfig.IDPConfig, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.OIDC, err = crypto.NewAESCrypto(keyConfig.OIDC, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.SAML, err = crypto.NewAESCrypto(keyConfig.SAML, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
key, err := crypto.LoadKey(keyConfig.OIDC.EncryptionKeyID, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.OIDCKey = []byte(key)
|
|
keys.OTP, err = crypto.NewAESCrypto(keyConfig.OTP, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.SMS, err = crypto.NewAESCrypto(keyConfig.SMS, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.SMTP, err = crypto.NewAESCrypto(keyConfig.SMTP, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.User, err = crypto.NewAESCrypto(keyConfig.User, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.Target, err = crypto.NewAESCrypto(keyConfig.Target, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
key, err = crypto.LoadKey(keyConfig.CSRFCookieKeyID, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.CSRFCookieKey = []byte(key)
|
|
key, err = crypto.LoadKey(keyConfig.UserAgentCookieKeyID, keyStorage)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keys.UserAgentCookieKey = []byte(key)
|
|
return keys, nil
|
|
}
|
|
|
|
func VerifyDefaultKeys(ctx context.Context, keyStorage crypto.KeyStorage) (err error) {
|
|
keys := make([]*crypto.Key, 0, len(defaultKeyIDs))
|
|
for _, keyID := range defaultKeyIDs {
|
|
_, err := crypto.LoadKey(keyID, keyStorage)
|
|
if err == nil {
|
|
continue
|
|
}
|
|
key, err := crypto.NewKey(keyID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
keys = append(keys, key)
|
|
}
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
if err := keyStorage.CreateKeys(ctx, keys...); err != nil {
|
|
return zerrors.ThrowInternal(err, "START-aGBq2", "cannot create default keys")
|
|
}
|
|
return nil
|
|
}
|