| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package fakeawskms |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"sort" |
| 24 |
|
| 25 |
"github.com/aws/aws-sdk-go-v2/service/kms" |
| 26 |
"github.com/tink-crypto/tink-go/v2/aead" |
| 27 |
"github.com/tink-crypto/tink-go/v2/keyset" |
| 28 |
"github.com/tink-crypto/tink-go/v2/tink" |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
type FakeAWSKMS struct { |
| 33 |
aeads map[string]tink.AEAD |
| 34 |
keyIDs []string |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func serializeEncryptionContext(encryptionContext map[string]string) []byte { |
| 39 |
names := make([]string, 0, len(encryptionContext)) |
| 40 |
for name := range encryptionContext { |
| 41 |
names = append(names, name) |
| 42 |
} |
| 43 |
sort.Strings(names) |
| 44 |
b := new(bytes.Buffer) |
| 45 |
b.WriteString("{") |
| 46 |
for i, name := range names { |
| 47 |
if i > 0 { |
| 48 |
b.WriteString(",") |
| 49 |
} |
| 50 |
fmt.Fprintf(b, "%q:%q", name, encryptionContext[name]) |
| 51 |
} |
| 52 |
b.WriteString("}") |
| 53 |
return b.Bytes() |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func New(validKeyIDs []string) (*FakeAWSKMS, error) { |
| 58 |
aeads := make(map[string]tink.AEAD) |
| 59 |
for _, keyID := range validKeyIDs { |
| 60 |
handle, err := keyset.NewHandle(aead.AES256GCMKeyTemplate()) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
a, err := aead.New(handle) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
aeads[keyID] = a |
| 69 |
} |
| 70 |
return &FakeAWSKMS{ |
| 71 |
aeads: aeads, |
| 72 |
keyIDs: validKeyIDs, |
| 73 |
}, nil |
| 74 |
} |
| 75 |
|
| 76 |
func (f *FakeAWSKMS) Encrypt(ctx context.Context, params *kms.EncryptInput, optFns ...func(*kms.Options)) (*kms.EncryptOutput, error) { |
| 77 |
if err := ctx.Err(); err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
a, ok := f.aeads[*params.KeyId] |
| 81 |
if !ok { |
| 82 |
return nil, fmt.Errorf("Unknown keyID: %q not in %q", *params.KeyId, f.keyIDs) |
| 83 |
} |
| 84 |
serializedEncryptionContext := serializeEncryptionContext(params.EncryptionContext) |
| 85 |
ciphertext, err := a.Encrypt(params.Plaintext, serializedEncryptionContext) |
| 86 |
if err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
return &kms.EncryptOutput{ |
| 90 |
CiphertextBlob: ciphertext, |
| 91 |
KeyId: params.KeyId, |
| 92 |
}, nil |
| 93 |
} |
| 94 |
|
| 95 |
func (f *FakeAWSKMS) Decrypt(ctx context.Context, params *kms.DecryptInput, optFns ...func(*kms.Options)) (*kms.DecryptOutput, error) { |
| 96 |
if err := ctx.Err(); err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
serializedEncryptionContext := serializeEncryptionContext(params.EncryptionContext) |
| 100 |
if params.KeyId != nil { |
| 101 |
a, ok := f.aeads[*params.KeyId] |
| 102 |
if !ok { |
| 103 |
return nil, fmt.Errorf("Unknown keyID: %q not in %q", *params.KeyId, f.keyIDs) |
| 104 |
} |
| 105 |
plaintext, err := a.Decrypt(params.CiphertextBlob, serializedEncryptionContext) |
| 106 |
if err != nil { |
| 107 |
return nil, fmt.Errorf("Decryption with keyID %q failed", *params.KeyId) |
| 108 |
} |
| 109 |
return &kms.DecryptOutput{ |
| 110 |
Plaintext: plaintext, |
| 111 |
KeyId: params.KeyId, |
| 112 |
}, nil |
| 113 |
} |
| 114 |
|
| 115 |
for keyID, a := range f.aeads { |
| 116 |
plaintext, err := a.Decrypt(params.CiphertextBlob, serializedEncryptionContext) |
| 117 |
if err == nil { |
| 118 |
return &kms.DecryptOutput{ |
| 119 |
Plaintext: plaintext, |
| 120 |
KeyId: &keyID, |
| 121 |
}, nil |
| 122 |
} |
| 123 |
} |
| 124 |
return nil, errors.New("unable to decrypt message") |
| 125 |
} |