| 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 azure |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"crypto" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"math/big" |
| 25 |
|
| 26 |
"golang.org/x/crypto/cryptobyte" |
| 27 |
"golang.org/x/crypto/cryptobyte/asn1" |
| 28 |
|
| 29 |
"github.com/sigstore/sigstore/pkg/signature" |
| 30 |
"github.com/sigstore/sigstore/pkg/signature/options" |
| 31 |
) |
| 32 |
|
| 33 |
var azureSupportedHashFuncs = []crypto.Hash{ |
| 34 |
crypto.SHA256, |
| 35 |
crypto.SHA384, |
| 36 |
crypto.SHA512, |
| 37 |
} |
| 38 |
|
| 39 |
//nolint:revive |
| 40 |
const ( |
| 41 |
AlgorithmES256 = "ES256" |
| 42 |
AlgorithmES384 = "ES384" |
| 43 |
AlgorithmES512 = "ES512" |
| 44 |
) |
| 45 |
|
| 46 |
var azureSupportedAlgorithms = []string{ |
| 47 |
AlgorithmES256, |
| 48 |
AlgorithmES384, |
| 49 |
AlgorithmES512, |
| 50 |
} |
| 51 |
|
| 52 |
// SignerVerifier creates and verifies digital signatures over a message using Azure KMS service |
| 53 |
type SignerVerifier struct { |
| 54 |
defaultCtx context.Context |
| 55 |
client *azureVaultClient |
| 56 |
} |
| 57 |
|
| 58 |
// LoadSignerVerifier generates signatures using the specified key in Azure Key Vault and hash algorithm. |
| 59 |
// |
| 60 |
// It also can verify signatures locally using the public key. hashFunc must not be crypto.Hash(0). |
| 61 |
func LoadSignerVerifier(defaultCtx context.Context, referenceStr string) (*SignerVerifier, error) { |
| 62 |
a := &SignerVerifier{ |
| 63 |
defaultCtx: defaultCtx, |
| 64 |
} |
| 65 |
|
| 66 |
var err error |
| 67 |
a.client, err = newAzureKMS(referenceStr) |
| 68 |
if err != nil { |
| 69 |
return nil, err |
| 70 |
} |
| 71 |
|
| 72 |
return a, nil |
| 73 |
} |
| 74 |
|
| 75 |
// SignMessage signs the provided message using Azure Key Vault. If the message is provided, |
| 76 |
// this method will compute the digest according to the hash function specified |
| 77 |
// when the Signer was created. |
| 78 |
// |
| 79 |
// SignMessage recognizes the following Options listed in order of preference: |
| 80 |
// |
| 81 |
// - WithContext() |
| 82 |
// |
| 83 |
// - WithDigest() |
| 84 |
// |
| 85 |
// - WithCryptoSignerOpts() |
| 86 |
// |
| 87 |
// All other options are ignored if specified. |
| 88 |
func (a *SignerVerifier) SignMessage(message io.Reader, opts ...signature.SignOption) ([]byte, error) { |
| 89 |
var digest []byte |
| 90 |
ctx := a.defaultCtx |
| 91 |
|
| 92 |
for _, opt := range opts { |
| 93 |
opt.ApplyContext(&ctx) |
| 94 |
opt.ApplyDigest(&digest) |
| 95 |
} |
| 96 |
|
| 97 |
hashFunc, _, err := a.client.getKeyVaultHashFunc(ctx) |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
|
| 102 |
digest, _, err = signature.ComputeDigestForSigning(message, hashFunc, azureSupportedHashFuncs, opts...) |
| 103 |
if err != nil { |
| 104 |
return nil, err |
| 105 |
} |
| 106 |
|
| 107 |
rawSig, err := a.client.sign(ctx, digest) |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
|
| 112 |
l := len(rawSig) |
| 113 |
r, s := &big.Int{}, &big.Int{} |
| 114 |
r.SetBytes(rawSig[0 : l/2]) |
| 115 |
s.SetBytes(rawSig[l/2:]) |
| 116 |
|
| 117 |
// Convert the concatenated r||s byte string to an ASN.1 sequence |
| 118 |
// This logic is borrowed from https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/crypto/ecdsa/ecdsa.go;l=121 |
| 119 |
var b cryptobyte.Builder |
| 120 |
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) { |
| 121 |
b.AddASN1BigInt(r) |
| 122 |
b.AddASN1BigInt(s) |
| 123 |
}) |
| 124 |
|
| 125 |
return b.Bytes() |
| 126 |
} |
| 127 |
|
| 128 |
// VerifySignature verifies the signature for the given message. Unless provided |
| 129 |
// in an option, the digest of the message will be computed using the hash function specified |
| 130 |
// when the SignerVerifier was created. |
| 131 |
// |
| 132 |
// This function returns nil if the verification succeeded, and an error message otherwise. |
| 133 |
// |
| 134 |
// This function recognizes the following Options listed in order of preference: |
| 135 |
// |
| 136 |
// - WithDigest() |
| 137 |
// |
| 138 |
// All other options are ignored if specified. |
| 139 |
func (a *SignerVerifier) VerifySignature(sig, message io.Reader, opts ...signature.VerifyOption) error { |
| 140 |
hashFunc, _, err := a.client.getKeyVaultHashFunc(a.defaultCtx) |
| 141 |
if err != nil { |
| 142 |
return err |
| 143 |
} |
| 144 |
|
| 145 |
var digest []byte |
| 146 |
var signerOpts crypto.SignerOpts = hashFunc |
| 147 |
for _, opt := range opts { |
| 148 |
opt.ApplyDigest(&digest) |
| 149 |
} |
| 150 |
|
| 151 |
digest, _, err = signature.ComputeDigestForVerifying(message, signerOpts.HashFunc(), azureSupportedHashFuncs, opts...) |
| 152 |
if err != nil { |
| 153 |
return err |
| 154 |
} |
| 155 |
|
| 156 |
sigBytes, err := io.ReadAll(sig) |
| 157 |
if err != nil { |
| 158 |
return fmt.Errorf("reading signature: %w", err) |
| 159 |
} |
| 160 |
|
| 161 |
// Convert the ASN.1 Sequence to a concatenated r||s byte string |
| 162 |
// This logic is borrowed from https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/crypto/ecdsa/ecdsa.go;l=339 |
| 163 |
var ( |
| 164 |
r, s = &big.Int{}, &big.Int{} |
| 165 |
inner cryptobyte.String |
| 166 |
) |
| 167 |
input := cryptobyte.String(sigBytes) |
| 168 |
if !input.ReadASN1(&inner, asn1.SEQUENCE) || |
| 169 |
!input.Empty() || |
| 170 |
!inner.ReadASN1Integer(r) || |
| 171 |
!inner.ReadASN1Integer(s) || |
| 172 |
!inner.Empty() { |
| 173 |
return errors.New("parsing signature") |
| 174 |
} |
| 175 |
|
| 176 |
rawSigBytes := []byte{} |
| 177 |
rawSigBytes = append(rawSigBytes, r.Bytes()...) |
| 178 |
rawSigBytes = append(rawSigBytes, s.Bytes()...) |
| 179 |
return a.client.verify(a.defaultCtx, rawSigBytes, digest) |
| 180 |
} |
| 181 |
|
| 182 |
// PublicKey returns the public key that can be used to verify signatures created by |
| 183 |
// this signer. All options provided in arguments to this method are ignored. |
| 184 |
func (a *SignerVerifier) PublicKey(_ ...signature.PublicKeyOption) (crypto.PublicKey, error) { |
| 185 |
return a.client.public(a.defaultCtx) |
| 186 |
} |
| 187 |
|
| 188 |
// CreateKey attempts to create a new key in Vault with the specified algorithm. |
| 189 |
func (a *SignerVerifier) CreateKey(ctx context.Context, _ string) (crypto.PublicKey, error) { |
| 190 |
return a.client.createKey(ctx) |
| 191 |
} |
| 192 |
|
| 193 |
type cryptoSignerWrapper struct { |
| 194 |
ctx context.Context |
| 195 |
hashFunc crypto.Hash |
| 196 |
sv *SignerVerifier |
| 197 |
errFunc func(error) |
| 198 |
} |
| 199 |
|
| 200 |
func (c cryptoSignerWrapper) Public() crypto.PublicKey { |
| 201 |
pk, err := c.sv.PublicKey(options.WithContext(c.ctx)) |
| 202 |
if err != nil && c.errFunc != nil { |
| 203 |
c.errFunc(err) |
| 204 |
} |
| 205 |
return pk |
| 206 |
} |
| 207 |
|
| 208 |
func (c cryptoSignerWrapper) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 209 |
hashFunc := c.hashFunc |
| 210 |
if opts != nil { |
| 211 |
hashFunc = opts.HashFunc() |
| 212 |
} |
| 213 |
azOptions := []signature.SignOption{ |
| 214 |
options.WithContext(c.ctx), |
| 215 |
options.WithDigest(digest), |
| 216 |
options.WithCryptoSignerOpts(hashFunc), |
| 217 |
} |
| 218 |
|
| 219 |
return c.sv.SignMessage(nil, azOptions...) |
| 220 |
} |
| 221 |
|
| 222 |
// CryptoSigner returns a crypto.Signer object that uses the underlying SignerVerifier, along with a crypto.SignerOpts object |
| 223 |
// that allows the KMS to be used in APIs that only accept the standard golang objects |
| 224 |
func (a *SignerVerifier) CryptoSigner(ctx context.Context, errFunc func(error)) (crypto.Signer, crypto.SignerOpts, error) { |
| 225 |
hashFunc, _, err := a.client.getKeyVaultHashFunc(a.defaultCtx) |
| 226 |
if err != nil { |
| 227 |
return nil, nil, err |
| 228 |
} |
| 229 |
|
| 230 |
csw := &cryptoSignerWrapper{ |
| 231 |
ctx: ctx, |
| 232 |
sv: a, |
| 233 |
hashFunc: hashFunc, |
| 234 |
errFunc: errFunc, |
| 235 |
} |
| 236 |
|
| 237 |
return csw, hashFunc, nil |
| 238 |
} |
| 239 |
|
| 240 |
// SupportedAlgorithms returns the list of algorithms supported by the Azure KMS service |
| 241 |
func (*SignerVerifier) SupportedAlgorithms() []string { |
| 242 |
return azureSupportedAlgorithms |
| 243 |
} |
| 244 |
|
| 245 |
// DefaultAlgorithm returns the default algorithm for the Azure KMS service |
| 246 |
func (*SignerVerifier) DefaultAlgorithm() string { |
| 247 |
return AlgorithmES256 |
| 248 |
} |