-
Notifications
You must be signed in to change notification settings - Fork 42
/
verifier.go
528 lines (467 loc) · 20.7 KB
/
verifier.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Package Verifier provides an implementation of notation.Verifier interface
package verifier
import (
"context"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/notaryproject/notation-core-go/signature"
"github.com/notaryproject/notation-go"
"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/internal/envelope"
"github.com/notaryproject/notation-go/internal/pkix"
"github.com/notaryproject/notation-go/internal/slices"
trustpolicyInternal "github.com/notaryproject/notation-go/internal/trustpolicy"
"github.com/notaryproject/notation-go/plugin"
"github.com/notaryproject/notation-go/plugin/proto"
"github.com/notaryproject/notation-go/verifier/trustpolicy"
"github.com/notaryproject/notation-go/verifier/truststore"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
)
// verifier implements notation.Verifier
type verifier struct {
trustPolicyDoc *trustpolicy.Document
trustStore truststore.X509TrustStore
pluginManager plugin.Manager
}
// NewFromConfig returns a verifier based on local file system
func NewFromConfig() (notation.Verifier, error) {
// load trust policy
policyDocument, err := trustpolicy.LoadDocument()
if err != nil {
return nil, err
}
// load trust store
x509TrustStore := truststore.NewX509TrustStore(dir.ConfigFS())
return New(policyDocument, x509TrustStore, plugin.NewCLIManager(dir.PluginFS()))
}
// New creates a new verifier given trustPolicy, trustStore and pluginManager
func New(trustPolicy *trustpolicy.Document, trustStore truststore.X509TrustStore, pluginManager plugin.Manager) (notation.Verifier, error) {
if trustPolicy == nil || trustStore == nil {
return nil, errors.New("trustPolicy or trustStore cannot be nil")
}
if err := trustPolicy.Validate(); err != nil {
return nil, err
}
return &verifier{
trustPolicyDoc: trustPolicy,
trustStore: trustStore,
pluginManager: pluginManager,
}, nil
}
// Verify verifies the signature blob and returns the verified descriptor
// upon successful verification.
func (v *verifier) Verify(ctx context.Context, desc ocispec.Descriptor, signature []byte, opts notation.VerifyOptions) (*notation.VerificationOutcome, error) {
artifactRef := opts.ArtifactReference
envelopeMediaType := opts.SignatureMediaType
pluginConfig := opts.PluginConfig
trustPolicy, err := v.trustPolicyDoc.GetApplicableTrustPolicy(artifactRef)
if err != nil {
return nil, notation.ErrorNoApplicableTrustPolicy{Msg: err.Error()}
}
// ignore the error since we already validated the policy document
verificationLevel, _ := trustPolicy.SignatureVerification.GetVerificationLevel()
outcome := ¬ation.VerificationOutcome{
RawSignature: signature,
VerificationLevel: verificationLevel,
}
// verificationLevel is skip
if reflect.DeepEqual(verificationLevel, trustpolicy.LevelSkip) {
return outcome, nil
}
err = v.processSignature(ctx, signature, envelopeMediaType, trustPolicy, pluginConfig, outcome)
if err != nil {
outcome.Error = err
return outcome, err
}
payload := &envelope.Payload{}
err = json.Unmarshal(outcome.EnvelopeContent.Payload.Content, payload)
if err != nil {
outcome.Error = err
return outcome, err
}
if !content.Equal(payload.TargetArtifact, desc) {
outcome.Error = errors.New("content descriptor mismatch")
}
return outcome, outcome.Error
}
func (v *verifier) processSignature(ctx context.Context, sigBlob []byte, envelopeMediaType string, trustPolicy *trustpolicy.TrustPolicy, pluginConfig map[string]string, outcome *notation.VerificationOutcome) error {
// verify integrity first. notation will always verify integrity no matter what the signing scheme is
envContent, integrityResult := verifyIntegrity(sigBlob, envelopeMediaType, outcome)
outcome.EnvelopeContent = envContent
outcome.VerificationResults = append(outcome.VerificationResults, integrityResult)
if integrityResult.Error != nil {
return integrityResult.Error
}
// check if we need to verify using a plugin
var pluginCapabilities []proto.Capability
verificationPluginName, err := getVerificationPlugin(&outcome.EnvelopeContent.SignerInfo)
// use plugin, but getPluginName returns an error
if err != nil && err != errExtendedAttributeNotExist {
return err
}
var installedPlugin plugin.Plugin
if verificationPluginName != "" {
if _, err := getVerificationPluginMinVersion(&outcome.EnvelopeContent.SignerInfo); err != nil && err != errExtendedAttributeNotExist {
return notation.ErrorVerificationInconclusive{Msg: fmt.Sprintf("error while getting plugin minimum version, error: %s", err)}
}
// TODO verify the plugin's version is equal to or greater than `outcome.SignerInfo.SignedAttributes.HeaderVerificationPluginMinVersion`
// https://github.com/notaryproject/notation-go/issues/102
if v.pluginManager == nil {
return notation.ErrorVerificationInconclusive{Msg: "plugin unsupported due to nil verifier.pluginManager"}
}
installedPlugin, err = v.pluginManager.Get(ctx, verificationPluginName)
if err != nil {
return notation.ErrorVerificationInconclusive{Msg: fmt.Sprintf("error while locating the verification plugin %q, make sure the plugin is installed successfully before verifying the signature. error: %s", verificationPluginName, err)}
}
// filter the "verification" capabilities supported by the installed plugin
metadata, err := installedPlugin.GetMetadata(ctx, &proto.GetMetadataRequest{PluginConfig: pluginConfig})
if err != nil {
return err
}
for _, capability := range metadata.Capabilities {
if capability == proto.CapabilityRevocationCheckVerifier || capability == proto.CapabilityTrustedIdentityVerifier {
pluginCapabilities = append(pluginCapabilities, capability)
}
}
if len(pluginCapabilities) == 0 {
return notation.ErrorVerificationInconclusive{Msg: fmt.Sprintf("digital signature requires plugin %q with signature verification capabilities (%q and/or %q) installed", verificationPluginName, proto.CapabilityTrustedIdentityVerifier, proto.CapabilityRevocationCheckVerifier)}
}
}
// verify x509 trust store based authenticity
authenticityResult := verifyAuthenticity(ctx, trustPolicy, v.trustStore, outcome)
outcome.VerificationResults = append(outcome.VerificationResults, authenticityResult)
if isCriticalFailure(authenticityResult) {
return authenticityResult.Error
}
// verify x509 trusted identity based authenticity (only if notation needs to perform this verification rather than a plugin)
if !slices.Contains(pluginCapabilities, proto.CapabilityTrustedIdentityVerifier) {
err = verifyX509TrustedIdentities(outcome.EnvelopeContent.SignerInfo.CertificateChain, trustPolicy)
if err != nil {
authenticityResult.Error = err
}
if isCriticalFailure(authenticityResult) {
return authenticityResult.Error
}
}
// verify expiry
expiryResult := verifyExpiry(outcome)
outcome.VerificationResults = append(outcome.VerificationResults, expiryResult)
if isCriticalFailure(expiryResult) {
return expiryResult.Error
}
// verify authentic timestamp
authenticTimestampResult := verifyAuthenticTimestamp(outcome)
outcome.VerificationResults = append(outcome.VerificationResults, authenticTimestampResult)
if isCriticalFailure(authenticTimestampResult) {
return authenticTimestampResult.Error
}
// verify revocation
// check if we need to bypass the revocation check, since revocation can be skipped using a trust policy or a plugin may override the check
if outcome.VerificationLevel.Enforcement[trustpolicy.TypeRevocation] != trustpolicy.ActionSkip &&
!slices.Contains(pluginCapabilities, proto.CapabilityRevocationCheckVerifier) {
// TODO perform X509 revocation check (not in RC1)
// https://github.com/notaryproject/notation-go/issues/110
}
// perform extended verification using verification plugin if present
if installedPlugin != nil {
var capabilitiesToVerify []proto.Capability
for _, pc := range pluginCapabilities {
// skip the revocation capability if the trust policy is configured to skip it
if outcome.VerificationLevel.Enforcement[trustpolicy.TypeRevocation] == trustpolicy.ActionSkip && pc == proto.CapabilityRevocationCheckVerifier {
continue
}
capabilitiesToVerify = append(capabilitiesToVerify, pc)
}
if len(capabilitiesToVerify) > 0 {
response, err := executePlugin(ctx, installedPlugin, trustPolicy, capabilitiesToVerify, outcome.EnvelopeContent, pluginConfig)
if err != nil {
return err
}
return processPluginResponse(capabilitiesToVerify, response, outcome)
}
}
return nil
}
func processPluginResponse(capabilitiesToVerify []proto.Capability, response *proto.VerifySignatureResponse, outcome *notation.VerificationOutcome) error {
verificationPluginName, err := getVerificationPlugin(&outcome.EnvelopeContent.SignerInfo)
if err != nil {
return err
}
// verify all extended critical attributes are processed by the plugin
for _, attr := range getNonPluginExtendedCriticalAttributes(&outcome.EnvelopeContent.SignerInfo) {
if !slices.ContainsAny(response.ProcessedAttributes, attr.Key) {
return fmt.Errorf("extended critical attribute %q was not processed by the verification plugin %q (all extended critical attributes must be processed by the verification plugin)", attr.Key, verificationPluginName)
}
}
for _, capability := range capabilitiesToVerify {
pluginResult := response.VerificationResults[capability]
if pluginResult == nil {
// verification result is empty for this capability
return notation.ErrorVerificationInconclusive{Msg: fmt.Sprintf("verification plugin %q failed to verify %q", verificationPluginName, capability)}
}
switch capability {
case proto.CapabilityTrustedIdentityVerifier:
if !pluginResult.Success {
// find the Authenticity VerificationResult that we already created during x509 trust store verification
var authenticityResult *notation.ValidationResult
for _, r := range outcome.VerificationResults {
if r.Type == trustpolicy.TypeAuthenticity {
authenticityResult = r
break
}
}
authenticityResult.Error = fmt.Errorf("trusted identify verification by plugin %q failed with reason %q", verificationPluginName, pluginResult.Reason)
if isCriticalFailure(authenticityResult) {
return authenticityResult.Error
}
}
case proto.CapabilityRevocationCheckVerifier:
var revocationResult *notation.ValidationResult
if !pluginResult.Success {
revocationResult = ¬ation.ValidationResult{
Error: fmt.Errorf("revocation check by verification plugin %q failed with reason %q", verificationPluginName, pluginResult.Reason),
Type: trustpolicy.TypeRevocation,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeRevocation],
}
} else {
revocationResult = ¬ation.ValidationResult{
Type: trustpolicy.TypeRevocation,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeRevocation],
}
}
outcome.VerificationResults = append(outcome.VerificationResults, revocationResult)
if isCriticalFailure(revocationResult) {
return revocationResult.Error
}
}
}
return nil
}
func verifyIntegrity(sigBlob []byte, envelopeMediaType string, outcome *notation.VerificationOutcome) (*signature.EnvelopeContent, *notation.ValidationResult) {
// parse the signature
sigEnv, err := signature.ParseEnvelope(envelopeMediaType, sigBlob)
if err != nil {
return nil, ¬ation.ValidationResult{
Error: fmt.Errorf("unable to parse the digital signature, error : %s", err),
Type: trustpolicy.TypeIntegrity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeIntegrity],
}
}
// verify integrity
envContent, err := sigEnv.Verify()
if err != nil {
switch err.(type) {
case *signature.SignatureEnvelopeNotFoundError, *signature.InvalidSignatureError, *signature.SignatureIntegrityError:
return nil, ¬ation.ValidationResult{
Error: err,
Type: trustpolicy.TypeIntegrity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeIntegrity],
}
default:
// unexpected error
return nil, ¬ation.ValidationResult{
Error: notation.ErrorVerificationInconclusive{Msg: err.Error()},
Type: trustpolicy.TypeIntegrity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeIntegrity],
}
}
}
if err := envelope.ValidatePayloadContentType(&envContent.Payload); err != nil {
return nil, ¬ation.ValidationResult{
Error: err,
Type: trustpolicy.TypeIntegrity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeIntegrity],
}
}
// integrity has been verified successfully
return envContent, ¬ation.ValidationResult{
Type: trustpolicy.TypeIntegrity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeIntegrity],
}
}
func verifyAuthenticity(ctx context.Context, trustPolicy *trustpolicy.TrustPolicy, x509TrustStore truststore.X509TrustStore, outcome *notation.VerificationOutcome) *notation.ValidationResult {
// verify authenticity
trustCerts, err := loadX509TrustStores(ctx, outcome.EnvelopeContent.SignerInfo.SignedAttributes.SigningScheme, trustPolicy, x509TrustStore)
if err != nil {
return ¬ation.ValidationResult{
Error: notation.ErrorVerificationInconclusive{Msg: fmt.Sprintf("error while loading the trust store, %v", err)},
Type: trustpolicy.TypeAuthenticity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticity],
}
}
if len(trustCerts) < 1 {
return ¬ation.ValidationResult{
Error: notation.ErrorVerificationInconclusive{Msg: "no trusted certificates are found to verify authenticity"},
Type: trustpolicy.TypeAuthenticity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticity],
}
}
_, err = signature.VerifyAuthenticity(&outcome.EnvelopeContent.SignerInfo, trustCerts)
if err != nil {
switch err.(type) {
case *signature.SignatureAuthenticityError:
return ¬ation.ValidationResult{
Error: err,
Type: trustpolicy.TypeAuthenticity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticity],
}
default:
return ¬ation.ValidationResult{
Error: notation.ErrorVerificationInconclusive{Msg: "authenticity verification failed with error : " + err.Error()},
Type: trustpolicy.TypeAuthenticity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticity],
}
}
}
return ¬ation.ValidationResult{
Type: trustpolicy.TypeAuthenticity,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticity],
}
}
func verifyExpiry(outcome *notation.VerificationOutcome) *notation.ValidationResult {
if expiry := outcome.EnvelopeContent.SignerInfo.SignedAttributes.Expiry; !expiry.IsZero() && !time.Now().Before(expiry) {
return ¬ation.ValidationResult{
Error: fmt.Errorf("digital signature has expired on %q", expiry.Format(time.RFC1123Z)),
Type: trustpolicy.TypeExpiry,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeExpiry],
}
}
return ¬ation.ValidationResult{
Type: trustpolicy.TypeExpiry,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeExpiry],
}
}
func verifyAuthenticTimestamp(outcome *notation.VerificationOutcome) *notation.ValidationResult {
invalidTimestamp := false
var err error
if signerInfo := outcome.EnvelopeContent.SignerInfo; signerInfo.SignedAttributes.SigningScheme == signature.SigningSchemeX509 {
// TODO verify RFC3161 TSA signature if present (not in RC1)
// https://github.com/notaryproject/notation-go/issues/78
if len(signerInfo.UnsignedAttributes.TimestampSignature) == 0 {
// if there is no TSA signature, then every certificate should be valid at the time of verification
now := time.Now()
for _, cert := range signerInfo.CertificateChain {
if now.Before(cert.NotBefore) {
invalidTimestamp = true
err = fmt.Errorf("certificate %q is not valid yet, it will be valid from %q", cert.Subject, cert.NotBefore.Format(time.RFC1123Z))
break
}
if now.After(cert.NotAfter) {
invalidTimestamp = true
err = fmt.Errorf("certificate %q is not valid anymore, it was expired at %q", cert.Subject, cert.NotAfter.Format(time.RFC1123Z))
break
}
}
}
} else if signerInfo.SignedAttributes.SigningScheme == signature.SigningSchemeX509SigningAuthority {
authenticSigningTime := signerInfo.SignedAttributes.SigningTime
// TODO use authenticSigningTime from signerInfo
// https://github.com/notaryproject/notation-core-go/issues/38
for _, cert := range signerInfo.CertificateChain {
if authenticSigningTime.Before(cert.NotBefore) || authenticSigningTime.After(cert.NotAfter) {
invalidTimestamp = true
err = fmt.Errorf("certificate %q was not valid when the digital signature was produced at %q", cert.Subject, authenticSigningTime.Format(time.RFC1123Z))
break
}
}
}
if invalidTimestamp {
return ¬ation.ValidationResult{
Error: err,
Type: trustpolicy.TypeAuthenticTimestamp,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticTimestamp],
}
}
return ¬ation.ValidationResult{
Type: trustpolicy.TypeAuthenticTimestamp,
Action: outcome.VerificationLevel.Enforcement[trustpolicy.TypeAuthenticTimestamp],
}
}
func executePlugin(ctx context.Context, installedPlugin plugin.Plugin, trustPolicy *trustpolicy.TrustPolicy, capabilitiesToVerify []proto.Capability, envelopeContent *signature.EnvelopeContent, pluginConfig map[string]string) (*proto.VerifySignatureResponse, error) {
// sanity check
if installedPlugin == nil {
return nil, errors.New("installedPlugin cannot be nil")
}
signerInfo, payloadInfo := &envelopeContent.SignerInfo, envelopeContent.Payload
var attributesToProcess []string
extendedAttributes := make(map[string]interface{})
for _, attr := range getNonPluginExtendedCriticalAttributes(signerInfo) {
extendedAttributes[attr.Key.(string)] = attr.Value
attributesToProcess = append(attributesToProcess, attr.Key.(string))
}
var certChain [][]byte
for _, cert := range signerInfo.CertificateChain {
certChain = append(certChain, cert.Raw)
}
var authenticSigningTime *time.Time
if signerInfo.SignedAttributes.SigningScheme == signature.SigningSchemeX509SigningAuthority {
authenticSigningTime = &signerInfo.SignedAttributes.SigningTime
// TODO use authenticSigningTime from signerInfo
// https://github.com/notaryproject/notation-core-go/issues/38
}
signature := proto.Signature{
CriticalAttributes: proto.CriticalAttributes{
ContentType: payloadInfo.ContentType,
SigningScheme: string(signerInfo.SignedAttributes.SigningScheme),
Expiry: &signerInfo.SignedAttributes.Expiry,
AuthenticSigningTime: authenticSigningTime,
ExtendedAttributes: extendedAttributes,
},
UnprocessedAttributes: attributesToProcess,
CertificateChain: certChain,
}
policy := proto.TrustPolicy{
TrustedIdentities: trustPolicy.TrustedIdentities,
SignatureVerification: capabilitiesToVerify,
}
req := &proto.VerifySignatureRequest{
Signature: signature,
TrustPolicy: policy,
PluginConfig: pluginConfig,
}
return installedPlugin.VerifySignature(ctx, req)
}
func verifyX509TrustedIdentities(certs []*x509.Certificate, trustPolicy *trustpolicy.TrustPolicy) error {
if slices.Contains(trustPolicy.TrustedIdentities, trustpolicyInternal.Wildcard) {
return nil
}
var trustedX509Identities []map[string]string
for _, identity := range trustPolicy.TrustedIdentities {
identityPrefix, identityValue, found := strings.Cut(identity, ":")
if !found {
return fmt.Errorf("trust policy statement %q has trusted identity %q missing separator", trustPolicy.Name, identity)
}
// notation natively supports x509.subject identities only
if identityPrefix == trustpolicyInternal.X509Subject {
// identityValue cannot be empty
if identityValue == "" {
return fmt.Errorf("trust policy statement %q has trusted identity %q without an identity value", trustPolicy.Name, identity)
}
parsedSubject, err := pkix.ParseDistinguishedName(identityValue)
if err != nil {
return err
}
trustedX509Identities = append(trustedX509Identities, parsedSubject)
}
}
if len(trustedX509Identities) == 0 {
return fmt.Errorf("no x509 trusted identities are configured in the trust policy %q", trustPolicy.Name)
}
leafCert := certs[0] // trusted identities only supported on the leaf cert
leafCertDN, err := pkix.ParseDistinguishedName(leafCert.Subject.String()) // parse the certificate subject following rfc 4514 DN syntax
if err != nil {
return fmt.Errorf("error while parsing the certificate subject from the digital signature. error : %q", err)
}
for _, trustedX509Identity := range trustedX509Identities {
if pkix.IsSubsetDN(trustedX509Identity, leafCertDN) {
return nil
}
}
return fmt.Errorf("signing certificate from the digital signature does not match the X.509 trusted identities %q defined in the trust policy %q", trustedX509Identities, trustPolicy.Name)
}