| 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 aws |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"crypto" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
|
| 24 |
"github.com/aws/aws-sdk-go-v2/config" |
| 25 |
"github.com/aws/aws-sdk-go-v2/service/kms/types" |
| 26 |
"github.com/sigstore/sigstore/pkg/signature" |
| 27 |
"github.com/sigstore/sigstore/pkg/signature/options" |
| 28 |
) |
| 29 |
|
| 30 |
var awsSupportedAlgorithms = []types.CustomerMasterKeySpec{ |
| 31 |
types.CustomerMasterKeySpecRsa2048, |
| 32 |
types.CustomerMasterKeySpecRsa3072, |
| 33 |
types.CustomerMasterKeySpecRsa4096, |
| 34 |
types.CustomerMasterKeySpecEccNistP256, |
| 35 |
types.CustomerMasterKeySpecEccNistP384, |
| 36 |
types.CustomerMasterKeySpecEccNistP521, |
| 37 |
} |
| 38 |
|
| 39 |
var awsSupportedHashFuncs = []crypto.Hash{ |
| 40 |
crypto.SHA256, |
| 41 |
crypto.SHA384, |
| 42 |
crypto.SHA512, |
| 43 |
} |
| 44 |
|
| 45 |
// SignerVerifier is a signature.SignerVerifier that uses the AWS Key Management Service |
| 46 |
type SignerVerifier struct { |
| 47 |
client *awsClient |
| 48 |
} |
| 49 |
|
| 50 |
// LoadSignerVerifier generates signatures using the specified key object in AWS KMS and hash algorithm. |
| 51 |
// |
| 52 |
// It also can verify signatures locally using the public key. hashFunc must not be crypto.Hash(0). |
| 53 |
func LoadSignerVerifier(ctx context.Context, referenceStr string, opts ...func(*config.LoadOptions) error) (*SignerVerifier, error) { |
| 54 |
a := &SignerVerifier{} |
| 55 |
|
| 56 |
var err error |
| 57 |
a.client, err = newAWSClient(ctx, referenceStr, opts...) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
|
| 62 |
return a, nil |
| 63 |
} |
| 64 |
|
| 65 |
// SignMessage signs the provided message using AWS KMS. If the message is provided, |
| 66 |
// this method will compute the digest according to the hash function specified |
| 67 |
// when the Signer was created. |
| 68 |
// |
| 69 |
// SignMessage recognizes the following Options listed in order of preference: |
| 70 |
// |
| 71 |
// - WithContext() |
| 72 |
// |
| 73 |
// - WithDigest() |
| 74 |
// |
| 75 |
// - WithCryptoSignerOpts() |
| 76 |
// |
| 77 |
// All other options are ignored if specified. |
| 78 |
func (a *SignerVerifier) SignMessage(message io.Reader, opts ...signature.SignOption) ([]byte, error) { |
| 79 |
var digest []byte |
| 80 |
var err error |
| 81 |
ctx := context.Background() |
| 82 |
|
| 83 |
for _, opt := range opts { |
| 84 |
opt.ApplyContext(&ctx) |
| 85 |
opt.ApplyDigest(&digest) |
| 86 |
} |
| 87 |
|
| 88 |
var signerOpts crypto.SignerOpts |
| 89 |
signerOpts, err = a.client.getHashFunc(ctx) |
| 90 |
if err != nil { |
| 91 |
return nil, fmt.Errorf("getting fetching default hash function: %w", err) |
| 92 |
} |
| 93 |
for _, opt := range opts { |
| 94 |
opt.ApplyCryptoSignerOpts(&signerOpts) |
| 95 |
} |
| 96 |
|
| 97 |
hf := signerOpts.HashFunc() |
| 98 |
|
| 99 |
if len(digest) == 0 { |
| 100 |
digest, hf, err = signature.ComputeDigestForSigning(message, hf, awsSupportedHashFuncs, opts...) |
| 101 |
if err != nil { |
| 102 |
return nil, err |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return a.client.sign(ctx, digest, hf) |
| 107 |
} |
| 108 |
|
| 109 |
// PublicKey returns the public key that can be used to verify signatures created by |
| 110 |
// this signer. If the caller wishes to specify the context to use to obtain |
| 111 |
// the public key, pass option.WithContext(desiredCtx). |
| 112 |
// |
| 113 |
// All other options are ignored if specified. |
| 114 |
func (a *SignerVerifier) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey, error) { |
| 115 |
ctx := context.Background() |
| 116 |
for _, opt := range opts { |
| 117 |
opt.ApplyContext(&ctx) |
| 118 |
} |
| 119 |
|
| 120 |
cmk, err := a.client.getCMK(ctx) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
return cmk.PublicKey, nil |
| 125 |
} |
| 126 |
|
| 127 |
// VerifySignature verifies the signature for the given message. Unless provided |
| 128 |
// in an option, the digest of the message will be computed using the hash function specified |
| 129 |
// when the SignerVerifier was created. |
| 130 |
// |
| 131 |
// This function returns nil if the verification succeeded, and an error message otherwise. |
| 132 |
// |
| 133 |
// This function recognizes the following Options listed in order of preference: |
| 134 |
// |
| 135 |
// - WithContext() |
| 136 |
// |
| 137 |
// - WithDigest() |
| 138 |
// |
| 139 |
// - WithRemoteVerification() |
| 140 |
// |
| 141 |
// - WithCryptoSignerOpts() |
| 142 |
// |
| 143 |
// All other options are ignored if specified. |
| 144 |
func (a *SignerVerifier) VerifySignature(sig, message io.Reader, opts ...signature.VerifyOption) (err error) { |
| 145 |
ctx := context.Background() |
| 146 |
var digest []byte |
| 147 |
var remoteVerification bool |
| 148 |
|
| 149 |
for _, opt := range opts { |
| 150 |
opt.ApplyContext(&ctx) |
| 151 |
opt.ApplyDigest(&digest) |
| 152 |
opt.ApplyRemoteVerification(&remoteVerification) |
| 153 |
} |
| 154 |
|
| 155 |
if !remoteVerification { |
| 156 |
return a.client.verify(ctx, sig, message, opts...) |
| 157 |
} |
| 158 |
|
| 159 |
var signerOpts crypto.SignerOpts |
| 160 |
signerOpts, err = a.client.getHashFunc(ctx) |
| 161 |
if err != nil { |
| 162 |
return fmt.Errorf("getting hash func: %w", err) |
| 163 |
} |
| 164 |
for _, opt := range opts { |
| 165 |
opt.ApplyCryptoSignerOpts(&signerOpts) |
| 166 |
} |
| 167 |
hf := signerOpts.HashFunc() |
| 168 |
|
| 169 |
if len(digest) == 0 { |
| 170 |
digest, _, err = signature.ComputeDigestForVerifying(message, hf, awsSupportedHashFuncs, opts...) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
sigBytes, err := io.ReadAll(sig) |
| 177 |
if err != nil { |
| 178 |
return fmt.Errorf("reading signature: %w", err) |
| 179 |
} |
| 180 |
return a.client.verifyRemotely(ctx, sigBytes, digest) |
| 181 |
} |
| 182 |
|
| 183 |
// CreateKey attempts to create a new key in Vault with the specified algorithm. |
| 184 |
func (a *SignerVerifier) CreateKey(ctx context.Context, algorithm string) (crypto.PublicKey, error) { |
| 185 |
return a.client.createKey(ctx, algorithm) |
| 186 |
} |
| 187 |
|
| 188 |
type cryptoSignerWrapper struct { |
| 189 |
ctx context.Context |
| 190 |
hashFunc crypto.Hash |
| 191 |
sv *SignerVerifier |
| 192 |
errFunc func(error) |
| 193 |
} |
| 194 |
|
| 195 |
func (c cryptoSignerWrapper) Public() crypto.PublicKey { |
| 196 |
pk, err := c.sv.PublicKey(options.WithContext(c.ctx)) |
| 197 |
if err != nil && c.errFunc != nil { |
| 198 |
c.errFunc(err) |
| 199 |
} |
| 200 |
return pk |
| 201 |
} |
| 202 |
|
| 203 |
func (c cryptoSignerWrapper) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 204 |
hashFunc := c.hashFunc |
| 205 |
if opts != nil { |
| 206 |
hashFunc = opts.HashFunc() |
| 207 |
} |
| 208 |
awsOptions := []signature.SignOption{ |
| 209 |
options.WithContext(c.ctx), |
| 210 |
options.WithDigest(digest), |
| 211 |
options.WithCryptoSignerOpts(hashFunc), |
| 212 |
} |
| 213 |
|
| 214 |
return c.sv.SignMessage(nil, awsOptions...) |
| 215 |
} |
| 216 |
|
| 217 |
// CryptoSigner returns a crypto.Signer object that uses the underlying SignerVerifier, along with a crypto.SignerOpts object |
| 218 |
// that allows the KMS to be used in APIs that only accept the standard golang objects |
| 219 |
func (a *SignerVerifier) CryptoSigner(ctx context.Context, errFunc func(error)) (crypto.Signer, crypto.SignerOpts, error) { |
| 220 |
defaultHf, err := a.client.getHashFunc(ctx) |
| 221 |
if err != nil { |
| 222 |
return nil, nil, fmt.Errorf("getting fetching default hash function: %w", err) |
| 223 |
} |
| 224 |
|
| 225 |
csw := &cryptoSignerWrapper{ |
| 226 |
ctx: ctx, |
| 227 |
sv: a, |
| 228 |
hashFunc: defaultHf, |
| 229 |
errFunc: errFunc, |
| 230 |
} |
| 231 |
|
| 232 |
return csw, defaultHf, nil |
| 233 |
} |
| 234 |
|
| 235 |
// SupportedAlgorithms returns the list of algorithms supported by the AWS KMS service |
| 236 |
func (*SignerVerifier) SupportedAlgorithms() []string { |
| 237 |
s := make([]string, len(awsSupportedAlgorithms)) |
| 238 |
for i := range awsSupportedAlgorithms { |
| 239 |
s[i] = string(awsSupportedAlgorithms[i]) |
| 240 |
} |
| 241 |
return s |
| 242 |
} |
| 243 |
|
| 244 |
// DefaultAlgorithm returns the default algorithm for the AWS KMS service |
| 245 |
func (*SignerVerifier) DefaultAlgorithm() string { |
| 246 |
return string(types.CustomerMasterKeySpecEccNistP256) |
| 247 |
} |