Debian Codegraph @

signature/internal/base/envelope.go

raw
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 base
15
16 import (
17 "crypto/x509"
18 "errors"
19 "fmt"
20 "time"
21
22 "github.com/notaryproject/notation-core-go/signature"
23 nx509 "github.com/notaryproject/notation-core-go/x509"
24 )
25
26 // Envelope represents a general envelope wrapping a raw signature and envelope
27 // in specific format.
28 // Envelope manipulates the common validation shared by internal envelopes.
29 type Envelope struct {
30 signature.Envelope // internal envelope in a specific format (e.g. COSE, JWS)
31 Raw []byte // raw signature
32 }
33
34 // Sign generates signature in terms of given SignRequest.
35 //
36 // Reference: https://github.com/notaryproject/notaryproject/blob/main/specs/signing-and-verification-workflow.md#signing-steps
37 func (e *Envelope) Sign(req *signature.SignRequest) ([]byte, error) {
38 // Canonicalize request.
39 req.SigningTime = req.SigningTime.Truncate(time.Second)
40 req.Expiry = req.Expiry.Truncate(time.Second)
41 err := validateSignRequest(req)
42 if err != nil {
43 return nil, err
44 }
45
46 raw, err := e.Envelope.Sign(req)
47 if err != nil {
48 return nil, err
49 }
50
51 // validate certificate chain
52 content, err := e.Envelope.Content()
53 if err != nil {
54 return nil, err
55 }
56 if err := validateCertificateChain(
57 content.SignerInfo.CertificateChain,
58 &content.SignerInfo.SignedAttributes.SigningTime,
59 content.SignerInfo.SignatureAlgorithm,
60 ); err != nil {
61 return nil, err
62 }
63
64 // store the raw signature
65 e.Raw = raw
66
67 return e.Raw, nil
68 }
69
70 // Verify performs integrity and other signature specification related
71 // validations.
72 // It returns envelope content containing the payload to be signed and
73 // SignerInfo object containing the information about the signature.
74 //
75 // Reference: https://github.com/notaryproject/notaryproject/blob/main/specs/trust-store-trust-policy.md#steps
76 func (e *Envelope) Verify() (*signature.EnvelopeContent, error) {
77 // validation before the core verify process.
78 if len(e.Raw) == 0 {
79 return nil, &signature.SignatureNotFoundError{}
80 }
81
82 // core verify process.
83 content, err := e.Envelope.Verify()
84 if err != nil {
85 return nil, err
86 }
87
88 // validation after the core verify process.
89 if err = validateEnvelopeContent(content); err != nil {
90 return nil, err
91 }
92
93 return content, nil
94 }
95
96 // Content returns the validated signature information and payload.
97 func (e *Envelope) Content() (*signature.EnvelopeContent, error) {
98 if len(e.Raw) == 0 {
99 return nil, &signature.SignatureNotFoundError{}
100 }
101
102 content, err := e.Envelope.Content()
103 if err != nil {
104 return nil, err
105 }
106
107 if err = validateEnvelopeContent(content); err != nil {
108 return nil, err
109 }
110
111 return content, nil
112 }
113
114 // validateSignRequest performs basic set of validations on SignRequest struct.
115 func validateSignRequest(req *signature.SignRequest) error {
116 if err := validatePayload(&req.Payload); err != nil {
117 return &signature.InvalidSignRequestError{Msg: err.Error()}
118 }
119
120 if err := validateSigningAndExpiryTime(req.SigningTime, req.Expiry); err != nil {
121 return err
122 }
123
124 if req.Signer == nil {
125 return &signature.InvalidSignRequestError{Msg: "signer is nil"}
126 }
127
128 if _, err := req.Signer.KeySpec(); err != nil {
129 return err
130 }
131
132 return validateSigningSchema(req.SigningScheme)
133 }
134
135 // validateSigningSchema validates the schema.
136 func validateSigningSchema(schema signature.SigningScheme) error {
137 if schema == "" {
138 return &signature.InvalidSignRequestError{Msg: "SigningScheme not present"}
139 }
140 return nil
141 }
142
143 // validateEnvelopeContent validates the content which includes signerInfo and
144 // payload.
145 func validateEnvelopeContent(content *signature.EnvelopeContent) error {
146 if err := validatePayload(&content.Payload); err != nil {
147 return &signature.InvalidSignatureError{Msg: err.Error()}
148 }
149 return validateSignerInfo(&content.SignerInfo)
150 }
151
152 // validateSignerInfo performs basic set of validations on SignerInfo struct.
153 func validateSignerInfo(info *signature.SignerInfo) error {
154 if len(info.Signature) == 0 {
155 return &signature.InvalidSignatureError{Msg: "signature not present or is empty"}
156 }
157
158 if info.SignatureAlgorithm == 0 {
159 return &signature.InvalidSignatureError{Msg: "SignatureAlgorithm is not present"}
160 }
161
162 signingTime := info.SignedAttributes.SigningTime
163 if err := validateSigningAndExpiryTime(signingTime, info.SignedAttributes.Expiry); err != nil {
164 return err
165 }
166
167 if err := validateSigningSchema(info.SignedAttributes.SigningScheme); err != nil {
168 return err
169 }
170
171 return validateCertificateChain(
172 info.CertificateChain,
173 nil,
174 info.SignatureAlgorithm,
175 )
176 }
177
178 // validateSigningAndExpiryTime checks that signing time is within the valid
179 // range of time duration and expire time is valid.
180 func validateSigningAndExpiryTime(signingTime, expireTime time.Time) error {
181 if signingTime.IsZero() {
182 return &signature.InvalidSignatureError{Msg: "signing-time not present"}
183 }
184
185 if !expireTime.IsZero() && (expireTime.Before(signingTime) || expireTime.Equal(signingTime)) {
186 return &signature.InvalidSignatureError{Msg: "expiry cannot be equal or before the signing time"}
187 }
188 return nil
189 }
190
191 // validatePayload performs validation of the payload.
192 func validatePayload(payload *signature.Payload) error {
193 if len(payload.Content) == 0 {
194 return errors.New("content not present")
195 }
196
197 return nil
198 }
199
200 // validateCertificateChain performs the validation of the certificate chain.
201 func validateCertificateChain(certChain []*x509.Certificate, signTime *time.Time, expectedAlg signature.Algorithm) error {
202 if len(certChain) == 0 {
203 return &signature.InvalidSignatureError{Msg: "certificate-chain not present or is empty"}
204 }
205
206 err := nx509.ValidateCodeSigningCertChain(certChain, signTime)
207 if err != nil {
208 return &signature.InvalidSignatureError{
209 Msg: fmt.Sprintf("certificate-chain is invalid, %s", err),
210 }
211 }
212
213 signingAlg, err := getSignatureAlgorithm(certChain[0])
214 if err != nil {
215 return &signature.InvalidSignatureError{Msg: err.Error()}
216 }
217 if signingAlg != expectedAlg {
218 return &signature.InvalidSignatureError{
219 Msg: fmt.Sprintf("mismatch between signature algorithm derived from signing certificate (%v) and signing algorithm specified (%vs)", signingAlg, expectedAlg),
220 }
221 }
222
223 return nil
224 }
225
226 // getSignatureAlgorithm picks up a recommended signing algorithm for given
227 // certificate.
228 func getSignatureAlgorithm(signingCert *x509.Certificate) (signature.Algorithm, error) {
229 keySpec, err := signature.ExtractKeySpec(signingCert)
230 if err != nil {
231 return 0, err
232 }
233
234 return keySpec.SignatureAlgorithm(), nil
235 }