2020-03-23 06:06:44 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2020-06-05 05:50:04 +00:00
|
|
|
"database/sql/driver"
|
2023-06-21 14:06:18 +00:00
|
|
|
"encoding/base64"
|
2020-06-05 05:50:04 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-03-23 06:06:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypeEncryption CryptoType = iota
|
2023-07-14 06:49:57 +00:00
|
|
|
TypeHash // Depcrecated: use [passwap.Swapper] instead
|
2020-03-23 06:06:44 +00:00
|
|
|
)
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
type EncryptionAlgorithm interface {
|
2024-04-05 09:35:49 +00:00
|
|
|
Algorithm() string
|
2020-03-23 06:06:44 +00:00
|
|
|
EncryptionKeyID() string
|
|
|
|
DecryptionKeyIDs() []string
|
|
|
|
Encrypt(value []byte) ([]byte, error)
|
|
|
|
Decrypt(hashed []byte, keyID string) ([]byte, error)
|
|
|
|
DecryptString(hashed []byte, keyID string) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CryptoValue struct {
|
|
|
|
CryptoType CryptoType
|
|
|
|
Algorithm string
|
|
|
|
KeyID string
|
|
|
|
Crypted []byte
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-23 06:06:44 +00:00
|
|
|
type CryptoType int
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
func Crypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
|
|
|
return Encrypt(value, alg)
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
func Encrypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
2020-03-23 06:06:44 +00:00
|
|
|
encrypted, err := alg.Encrypt(value)
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "CRYPT-qCD0JB", "error encrypting value")
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|
|
|
|
return &CryptoValue{
|
|
|
|
CryptoType: TypeEncryption,
|
|
|
|
Algorithm: alg.Algorithm(),
|
|
|
|
KeyID: alg.EncryptionKeyID(),
|
|
|
|
Crypted: encrypted,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
func Decrypt(value *CryptoValue, alg EncryptionAlgorithm) ([]byte, error) {
|
|
|
|
if err := checkEncryptionAlgorithm(value, alg); err != nil {
|
2020-03-23 06:06:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return alg.Decrypt(value.Crypted, value.KeyID)
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
func DecryptString(value *CryptoValue, alg EncryptionAlgorithm) (string, error) {
|
|
|
|
if err := checkEncryptionAlgorithm(value, alg); err != nil {
|
2020-03-23 06:06:44 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return alg.DecryptString(value.Crypted, value.KeyID)
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
func checkEncryptionAlgorithm(value *CryptoValue, alg EncryptionAlgorithm) error {
|
2020-03-23 06:06:44 +00:00
|
|
|
if value.Algorithm != alg.Algorithm() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|
|
|
|
for _, id := range alg.DecryptionKeyIDs() {
|
|
|
|
if id == value.KeyID {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 14:06:18 +00:00
|
|
|
func CheckToken(alg EncryptionAlgorithm, token string, content string) error {
|
|
|
|
if token == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-Sfefs", "Errors.Intent.InvalidToken")
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
|
|
|
data, err := base64.RawURLEncoding.DecodeString(token)
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Swg31", "Errors.Intent.InvalidToken")
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
|
|
|
decryptedToken, err := alg.DecryptString(data, alg.EncryptionKeyID())
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Sf4gt", "Errors.Intent.InvalidToken")
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
|
|
|
if decryptedToken != content {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-CRYPTO", "Errors.Intent.InvalidToken")
|
2023-06-21 14:06:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-05 09:35:49 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|