-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_keys_verify.go
183 lines (157 loc) · 5.34 KB
/
path_keys_verify.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
175
176
177
178
179
180
181
182
183
package awskms
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"fmt"
"math/big"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1"
)
func (b *backend) pathVerify() *framework.Path {
return &framework.Path{
Pattern: "verify/" + framework.GenericNameRegex("key"),
HelpSynopsis: "Verify a signature using a named key",
HelpDescription: `
Use the named key to verify the given signature. The response will be the
base64-encoded encrypted value (ciphertext).
`,
Fields: map[string]*framework.FieldSchema{
"key": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
Name of the key in Vault to use for verification. This key must already exist in
Vault and must map back to a Google Cloud KMS key.
`,
},
"digest": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
Digest to verify. This digest must use the same SHA algorithm as the underlying
Cloud KMS key. The digest must be the base64-encoded binary value. This field is
required.
`,
},
"key_version": &framework.FieldSchema{
Type: framework.TypeInt,
Description: `
Integer version of the crypto key version to use for verification. This field is
required.
`,
},
"signature": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
Base64-encoded signature to use for verification. This field is required.
`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: withFieldValidator(b.pathVerifyWrite),
logical.UpdateOperation: withFieldValidator(b.pathVerifyWrite),
},
}
}
// pathVerifyWrite corresponds to PUT/POST awskms/sign/:key and is used to
// verify the digest using the named key.
func (b *backend) pathVerifyWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("key").(string)
digest := d.Get("digest").(string)
signature := d.Get("signature").(string)
keyVersion := d.Get("key_version").(int)
if digest == "" {
return nil, errMissingFields("digest")
}
if signature == "" {
return nil, errMissingFields("signature")
}
if keyVersion == 0 {
return nil, errMissingFields("key_version")
}
sig, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
return nil, errwrap.Wrapf("failed to base64 decode signature: {{err}}", err)
}
dig, err := base64.StdEncoding.DecodeString(digest)
if err != nil {
return nil, errwrap.Wrapf("failed to base64 decode digest: {{err}}", err)
}
k, err := b.Key(ctx, req.Storage, key)
if err != nil {
if err == ErrKeyNotFound {
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
}
return nil, err
}
if k.MinVersion > 0 && keyVersion < k.MinVersion {
resp := fmt.Sprintf("requested version %d is less than minimum allowed version of %d",
keyVersion, k.MinVersion)
return logical.ErrorResponse(resp), logical.ErrPermissionDenied
}
if k.MaxVersion > 0 && keyVersion > k.MaxVersion {
resp := fmt.Sprintf("requested version %d is greater than maximum allowed version of %d",
keyVersion, k.MaxVersion)
return logical.ErrorResponse(resp), logical.ErrPermissionDenied
}
kmsClient, closer, err := b.KMSClient(req.Storage)
if err != nil {
return nil, err
}
defer closer()
// Get the public key
pk, err := kmsClient.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{
Name: fmt.Sprintf("%s/cryptoKeyVersions/%d", k.CryptoKeyID, keyVersion),
})
if err != nil {
return nil, errwrap.Wrapf("failed to get public key: {{err}}", err)
}
// Extract the PEM-encoded data block
block, _ := pem.Decode([]byte(pk.Pem))
if block == nil {
return nil, fmt.Errorf("public key is not in pem format: %s", pk.Pem)
}
// Decode the public key
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, errwrap.Wrapf("failed to parse public key: {{err}}", err)
}
validSig := false
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 {
return nil, errwrap.Wrapf("failed to unmarshal signature: {{err}}", err)
}
validSig = ecdsa.Verify(pub.(*ecdsa.PublicKey), dig, parsedSig.R, parsedSig.S)
case kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384:
var parsedSig struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(sig, &parsedSig); err != nil {
return nil, errwrap.Wrapf("failed to unmarshal signature: {{err}}", err)
}
validSig = ecdsa.Verify(pub.(*ecdsa.PublicKey), dig, parsedSig.R, parsedSig.S)
case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA256:
err := rsa.VerifyPSS(pub.(*rsa.PublicKey), crypto.SHA256, dig, sig, &rsa.PSSOptions{})
validSig = err == nil
case kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_3072_SHA256,
kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256:
err := rsa.VerifyPKCS1v15(pub.(*rsa.PublicKey), crypto.SHA256, dig, sig)
validSig = err == nil
default:
return nil, fmt.Errorf("unknown key signing algorithm: %s", pk.Algorithm)
}
return &logical.Response{
Data: map[string]interface{}{
"valid": validSig,
},
}, nil
}