Debian Codegraph @

integration/awskms/aws_kms_integration_test.go

raw
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 "encoding/hex"
20 "os"
21 "path/filepath"
22 "testing"
23
24 // Placeholder for internal flag import.
25 // context is used to cancel outstanding requests
26 "github.com/tink-crypto/tink-go/v2/aead"
27 "github.com/tink-crypto/tink-go/v2/tink"
28
29 "github.com/tink-crypto/tink-go-awskms/v2/integration/awskms"
30 )
31
32 const (
33 keyPrefix = "aws-kms://arn:aws:kms:us-east-2:235739564943:"
34 keyAliasURI = "aws-kms://arn:aws:kms:us-east-2:235739564943:alias/unit-and-integration-testing"
35 keyURI = "aws-kms://arn:aws:kms:us-east-2:235739564943:key/3ee50705-5a82-4f5b-9753-05c4f473922f"
36 keyURI2 = "aws-kms://arn:aws:kms:us-east-2:235739564943:key/b3ca2efd-a8fb-47f2-b541-7e20f8c5cd11"
37 )
38
39 const (
40 credCSVFile = "testdata/aws/credentials.csv"
41 credINIFile = "testdata/aws/credentials.ini"
42 )
43
44 // Placeholder for internal initialization.
45
46 func testFilePath(t *testing.T, filename string) string {
47 t.Helper()
48 srcDir, ok := os.LookupEnv("TEST_SRCDIR")
49 if ok {
50 workspaceDir, ok := os.LookupEnv("TEST_WORKSPACE")
51 if !ok {
52 t.Fatal("TEST_WORKSPACE not found")
53 }
54 return filepath.Join(srcDir, workspaceDir, filename)
55 }
56 return filepath.Join("../..", filename)
57 }
58
59 func TestNewClientWithCredentialsGetAEADEncryptDecrypt(t *testing.T) {
60 credFilePath := testFilePath(t, credCSVFile)
61 client, err := awskms.NewClientWithOptions(keyURI, awskms.WithCredentialPath(credFilePath))
62 if err != nil {
63 t.Fatalf("error setting up AWS client: %v", err)
64 }
65 a, err := client.GetAEAD(keyURI)
66 if err != nil {
67 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
68 }
69 plaintext := []byte("plaintext")
70 associatedData := []byte("associatedData")
71 ciphertext, err := a.Encrypt(plaintext, associatedData)
72 if err != nil {
73 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %v, want nil", err)
74 }
75
76 t.Logf("valid ciphertext for keyURI %s: %s", keyURI, hex.EncodeToString(ciphertext))
77
78 gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
79 if err != nil {
80 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
81 }
82 if !bytes.Equal(gotPlaintext, plaintext) {
83 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
84 }
85
86 invalidAssociatedData := []byte("invalidAssociatedData")
87 _, err = a.Decrypt(ciphertext, invalidAssociatedData)
88 if err == nil {
89 t.Error("a.Decrypt(ciphertext, invalidAssociatedData) err = nil, want error")
90 }
91 }
92
93 func TestEmptyAssociatedDataEncryptDecrypt(t *testing.T) {
94 credFilePath := testFilePath(t, credCSVFile)
95 client, err := awskms.NewClientWithOptions(keyURI, awskms.WithCredentialPath(credFilePath))
96 if err != nil {
97 t.Fatalf("error setting up AWS client: %v", err)
98 }
99 a, err := client.GetAEAD(keyURI)
100 if err != nil {
101 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
102 }
103 plaintext := []byte("plaintext")
104 emptyAssociatedData := []byte{}
105 ciphertext, err := a.Encrypt(plaintext, emptyAssociatedData)
106 if err != nil {
107 t.Fatalf("a.Encrypt(plaintext, emptyAssociatedData) err = %v, want nil", err)
108 }
109 gotPlaintext, err := a.Decrypt(ciphertext, emptyAssociatedData)
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 gotPlaintext2, err := a.Decrypt(ciphertext, nil)
118 if err != nil {
119 t.Fatalf("a.Decrypt(ciphertext, nil) err = %v, want nil", err)
120 }
121 if !bytes.Equal(gotPlaintext2, plaintext) {
122 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
123 }
124 }
125
126 func TestKeyCommitment(t *testing.T) {
127 credFilePath := testFilePath(t, credCSVFile)
128 client, err := awskms.NewClientWithOptions(keyPrefix, awskms.WithCredentialPath(credFilePath))
129 if err != nil {
130 t.Fatalf("error setting up AWS client: %v", err)
131 }
132
133 // Create AEAD primitives for two keys.
134 keys := []string{keyURI, keyURI2}
135 aeads := make([]tink.AEAD, 0, len(keys))
136 for _, k := range keys {
137 a, err := client.GetAEAD(k)
138 if err != nil {
139 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
140 }
141 aeads = append(aeads, a)
142 }
143
144 // Create a ciphertext using the first primitive.
145 plaintext := []byte("plaintext")
146 associatedData := []byte("associated data")
147 ciphertext, err := aeads[0].Encrypt(plaintext, associatedData)
148 if err != nil {
149 t.Fatalf("aeads[0].Encrypt(plaintext, associatedData) err = %v, want nil", err)
150 }
151 gotPlaintext, err := aeads[0].Decrypt(ciphertext, associatedData)
152 if err != nil {
153 t.Fatalf("aeads[0].Decrypt(ciphertext, associatedData) err = %v, want nil", err)
154 }
155 if !bytes.Equal(gotPlaintext, plaintext) {
156 t.Errorf("aeads[0].Decrypt() = %q, want %q", gotPlaintext, plaintext)
157 }
158
159 // Attempt to decrypt using the other primitive.
160 _, err = aeads[1].Decrypt(ciphertext, associatedData)
161 if err == nil {
162 t.Fatalf("aeads[1].Decrypt(ciphertext, associatedData) err = nil, want non-nil")
163 }
164 }
165
166 func TestKMSEnvelopeAEADEncryptAndDecrypt(t *testing.T) {
167 for _, credFile := range []string{credCSVFile, credINIFile} {
168 credFilePath := testFilePath(t, credFile)
169 client, err := awskms.NewClientWithOptions(keyURI, awskms.WithCredentialPath(credFilePath))
170 if err != nil {
171 t.Fatalf("awskms.NewClientWithOptions() err = %q, want nil", err)
172 }
173
174 kekAEAD, err := client.GetAEAD(keyURI)
175 if err != nil {
176 t.Fatalf("client.GetAEAD(keyURI) err = %q, want nil", err)
177 }
178
179 dekTemplate := aead.AES128CTRHMACSHA256KeyTemplate()
180 a := aead.NewKMSEnvelopeAEAD2(dekTemplate, kekAEAD)
181 if err != nil {
182 t.Fatalf("aead.NewKMSEnvelopeAEAD2(dekTemplate, kekAEAD) err = %q, want nil", err)
183 }
184 plaintext := []byte("plaintext")
185 for _, associatedData := range [][]byte{nil, []byte("associated data")} {
186 ciphertext, err := a.Encrypt(plaintext, associatedData)
187 if err != nil {
188 t.Fatalf("a.Encrypt(plaintext, associatedData) err = %q, want nil", err)
189 }
190 gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
191 if err != nil {
192 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %q, want nil", err)
193 }
194 if !bytes.Equal(gotPlaintext, plaintext) {
195 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, plaintext)
196 }
197 }
198 }
199 }
200
201 func TestDecryptValidCiphertextForKeyURI(t *testing.T) {
202 // This is a valid ciphertext for keyURI that was generated with
203 // TestNewClientWithCredentialsGetAEADEncryptDecrypt.
204 // If the key changes, this ciphertext needs to be updated.
205 ciphertext, err := hex.DecodeString("01020200787a5511910e44ea9520df697fb6150f6261b59cee44cc846915252c083f03aad00168fe1a9ca1a6af5d571c1aa6e0afcba000000067306506092a864886f70d010706a0583056020100305106092a864886f70d010701301e060960864801650304012e3011040cb085893bc4757850d73a68ca02011080241fcff6b61f510d20c236ee7292b626e35a22b615e57326911daafaa399525431748d3e24")
206 if err != nil {
207 t.Fatalf("hex.DecodeString() err = %v, want nil", err)
208 }
209 wantPlaintext := []byte("plaintext")
210 associatedData := []byte("associatedData")
211 credFilePath := testFilePath(t, credCSVFile)
212
213 client, err := awskms.NewClientWithOptions(keyURI, awskms.WithCredentialPath(credFilePath))
214 if err != nil {
215 t.Fatalf("error setting up AWS client: %v", err)
216 }
217 a, err := client.GetAEAD(keyURI)
218 if err != nil {
219 t.Fatalf("client.GetAEAD(keyURI) err = %v, want nil", err)
220 }
221 gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
222 if err != nil {
223 t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %v, want nil", err)
224 }
225 if !bytes.Equal(gotPlaintext, wantPlaintext) {
226 t.Errorf("a.Decrypt() = %q, want %q", gotPlaintext, wantPlaintext)
227 }
228 }