Debian Codegraph @

v3/integration/awskms/aws_kms_integration_test.go

raw symbols SCIP
1 // Copyright 2019 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 awskms_test
16
17 import (
18 "bytes"
19 "context"
20 "encoding/hex"
21 "errors"
22 "os"
23 "path/filepath"
24 "testing"
25
26 // Placeholder for internal flag import.
27 // context is used to cancel outstanding requests
28 "github.com/tink-crypto/tink-go/v2/aead"
29 "github.com/tink-crypto/tink-go/v2/tink"
30
31 "github.com/tink-crypto/tink-go-awskms/v3/integration/awskms"
32 )
33
34 const (
35 keyPrefix = "aws-kms://arn:aws:kms:us-east-2:235739564943:"
36 keyAliasURI = "aws-kms://arn:aws:kms:us-east-2:235739564943:alias/unit-and-integration-testing"
37 keyID = "arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
38 keyURI = "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
39 keyURI2 = "aws-kms://arn:aws:kms:us-east-2:235739564943:key/b3ca2efd-a8fb-47f2-b541-7e20f8c5cd11"
40 )
41
42 const (
43 credCSVFile = "testdata/aws/credentials.csv"
44 credINIFile = "testdata/aws/credentials.ini"
45 )
46
47 // Placeholder for internal initialization.
48
49 func testFilePath(t *testing.T, filename string) string {
50 t.Helper()
51 srcDir, ok := os.LookupEnv("TEST_SRCDIR")
52 if ok {
53 workspaceDir, ok := os.LookupEnv("TEST_WORKSPACE")
54 if !ok {
55 t.Fatal("TEST_WORKSPACE not found")
56 }
57 return filepath.Join(srcDir, workspaceDir, filename)
58 }
59 return filepath.Join("../../..", filename)
60 }
61
62 func TestNewClientWithCredentialsGetAEADEncryptDecrypt(t *testing.T) {
63 credFilePath := testFilePath(t, credCSVFile)
64 client, err := awskms.NewClientWithOptions(t.Context(), keyURI, awskms.WithCredentialPath(credFilePath))
65 if err != nil {
66 t.Fatalf("error setting up AWS client: %v", err)
67 }
68 a, err := client.GetAEAD(keyURI)
69 if err != nil {
70 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
71 }
72 plaintext := []byte("plaintext")
73 associatedData := []byte("associatedData")
74 ciphertext, err := a.Encrypt(plaintext, associatedData)
75 if err != nil {
76 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
77 }
78
79 t.Logf("ciphertext: %s", hex.EncodeToString(ciphertext))
80
81 gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
82 if err != nil {
83 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
84 }
85 if !bytes.Equal(gotPlaintext, plaintext) {
86 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
87 }
88
89 invalidAssociatedData := []byte("invalidAssociatedData")
90 _, err = a.Decrypt(ciphertext, invalidAssociatedData)
91 if err == nil {
92 t.Error("a.Decrypt(ciphertext, invalidAssociatedData) err = nil, want error")
93 }
94 }
95
96 func TestNewAEADWithContextEncryptDecrypt(t *testing.T) {
97 credFilePath := testFilePath(t, credCSVFile)
98 a, err := awskms.NewAEADWithContext(t.Context(), keyID, awskms.WithCredentialPath(credFilePath))
99 if err != nil {
100 t.Fatalf("awskms.NewAEADWithContext() err = %v", err)
101 }
102 plaintext := []byte("plaintext")
103 associatedData := []byte("associatedData")
104 ciphertext, err := a.EncryptWithContext(t.Context(), plaintext, associatedData)
105 if err != nil {
106 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
107 }
108
109 gotPlaintext, err := a.DecryptWithContext(t.Context(), ciphertext, associatedData)
110 if err != nil {
111 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
112 }
113 if !bytes.Equal(gotPlaintext, plaintext) {
114 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
115 }
116
117 invalidAssociatedData := []byte("invalidAssociatedData")
118 _, err = a.DecryptWithContext(t.Context(), ciphertext, invalidAssociatedData)
119 if err == nil {
120 t.Error("a.Decrypt(ciphertext, invalidAssociatedData) err = nil, want error")
121 }
122
123 // Check that the context is not ignored.
124 cancelledCtx, cancel := context.WithCancelCause(t.Context())
125 causeErr := errors.New("cause error message")
126 cancel(causeErr)
127 _, err = a.EncryptWithContext(cancelledCtx, plaintext, associatedData)
128 if err == nil {
129 t.Error("a.EncryptWithContext(cancelledCtx, plaintext, associatedData) err = nil, want error")
130 }
131 _, err = a.DecryptWithContext(cancelledCtx, ciphertext, associatedData)
132 if err == nil {
133 t.Error("a.DecryptWithContext(cancelledCtx, ciphertext, associatedData) err = nil, want error")
134 }
135
136 // Check that it is compatible with the AEAD returned by NewClientWithOptions.
137 client, err := awskms.NewClientWithOptions(t.Context(), keyURI, awskms.WithCredentialPath(credFilePath))
138 if err != nil {
139 t.Fatalf("error setting up AWS client: %v", err)
140 }
141 clientAEAD, err := client.GetAEAD(keyURI)
142 if err != nil {
143 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
144 }
145 gotPlaintextFromClient, err := clientAEAD.Decrypt(ciphertext, associatedData)
146 if err != nil {
147 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
148 }
149 if !bytes.Equal(gotPlaintextFromClient, plaintext) {
150 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintextFromClient, plaintext)
151 }
152 }
153
154 func TestEmptyAssociatedDataEncryptDecrypt(t *testing.T) {
155 credFilePath := testFilePath(t, credCSVFile)
156 client, err := awskms.NewClientWithOptions(t.Context(), keyURI, awskms.WithCredentialPath(credFilePath))
157 if err != nil {
158 t.Fatalf("error setting up AWS client: %v", err)
159 }
160 a, err := client.GetAEAD(keyURI)
161 if err != nil {
162 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
163 }
164 plaintext := []byte("plaintext")
165 emptyAssociatedData := []byte{}
166 ciphertext, err := a.Encrypt(plaintext, emptyAssociatedData)
167 if err != nil {
168 t.Fatalf("a.Encrypt(plaintext, emptyAssociatedData) err = %v, want nil", err)
169 }
170 gotPlaintext, err := a.Decrypt(ciphertext, emptyAssociatedData)
171 if err != nil {
172 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
173 }
174 if !bytes.Equal(gotPlaintext, plaintext) {
175 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
176 }
177
178 gotPlaintext2, err := a.Decrypt(ciphertext, nil)
179 if err != nil {
180 t.Fatalf("a.Decrypt(ciphertext, nil) err = %v, want nil", err)
181 }
182 if !bytes.Equal(gotPlaintext2, plaintext) {
183 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
184 }
185 }
186
187 func TestKeyCommitment(t *testing.T) {
188 credFilePath := testFilePath(t, credCSVFile)
189 client, err := awskms.NewClientWithOptions(t.Context(), keyPrefix, awskms.WithCredentialPath(credFilePath))
190 if err != nil {
191 t.Fatalf("error setting up AWS client: %v", err)
192 }
193
194 // Create AEAD primitives for two keys.
195 keys := []string{keyURI, keyURI2}
196 aeads := make([]tink.AEAD, 0, len(keys))
197 for _, k := range keys {
198 a, err := client.GetAEAD(k)
199 if err != nil {
200 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
201 }
202 aeads = append(aeads, a)
203 }
204
205 // Create a ciphertext using the first primitive.
206 plaintext := []byte("plaintext")
207 associatedData := []byte("associated data")
208 ciphertext, err := aeads[0].Encrypt(plaintext, associatedData)
209 if err != nil {
210 t.Fatalf("aeads[0].Encrypt(plaintext, associatedData) err = %v, want nil", err)
211 }
212 gotPlaintext, err := aeads[0].Decrypt(ciphertext, associatedData)
213 if err != nil {
214 t.Fatalf("aeads[0].Decrypt(ciphertext, associatedData) err = %v, want nil", err)
215 }
216 if !bytes.Equal(gotPlaintext, plaintext) {
217 t.Errorf("aeads[0].Decrypt() = %q, want %q", gotPlaintext, plaintext)
218 }
219
220 // Attempt to decrypt using the other primitive.
221 _, err = aeads[1].Decrypt(ciphertext, associatedData)
222 if err == nil {
223 t.Fatalf("aeads[1].Decrypt(ciphertext, associatedData) err = nil, want non-nil")
224 }
225 }
226
227 func TestKMSEnvelopeAEADEncryptAndDecrypt(t *testing.T) {
228 for _, credFile := range []string{credCSVFile, credINIFile} {
229 credFilePath := testFilePath(t, credFile)
230 client, err := awskms.NewClientWithOptions(t.Context(), keyURI, awskms.WithCredentialPath(credFilePath))
231 if err != nil {
232 t.Fatalf("awskms.NewClientWithOptions() err = %q, want nil", err)
233 }
234
235 kekAEAD, err := client.GetAEAD(keyURI)
236 if err != nil {
237 t.Fatalf("client.GetAEAD(keyURI) err = %q, want nil", err)
238 }
239
240 dekTemplate := aead.AES128CTRHMACSHA256KeyTemplate()
241 a := aead.NewKMSEnvelopeAEAD2(dekTemplate, kekAEAD)
242 if err != nil {
243 t.Fatalf("aead.NewKMSEnvelopeAEAD2(dekTemplate, kekAEAD) err = %q, want nil", err)
244 }
245 plaintext := []byte("plaintext")
246 for _, associatedData := range [][]byte{nil, []byte("associated data")} {
247 ciphertext, err := a.Encrypt(plaintext, associatedData)
248 if err != nil {
249 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %q, want nil", err)
250 }
251 gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
252 if err != nil {
253 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %q, want nil", err)
254 }
255 if !bytes.Equal(gotPlaintext, plaintext) {
256 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
257 }
258 }
259 }
260 }
261
262 func TestDecryptValidCiphertextForKeyURI(t *testing.T) {
263 // This is a valid ciphertext for keyURI that was generated with
264 // TestNewClientWithCredentialsGetAEADEncryptDecrypt in aws_kms_integration_test.go of the v2 package.
265 // If the key changes, this ciphertext needs to be updated.
266 ciphertext, err := hex.DecodeString("01020200787a5511910e44ea9520df697fb6150f6261b59cee44cc846915252c083f03aad00168fe1a9ca1a6af5d571c1aa6e0afcba000000067306506092a864886f70d010706a0583056020100305106092a864886f70d010701301e060960864801650304012e3011040cb085893bc4757850d73a68ca02011080241fcff6b61f510d20c236ee7292b626e35a22b615e57326911daafaa399525431748d3e24")
267 if err != nil {
268 t.Fatalf("hex.DecodeString() err = %v, want nil", err)
269 }
270 wantPlaintext := []byte("plaintext")
271 associatedData := []byte("associatedData")
272 credFilePath := testFilePath(t, credCSVFile)
273
274 client, err := awskms.NewClientWithOptions(t.Context(), keyURI, awskms.WithCredentialPath(credFilePath))
275 if err != nil {
276 t.Fatalf("error setting up AWS client: %v", err)
277 }
278 a, err := client.GetAEAD(keyURI)
279 if err != nil {
280 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
281 }
282 gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
283 if err != nil {
284 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
285 }
286 if !bytes.Equal(gotPlaintext, wantPlaintext) {
287 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, wantPlaintext)
288 }
289 }