| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package fakeawskms provides a partial fake implementation of kmsiface.KMSAPI. |
| 16 | package fakeawskms |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "sort" |
| 23 | |
| 24 | "github.com/aws/aws-sdk-go/service/kms" |
| 25 | "github.com/aws/aws-sdk-go/service/kms/kmsiface" |
| 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 | type fakeAWSKMS struct { |
| 32 | kmsiface.KMSAPI |
| 33 | aeads map[string]tink.AEAD |
| 34 | keyIDs []string |
| 35 | } |
| 36 | |
| 37 | // serializeContext serializes the context map in a canonical way into a byte array. |
| 38 | func serializeContext(context map[string]*string) []byte { |
| 39 | names := make([]string, 0, len(context)) |
| 40 | for name := range context { |
| 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, *context[name]) |
| 51 | } |
| 52 | b.WriteString("}") |
| 53 | return b.Bytes() |
| 54 | } |
| 55 | |
| 56 | // New returns a new fake AWS KMS API. |
| 57 | func New(validKeyIDs []string) (kmsiface.KMSAPI, 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(request *kms.EncryptInput) (*kms.EncryptOutput, error) { |
| 77 | a, ok := f.aeads[*request.KeyId] |
| 78 | if !ok { |
| 79 | return nil, fmt.Errorf("Unknown keyID: %q not in %q", *request.KeyId, f.keyIDs) |
| 80 | } |
| 81 | serializedContext := serializeContext(request.EncryptionContext) |
| 82 | ciphertext, err := a.Encrypt(request.Plaintext, serializedContext) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | return &kms.EncryptOutput{ |
| 87 | CiphertextBlob: ciphertext, |
| 88 | KeyId: request.KeyId, |
| 89 | }, nil |
| 90 | } |
| 91 | |
| 92 | func (f *fakeAWSKMS) Decrypt(request *kms.DecryptInput) (*kms.DecryptOutput, error) { |
| 93 | serializedContext := serializeContext(request.EncryptionContext) |
| 94 | if request.KeyId != nil { |
| 95 | a, ok := f.aeads[*request.KeyId] |
| 96 | if !ok { |
| 97 | return nil, fmt.Errorf("Unknown keyID: %q not in %q", *request.KeyId, f.keyIDs) |
| 98 | } |
| 99 | plaintext, err := a.Decrypt(request.CiphertextBlob, serializedContext) |
| 100 | if err != nil { |
| 101 | return nil, fmt.Errorf("Decryption with keyID %q failed", *request.KeyId) |
| 102 | } |
| 103 | return &kms.DecryptOutput{ |
| 104 | Plaintext: plaintext, |
| 105 | KeyId: request.KeyId, |
| 106 | }, nil |
| 107 | } |
| 108 | // When KeyId is not set, try out all AEADs. |
| 109 | for keyID, a := range f.aeads { |
| 110 | plaintext, err := a.Decrypt(request.CiphertextBlob, serializedContext) |
| 111 | if err == nil { |
| 112 | return &kms.DecryptOutput{ |
| 113 | Plaintext: plaintext, |
| 114 | KeyId: &keyID, |
| 115 | }, nil |
| 116 | } |
| 117 | } |
| 118 | return nil, errors.New("unable to decrypt message") |
| 119 | } |