forked from hashicorp/vault-plugin-secrets-gcpkms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_decrypt_test.go
174 lines (144 loc) · 3.93 KB
/
path_decrypt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpkms
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
func TestPathDecrypt_Write(t *testing.T) {
t.Run("field_validation", func(t *testing.T) {
testFieldValidation(t, logical.UpdateOperation, "decrypt/my-key")
})
t.Run("asymmetric", func(t *testing.T) {
algorithms := []kmspb.CryptoKeyVersion_CryptoKeyVersionAlgorithm{
kmspb.CryptoKeyVersion_RSA_DECRYPT_OAEP_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_DECRYPT_OAEP_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_DECRYPT_OAEP_4096_SHA256,
}
for _, algo := range algorithms {
algo := algo
name := strings.ToLower(algo.String())
t.Run(name, func(t *testing.T) {
cryptoKey, cleanup := testCreateKMSCryptoKeyAsymmetricDecrypt(t, algo)
defer cleanup()
b, storage := testBackend(t)
ctx := context.Background()
if err := storage.Put(ctx, &logical.StorageEntry{
Key: "keys/my-key",
Value: []byte(`{"name":"my-key", "crypto_key_id":"` + cryptoKey + `"}`),
}); err != nil {
t.Fatal(err)
}
ckv := cryptoKey + "/cryptoKeyVersions/1"
// Get the public key
kmsClient := testKMSClient(t)
pk, err := kmsClient.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{
Name: ckv,
})
if err != nil {
t.Fatal(err)
}
// Extract the PEM-encoded data block
block, _ := pem.Decode([]byte(pk.Pem))
if block == nil {
t.Fatalf("not pem: %s", pk.Pem)
}
// Decode the public key
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
t.Fatal(err)
}
// Encrypt with the public key
exp := "hello world"
enc, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, pub.(*rsa.PublicKey), []byte(exp), nil)
if err != nil {
t.Fatal(err)
}
// Now decrypt it
resp, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.UpdateOperation,
Path: "decrypt/my-key",
Data: map[string]interface{}{
"ciphertext": base64.StdEncoding.EncodeToString(enc),
"key_version": 1,
},
})
if err != nil {
t.Fatal(err)
}
if v := resp.Data["plaintext"]; v != exp {
t.Errorf("expected %q to be %q", v, exp)
}
})
}
})
t.Run("symmetric", func(t *testing.T) {
cases := []struct {
name string
aad string
exp string
}{
{
"decrypts",
"",
"hello world",
},
{
"decrypts_aad",
"yo yo yo",
"hello world",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cryptoKey, cleanup := testCreateKMSCryptoKeySymmetric(t)
defer cleanup()
b, storage := testBackend(t)
ctx := context.Background()
if err := storage.Put(ctx, &logical.StorageEntry{
Key: "keys/my-key",
Value: []byte(`{"name":"my-key", "crypto_key_id":"` + cryptoKey + `"}`),
}); err != nil {
t.Fatal(err)
}
// Encrypt the data
kmsClient := testKMSClient(t)
encryptResp, err := kmsClient.Encrypt(ctx, &kmspb.EncryptRequest{
Name: cryptoKey,
Plaintext: []byte(tc.exp),
AdditionalAuthenticatedData: []byte(tc.aad),
})
if err != nil {
t.Fatal(err)
}
// Now decrypt it
resp, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.UpdateOperation,
Path: "decrypt/my-key",
Data: map[string]interface{}{
"additional_authenticated_data": tc.aad,
"ciphertext": base64.StdEncoding.EncodeToString(encryptResp.Ciphertext),
},
})
if err != nil {
t.Fatal(err)
}
if v, exp := resp.Data["plaintext"], tc.exp; v != exp {
t.Errorf("expected %q to be %q", v, exp)
}
})
}
})
}