-
Notifications
You must be signed in to change notification settings - Fork 9
/
path_keys_verify_test.go
125 lines (106 loc) · 3.24 KB
/
path_keys_verify_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpkms
import (
"context"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
func TestPathVerify_Write(t *testing.T) {
t.Run("field_validation", func(t *testing.T) {
testFieldValidation(t, logical.UpdateOperation, "verify/my-key")
})
t.Run("asymmetric", func(t *testing.T) {
algorithms := []kmspb.CryptoKeyVersion_CryptoKeyVersionAlgorithm{
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256,
kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256,
kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384,
}
for _, algo := range algorithms {
algo := algo
name := strings.ToLower(algo.String())
t.Run(name, func(t *testing.T) {
cryptoKey, cleanup := testCreateKMSCryptoKeyAsymmetricSign(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)
}
// Sign the digest
message := "hello world"
var digest []byte
var dig *kmspb.Digest
switch algo {
case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256,
kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256:
d := sha256.Sum256([]byte(message))
digest = d[:]
dig = &kmspb.Digest{
Digest: &kmspb.Digest_Sha256{
Sha256: digest,
},
}
case kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384:
d := sha512.Sum384([]byte(message))
digest = d[:]
dig = &kmspb.Digest{
Digest: &kmspb.Digest_Sha384{
Sha384: digest,
},
}
default:
t.Fatalf("unknown key signing algorithm: %s", algo)
}
ckv := cryptoKey + "/cryptoKeyVersions/1"
kmsClient := testKMSClient(t)
signResp, err := kmsClient.AsymmetricSign(ctx, &kmspb.AsymmetricSignRequest{
Name: ckv,
Digest: dig,
})
if err != nil {
t.Fatal(err)
}
// Now verify it
resp, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.UpdateOperation,
Path: "verify/my-key",
Data: map[string]interface{}{
"digest": base64.StdEncoding.EncodeToString(digest),
"signature": base64.StdEncoding.EncodeToString(signResp.Signature),
"key_version": 1,
},
})
if err != nil {
t.Fatal(err)
}
valid, ok := resp.Data["valid"]
if !ok {
t.Fatal("missing valid")
}
if b, ok := valid.(bool); !ok || !b {
t.Errorf("expected valid %t to be %t", b, true)
}
})
}
})
}