snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
84
cipher/aes.go
Normal file
84
cipher/aes.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package cipher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
stdcipher "crypto/cipher"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/ory/herodot"
|
||||
)
|
||||
|
||||
type AES struct {
|
||||
c SecretsProvider
|
||||
}
|
||||
|
||||
func NewCryptAES(c SecretsProvider) *AES {
|
||||
return &AES{c: c}
|
||||
}
|
||||
|
||||
// aesGCM returns an AES-256-GCM AEAD that generates a random nonce internally
|
||||
// and prepends it to the ciphertext (nonce | ciphertext | tag).
|
||||
func aesGCM(key *[32]byte) (stdcipher.AEAD, error) {
|
||||
block, err := aes.NewCipher(key[:])
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
gcm, err := stdcipher.NewGCMWithRandomNonce(block)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
return gcm, nil
|
||||
}
|
||||
|
||||
// Encrypt returns the AES-256-GCM encryption of plaintext.
|
||||
func (a *AES) Encrypt(ctx context.Context, message []byte) (string, error) {
|
||||
if len(message) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if len(a.c.SecretsCipher(ctx)) == 0 {
|
||||
return "", errors.WithStack(herodot.ErrMisconfiguration().WithReason("Unable to encrypt message because no cipher secrets were configured."))
|
||||
}
|
||||
|
||||
gcm, err := aesGCM(&a.c.SecretsCipher(ctx)[0])
|
||||
if err != nil {
|
||||
return "", errors.WithStack(herodot.ErrForbidden().WithWrap(err))
|
||||
}
|
||||
|
||||
return hex.EncodeToString(gcm.Seal(nil, nil, message, nil)), nil
|
||||
}
|
||||
|
||||
// Decrypt returns the decrypted AES data.
|
||||
func (a *AES) Decrypt(ctx context.Context, ciphertext string) ([]byte, error) {
|
||||
if len(ciphertext) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
secrets := a.c.SecretsCipher(ctx)
|
||||
if len(secrets) == 0 {
|
||||
return nil, errors.WithStack(herodot.ErrMisconfiguration().WithReason("Unable to decipher the encrypted message because no AES secrets were configured."))
|
||||
}
|
||||
|
||||
decode, err := hex.DecodeString(ciphertext)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(herodot.ErrBadRequest().WithWrap(err))
|
||||
}
|
||||
|
||||
for i := range secrets {
|
||||
gcm, err := aesGCM(&secrets[i])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if plaintext, err := gcm.Open(nil, nil, decode, nil); err == nil {
|
||||
return plaintext, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.WithStack(herodot.ErrForbidden().WithReason("Unable to decipher the encrypted message."))
|
||||
}
|
||||
Reference in New Issue
Block a user