-
Notifications
You must be signed in to change notification settings - Fork 10
/
path_sign_test.go
163 lines (141 loc) · 4.65 KB
/
path_sign_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpkms
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"math/big"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
func TestPathSign_Write(t *testing.T) {
t.Run("field_validation", func(t *testing.T) {
testFieldValidation(t, logical.UpdateOperation, "sign/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)
}
var digest []byte
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:
h := sha256.Sum256([]byte("hello world"))
digest = h[:]
case kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384:
h := sha512.Sum384([]byte("hello world"))
digest = h[:]
}
// Now sign it
resp, err := b.HandleRequest(ctx, &logical.Request{
Storage: storage,
Operation: logical.UpdateOperation,
Path: "sign/my-key",
Data: map[string]interface{}{
"digest": base64.StdEncoding.EncodeToString(digest),
"key_version": 1,
},
})
if err != nil {
t.Fatal(err)
}
sigb64, ok := resp.Data["signature"]
if !ok {
t.Fatal("missing signature")
}
sig, err := base64.StdEncoding.DecodeString(sigb64.(string))
if 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)
}
// Verify the signature
switch pk.Algorithm {
case kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256:
var parsedSig struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(sig, &parsedSig); err != nil {
t.Errorf("failed to unmarshal signature: %s", err)
}
if !ecdsa.Verify(pub.(*ecdsa.PublicKey), digest, parsedSig.R, parsedSig.S) {
t.Error("invalid signature")
}
case kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384:
var parsedSig struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(sig, &parsedSig); err != nil {
t.Errorf("failed to unmarshal signature: %s", err)
}
if !ecdsa.Verify(pub.(*ecdsa.PublicKey), digest, parsedSig.R, parsedSig.S) {
t.Error("invalid signature")
}
case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA256:
if err := rsa.VerifyPSS(pub.(*rsa.PublicKey), crypto.SHA256, digest, sig, &rsa.PSSOptions{}); err != nil {
t.Errorf("invalid signature: %s", err)
}
case kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256:
if err := rsa.VerifyPKCS1v15(pub.(*rsa.PublicKey), crypto.SHA256, digest, sig); err != nil {
t.Errorf("invalid signature: %s", err)
}
default:
t.Fatalf("unknown algorithm: %s", pk.Algorithm)
}
})
}
})
}