mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
64a3bb3149
# Which Problems Are Solved Implement a new API service that allows management of OIDC signing web keys. This allows users to manage rotation of the instance level keys. which are currently managed based on expiry. The API accepts the generation of the following key types and parameters: - RSA keys with 2048, 3072 or 4096 bit in size and: - Signing with SHA-256 (RS256) - Signing with SHA-384 (RS384) - Signing with SHA-512 (RS512) - ECDSA keys with - P256 curve - P384 curve - P512 curve - ED25519 keys # How the Problems Are Solved Keys are serialized for storage using the JSON web key format from the `jose` library. This is the format that will be used by OIDC for signing, verification and publication. Each instance can have a number of key pairs. All existing public keys are meant to be used for token verification and publication the keys endpoint. Keys can be activated and the active private key is meant to sign new tokens. There is always exactly 1 active signing key: 1. When the first key for an instance is generated, it is automatically activated. 2. Activation of the next key automatically deactivates the previously active key. 3. Keys cannot be manually deactivated from the API 4. Active keys cannot be deleted # Additional Changes - Query methods that later will be used by the OIDC package are already implemented. Preparation for #8031 - Fix indentation in french translation for instance event - Move user_schema translations to consistent positions in all translation files # Additional Context - Closes #8030 - Part of #7809 --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
144 lines
4.0 KiB
Go
144 lines
4.0 KiB
Go
package crypto
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
const (
|
|
TypeEncryption CryptoType = iota
|
|
TypeHash // Depcrecated: use [passwap.Swapper] 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)
|
|
}
|
|
|
|
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 interface{}) 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) {
|
|
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.Algorithm != alg.Algorithm() {
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
|
|
}
|
|
for _, id := range alg.DecryptionKeyIDs() {
|
|
if id == value.KeyID {
|
|
return nil
|
|
}
|
|
}
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
|
|
}
|
|
|
|
func CheckToken(alg EncryptionAlgorithm, token string, content string) error {
|
|
if token == "" {
|
|
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-Sfefs", "Errors.Intent.InvalidToken")
|
|
}
|
|
data, err := base64.RawURLEncoding.DecodeString(token)
|
|
if err != nil {
|
|
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Swg31", "Errors.Intent.InvalidToken")
|
|
}
|
|
decryptedToken, err := alg.DecryptString(data, alg.EncryptionKeyID())
|
|
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
|
|
}
|