Debian Codegraph @

v3/integration/awskms/internal/fakeawskms/fakeawskms.go

raw symbols SCIP
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 "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 // FakeAWSKMS is a fake implementation of awskms.KMSAPI.
32 type FakeAWSKMS struct {
33 aeads map[string]tink.AEAD
34 keyIDs []string
35 }
36
37 // serializeEncryptionContext serializes the context map in a canonical way into a byte array.
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 // New returns a new fake AWS KMS API.
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 // When KeyId is not set, try out all AEADs.
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 }