| 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 gcp |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"crypto" |
| 21 |
"fmt" |
| 22 |
"hash/crc32" |
| 23 |
"io" |
| 24 |
|
| 25 |
"github.com/sigstore/sigstore/pkg/signature" |
| 26 |
"github.com/sigstore/sigstore/pkg/signature/options" |
| 27 |
"google.golang.org/api/option" |
| 28 |
) |
| 29 |
|
| 30 |
var gcpSupportedHashFuncs = []crypto.Hash{ |
| 31 |
crypto.SHA256, |
| 32 |
crypto.SHA512, |
| 33 |
crypto.SHA384, |
| 34 |
} |
| 35 |
|
| 36 |
// SignerVerifier creates and verifies digital signatures over a message using GCP KMS service |
| 37 |
type SignerVerifier struct { |
| 38 |
defaultCtx context.Context |
| 39 |
client *gcpClient |
| 40 |
} |
| 41 |
|
| 42 |
// LoadSignerVerifier generates signatures using the specified key object in GCP KMS and hash algorithm. |
| 43 |
// |
| 44 |
// It also can verify signatures locally using the public key. hashFunc must not be crypto.Hash(0). |
| 45 |
func LoadSignerVerifier(defaultCtx context.Context, referenceStr string, opts ...option.ClientOption) (*SignerVerifier, error) { |
| 46 |
g := &SignerVerifier{ |
| 47 |
defaultCtx: defaultCtx, |
| 48 |
} |
| 49 |
|
| 50 |
var err error |
| 51 |
g.client, err = newGCPClient(defaultCtx, referenceStr, opts...) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
|
| 56 |
return g, nil |
| 57 |
} |
| 58 |
|
| 59 |
// SignMessage signs the provided message using GCP KMS. If the message is provided, |
| 60 |
// this method will compute the digest according to the hash function specified |
| 61 |
// when the Signer was created. |
| 62 |
// |
| 63 |
// SignMessage recognizes the following Options listed in order of preference: |
| 64 |
// |
| 65 |
// - WithContext() |
| 66 |
// |
| 67 |
// - WithDigest() |
| 68 |
// |
| 69 |
// - WithCryptoSignerOpts() |
| 70 |
// |
| 71 |
// All other options are ignored if specified. |
| 72 |
func (g *SignerVerifier) SignMessage(message io.Reader, opts ...signature.SignOption) ([]byte, error) { |
| 73 |
ctx := context.Background() |
| 74 |
var digest []byte |
| 75 |
var signerOpts crypto.SignerOpts |
| 76 |
var err error |
| 77 |
|
| 78 |
signerOpts, err = g.client.getHashFunc() |
| 79 |
if err != nil { |
| 80 |
return nil, fmt.Errorf("getting fetching default hash function: %w", err) |
| 81 |
} |
| 82 |
|
| 83 |
for _, opt := range opts { |
| 84 |
opt.ApplyContext(&ctx) |
| 85 |
opt.ApplyDigest(&digest) |
| 86 |
opt.ApplyCryptoSignerOpts(&signerOpts) |
| 87 |
} |
| 88 |
|
| 89 |
digest, hf, err := signature.ComputeDigestForSigning(message, signerOpts.HashFunc(), gcpSupportedHashFuncs, opts...) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
|
| 94 |
crc32cHasher := crc32.New(crc32.MakeTable(crc32.Castagnoli)) |
| 95 |
_, err = crc32cHasher.Write(digest) |
| 96 |
if err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
|
| 100 |
return g.client.sign(ctx, digest, hf, crc32cHasher.Sum32()) |
| 101 |
} |
| 102 |
|
| 103 |
// PublicKey returns the public key that can be used to verify signatures created by |
| 104 |
// this signer. If the caller wishes to specify the context to use to obtain |
| 105 |
// the public key, pass option.WithContext(desiredCtx). |
| 106 |
// |
| 107 |
// All other options are ignored if specified. |
| 108 |
func (g *SignerVerifier) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey, error) { |
| 109 |
ctx := context.Background() |
| 110 |
for _, opt := range opts { |
| 111 |
opt.ApplyContext(&ctx) |
| 112 |
} |
| 113 |
|
| 114 |
return g.client.public(ctx) |
| 115 |
} |
| 116 |
|
| 117 |
// VerifySignature verifies the signature for the given message. Unless provided |
| 118 |
// in an option, the digest of the message will be computed using the hash function specified |
| 119 |
// when the SignerVerifier was created. |
| 120 |
// |
| 121 |
// This function returns nil if the verification succeeded, and an error message otherwise. |
| 122 |
// |
| 123 |
// This function recognizes the following Options listed in order of preference: |
| 124 |
// |
| 125 |
// - WithDigest() |
| 126 |
// |
| 127 |
// All other options are ignored if specified. |
| 128 |
func (g *SignerVerifier) VerifySignature(signature, message io.Reader, opts ...signature.VerifyOption) error { |
| 129 |
return g.client.verify(signature, message, opts...) |
| 130 |
} |
| 131 |
|
| 132 |
// CreateKey attempts to create a new key in Vault with the specified algorithm. |
| 133 |
func (g *SignerVerifier) CreateKey(ctx context.Context, algorithm string) (crypto.PublicKey, error) { |
| 134 |
return g.client.createKey(ctx, algorithm) |
| 135 |
} |
| 136 |
|
| 137 |
type cryptoSignerWrapper struct { |
| 138 |
ctx context.Context |
| 139 |
hashFunc crypto.Hash |
| 140 |
sv *SignerVerifier |
| 141 |
errFunc func(error) |
| 142 |
} |
| 143 |
|
| 144 |
func (c cryptoSignerWrapper) Public() crypto.PublicKey { |
| 145 |
pk, err := c.sv.PublicKey(options.WithContext(c.ctx)) |
| 146 |
if err != nil && c.errFunc != nil { |
| 147 |
c.errFunc(err) |
| 148 |
} |
| 149 |
return pk |
| 150 |
} |
| 151 |
|
| 152 |
func (c cryptoSignerWrapper) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 153 |
hashFunc := c.hashFunc |
| 154 |
if opts != nil { |
| 155 |
hashFunc = opts.HashFunc() |
| 156 |
} |
| 157 |
gcpOptions := []signature.SignOption{ |
| 158 |
options.WithContext(c.ctx), |
| 159 |
options.WithDigest(digest), |
| 160 |
options.WithCryptoSignerOpts(hashFunc), |
| 161 |
} |
| 162 |
|
| 163 |
return c.sv.SignMessage(nil, gcpOptions...) |
| 164 |
} |
| 165 |
|
| 166 |
// CryptoSigner returns a crypto.Signer object that uses the underlying SignerVerifier, along with a crypto.SignerOpts object |
| 167 |
// that allows the KMS to be used in APIs that only accept the standard golang objects |
| 168 |
func (g *SignerVerifier) CryptoSigner(ctx context.Context, errFunc func(error)) (crypto.Signer, crypto.SignerOpts, error) { |
| 169 |
defaultHf, err := g.client.getHashFunc() |
| 170 |
if err != nil { |
| 171 |
return nil, nil, fmt.Errorf("getting fetching default hash function: %w", err) |
| 172 |
} |
| 173 |
|
| 174 |
csw := &cryptoSignerWrapper{ |
| 175 |
ctx: ctx, |
| 176 |
sv: g, |
| 177 |
hashFunc: defaultHf, |
| 178 |
errFunc: errFunc, |
| 179 |
} |
| 180 |
|
| 181 |
return csw, defaultHf, nil |
| 182 |
} |
| 183 |
|
| 184 |
// SupportedAlgorithms returns the list of algorithms supported by the GCP KMS service |
| 185 |
func (g *SignerVerifier) SupportedAlgorithms() (result []string) { |
| 186 |
for k := range algorithmMap { |
| 187 |
result = append(result, k) |
| 188 |
} |
| 189 |
return |
| 190 |
} |
| 191 |
|
| 192 |
// DefaultAlgorithm returns the default algorithm for the GCP KMS service |
| 193 |
func (g *SignerVerifier) DefaultAlgorithm() string { |
| 194 |
return AlgorithmECDSAP256SHA256 |
| 195 |
} |