Debian Codegraph @

pkg/signature/kms/hashivault/signer.go

raw
1 //
2 // Copyright 2021 The Sigstore Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package hashivault
17
18 import (
19 "context"
20 "crypto"
21 "errors"
22 "fmt"
23 "io"
24 "strconv"
25
26 "github.com/sigstore/sigstore/pkg/signature"
27 "github.com/sigstore/sigstore/pkg/signature/options"
28 )
29
30 // Taken from https://www.vaultproject.io/api/secret/transit
31 // nolint:revive
32 const (
33 AlgorithmECDSAP256 = "ecdsa-p256"
34 AlgorithmECDSAP384 = "ecdsa-p384"
35 AlgorithmECDSAP521 = "ecdsa-p521"
36 AlgorithmED25519 = "ed25519"
37 AlgorithmRSA2048 = "rsa-2048"
38 AlgorithmRSA3072 = "rsa-3072"
39 AlgorithmRSA4096 = "rsa-4096"
40 )
41
42 var hvSupportedAlgorithms = []string{
43 AlgorithmECDSAP256,
44 AlgorithmECDSAP384,
45 AlgorithmECDSAP521,
46 AlgorithmED25519,
47 AlgorithmRSA2048,
48 AlgorithmRSA3072,
49 AlgorithmRSA4096,
50 }
51
52 var hvSupportedHashFuncs = []crypto.Hash{
53 crypto.SHA224,
54 crypto.SHA256,
55 crypto.SHA384,
56 crypto.SHA512,
57 crypto.Hash(0),
58 }
59
60 // SignerVerifier creates and verifies digital signatures over a message using Hashicorp Vault KMS service
61 type SignerVerifier struct {
62 hashFunc crypto.Hash
63 client *hashivaultClient
64 }
65
66 // LoadSignerVerifier generates signatures using the specified key object in Vault and hash algorithm.
67 //
68 // It also can verify signatures (via a remote vall to the Vault instance). hashFunc should be
69 // set to crypto.Hash(0) if the key referred to by referenceStr is an ED25519 signing key.
70 func LoadSignerVerifier(referenceStr string, hashFunc crypto.Hash, opts ...signature.RPCOption) (*SignerVerifier, error) {
71 h := &SignerVerifier{}
72 ctx := context.Background()
73 rpcAuth := options.RPCAuth{}
74 var keyVersion string
75 for _, opt := range opts {
76 opt.ApplyRPCAuthOpts(&rpcAuth)
77 opt.ApplyContext(&ctx)
78 opt.ApplyKeyVersion(&keyVersion)
79 }
80
81 var keyVersionUint uint64
82 var err error
83 if keyVersion != "" {
84 keyVersionUint, err = strconv.ParseUint(keyVersion, 10, 64)
85 if err != nil {
86 return nil, fmt.Errorf("parsing key version: %w", err)
87 }
88 }
89
90 if rpcAuth.OIDC.Token != "" {
91 rpcAuth.Token, err = oidcLogin(ctx, rpcAuth.Address, rpcAuth.OIDC.Path, rpcAuth.OIDC.Role, rpcAuth.OIDC.Token)
92 if err != nil {
93 return nil, err
94 }
95 }
96 h.client, err = newHashivaultClient(rpcAuth.Address, rpcAuth.Token, rpcAuth.Path, referenceStr, keyVersionUint)
97 if err != nil {
98 return nil, err
99 }
100
101 switch hashFunc {
102 case 0, crypto.SHA224, crypto.SHA256, crypto.SHA384, crypto.SHA512:
103 h.hashFunc = hashFunc
104 default:
105 return nil, errors.New("hash function not supported by Hashivault")
106 }
107
108 return h, nil
109 }
110
111 // SignMessage signs the provided message using HashiCorp Vault KMS. If the message is provided,
112 // this method will compute the digest according to the hash function specified
113 // when the HashivaultSigner was created.
114 //
115 // SignMessage recognizes the following Options listed in order of preference:
116 //
117 // - WithDigest()
118 //
119 // All other options are ignored if specified.
120 func (h SignerVerifier) SignMessage(message io.Reader, opts ...signature.SignOption) ([]byte, error) {
121 var digest []byte
122 var signerOpts crypto.SignerOpts = h.hashFunc
123
124 for _, opt := range opts {
125 opt.ApplyDigest(&digest)
126 opt.ApplyCryptoSignerOpts(&signerOpts)
127 }
128
129 digest, hf, err := signature.ComputeDigestForSigning(message, signerOpts.HashFunc(), hvSupportedHashFuncs, opts...)
130 if err != nil {
131 return nil, err
132 }
133
134 return h.client.sign(digest, hf, opts...)
135 }
136
137 // PublicKey returns the public key that can be used to verify signatures created by
138 // this signer. All options provided in arguments to this method are ignored.
139 func (h SignerVerifier) PublicKey(_ ...signature.PublicKeyOption) (crypto.PublicKey, error) {
140 return h.client.public()
141 }
142
143 // VerifySignature verifies the signature for the given message. Unless provided
144 // in an option, the digest of the message will be computed using the hash function specified
145 // when the SignerVerifier was created.
146 //
147 // This function returns nil if the verification succeeded, and an error message otherwise.
148 //
149 // This function recognizes the following Options listed in order of preference:
150 //
151 // - WithDigest()
152 //
153 // - WithCryptoSignerOpts()
154 //
155 // All other options are ignored if specified.
156 func (h SignerVerifier) VerifySignature(sig, message io.Reader, opts ...signature.VerifyOption) error {
157 var digest []byte
158 var signerOpts crypto.SignerOpts = h.hashFunc
159
160 for _, opt := range opts {
161 opt.ApplyDigest(&digest)
162 opt.ApplyCryptoSignerOpts(&signerOpts)
163 }
164
165 digest, hf, err := signature.ComputeDigestForVerifying(message, signerOpts.HashFunc(), hvSupportedHashFuncs, opts...)
166 if err != nil {
167 return err
168 }
169
170 sigBytes, err := io.ReadAll(sig)
171 if err != nil {
172 return fmt.Errorf("reading signature: %w", err)
173 }
174
175 return h.client.verify(sigBytes, digest, hf, opts...)
176 }
177
178 // CreateKey attempts to create a new key in Vault with the specified algorithm.
179 func (h SignerVerifier) CreateKey(_ context.Context, algorithm string) (crypto.PublicKey, error) {
180 return h.client.createKey(algorithm)
181 }
182
183 type cryptoSignerWrapper struct {
184 ctx context.Context
185 hashFunc crypto.Hash
186 sv *SignerVerifier
187 errFunc func(error)
188 }
189
190 func (c cryptoSignerWrapper) Public() crypto.PublicKey {
191 pk, err := c.sv.PublicKey(options.WithContext(c.ctx))
192 if err != nil && c.errFunc != nil {
193 c.errFunc(err)
194 }
195 return pk
196 }
197
198 func (c cryptoSignerWrapper) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
199 hashFunc := c.hashFunc
200 if opts != nil {
201 hashFunc = opts.HashFunc()
202 }
203 hvOptions := []signature.SignOption{
204 options.WithContext(c.ctx),
205 options.WithDigest(digest),
206 options.WithCryptoSignerOpts(hashFunc),
207 }
208
209 return c.sv.SignMessage(nil, hvOptions...)
210 }
211
212 // CryptoSigner returns a crypto.Signer object that uses the underlying SignerVerifier, along with a crypto.SignerOpts object
213 // that allows the KMS to be used in APIs that only accept the standard golang objects
214 func (h *SignerVerifier) CryptoSigner(ctx context.Context, errFunc func(error)) (crypto.Signer, crypto.SignerOpts, error) {
215 csw := &cryptoSignerWrapper{
216 ctx: ctx,
217 sv: h,
218 hashFunc: h.hashFunc,
219 errFunc: errFunc,
220 }
221
222 return csw, h.hashFunc, nil
223 }
224
225 // SupportedAlgorithms returns the list of algorithms supported by the Hashicorp Vault service
226 func (h *SignerVerifier) SupportedAlgorithms() []string {
227 return hvSupportedAlgorithms
228 }
229
230 // DefaultAlgorithm returns the default algorithm for the Hashicorp Vault service
231 func (h *SignerVerifier) DefaultAlgorithm() string {
232 return AlgorithmECDSAP256
233 }