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
|
//
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package git
import (
"context"
"crypto/x509"
"encoding/pem"
"fmt"
"time"
cms "github.com/sigstore/gitsign/internal/fork/ietf-cms"
"github.com/sigstore/gitsign/internal/fulcio/fulcioroots"
"github.com/sigstore/sigstore/pkg/tuf"
)
// Verifier verifies git commit signature data.
type Verifier interface {
Verify(ctx context.Context, data, sig []byte, detached bool) (*x509.Certificate, error)
}
// CertVerifier is the default implementation of Verifier.
// It verifies git commits against a given CertPool. By default, the system
// CertPool + Fulcio roots are used for validation.
type CertVerifier struct {
roots *x509.CertPool
intermediates *x509.CertPool
tsa *x509.CertPool
}
type CertVerifierOption func(*CertVerifier) error
func NewCertVerifier(opts ...CertVerifierOption) (*CertVerifier, error) {
v := &CertVerifier{}
for _, o := range opts {
if err := o(v); err != nil {
return nil, err
}
}
// Use empty pool if not set - this makes it so that we don't fallback
// to the system pool.
if v.roots == nil {
v.roots = x509.NewCertPool()
}
return v, nil
}
// WithRootPool sets the base CertPool for the verifier.
func WithRootPool(pool *x509.CertPool) CertVerifierOption {
return func(v *CertVerifier) error {
v.roots = pool
return nil
}
}
// WithIntermediatePool sets the base intermediate CertPool for the verifier.
func WithIntermediatePool(pool *x509.CertPool) CertVerifierOption {
return func(v *CertVerifier) error {
v.intermediates = pool
return nil
}
}
// WithIntermediatePool sets the base intermediate CertPool for the verifier.
func WithTimestampCertPool(pool *x509.CertPool) CertVerifierOption {
return func(v *CertVerifier) error {
v.tsa = pool
return nil
}
}
// Verify verifies for a given Git data + signature pair.
//
// Data should be the Git data that was signed (i.e. everything in the commit
// besides the signature). Note: passing in the commit object itself will not
// work.
//
// Signatures should be CMS/PKCS7 formatted.
func (v *CertVerifier) Verify(_ context.Context, data, sig []byte, detached bool) (*x509.Certificate, error) {
// Try decoding as PEM
var der []byte
if blk, _ := pem.Decode(sig); blk != nil {
der = blk.Bytes
} else {
der = sig
}
// Parse signature
sd, err := cms.ParseSignedData(der)
if err != nil {
return nil, fmt.Errorf("failed to parse signature: %w", err)
}
// Generate verification options.
certs, err := sd.GetCertificates()
if err != nil {
return nil, fmt.Errorf("error getting signature certs: %w", err)
}
cert := certs[0]
opts := x509.VerifyOptions{
Roots: v.roots,
Intermediates: v.intermediates,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning},
// cosign hack: ignore the current time for now - we'll use the tlog to
// verify whether the commit was signed at a valid time.
CurrentTime: cert.NotBefore.Add(1 * time.Minute),
}
tsaOpts := x509.VerifyOptions{
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping},
}
if v.tsa != nil {
tsaOpts.Roots = v.tsa
}
if detached {
if _, err := sd.VerifyDetached(data, opts, tsaOpts); err != nil {
return nil, fmt.Errorf("failed to verify detached signature: %w", err)
}
} else {
if _, err := sd.Verify(opts, tsaOpts); err != nil {
return nil, fmt.Errorf("failed to verify attached signature: %w", err)
}
}
return cert, nil
}
// NewDefaultVerifier returns a new CertVerifier with the default Fulcio roots loaded from the local TUF client.
// See https://docs.sigstore.dev/system_config/custom_components/ for how to customize this behavior.
func NewDefaultVerifier(ctx context.Context) (*CertVerifier, error) {
if err := tuf.Initialize(ctx, tuf.DefaultRemoteRoot, nil); err != nil {
return nil, err
}
root, intermediate, err := fulcioroots.New(x509.NewCertPool(), fulcioroots.FromTUF(ctx))
if err != nil {
return nil, err
}
return NewCertVerifier(WithRootPool(root), WithIntermediatePool(intermediate))
}
|