Debian Codegraph @

verifier/helpers.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 verifier
15
16 import (
17 "context"
18 "crypto/x509"
19 "errors"
20 "fmt"
21 "strings"
22
23 "github.com/notaryproject/notation-core-go/signature"
24 "github.com/notaryproject/notation-go"
25 set "github.com/notaryproject/notation-go/internal/container"
26 notationsemver "github.com/notaryproject/notation-go/internal/semver"
27 "github.com/notaryproject/notation-go/internal/slices"
28 "github.com/notaryproject/notation-go/verifier/trustpolicy"
29 "github.com/notaryproject/notation-go/verifier/truststore"
30 )
31
32 const (
33 // HeaderVerificationPlugin specifies the name of the verification plugin
34 // that should be used to verify the signature.
35 HeaderVerificationPlugin = "io.cncf.notary.verificationPlugin"
36
37 // HeaderVerificationPluginMinVersion specifies the minimum version of the
38 // verification plugin that should be used to verify the signature.
39 HeaderVerificationPluginMinVersion = "io.cncf.notary.verificationPluginMinVersion"
40 )
41
42 // VerificationPluginHeaders specifies headers of a verification plugin
43 var VerificationPluginHeaders = []string{
44 HeaderVerificationPlugin,
45 HeaderVerificationPluginMinVersion,
46 }
47
48 var errExtendedAttributeNotExist = errors.New("extended attribute not exist")
49
50 func loadX509TrustStores(ctx context.Context, scheme signature.SigningScheme, policyName string, trustStores []string, x509TrustStore truststore.X509TrustStore) ([]*x509.Certificate, error) {
51 var typeToLoad truststore.Type
52 switch scheme {
53 case signature.SigningSchemeX509:
54 typeToLoad = truststore.TypeCA
55 case signature.SigningSchemeX509SigningAuthority:
56 typeToLoad = truststore.TypeSigningAuthority
57 default:
58 return nil, truststore.TrustStoreError{Msg: fmt.Sprintf("error while loading the trust store, unrecognized signing scheme %q", scheme)}
59 }
60 return loadX509TrustStoresWithType(ctx, typeToLoad, policyName, trustStores, x509TrustStore)
61 }
62
63 // isCriticalFailure checks whether a VerificationResult fails the entire
64 // signature verification workflow.
65 // signature verification workflow is considered failed if there is a
66 // VerificationResult with "Enforced" as the action but the result was
67 // unsuccessful.
68 func isCriticalFailure(result *notation.ValidationResult) bool {
69 return result.Action == trustpolicy.ActionEnforce && result.Error != nil
70 }
71
72 func getNonPluginExtendedCriticalAttributes(signerInfo *signature.SignerInfo) []signature.Attribute {
73 var criticalExtendedAttrs []signature.Attribute
74 for _, attr := range signerInfo.SignedAttributes.ExtendedAttributes {
75 attrStrKey, ok := attr.Key.(string)
76 // filter the plugin extended attributes
77 if ok && !slices.Contains(VerificationPluginHeaders, attrStrKey) {
78 // TODO support other attribute types
79 // (COSE attribute keys can be numbers)
80 criticalExtendedAttrs = append(criticalExtendedAttrs, attr)
81 }
82 }
83 return criticalExtendedAttrs
84 }
85
86 // extractCriticalStringExtendedAttribute extracts a critical string Extended
87 // attribute from a signer.
88 func extractCriticalStringExtendedAttribute(signerInfo *signature.SignerInfo, key string) (string, error) {
89 attr, err := signerInfo.ExtendedAttribute(key)
90 // not exist
91 if err != nil {
92 return "", errExtendedAttributeNotExist
93 }
94 // not critical
95 if !attr.Critical {
96 return "", fmt.Errorf("%v is not a critical Extended attribute", key)
97 }
98 // not string
99 val, ok := attr.Value.(string)
100 if !ok {
101 return "", fmt.Errorf("%v from extended attribute is not a string", key)
102 }
103 return val, nil
104 }
105
106 // getVerificationPlugin get plugin name from the Extended attributes.
107 func getVerificationPlugin(signerInfo *signature.SignerInfo) (string, error) {
108 name, err := extractCriticalStringExtendedAttribute(signerInfo, HeaderVerificationPlugin)
109 if err != nil {
110 return "", err
111 }
112 // not an empty string
113 if strings.TrimSpace(name) == "" {
114 return "", fmt.Errorf("%v from extended attribute is an empty string", HeaderVerificationPlugin)
115 }
116 return name, nil
117 }
118
119 // getVerificationPlugin get plugin version from the Extended attributes.
120 func getVerificationPluginMinVersion(signerInfo *signature.SignerInfo) (string, error) {
121 version, err := extractCriticalStringExtendedAttribute(signerInfo, HeaderVerificationPluginMinVersion)
122 if err != nil {
123 return "", err
124 }
125 if strings.TrimSpace(version) == "" {
126 return "", fmt.Errorf("%v from extended attribute is an empty string", HeaderVerificationPluginMinVersion)
127 }
128 if !notationsemver.IsValid(version) {
129 return "", fmt.Errorf("%v from extended attribute is not a valid SemVer", HeaderVerificationPluginMinVersion)
130 }
131 return version, nil
132 }
133
134 func loadX509TSATrustStores(ctx context.Context, scheme signature.SigningScheme, policyName string, trustStores []string, x509TrustStore truststore.X509TrustStore) ([]*x509.Certificate, error) {
135 var typeToLoad truststore.Type
136 switch scheme {
137 case signature.SigningSchemeX509:
138 typeToLoad = truststore.TypeTSA
139 default:
140 return nil, truststore.TrustStoreError{Msg: fmt.Sprintf("error while loading the TSA trust store, signing scheme must be notary.x509, but got %s", scheme)}
141 }
142 return loadX509TrustStoresWithType(ctx, typeToLoad, policyName, trustStores, x509TrustStore)
143 }
144
145 func loadX509TrustStoresWithType(ctx context.Context, trustStoreType truststore.Type, policyName string, trustStores []string, x509TrustStore truststore.X509TrustStore) ([]*x509.Certificate, error) {
146 processedStoreSet := set.New[string]()
147 var certificates []*x509.Certificate
148 for _, trustStore := range trustStores {
149 if processedStoreSet.Contains(trustStore) {
150 // we loaded this trust store already
151 continue
152 }
153
154 storeType, name, found := strings.Cut(trustStore, ":")
155 if !found {
156 return nil, truststore.TrustStoreError{Msg: fmt.Sprintf("error while loading the trust store, trust policy statement %q is missing separator in trust store value %q. The required format is <TrustStoreType>:<TrustStoreName>", policyName, trustStore)}
157 }
158 if trustStoreType != truststore.Type(storeType) {
159 continue
160 }
161
162 certs, err := x509TrustStore.GetCertificates(ctx, trustStoreType, name)
163 if err != nil {
164 return nil, err
165 }
166 certificates = append(certificates, certs...)
167 processedStoreSet.Add(trustStore)
168 }
169 return certificates, nil
170 }
171
172 // isTSATrustStoreInPolicy checks if tsa trust store is configured in
173 // trust policy
174 func isTSATrustStoreInPolicy(policyName string, trustStores []string) (bool, error) {
175 for _, trustStore := range trustStores {
176 storeType, _, found := strings.Cut(trustStore, ":")
177 if !found {
178 return false, truststore.TrustStoreError{Msg: fmt.Sprintf("invalid trust policy statement: %q is missing separator in trust store value %q. The required format is <TrustStoreType>:<TrustStoreName>", policyName, trustStore)}
179 }
180 if truststore.Type(storeType) == truststore.TypeTSA {
181 return true, nil
182 }
183 }
184 return false, nil
185 }