-
Notifications
You must be signed in to change notification settings - Fork 10
/
path_sign.go
160 lines (137 loc) · 4.39 KB
/
path_sign.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpkms
import (
"context"
"encoding/base64"
"fmt"
"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) pathSign() *framework.Path {
return &framework.Path{
Pattern: "sign/" + framework.GenericNameRegex("key"),
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixGoogleCloudKMS,
OperationVerb: "sign",
},
HelpSynopsis: "Signs a message or digest using a named key",
HelpDescription: `
Use the named key to sign a digest string. The response will be the
base64-encoded signature.
`,
Fields: map[string]*framework.FieldSchema{
"key": &framework.FieldSchema{
Type: framework.TypeString,
Description: `
Name of the key in Vault to use for signing. 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 sign. 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 signing. This field is
required.
`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: withFieldValidator(b.pathSignWrite),
},
}
}
// pathSignWrite corresponds to PUT/POST gcpkms/sign/:key and is used to sign
// the digest using the named key.
func (b *backend) pathSignWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("key").(string)
digest := d.Get("digest").(string)
keyVersion := d.Get("key_version").(int)
if digest == "" {
return nil, errMissingFields("digest")
}
if keyVersion == 0 {
return nil, errMissingFields("key_version")
}
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()
ckv, err := kmsClient.GetCryptoKeyVersion(ctx, &kmspb.GetCryptoKeyVersionRequest{
Name: fmt.Sprintf("%s/cryptoKeyVersions/%d", k.CryptoKeyID, keyVersion),
})
if err != nil {
return nil, errwrap.Wrapf("failed to get underlying crypto key: {{err}}", err)
}
var dig *kmspb.Digest
switch ckv.Algorithm {
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, err := base64.StdEncoding.DecodeString(digest)
if err != nil {
return nil, errwrap.Wrapf("failed to decode base64 digest: {{err}}", err)
}
dig = &kmspb.Digest{
Digest: &kmspb.Digest_Sha256{
Sha256: d,
},
}
case kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384:
d, err := base64.StdEncoding.DecodeString(digest)
if err != nil {
return nil, errwrap.Wrapf("failed to decode base64 digest: {{err}}", err)
}
dig = &kmspb.Digest{
Digest: &kmspb.Digest_Sha384{
Sha384: d,
},
}
default:
return nil, fmt.Errorf("unknown key signing algorithm: %s", ckv.Algorithm)
}
resp, err := kmsClient.AsymmetricSign(ctx, &kmspb.AsymmetricSignRequest{
Name: ckv.Name,
Digest: dig,
})
if err != nil {
return nil, errwrap.Wrapf("failed to sign digest: {{err}}", err)
}
return &logical.Response{
Data: map[string]interface{}{
"signature": base64.StdEncoding.EncodeToString(resp.Signature),
},
}, nil
}