| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package signer provides notation signing functionality. It implements the |
| 15 |
// notation.Signer interface by providing builtinSigner for local signing and |
| 16 |
// PluginSigner for remote signing. |
| 17 |
package signer |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"crypto" |
| 22 |
"crypto/tls" |
| 23 |
"crypto/x509" |
| 24 |
"encoding/json" |
| 25 |
"errors" |
| 26 |
"fmt" |
| 27 |
"time" |
| 28 |
|
| 29 |
"github.com/notaryproject/notation-core-go/signature" |
| 30 |
"github.com/notaryproject/notation-go" |
| 31 |
"github.com/notaryproject/notation-go/internal/envelope" |
| 32 |
"github.com/notaryproject/notation-go/log" |
| 33 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 34 |
) |
| 35 |
|
| 36 |
// signingAgent is the unprotected header field used by signature. |
| 37 |
const signingAgent = "notation-go/1.3.2" |
| 38 |
|
| 39 |
// GenericSigner implements notation.Signer and embeds signature.Signer |
| 40 |
type GenericSigner struct { |
| 41 |
signer signature.Signer |
| 42 |
} |
| 43 |
|
| 44 |
// New returns a builtinSigner given key and cert chain |
| 45 |
// |
| 46 |
// Deprecated: New function exists for historical compatibility and should not be used. |
| 47 |
// To create GenericSigner, use NewGenericSigner() function. |
| 48 |
func New(key crypto.PrivateKey, certChain []*x509.Certificate) (notation.Signer, error) { |
| 49 |
return NewGenericSigner(key, certChain) |
| 50 |
} |
| 51 |
|
| 52 |
// NewGenericSigner returns a builtinSigner given key and cert chain |
| 53 |
func NewGenericSigner(key crypto.PrivateKey, certChain []*x509.Certificate) (*GenericSigner, error) { |
| 54 |
localSigner, err := signature.NewLocalSigner(certChain, key) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
return &GenericSigner{ |
| 59 |
signer: localSigner, |
| 60 |
}, nil |
| 61 |
} |
| 62 |
|
| 63 |
// NewFromFiles returns a builtinSigner given key and certChain paths. |
| 64 |
func NewFromFiles(keyPath, certChainPath string) (notation.Signer, error) { |
| 65 |
return NewGenericSignerFromFiles(keyPath, certChainPath) |
| 66 |
} |
| 67 |
|
| 68 |
// NewGenericSignerFromFiles returns a builtinSigner given key and certChain paths. |
| 69 |
func NewGenericSignerFromFiles(keyPath, certChainPath string) (*GenericSigner, error) { |
| 70 |
if keyPath == "" { |
| 71 |
return nil, errors.New("key path not specified") |
| 72 |
} |
| 73 |
if certChainPath == "" { |
| 74 |
return nil, errors.New("certificate path not specified") |
| 75 |
} |
| 76 |
|
| 77 |
// read key / cert pair |
| 78 |
cert, err := tls.LoadX509KeyPair(certChainPath, keyPath) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
if len(cert.Certificate) == 0 { |
| 83 |
return nil, fmt.Errorf("%q does not contain certificate", certChainPath) |
| 84 |
} |
| 85 |
|
| 86 |
// parse cert |
| 87 |
certs := make([]*x509.Certificate, len(cert.Certificate)) |
| 88 |
for i, c := range cert.Certificate { |
| 89 |
certs[i], err = x509.ParseCertificate(c) |
| 90 |
if err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// create signer |
| 96 |
return NewGenericSigner(cert.PrivateKey, certs) |
| 97 |
} |
| 98 |
|
| 99 |
// Sign signs the artifact described by its descriptor and returns the |
| 100 |
// marshalled envelope. |
| 101 |
func (s *GenericSigner) Sign(ctx context.Context, desc ocispec.Descriptor, opts notation.SignerSignOptions) ([]byte, *signature.SignerInfo, error) { |
| 102 |
logger := log.GetLogger(ctx) |
| 103 |
logger.Debugf("Generic signing for %v in signature media type %v", desc.Digest, opts.SignatureMediaType) |
| 104 |
// Generate payload to be signed. |
| 105 |
payload := envelope.Payload{TargetArtifact: envelope.SanitizeTargetArtifact(desc)} |
| 106 |
payloadBytes, err := json.Marshal(payload) |
| 107 |
if err != nil { |
| 108 |
return nil, nil, fmt.Errorf("envelope payload can't be marshalled: %w", err) |
| 109 |
} |
| 110 |
var signingAgentId string |
| 111 |
if opts.SigningAgent != "" { |
| 112 |
signingAgentId = opts.SigningAgent |
| 113 |
} else { |
| 114 |
signingAgentId = signingAgent |
| 115 |
} |
| 116 |
if opts.Timestamper != nil && opts.TSARootCAs == nil { |
| 117 |
return nil, nil, errors.New("timestamping: got Timestamper but nil TSARootCAs") |
| 118 |
} |
| 119 |
if opts.TSARootCAs != nil && opts.Timestamper == nil { |
| 120 |
return nil, nil, errors.New("timestamping: got TSARootCAs but nil Timestamper") |
| 121 |
} |
| 122 |
signReq := &signature.SignRequest{ |
| 123 |
Payload: signature.Payload{ |
| 124 |
ContentType: envelope.MediaTypePayloadV1, |
| 125 |
Content: payloadBytes, |
| 126 |
}, |
| 127 |
Signer: s.signer, |
| 128 |
SigningTime: time.Now(), |
| 129 |
SigningScheme: signature.SigningSchemeX509, |
| 130 |
SigningAgent: signingAgentId, |
| 131 |
Timestamper: opts.Timestamper, |
| 132 |
TSARootCAs: opts.TSARootCAs, |
| 133 |
TSARevocationValidator: opts.TSARevocationValidator, |
| 134 |
} |
| 135 |
|
| 136 |
// Add expiry only if ExpiryDuration is not zero |
| 137 |
if opts.ExpiryDuration != 0 { |
| 138 |
signReq.Expiry = signReq.SigningTime.Add(opts.ExpiryDuration) |
| 139 |
} |
| 140 |
logger.Debugf("Sign request:") |
| 141 |
logger.Debugf(" ContentType: %v", signReq.Payload.ContentType) |
| 142 |
logger.Debugf(" Content: %s", string(signReq.Payload.Content)) |
| 143 |
logger.Debugf(" SigningTime: %v", signReq.SigningTime) |
| 144 |
logger.Debugf(" Expiry: %v", signReq.Expiry) |
| 145 |
logger.Debugf(" SigningScheme: %v", signReq.SigningScheme) |
| 146 |
logger.Debugf(" SigningAgent: %v", signReq.SigningAgent) |
| 147 |
if signReq.Timestamper != nil { |
| 148 |
logger.Debug("Enabled timestamping") |
| 149 |
if signReq.TSARevocationValidator != nil { |
| 150 |
logger.Debug("Enabled timestamping certificate chain revocation check") |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Add ctx to the SignRequest |
| 155 |
signReq = signReq.WithContext(ctx) |
| 156 |
|
| 157 |
// perform signing |
| 158 |
sigEnv, err := signature.NewEnvelope(opts.SignatureMediaType) |
| 159 |
if err != nil { |
| 160 |
return nil, nil, err |
| 161 |
} |
| 162 |
sig, err := sigEnv.Sign(signReq) |
| 163 |
if err != nil { |
| 164 |
return nil, nil, err |
| 165 |
} |
| 166 |
envContent, err := sigEnv.Verify() |
| 167 |
if err != nil { |
| 168 |
return nil, nil, fmt.Errorf("generated signature failed verification: %v", err) |
| 169 |
} |
| 170 |
if err := envelope.ValidatePayloadContentType(&envContent.Payload); err != nil { |
| 171 |
return nil, nil, err |
| 172 |
} |
| 173 |
return sig, &envContent.SignerInfo, nil |
| 174 |
} |