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
|
|
|
)
|
|
|
|
|
|
|
|
type Crypto interface {
|
|
|
|
Algorithm() string
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
type EncryptionAlgorithm interface {
|
2020-03-23 06:06:44 +00:00
|
|
|
Crypto
|
|
|
|
EncryptionKeyID() string
|
|
|
|
DecryptionKeyIDs() []string
|
|
|
|
Encrypt(value []byte) ([]byte, error)
|
|
|
|
Decrypt(hashed []byte, keyID string) ([]byte, error)
|
|
|
|
DecryptString(hashed []byte, keyID string) (string, error)
|
|
|
|
}
|
|
|
|
|
2023-07-14 06:49:57 +00:00
|
|
|
// Depcrecated: use [passwap.Swapper] instead
|
2020-03-30 07:28:00 +00:00
|
|
|
type HashAlgorithm interface {
|
2020-03-23 06:06:44 +00:00
|
|
|
Crypto
|
|
|
|
Hash(value []byte) ([]byte, error)
|
|
|
|
CompareHash(hashed, comparer []byte) 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
|
|
|
|
|
|
|
|
func Crypt(value []byte, c Crypto) (*CryptoValue, error) {
|
|
|
|
switch alg := c.(type) {
|
2020-03-30 07:28:00 +00:00
|
|
|
case EncryptionAlgorithm:
|
2020-03-23 06:06:44 +00:00
|
|
|
return Encrypt(value, alg)
|
2020-03-30 07:28:00 +00:00
|
|
|
case HashAlgorithm:
|
2020-03-23 06:06:44 +00:00
|
|
|
return Hash(value, alg)
|
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "CRYPT-r4IaHZ", "algorithm not supported")
|
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
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
func Hash(value []byte, alg HashAlgorithm) (*CryptoValue, error) {
|
2020-03-23 06:06:44 +00:00
|
|
|
hashed, err := alg.Hash(value)
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "CRYPT-rBVaJU", "error hashing value")
|
2020-03-23 06:06:44 +00:00
|
|
|
}
|
|
|
|
return &CryptoValue{
|
|
|
|
CryptoType: TypeHash,
|
|
|
|
Algorithm: alg.Algorithm(),
|
|
|
|
Crypted: hashed,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:28:00 +00:00
|
|
|
func CompareHash(value *CryptoValue, comparer []byte, alg HashAlgorithm) error {
|
2020-03-30 09:26:02 +00:00
|
|
|
if value.Algorithm != alg.Algorithm() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInvalidArgument(nil, "CRYPT-HF32f", "value was hashed with a different algorithm")
|
2020-03-30 09:26:02 +00:00
|
|
|
}
|
2020-03-23 06:06:44 +00:00
|
|
|
return alg.CompareHash(value.Crypted, comparer)
|
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
|
|
|
|
func FillHash(value []byte, alg HashAlgorithm) *CryptoValue {
|
|
|
|
return &CryptoValue{
|
|
|
|
CryptoType: TypeHash,
|
|
|
|
Algorithm: alg.Algorithm(),
|
|
|
|
Crypted: value,
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|