| 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 |
"crypto/x509/pkix" |
| 20 |
"encoding/json" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"net/http" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"strconv" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
"golang.org/x/crypto/ocsp" |
| 31 |
|
| 32 |
"github.com/notaryproject/notation-core-go/revocation" |
| 33 |
"github.com/notaryproject/notation-core-go/revocation/purpose" |
| 34 |
"github.com/notaryproject/notation-core-go/revocation/result" |
| 35 |
"github.com/notaryproject/notation-core-go/signature" |
| 36 |
_ "github.com/notaryproject/notation-core-go/signature/cose" |
| 37 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 38 |
corex509 "github.com/notaryproject/notation-core-go/x509" |
| 39 |
"github.com/notaryproject/notation-go" |
| 40 |
"github.com/notaryproject/notation-go/dir" |
| 41 |
"github.com/notaryproject/notation-go/internal/envelope" |
| 42 |
"github.com/notaryproject/notation-go/internal/mock" |
| 43 |
"github.com/notaryproject/notation-go/log" |
| 44 |
"github.com/notaryproject/notation-go/plugin/proto" |
| 45 |
"github.com/notaryproject/notation-go/signer" |
| 46 |
"github.com/notaryproject/notation-go/verifier/trustpolicy" |
| 47 |
"github.com/notaryproject/notation-go/verifier/truststore" |
| 48 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 49 |
) |
| 50 |
|
| 51 |
var policy = dummyPolicyDocument() |
| 52 |
var invalidPolicy = dummyInvalidPolicyDocument() |
| 53 |
var store = truststore.NewX509TrustStore(dir.ConfigFS()) |
| 54 |
var pm = mock.PluginManager{} |
| 55 |
|
| 56 |
func TestNewVerifier_Error(t *testing.T) { |
| 57 |
policyDocument := dummyPolicyDocument() |
| 58 |
_, err := New(&policyDocument, nil, nil) |
| 59 |
expectedErr := errors.New("trustStore cannot be nil") |
| 60 |
if err == nil || err.Error() != expectedErr.Error() { |
| 61 |
t.Fatalf("TestNewVerifier_Error expected error %v, got %v", expectedErr, err) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestNewFromConfig(t *testing.T) { |
| 66 |
tempRoot := t.TempDir() |
| 67 |
dir.UserConfigDir = tempRoot |
| 68 |
expectedErrMsg := "trust policy is not present. To create a trust policy, see: https://notaryproject.dev/docs/quickstart/#create-a-trust-policy" |
| 69 |
_, err := NewFromConfig() |
| 70 |
if err == nil || err.Error() != expectedErrMsg { |
| 71 |
t.Fatalf("expected %s, but got %s", expectedErrMsg, err) |
| 72 |
} |
| 73 |
|
| 74 |
path := filepath.Join(tempRoot, "trustpolicy.json") |
| 75 |
policyJson, _ := json.Marshal(dummyPolicyDocument()) |
| 76 |
if err := os.WriteFile(path, policyJson, 0600); err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
t.Cleanup(func() { os.RemoveAll(tempRoot) }) |
| 80 |
_, err = NewFromConfig() |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func TestInvalidArtifactUriValidations(t *testing.T) { |
| 87 |
verifier := verifier{ |
| 88 |
trustPolicyDoc: &policy, |
| 89 |
pluginManager: mock.PluginManager{}, |
| 90 |
} |
| 91 |
|
| 92 |
tests := []struct { |
| 93 |
uri string |
| 94 |
wantErr bool |
| 95 |
}{ |
| 96 |
{"", true}, |
| 97 |
{"invaliduri", true}, |
| 98 |
{"domain.com/repository@sha256:", true}, |
| 99 |
{"domain.com/repository@sha256", true}, |
| 100 |
{"domain.com/repository@", true}, |
| 101 |
{"domain.com/repository", true}, |
| 102 |
{"domain.com/repositorysha256:digest", true}, |
| 103 |
{"domain.com/repositorysha256digest", true}, |
| 104 |
{"repository@sha256:digest", true}, |
| 105 |
} |
| 106 |
for i, tt := range tests { |
| 107 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 108 |
opts := notation.VerifierVerifyOptions{ArtifactReference: tt.uri} |
| 109 |
_, err := verifier.Verify(context.Background(), ocispec.Descriptor{}, []byte{}, opts) |
| 110 |
if err != nil != tt.wantErr { |
| 111 |
t.Fatalf("TestInvalidArtifactUriValidations expected error for %q", tt.uri) |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestErrorNoApplicableTrustPolicy_Error(t *testing.T) { |
| 118 |
verifier := verifier{ |
| 119 |
trustPolicyDoc: &policy, |
| 120 |
pluginManager: mock.PluginManager{}, |
| 121 |
} |
| 122 |
opts := notation.VerifierVerifyOptions{ArtifactReference: "non-existent-domain.com/repo@sha256:73c803930ea3ba1e54bc25c2bdc53edd0284c62ed651fe7b00369da519a3c333"} |
| 123 |
_, err := verifier.Verify(context.Background(), ocispec.Descriptor{}, []byte{}, opts) |
| 124 |
if !errors.Is(err, notation.ErrorNoApplicableTrustPolicy{Msg: "artifact \"non-existent-domain.com/repo@sha256:73c803930ea3ba1e54bc25c2bdc53edd0284c62ed651fe7b00369da519a3c333\" has no applicable trust policy statement. Trust policy applicability for a given artifact is determined by registryScopes. To create a trust policy, see: https://notaryproject.dev/docs/quickstart/#create-a-trust-policy"}) { |
| 125 |
t.Fatalf("no applicable trust policy must throw error") |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestNotationVerificationCombinations(t *testing.T) { |
| 130 |
assertNotationVerification(t, signature.SigningSchemeX509) |
| 131 |
assertNotationVerification(t, signature.SigningSchemeX509SigningAuthority) |
| 132 |
} |
| 133 |
|
| 134 |
func assertNotationVerification(t *testing.T, scheme signature.SigningScheme) { |
| 135 |
var validSigEnv []byte |
| 136 |
var invalidSigEnv []byte |
| 137 |
var expiredSigEnv []byte |
| 138 |
|
| 139 |
if scheme == signature.SigningSchemeX509 { |
| 140 |
validSigEnv = mock.MockCaValidSigEnv |
| 141 |
invalidSigEnv = mock.MockCaInvalidSigEnv |
| 142 |
expiredSigEnv = mock.MockCaExpiredSigEnv |
| 143 |
} else if scheme == signature.SigningSchemeX509SigningAuthority { |
| 144 |
validSigEnv = mock.MockSaValidSigEnv |
| 145 |
invalidSigEnv = mock.MockSaInvalidSigEnv |
| 146 |
expiredSigEnv = mock.MockSaExpiredSigEnv |
| 147 |
} |
| 148 |
|
| 149 |
type testCase struct { |
| 150 |
signatureBlob []byte |
| 151 |
verificationType trustpolicy.ValidationType |
| 152 |
verificationLevel *trustpolicy.VerificationLevel |
| 153 |
policyDocument trustpolicy.Document |
| 154 |
opts notation.VerifierVerifyOptions |
| 155 |
expectedErr error |
| 156 |
} |
| 157 |
|
| 158 |
var testCases []testCase |
| 159 |
verificationLevels := []*trustpolicy.VerificationLevel{trustpolicy.LevelStrict, trustpolicy.LevelPermissive, trustpolicy.LevelAudit} |
| 160 |
|
| 161 |
// Unsupported Signature Envelope |
| 162 |
for _, level := range verificationLevels { |
| 163 |
policyDocument := dummyPolicyDocument() |
| 164 |
expectedErr := fmt.Errorf("unable to parse the digital signature, error : signature envelope format with media type \"application/unsupported+json\" is not supported") |
| 165 |
testCases = append(testCases, testCase{ |
| 166 |
verificationType: trustpolicy.TypeIntegrity, |
| 167 |
verificationLevel: level, |
| 168 |
policyDocument: policyDocument, |
| 169 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/unsupported+json"}, |
| 170 |
expectedErr: expectedErr, |
| 171 |
}) |
| 172 |
} |
| 173 |
|
| 174 |
// Integrity Success |
| 175 |
for _, level := range verificationLevels { |
| 176 |
policyDocument := dummyPolicyDocument() |
| 177 |
testCases = append(testCases, testCase{ |
| 178 |
signatureBlob: validSigEnv, |
| 179 |
verificationType: trustpolicy.TypeIntegrity, |
| 180 |
verificationLevel: level, |
| 181 |
policyDocument: policyDocument, |
| 182 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
// Integrity Failure |
| 187 |
for _, level := range verificationLevels { |
| 188 |
policyDocument := dummyPolicyDocument() |
| 189 |
expectedErr := fmt.Errorf("signature is invalid. Error: illegal base64 data at input byte 242") |
| 190 |
testCases = append(testCases, testCase{ |
| 191 |
signatureBlob: invalidSigEnv, |
| 192 |
verificationType: trustpolicy.TypeIntegrity, |
| 193 |
verificationLevel: level, |
| 194 |
policyDocument: policyDocument, |
| 195 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 196 |
expectedErr: expectedErr, |
| 197 |
}) |
| 198 |
} |
| 199 |
|
| 200 |
// Authenticity Success |
| 201 |
for _, level := range verificationLevels { |
| 202 |
policyDocument := dummyPolicyDocument() // trust store is configured with the root certificate of the signature by default |
| 203 |
testCases = append(testCases, testCase{ |
| 204 |
signatureBlob: validSigEnv, |
| 205 |
verificationType: trustpolicy.TypeAuthenticity, |
| 206 |
verificationLevel: level, |
| 207 |
policyDocument: policyDocument, |
| 208 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 209 |
}) |
| 210 |
} |
| 211 |
|
| 212 |
// Authenticity Failure |
| 213 |
for _, level := range verificationLevels { |
| 214 |
policyDocument := dummyPolicyDocument() |
| 215 |
policyDocument.TrustPolicies[0].TrustStores = []string{"ca:valid-trust-store-2", "signingAuthority:valid-trust-store-2"} // trust store is not configured with the root certificate of the signature |
| 216 |
expectedErr := fmt.Errorf("the signature's certificate chain does not contain any trusted certificate") |
| 217 |
testCases = append(testCases, testCase{ |
| 218 |
signatureBlob: validSigEnv, |
| 219 |
verificationType: trustpolicy.TypeAuthenticity, |
| 220 |
verificationLevel: level, |
| 221 |
policyDocument: policyDocument, |
| 222 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 223 |
expectedErr: expectedErr, |
| 224 |
}) |
| 225 |
} |
| 226 |
|
| 227 |
// Authenticity Failure with trust store missing separator |
| 228 |
for _, level := range verificationLevels { |
| 229 |
policyDocument := dummyPolicyDocument() |
| 230 |
policyDocument.TrustPolicies[0].TrustStores = []string{"ca:valid-trust-store-2", "signingAuthority"} |
| 231 |
expectedErr := fmt.Errorf("error while loading the trust store, trust policy statement \"test-statement-name\" is missing separator in trust store value \"signingAuthority\". The required format is <TrustStoreType>:<TrustStoreName>") |
| 232 |
testCases = append(testCases, testCase{ |
| 233 |
signatureBlob: validSigEnv, |
| 234 |
verificationType: trustpolicy.TypeAuthenticity, |
| 235 |
verificationLevel: level, |
| 236 |
policyDocument: policyDocument, |
| 237 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 238 |
expectedErr: expectedErr, |
| 239 |
}) |
| 240 |
} |
| 241 |
|
| 242 |
// TrustedIdentity Failure |
| 243 |
for _, level := range verificationLevels { |
| 244 |
policyDocument := dummyPolicyDocument() |
| 245 |
policyDocument.TrustPolicies[0].TrustedIdentities = []string{"x509.subject:CN=LOL,O=DummyOrg,L=Hyderabad,ST=TG,C=IN"} // configure policy to not trust "CN=Notation Test Leaf Cert,O=Notary,L=Seattle,ST=WA,C=US" which is the subject of the signature's signing certificate |
| 246 |
expectedErr := fmt.Errorf("signing certificate from the digital signature does not match the X.509 trusted identities [map[\"C\":\"IN\" \"CN\":\"LOL\" \"L\":\"Hyderabad\" \"O\":\"DummyOrg\" \"ST\":\"TG\"]] defined in the trust policy \"test-statement-name\"") |
| 247 |
testCases = append(testCases, testCase{ |
| 248 |
signatureBlob: validSigEnv, |
| 249 |
verificationType: trustpolicy.TypeAuthenticity, |
| 250 |
verificationLevel: level, |
| 251 |
policyDocument: policyDocument, |
| 252 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 253 |
expectedErr: expectedErr, |
| 254 |
}) |
| 255 |
} |
| 256 |
|
| 257 |
// TrustedIdentity Failure without separator |
| 258 |
for _, level := range verificationLevels { |
| 259 |
policyDocument := dummyPolicyDocument() |
| 260 |
policyDocument.TrustPolicies[0].TrustedIdentities = []string{"x509.subject"} |
| 261 |
expectedErr := fmt.Errorf("trust policy statement \"test-statement-name\" has trusted identity \"x509.subject\" missing separator") |
| 262 |
testCases = append(testCases, testCase{ |
| 263 |
signatureBlob: validSigEnv, |
| 264 |
verificationType: trustpolicy.TypeAuthenticity, |
| 265 |
verificationLevel: level, |
| 266 |
policyDocument: policyDocument, |
| 267 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 268 |
expectedErr: expectedErr, |
| 269 |
}) |
| 270 |
} |
| 271 |
|
| 272 |
// TrustedIdentity Failure with empty value |
| 273 |
for _, level := range verificationLevels { |
| 274 |
policyDocument := dummyPolicyDocument() |
| 275 |
policyDocument.TrustPolicies[0].TrustedIdentities = []string{"x509.subject:"} |
| 276 |
expectedErr := fmt.Errorf("trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:\" without an identity value") |
| 277 |
testCases = append(testCases, testCase{ |
| 278 |
signatureBlob: validSigEnv, |
| 279 |
verificationType: trustpolicy.TypeAuthenticity, |
| 280 |
verificationLevel: level, |
| 281 |
policyDocument: policyDocument, |
| 282 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 283 |
expectedErr: expectedErr, |
| 284 |
}) |
| 285 |
} |
| 286 |
|
| 287 |
// Expiry Success |
| 288 |
for _, level := range verificationLevels { |
| 289 |
policyDocument := dummyPolicyDocument() |
| 290 |
testCases = append(testCases, testCase{ |
| 291 |
signatureBlob: validSigEnv, |
| 292 |
verificationType: trustpolicy.TypeExpiry, |
| 293 |
verificationLevel: level, |
| 294 |
policyDocument: policyDocument, |
| 295 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 296 |
}) |
| 297 |
} |
| 298 |
|
| 299 |
// Expiry Failure |
| 300 |
for _, level := range verificationLevels { |
| 301 |
policyDocument := dummyPolicyDocument() |
| 302 |
expectedErr := fmt.Errorf("digital signature has expired on \"Fri, 29 Jul 2022 23:59:00 +0000\"") |
| 303 |
testCases = append(testCases, testCase{ |
| 304 |
signatureBlob: expiredSigEnv, |
| 305 |
verificationType: trustpolicy.TypeExpiry, |
| 306 |
verificationLevel: level, |
| 307 |
policyDocument: policyDocument, |
| 308 |
opts: notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"}, |
| 309 |
expectedErr: expectedErr, |
| 310 |
}) |
| 311 |
} |
| 312 |
|
| 313 |
for i, tt := range testCases { |
| 314 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 315 |
tt.policyDocument.TrustPolicies[0].SignatureVerification.VerificationLevel = tt.verificationLevel.Name |
| 316 |
expectedResult := notation.ValidationResult{ |
| 317 |
Type: tt.verificationType, |
| 318 |
Action: tt.verificationLevel.Enforcement[tt.verificationType], |
| 319 |
Error: tt.expectedErr, |
| 320 |
} |
| 321 |
|
| 322 |
dir.UserConfigDir = "testdata" |
| 323 |
|
| 324 |
pluginManager := mock.PluginManager{} |
| 325 |
pluginManager.GetPluginError = errors.New("plugin should not be invoked when verification plugin is not specified in the signature") |
| 326 |
pluginManager.PluginRunnerLoadError = errors.New("plugin should not be invoked when verification plugin is not specified in the signature") |
| 327 |
|
| 328 |
revocationClient, err := revocation.New(&http.Client{Timeout: 2 * time.Second}) |
| 329 |
if err != nil { |
| 330 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 331 |
} |
| 332 |
verifier := verifier{ |
| 333 |
trustPolicyDoc: &tt.policyDocument, |
| 334 |
trustStore: truststore.NewX509TrustStore(dir.ConfigFS()), |
| 335 |
pluginManager: pluginManager, |
| 336 |
revocationClient: revocationClient, |
| 337 |
} |
| 338 |
outcome, _ := verifier.Verify(context.Background(), ocispec.Descriptor{}, tt.signatureBlob, tt.opts) |
| 339 |
verifyResult(outcome, expectedResult, tt.expectedErr, t) |
| 340 |
}) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
func TestVerifyRevocationEnvelope(t *testing.T) { |
| 345 |
// Test values |
| 346 |
desc := ocispec.Descriptor{ |
| 347 |
MediaType: "application/vnd.docker.distribution.manifest.v2+json", |
| 348 |
Digest: "sha256:60043cf45eaebc4c0867fea485a039b598f52fd09fd5b07b0b2d2f88fad9d74e", |
| 349 |
Size: 528, |
| 350 |
URLs: []string{}, |
| 351 |
Annotations: map[string]string{}, |
| 352 |
Data: []byte("test data"), |
| 353 |
Platform: nil, |
| 354 |
ArtifactType: "", |
| 355 |
} |
| 356 |
payload := envelope.Payload{ |
| 357 |
TargetArtifact: desc, |
| 358 |
} |
| 359 |
opts := notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 360 |
pluginManager := mock.PluginManager{} |
| 361 |
pluginManager.GetPluginError = errors.New("plugin should not be invoked when verification plugin is not specified in the signature") |
| 362 |
pluginManager.PluginRunnerLoadError = errors.New("plugin should not be invoked when verification plugin is not specified in the signature") |
| 363 |
|
| 364 |
// Get revokable certs and set up mock client (will always say certs are revoked) |
| 365 |
revokableChain := testhelper.GetRevokableRSAChain(2) |
| 366 |
httpClient := testhelper.MockClient(revokableChain, []ocsp.ResponseStatus{ocsp.Revoked}, nil, true) |
| 367 |
revocationClient, err := revocation.New(httpClient) |
| 368 |
if err != nil { |
| 369 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 370 |
} |
| 371 |
|
| 372 |
// Generate blob with revokable certs |
| 373 |
internalSigner, err := signer.New(revokableChain[0].PrivateKey, []*x509.Certificate{revokableChain[0].Cert, revokableChain[1].Cert}) |
| 374 |
if err != nil { |
| 375 |
t.Fatalf("Unexpected error while creating signer: %v", err) |
| 376 |
} |
| 377 |
envelopeBlob, _, err := internalSigner.Sign(context.Background(), payload.TargetArtifact, notation.SignerSignOptions{ExpiryDuration: 24 * time.Hour, SignatureMediaType: "application/jose+json"}) |
| 378 |
if err != nil { |
| 379 |
t.Fatalf("Unexpected error while generating blob: %v", err) |
| 380 |
} |
| 381 |
|
| 382 |
t.Run("enforced revoked cert", func(t *testing.T) { |
| 383 |
testedLevel := trustpolicy.LevelStrict |
| 384 |
policyDoc := dummyPolicyDocument() |
| 385 |
policyDoc.TrustPolicies[0].SignatureVerification.VerificationLevel = testedLevel.Name |
| 386 |
policyDoc.TrustPolicies[0].SignatureVerification.Override = map[trustpolicy.ValidationType]trustpolicy.ValidationAction{ |
| 387 |
trustpolicy.TypeAuthenticity: trustpolicy.ActionLog, |
| 388 |
trustpolicy.TypeRevocation: trustpolicy.ActionEnforce, |
| 389 |
} |
| 390 |
var expectedErr error = fmt.Errorf("signing certificate with subject %q is revoked", revokableChain[0].Cert.Subject.String()) |
| 391 |
expectedResult := notation.ValidationResult{ |
| 392 |
Type: trustpolicy.TypeRevocation, |
| 393 |
Action: trustpolicy.ActionEnforce, |
| 394 |
Error: expectedErr, |
| 395 |
} |
| 396 |
|
| 397 |
dir.UserConfigDir = "testdata" |
| 398 |
|
| 399 |
verifier := verifier{ |
| 400 |
trustPolicyDoc: &policyDoc, |
| 401 |
trustStore: truststore.NewX509TrustStore(dir.ConfigFS()), |
| 402 |
pluginManager: pluginManager, |
| 403 |
revocationClient: revocationClient, |
| 404 |
} |
| 405 |
outcome, err := verifier.Verify(context.Background(), desc, envelopeBlob, opts) |
| 406 |
if err == nil || err.Error() != expectedErr.Error() { |
| 407 |
t.Fatalf("Expected verify to fail with %v, but got %v", expectedErr, err) |
| 408 |
} |
| 409 |
verifyResult(outcome, expectedResult, expectedErr, t) |
| 410 |
}) |
| 411 |
t.Run("log revoked cert", func(t *testing.T) { |
| 412 |
testedLevel := trustpolicy.LevelStrict |
| 413 |
policyDoc := dummyPolicyDocument() |
| 414 |
policyDoc.TrustPolicies[0].SignatureVerification.VerificationLevel = testedLevel.Name |
| 415 |
policyDoc.TrustPolicies[0].SignatureVerification.Override = map[trustpolicy.ValidationType]trustpolicy.ValidationAction{ |
| 416 |
trustpolicy.TypeAuthenticity: trustpolicy.ActionLog, |
| 417 |
trustpolicy.TypeRevocation: trustpolicy.ActionLog, |
| 418 |
} |
| 419 |
var expectedErr error = fmt.Errorf("signing certificate with subject %q is revoked", revokableChain[0].Cert.Subject.String()) |
| 420 |
expectedResult := notation.ValidationResult{ |
| 421 |
Type: trustpolicy.TypeRevocation, |
| 422 |
Action: trustpolicy.ActionLog, |
| 423 |
Error: expectedErr, |
| 424 |
} |
| 425 |
|
| 426 |
dir.UserConfigDir = "testdata" |
| 427 |
|
| 428 |
verifier := verifier{ |
| 429 |
trustPolicyDoc: &policyDoc, |
| 430 |
trustStore: truststore.NewX509TrustStore(dir.ConfigFS()), |
| 431 |
pluginManager: pluginManager, |
| 432 |
revocationClient: revocationClient, |
| 433 |
} |
| 434 |
ctx := context.Background() |
| 435 |
outcome, err := verifier.Verify(ctx, desc, envelopeBlob, opts) |
| 436 |
if err != nil { |
| 437 |
t.Fatalf("Unexpected error while verifying: %v", err) |
| 438 |
} |
| 439 |
verifyResult(outcome, expectedResult, expectedErr, t) |
| 440 |
}) |
| 441 |
t.Run("skip revoked cert", func(t *testing.T) { |
| 442 |
testedLevel := trustpolicy.LevelStrict |
| 443 |
policyDoc := dummyPolicyDocument() |
| 444 |
policyDoc.TrustPolicies[0].SignatureVerification.VerificationLevel = testedLevel.Name |
| 445 |
policyDoc.TrustPolicies[0].SignatureVerification.Override = map[trustpolicy.ValidationType]trustpolicy.ValidationAction{ |
| 446 |
trustpolicy.TypeAuthenticity: trustpolicy.ActionLog, |
| 447 |
trustpolicy.TypeRevocation: trustpolicy.ActionSkip, |
| 448 |
} |
| 449 |
|
| 450 |
dir.UserConfigDir = "testdata" |
| 451 |
|
| 452 |
verifier := verifier{ |
| 453 |
trustPolicyDoc: &policyDoc, |
| 454 |
trustStore: truststore.NewX509TrustStore(dir.ConfigFS()), |
| 455 |
pluginManager: pluginManager, |
| 456 |
revocationClient: revocationClient, |
| 457 |
} |
| 458 |
outcome, err := verifier.Verify(context.Background(), desc, envelopeBlob, opts) |
| 459 |
if err != nil { |
| 460 |
t.Fatalf("Unexpected error while verifying: %v", err) |
| 461 |
} |
| 462 |
for _, result := range outcome.VerificationResults { |
| 463 |
if result.Type == trustpolicy.TypeRevocation { |
| 464 |
t.Fatal("expected no result for TypeRevocation after skip") |
| 465 |
} |
| 466 |
} |
| 467 |
}) |
| 468 |
} |
| 469 |
|
| 470 |
func createMockOutcome(certChain []*x509.Certificate, signingTime time.Time) *notation.VerificationOutcome { |
| 471 |
return ¬ation.VerificationOutcome{ |
| 472 |
EnvelopeContent: &signature.EnvelopeContent{ |
| 473 |
SignerInfo: signature.SignerInfo{ |
| 474 |
SignedAttributes: signature.SignedAttributes{ |
| 475 |
SigningTime: signingTime, |
| 476 |
SigningScheme: signature.SigningSchemeX509SigningAuthority, |
| 477 |
}, |
| 478 |
CertificateChain: certChain, |
| 479 |
}, |
| 480 |
}, |
| 481 |
VerificationLevel: &trustpolicy.VerificationLevel{ |
| 482 |
Enforcement: map[trustpolicy.ValidationType]trustpolicy.ValidationAction{trustpolicy.TypeRevocation: trustpolicy.ActionEnforce}, |
| 483 |
}, |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
func TestVerifyRevocation(t *testing.T) { |
| 488 |
zeroTime := time.Time{} |
| 489 |
|
| 490 |
revokableTuples := testhelper.GetRevokableRSAChain(3) |
| 491 |
revokableTuples[0].Cert.NotBefore = zeroTime |
| 492 |
revokableTuples[1].Cert.NotBefore = zeroTime |
| 493 |
revokableTuples[2].Cert.NotBefore = zeroTime |
| 494 |
revokableChain := []*x509.Certificate{revokableTuples[0].Cert, revokableTuples[1].Cert, revokableTuples[2].Cert} |
| 495 |
invalidChain := []*x509.Certificate{revokableTuples[1].Cert, revokableTuples[0].Cert, revokableTuples[2].Cert} |
| 496 |
|
| 497 |
goodClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Good}, nil, true) |
| 498 |
revokedClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Revoked}, nil, true) |
| 499 |
revokedInvalidityDate := time.Now().Add(-1 * time.Hour) |
| 500 |
revokedInvalidityClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Revoked}, &revokedInvalidityDate, true) |
| 501 |
unknownClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Unknown}, nil, true) |
| 502 |
unknownRevokedClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Unknown, ocsp.Revoked}, nil, true) |
| 503 |
revokedUnknownClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Revoked, ocsp.Unknown}, nil, true) |
| 504 |
pkixNoCheckClient := testhelper.MockClient(revokableTuples, []ocsp.ResponseStatus{ocsp.Good}, nil, false) |
| 505 |
timeoutClient := &http.Client{Timeout: 1 * time.Nanosecond} |
| 506 |
|
| 507 |
unknownMsg := fmt.Sprintf("signing certificate with subject %q revocation status is unknown", revokableChain[0].Subject.String()) |
| 508 |
revokedMsg := fmt.Sprintf("signing certificate with subject %q is revoked", revokableChain[0].Subject.String()) |
| 509 |
multiMsg := fmt.Sprintf("signing certificate with subject %q is revoked", revokableChain[1].Subject.String()) |
| 510 |
ctx := context.Background() |
| 511 |
|
| 512 |
t.Run("verifyRevocation nil client", func(t *testing.T) { |
| 513 |
v := &verifier{} |
| 514 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 515 |
expectedErrMsg := "unable to check revocation status, code signing revocation validator cannot be nil" |
| 516 |
if result.Error == nil || result.Error.Error() != expectedErrMsg { |
| 517 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", expectedErrMsg, result.Error) |
| 518 |
} |
| 519 |
}) |
| 520 |
t.Run("verifyRevocation invalid chain", func(t *testing.T) { |
| 521 |
revocationClient, err := revocation.New(goodClient) |
| 522 |
if err != nil { |
| 523 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 524 |
} |
| 525 |
v := &verifier{ |
| 526 |
revocationClient: revocationClient, |
| 527 |
} |
| 528 |
result := v.verifyRevocation(ctx, createMockOutcome(invalidChain, time.Now())) |
| 529 |
expectedErrMsg := "unable to check revocation status, err: invalid chain: expected chain to be correct and complete: invalid certificates or certificate with subject \"CN=Notation Test Revokable RSA Chain Cert 2,O=Notary,L=Seattle,ST=WA,C=US\" is not issued by \"CN=Notation Test Revokable RSA Chain Cert 3,O=Notary,L=Seattle,ST=WA,C=US\". Error: x509: invalid signature: parent certificate cannot sign this kind of certificate" |
| 530 |
if result.Error == nil || result.Error.Error() != expectedErrMsg { |
| 531 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", expectedErrMsg, result.Error) |
| 532 |
} |
| 533 |
}) |
| 534 |
t.Run("verifyRevocation non-revoked", func(t *testing.T) { |
| 535 |
revocationClient, err := revocation.New(goodClient) |
| 536 |
if err != nil { |
| 537 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 538 |
} |
| 539 |
v := &verifier{ |
| 540 |
revocationClient: revocationClient, |
| 541 |
} |
| 542 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 543 |
if result.Error != nil { |
| 544 |
t.Fatalf("expected verifyRevocation to succeed, but got %v", result.Error) |
| 545 |
} |
| 546 |
}) |
| 547 |
t.Run("verifyRevocation OCSP revoked no invalidity", func(t *testing.T) { |
| 548 |
revocationClient, err := revocation.New(revokedClient) |
| 549 |
if err != nil { |
| 550 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 551 |
} |
| 552 |
v := &verifier{ |
| 553 |
revocationClient: revocationClient, |
| 554 |
} |
| 555 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 556 |
if result.Error == nil || result.Error.Error() != revokedMsg { |
| 557 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", revokedMsg, result.Error) |
| 558 |
} |
| 559 |
}) |
| 560 |
t.Run("verifyRevocation OCSP revoked with invalidiy", func(t *testing.T) { |
| 561 |
revocationClient, err := revocation.New(revokedInvalidityClient) |
| 562 |
if err != nil { |
| 563 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 564 |
} |
| 565 |
v := &verifier{ |
| 566 |
revocationClient: revocationClient, |
| 567 |
} |
| 568 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 569 |
if result.Error == nil || result.Error.Error() != revokedMsg { |
| 570 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", revokedMsg, result.Error) |
| 571 |
} |
| 572 |
}) |
| 573 |
t.Run("verifyRevocation OCSP unknown", func(t *testing.T) { |
| 574 |
revocationClient, err := revocation.New(unknownClient) |
| 575 |
if err != nil { |
| 576 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 577 |
} |
| 578 |
v := &verifier{ |
| 579 |
revocationClient: revocationClient, |
| 580 |
} |
| 581 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 582 |
if result.Error == nil || result.Error.Error() != unknownMsg { |
| 583 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", unknownMsg, result.Error) |
| 584 |
} |
| 585 |
}) |
| 586 |
t.Run("verifyRevocation OCSP unknown then revoked", func(t *testing.T) { |
| 587 |
revocationClient, err := revocation.New(unknownRevokedClient) |
| 588 |
if err != nil { |
| 589 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 590 |
} |
| 591 |
v := &verifier{ |
| 592 |
revocationClient: revocationClient, |
| 593 |
} |
| 594 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 595 |
if result.Error == nil || result.Error.Error() != multiMsg { |
| 596 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", multiMsg, result.Error) |
| 597 |
} |
| 598 |
}) |
| 599 |
t.Run("verifyRevocation OCSP revoked then unknown", func(t *testing.T) { |
| 600 |
revocationClient, err := revocation.New(revokedUnknownClient) |
| 601 |
if err != nil { |
| 602 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 603 |
} |
| 604 |
v := &verifier{ |
| 605 |
revocationClient: revocationClient, |
| 606 |
} |
| 607 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 608 |
if result.Error == nil || result.Error.Error() != revokedMsg { |
| 609 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", revokedMsg, result.Error) |
| 610 |
} |
| 611 |
}) |
| 612 |
t.Run("verifyRevocation missing id-pkix-ocsp-nocheck", func(t *testing.T) { |
| 613 |
revocationClient, err := revocation.New(pkixNoCheckClient) |
| 614 |
if err != nil { |
| 615 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 616 |
} |
| 617 |
v := &verifier{ |
| 618 |
revocationClient: revocationClient, |
| 619 |
} |
| 620 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 621 |
if result.Error != nil { |
| 622 |
t.Fatalf("expected verifyRevocation to succeed, but got %v", result.Error) |
| 623 |
} |
| 624 |
}) |
| 625 |
t.Run("verifyRevocation timeout", func(t *testing.T) { |
| 626 |
revocationClient, err := revocation.New(timeoutClient) |
| 627 |
if err != nil { |
| 628 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 629 |
} |
| 630 |
v := &verifier{ |
| 631 |
revocationClient: revocationClient, |
| 632 |
} |
| 633 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now())) |
| 634 |
if result.Error == nil || result.Error.Error() != unknownMsg { |
| 635 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", unknownMsg, result.Error) |
| 636 |
} |
| 637 |
}) |
| 638 |
t.Run("verifyRevocation older signing time no invalidity", func(t *testing.T) { |
| 639 |
revocationClient, err := revocation.New(revokedClient) |
| 640 |
if err != nil { |
| 641 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 642 |
} |
| 643 |
v := &verifier{ |
| 644 |
revocationClient: revocationClient, |
| 645 |
} |
| 646 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now().Add(-4*time.Hour))) |
| 647 |
if result.Error == nil || result.Error.Error() != revokedMsg { |
| 648 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", revokedMsg, result.Error) |
| 649 |
} |
| 650 |
}) |
| 651 |
t.Run("verifyRevocation zero signing time", func(t *testing.T) { |
| 652 |
revocationClient, err := revocation.New(revokedClient) |
| 653 |
if err != nil { |
| 654 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 655 |
} |
| 656 |
expectedErrMsg := "signing certificate with subject \"CN=Notation Test Revokable RSA Chain Cert 3,O=Notary,L=Seattle,ST=WA,C=US\" is revoked" |
| 657 |
v := &verifier{ |
| 658 |
revocationClient: revocationClient, |
| 659 |
} |
| 660 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, zeroTime)) |
| 661 |
if result.Error == nil || result.Error.Error() != expectedErrMsg { |
| 662 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", expectedErrMsg, result.Error) |
| 663 |
} |
| 664 |
if !zeroTime.IsZero() { |
| 665 |
t.Fatalf("exected zeroTime.IsZero() to be true") |
| 666 |
} |
| 667 |
}) |
| 668 |
t.Run("verifyRevocation older signing time with invalidity", func(t *testing.T) { |
| 669 |
revocationClient, err := revocation.New(revokedInvalidityClient) |
| 670 |
if err != nil { |
| 671 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 672 |
} |
| 673 |
v := &verifier{ |
| 674 |
revocationClient: revocationClient, |
| 675 |
} |
| 676 |
result := v.verifyRevocation(ctx, createMockOutcome(revokableChain, time.Now().Add(-4*time.Hour))) |
| 677 |
if result.Error != nil { |
| 678 |
t.Fatalf("expected verifyRevocation to succeed, but got %v", result.Error) |
| 679 |
} |
| 680 |
}) |
| 681 |
t.Run("verifyRevocation non-authentic signing time with invalidity", func(t *testing.T) { |
| 682 |
revocationClient, err := revocation.New(revokedInvalidityClient) |
| 683 |
if err != nil { |
| 684 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 685 |
} |
| 686 |
// Specifying older signing time (which should succeed), but will use zero time since no authentic signing time |
| 687 |
outcome := createMockOutcome(revokableChain, time.Now().Add(-4*time.Hour)) |
| 688 |
outcome.EnvelopeContent.SignerInfo.SignedAttributes.SigningScheme = "notary.x509" |
| 689 |
authenticSigningTime, err := outcome.EnvelopeContent.SignerInfo.AuthenticSigningTime() |
| 690 |
expectedErr := errors.New("authentic signing time not supported under signing scheme \"notary.x509\"") |
| 691 |
if !authenticSigningTime.IsZero() || err == nil || err.Error() != expectedErr.Error() { |
| 692 |
t.Fatalf("expected AuthenticSigningTime to fail with %v, but got %v", expectedErr, err) |
| 693 |
} |
| 694 |
v := &verifier{ |
| 695 |
revocationClient: revocationClient, |
| 696 |
} |
| 697 |
result := v.verifyRevocation(ctx, outcome) |
| 698 |
if result.Error == nil || result.Error.Error() != revokedMsg { |
| 699 |
t.Fatalf("expected verifyRevocation to fail with %s, but got %v", revokedMsg, result.Error) |
| 700 |
} |
| 701 |
}) |
| 702 |
} |
| 703 |
|
| 704 |
func TestNew(t *testing.T) { |
| 705 |
if _, err := New(&policy, store, pm); err != nil { |
| 706 |
t.Fatalf("expected New constructor to succeed, but got %v", err) |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
func TestNewWithOptions(t *testing.T) { |
| 711 |
if _, err := NewWithOptions(&policy, store, pm, VerifierOptions{}); err != nil { |
| 712 |
t.Fatalf("expected NewWithOptions constructor to succeed, but got %v", err) |
| 713 |
} |
| 714 |
|
| 715 |
r, err := revocation.New(&http.Client{}) |
| 716 |
if err != nil { |
| 717 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 718 |
} |
| 719 |
opts := VerifierOptions{RevocationClient: r} |
| 720 |
|
| 721 |
_, err = NewWithOptions(&policy, store, pm, opts) |
| 722 |
if err != nil { |
| 723 |
t.Fatalf("expected NewWithOptions constructor to succeed, but got %v", err) |
| 724 |
} |
| 725 |
|
| 726 |
revocationCodeSigningValidator, err := revocation.NewWithOptions(revocation.Options{ |
| 727 |
OCSPHTTPClient: &http.Client{}, |
| 728 |
CertChainPurpose: purpose.CodeSigning, |
| 729 |
}) |
| 730 |
if err != nil { |
| 731 |
t.Fatal(err) |
| 732 |
} |
| 733 |
revocationTimestampingValidator, err := revocation.NewWithOptions(revocation.Options{ |
| 734 |
OCSPHTTPClient: &http.Client{}, |
| 735 |
CertChainPurpose: purpose.Timestamping, |
| 736 |
}) |
| 737 |
if err != nil { |
| 738 |
t.Fatal(err) |
| 739 |
} |
| 740 |
opts.RevocationCodeSigningValidator = revocationCodeSigningValidator |
| 741 |
opts.RevocationTimestampingValidator = revocationTimestampingValidator |
| 742 |
_, err = NewWithOptions(&policy, store, pm, opts) |
| 743 |
if err != nil { |
| 744 |
t.Fatalf("expected NewWithOptions constructor to succeed, but got %v", err) |
| 745 |
} |
| 746 |
|
| 747 |
opts.RevocationClient = nil |
| 748 |
_, err = NewWithOptions(&policy, store, pm, opts) |
| 749 |
if err != nil { |
| 750 |
t.Fatalf("expected NewWithOptions constructor to succeed, but got %v", err) |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
func TestNewWithOptionsError(t *testing.T) { |
| 755 |
r, err := revocation.New(&http.Client{}) |
| 756 |
if err != nil { |
| 757 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 758 |
} |
| 759 |
rt, err := revocation.NewWithOptions(revocation.Options{ |
| 760 |
OCSPHTTPClient: &http.Client{}, |
| 761 |
CertChainPurpose: purpose.Timestamping, |
| 762 |
}) |
| 763 |
if err != nil { |
| 764 |
t.Fatalf("unexpected error while creating revocation timestamp object: %v", err) |
| 765 |
} |
| 766 |
opts := VerifierOptions{ |
| 767 |
RevocationClient: r, |
| 768 |
RevocationTimestampingValidator: rt, |
| 769 |
} |
| 770 |
|
| 771 |
_, err = NewWithOptions(nil, store, pm, opts) |
| 772 |
expectedErrMsg := "trustPolicy cannot be nil" |
| 773 |
if err == nil || err.Error() != expectedErrMsg { |
| 774 |
t.Errorf("expected %s, but got %s", expectedErrMsg, err) |
| 775 |
} |
| 776 |
|
| 777 |
_, err = NewWithOptions(&policy, nil, pm, opts) |
| 778 |
expectedErrMsg = "trustStore cannot be nil" |
| 779 |
if err == nil || err.Error() != expectedErrMsg { |
| 780 |
t.Errorf("expected %s, but got %s", expectedErrMsg, err) |
| 781 |
} |
| 782 |
|
| 783 |
_, err = NewWithOptions(&invalidPolicy, store, pm, opts) |
| 784 |
expectedErrMsg = "trust policy document has empty version, version must be specified" |
| 785 |
if err == nil || err.Error() != expectedErrMsg { |
| 786 |
t.Errorf("expected %s, but got %s", expectedErrMsg, err) |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
func TestVerificationPluginInteractions(t *testing.T) { |
| 791 |
assertPluginVerification(signature.SigningSchemeX509, t) |
| 792 |
assertPluginVerification(signature.SigningSchemeX509SigningAuthority, t) |
| 793 |
} |
| 794 |
|
| 795 |
func assertPluginVerification(scheme signature.SigningScheme, t *testing.T) { |
| 796 |
var pluginSigEnv []byte |
| 797 |
if scheme == signature.SigningSchemeX509 { |
| 798 |
pluginSigEnv = mock.MockCaPluginSigEnv |
| 799 |
} else if scheme == signature.SigningSchemeX509SigningAuthority { |
| 800 |
pluginSigEnv = mock.MockSaPluginSigEnv |
| 801 |
} |
| 802 |
|
| 803 |
policyDocument := dummyPolicyDocument() |
| 804 |
dir.UserConfigDir = "testdata" |
| 805 |
x509TrustStore := truststore.NewX509TrustStore(dir.ConfigFS()) |
| 806 |
|
| 807 |
// verification plugin is not installed |
| 808 |
pluginManager := mock.PluginManager{} |
| 809 |
pluginManager.GetPluginError = errors.New("plugin not found") |
| 810 |
|
| 811 |
revocationClient, err := revocation.New(&http.Client{Timeout: 2 * time.Second}) |
| 812 |
if err != nil { |
| 813 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 814 |
} |
| 815 |
v := verifier{ |
| 816 |
trustPolicyDoc: &policyDocument, |
| 817 |
trustStore: x509TrustStore, |
| 818 |
pluginManager: pluginManager, |
| 819 |
revocationClient: revocationClient, |
| 820 |
} |
| 821 |
opts := notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 822 |
outcome, err := v.Verify(context.Background(), ocispec.Descriptor{}, pluginSigEnv, opts) |
| 823 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "error while locating the verification plugin \"plugin-name\", make sure the plugin is installed successfully before verifying the signature. error: plugin not found" { |
| 824 |
t.Fatalf("verification should fail if the verification plugin is not found") |
| 825 |
} |
| 826 |
|
| 827 |
// plugin is installed but without verification capabilities |
| 828 |
pluginManager = mock.PluginManager{} |
| 829 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilitySignatureGenerator} |
| 830 |
|
| 831 |
v = verifier{ |
| 832 |
trustPolicyDoc: &policyDocument, |
| 833 |
trustStore: x509TrustStore, |
| 834 |
pluginManager: pluginManager, |
| 835 |
revocationClient: revocationClient, |
| 836 |
} |
| 837 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 838 |
outcome, err = v.Verify(context.Background(), ocispec.Descriptor{}, pluginSigEnv, opts) |
| 839 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "digital signature requires plugin \"plugin-name\" with signature verification capabilities (\"SIGNATURE_VERIFIER.TRUSTED_IDENTITY\" and/or \"SIGNATURE_VERIFIER.REVOCATION_CHECK\") installed" { |
| 840 |
t.Fatalf("verification should fail if the verification plugin is not found") |
| 841 |
} |
| 842 |
|
| 843 |
// plugin interactions with trusted identity verification success |
| 844 |
pluginManager = mock.PluginManager{} |
| 845 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityTrustedIdentityVerifier} |
| 846 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 847 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 848 |
proto.CapabilityTrustedIdentityVerifier: { |
| 849 |
Success: true, |
| 850 |
}, |
| 851 |
}, |
| 852 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 853 |
} |
| 854 |
|
| 855 |
v = verifier{ |
| 856 |
trustPolicyDoc: &policyDocument, |
| 857 |
trustStore: x509TrustStore, |
| 858 |
pluginManager: pluginManager, |
| 859 |
revocationClient: revocationClient, |
| 860 |
} |
| 861 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 862 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 863 |
if err != nil || outcome.Error != nil { |
| 864 |
t.Fatalf("verification should succeed when the verification plugin succeeds for trusted identity verification. error : %v", outcome.Error) |
| 865 |
} |
| 866 |
|
| 867 |
// plugin interactions with trusted identity verification failure |
| 868 |
pluginManager = mock.PluginManager{} |
| 869 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityTrustedIdentityVerifier} |
| 870 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 871 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 872 |
proto.CapabilityTrustedIdentityVerifier: { |
| 873 |
Success: false, |
| 874 |
Reason: "i feel like failing today", |
| 875 |
}, |
| 876 |
}, |
| 877 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 878 |
} |
| 879 |
|
| 880 |
v = verifier{ |
| 881 |
trustPolicyDoc: &policyDocument, |
| 882 |
trustStore: x509TrustStore, |
| 883 |
pluginManager: pluginManager, |
| 884 |
revocationClient: revocationClient, |
| 885 |
} |
| 886 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 887 |
outcome, err = v.Verify(context.Background(), ocispec.Descriptor{}, pluginSigEnv, opts) |
| 888 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "trusted identify verification by plugin \"plugin-name\" failed with reason \"i feel like failing today\"" { |
| 889 |
t.Fatalf("verification should fail when the verification plugin fails for trusted identity verification. error : %v", outcome.Error) |
| 890 |
} |
| 891 |
|
| 892 |
// plugin interactions with revocation verification success |
| 893 |
pluginManager = mock.PluginManager{} |
| 894 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityRevocationCheckVerifier} |
| 895 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 896 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 897 |
proto.CapabilityRevocationCheckVerifier: { |
| 898 |
Success: true, |
| 899 |
}, |
| 900 |
}, |
| 901 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 902 |
} |
| 903 |
|
| 904 |
v = verifier{ |
| 905 |
trustPolicyDoc: &policyDocument, |
| 906 |
trustStore: x509TrustStore, |
| 907 |
pluginManager: pluginManager, |
| 908 |
revocationClient: revocationClient, |
| 909 |
} |
| 910 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 911 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 912 |
if err != nil || outcome.Error != nil { |
| 913 |
t.Fatalf("verification should succeed when the verification plugin succeeds for revocation verification. error : %v", outcome.Error) |
| 914 |
} |
| 915 |
|
| 916 |
// plugin interactions with trusted revocation failure |
| 917 |
pluginManager = mock.PluginManager{} |
| 918 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityRevocationCheckVerifier} |
| 919 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 920 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 921 |
proto.CapabilityRevocationCheckVerifier: { |
| 922 |
Success: false, |
| 923 |
Reason: "i feel like failing today", |
| 924 |
}, |
| 925 |
}, |
| 926 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 927 |
} |
| 928 |
|
| 929 |
v = verifier{ |
| 930 |
trustPolicyDoc: &policyDocument, |
| 931 |
trustStore: x509TrustStore, |
| 932 |
pluginManager: pluginManager, |
| 933 |
revocationClient: revocationClient, |
| 934 |
} |
| 935 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 936 |
outcome, err = v.Verify(context.Background(), ocispec.Descriptor{}, pluginSigEnv, opts) |
| 937 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "revocation check by verification plugin \"plugin-name\" failed with reason \"i feel like failing today\"" { |
| 938 |
t.Fatalf("verification should fail when the verification plugin fails for revocation check verification. error : %v", outcome.Error) |
| 939 |
} |
| 940 |
|
| 941 |
// plugin interactions with both trusted identity & revocation verification |
| 942 |
pluginManager = mock.PluginManager{} |
| 943 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityRevocationCheckVerifier, proto.CapabilityTrustedIdentityVerifier} |
| 944 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 945 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 946 |
proto.CapabilityRevocationCheckVerifier: { |
| 947 |
Success: true, |
| 948 |
}, |
| 949 |
proto.CapabilityTrustedIdentityVerifier: { |
| 950 |
Success: true, |
| 951 |
}, |
| 952 |
}, |
| 953 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 954 |
} |
| 955 |
|
| 956 |
v = verifier{ |
| 957 |
trustPolicyDoc: &policyDocument, |
| 958 |
trustStore: x509TrustStore, |
| 959 |
pluginManager: pluginManager, |
| 960 |
revocationClient: revocationClient, |
| 961 |
} |
| 962 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 963 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 964 |
if err != nil || outcome.Error != nil { |
| 965 |
t.Fatalf("verification should succeed when the verification plugin succeeds for both trusted identity and revocation check verifications. error : %v", outcome.Error) |
| 966 |
} |
| 967 |
|
| 968 |
// plugin interactions with skipped revocation |
| 969 |
policyDocument.TrustPolicies[0].SignatureVerification.Override = map[trustpolicy.ValidationType]trustpolicy.ValidationAction{trustpolicy.TypeRevocation: trustpolicy.ActionSkip} |
| 970 |
pluginManager = mock.PluginManager{} |
| 971 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityRevocationCheckVerifier} |
| 972 |
pluginManager.PluginRunnerExecuteError = errors.New("revocation plugin should not be invoked when the trust policy skips revocation check") |
| 973 |
|
| 974 |
v = verifier{ |
| 975 |
trustPolicyDoc: &policyDocument, |
| 976 |
trustStore: x509TrustStore, |
| 977 |
pluginManager: pluginManager, |
| 978 |
revocationClient: revocationClient, |
| 979 |
} |
| 980 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 981 |
trustPolicy, err := (&policyDocument).GetApplicableTrustPolicy(opts.ArtifactReference) |
| 982 |
if err != nil { |
| 983 |
t.Fatalf("cannot get trustPolicy") |
| 984 |
} |
| 985 |
verificationLevel, _ := trustPolicy.SignatureVerification.GetVerificationLevel() |
| 986 |
outcome = ¬ation.VerificationOutcome{ |
| 987 |
VerificationResults: []*notation.ValidationResult{}, |
| 988 |
VerificationLevel: verificationLevel, |
| 989 |
} |
| 990 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 991 |
if err != nil || outcome.Error != nil { |
| 992 |
t.Fatalf("revocation plugin should not be invoked when the trust policy skips the revocation check. error : %v", outcome.Error) |
| 993 |
} |
| 994 |
|
| 995 |
// plugin unexpected response |
| 996 |
pluginManager = mock.PluginManager{} |
| 997 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityTrustedIdentityVerifier} |
| 998 |
pluginManager.PluginRunnerExecuteResponse = "invalid plugin response" |
| 999 |
pluginManager.PluginRunnerExecuteError = errors.New("invalid plugin response") |
| 1000 |
|
| 1001 |
v = verifier{ |
| 1002 |
trustPolicyDoc: &policyDocument, |
| 1003 |
trustStore: x509TrustStore, |
| 1004 |
pluginManager: pluginManager, |
| 1005 |
revocationClient: revocationClient, |
| 1006 |
} |
| 1007 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 1008 |
trustPolicy, err = (&policyDocument).GetApplicableTrustPolicy(opts.ArtifactReference) |
| 1009 |
if err != nil { |
| 1010 |
t.Fatalf("cannot get trustPolicy") |
| 1011 |
} |
| 1012 |
verificationLevel, _ = trustPolicy.SignatureVerification.GetVerificationLevel() |
| 1013 |
outcome = ¬ation.VerificationOutcome{ |
| 1014 |
VerificationResults: []*notation.ValidationResult{}, |
| 1015 |
VerificationLevel: verificationLevel, |
| 1016 |
} |
| 1017 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 1018 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "failed to verify with plugin plugin-name: invalid plugin response" { |
| 1019 |
t.Fatalf("verification should fail when the verification plugin returns unexpected response. error : %v", outcome.Error) |
| 1020 |
} |
| 1021 |
|
| 1022 |
// plugin did not process all extended critical attributes |
| 1023 |
pluginManager = mock.PluginManager{} |
| 1024 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityTrustedIdentityVerifier} |
| 1025 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 1026 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 1027 |
proto.CapabilityTrustedIdentityVerifier: { |
| 1028 |
Success: true, |
| 1029 |
}, |
| 1030 |
}, |
| 1031 |
ProcessedAttributes: []interface{}{}, // exclude the critical attribute |
| 1032 |
} |
| 1033 |
|
| 1034 |
v = verifier{ |
| 1035 |
trustPolicyDoc: &policyDocument, |
| 1036 |
trustStore: x509TrustStore, |
| 1037 |
pluginManager: pluginManager, |
| 1038 |
revocationClient: revocationClient, |
| 1039 |
} |
| 1040 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 1041 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 1042 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "extended critical attribute \"SomeKey\" was not processed by the verification plugin \"plugin-name\" (all extended critical attributes must be processed by the verification plugin)" { |
| 1043 |
t.Fatalf("verification should fail when the verification plugin fails to process an extended critical attribute. error : %v", outcome.Error) |
| 1044 |
} |
| 1045 |
|
| 1046 |
// plugin returned empty result for a capability |
| 1047 |
pluginManager = mock.PluginManager{} |
| 1048 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityTrustedIdentityVerifier} |
| 1049 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 1050 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{}, |
| 1051 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 1052 |
} |
| 1053 |
|
| 1054 |
v = verifier{ |
| 1055 |
trustPolicyDoc: &policyDocument, |
| 1056 |
trustStore: x509TrustStore, |
| 1057 |
pluginManager: pluginManager, |
| 1058 |
revocationClient: revocationClient, |
| 1059 |
} |
| 1060 |
opts = notation.VerifierVerifyOptions{ArtifactReference: mock.SampleArtifactUri, SignatureMediaType: "application/jose+json"} |
| 1061 |
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts) |
| 1062 |
if err == nil || outcome.Error == nil || outcome.Error.Error() != "verification plugin \"plugin-name\" failed to verify \"SIGNATURE_VERIFIER.TRUSTED_IDENTITY\"" { |
| 1063 |
t.Fatalf("verification should fail when the verification plugin does not return response for a capability. error : %v", outcome.Error) |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
func TestVerifyX509TrustedIdentities(t *testing.T) { |
| 1068 |
certs, _ := corex509.ReadCertificateFile(filepath.FromSlash("testdata/verifier/signing-cert.pem")) // cert's subject is "CN=SomeCN,OU=SomeOU,O=SomeOrg,L=Seattle,ST=WA,C=US" |
| 1069 |
unsupportedCerts, _ := corex509.ReadCertificateFile(filepath.FromSlash("testdata/verifier/bad-cert.pem")) // cert's subject is "CN=bad=#CN,OU=SomeOU,O=SomeOrg,L=Seattle,ST=WA,C=US" |
| 1070 |
|
| 1071 |
tests := []struct { |
| 1072 |
certs []*x509.Certificate |
| 1073 |
x509Identities []string |
| 1074 |
wantErr bool |
| 1075 |
}{ |
| 1076 |
{certs, []string{"x509.subject:C=US,O=SomeOrg,ST=WA"}, false}, |
| 1077 |
{certs, []string{"x509.subject:C=US,O=SomeOrg,ST=WA", "nonX509Prefix:my-custom-identity"}, false}, |
| 1078 |
{certs, []string{"x509.subject:C=US,O=SomeOrg,ST=WA", "x509.subject:C=IND,O=SomeOrg,ST=TS"}, false}, |
| 1079 |
{certs, []string{"nonX509Prefix:my-custom-identity"}, true}, |
| 1080 |
{certs, []string{"*"}, false}, |
| 1081 |
{certs, []string{"x509.subject:C=IND,O=SomeOrg,ST=TS"}, true}, |
| 1082 |
{certs, []string{"x509.subject:C=IND,O=SomeOrg,ST=TS", "nonX509Prefix:my-custom-identity"}, true}, |
| 1083 |
{certs, []string{"x509.subject:C=IND,O=SomeOrg,ST=TS", "x509.subject:C=LOL,O=LOL,ST=LOL"}, true}, |
| 1084 |
{certs, []string{"x509.subject:C=bad=#identity,O=LOL,ST=LOL"}, true}, |
| 1085 |
{unsupportedCerts, []string{"x509.subject:C=US,O=SomeOrg,ST=WA", "nonX509Prefix:my-custom-identity"}, true}, |
| 1086 |
} |
| 1087 |
for i, tt := range tests { |
| 1088 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 1089 |
trustPolicy := trustpolicy.TrustPolicy{ |
| 1090 |
Name: "test-statement-name", |
| 1091 |
RegistryScopes: []string{"registry.acme-rockets.io/software/net-monitor"}, |
| 1092 |
SignatureVerification: trustpolicy.SignatureVerification{VerificationLevel: "strict"}, |
| 1093 |
TrustStores: []string{"ca:test-store"}, |
| 1094 |
TrustedIdentities: tt.x509Identities, |
| 1095 |
} |
| 1096 |
err := verifyX509TrustedIdentities(trustPolicy.Name, trustPolicy.TrustedIdentities, tt.certs) |
| 1097 |
|
| 1098 |
if tt.wantErr != (err != nil) { |
| 1099 |
t.Fatalf("TestVerifyX509TrustedIdentities Error: %q WantErr: %v", err, tt.wantErr) |
| 1100 |
} |
| 1101 |
}) |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
func TestVerifyUserMetadata(t *testing.T) { |
| 1106 |
policyDocument := dummyPolicyDocument() |
| 1107 |
policyDocument.TrustPolicies[0].SignatureVerification.VerificationLevel = trustpolicy.LevelAudit.Name |
| 1108 |
|
| 1109 |
pluginManager := mock.PluginManager{} |
| 1110 |
pluginManager.GetPluginError = errors.New("plugin should not be invoked when verification plugin is not specified in the signature") |
| 1111 |
pluginManager.PluginRunnerLoadError = errors.New("plugin should not be invoked when verification plugin is not specified in the signature") |
| 1112 |
revocationClient, err := revocation.New(&http.Client{Timeout: 2 * time.Second}) |
| 1113 |
if err != nil { |
| 1114 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 1115 |
} |
| 1116 |
verifier := verifier{ |
| 1117 |
trustPolicyDoc: &policyDocument, |
| 1118 |
trustStore: truststore.NewX509TrustStore(dir.ConfigFS()), |
| 1119 |
pluginManager: pluginManager, |
| 1120 |
revocationClient: revocationClient, |
| 1121 |
} |
| 1122 |
|
| 1123 |
tests := []struct { |
| 1124 |
metadata map[string]string |
| 1125 |
wantErr bool |
| 1126 |
}{ |
| 1127 |
{map[string]string{}, false}, |
| 1128 |
{map[string]string{"io.wabbit-networks.buildId": "123"}, false}, |
| 1129 |
{map[string]string{"io.wabbit-networks.buildId": "321"}, true}, |
| 1130 |
{map[string]string{"io.wabbit-networks.buildId": "123", "io.wabbit-networks.buildTime": "1672944615"}, false}, |
| 1131 |
{map[string]string{"io.wabbit-networks.buildId": "123", "io.wabbit-networks.buildTime": "1"}, true}, |
| 1132 |
} |
| 1133 |
|
| 1134 |
for i, tt := range tests { |
| 1135 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 1136 |
_, err := verifier.Verify( |
| 1137 |
context.Background(), |
| 1138 |
mock.MetadataSigEnvDescriptor, |
| 1139 |
mock.MockSigEnvWithMetadata, |
| 1140 |
notation.VerifierVerifyOptions{ |
| 1141 |
ArtifactReference: mock.SampleArtifactUri, |
| 1142 |
SignatureMediaType: "application/jose+json", |
| 1143 |
UserMetadata: tt.metadata, |
| 1144 |
}, |
| 1145 |
) |
| 1146 |
|
| 1147 |
if tt.wantErr != (err != nil) { |
| 1148 |
t.Fatalf("TestVerifyUserMetadata Error: %q WantErr: %v", err, tt.wantErr) |
| 1149 |
} |
| 1150 |
}) |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
func TestPluginVersionCompatibility(t *testing.T) { |
| 1155 |
|
| 1156 |
errTemplate := "found plugin io.cncf.notary.plugin.unittest.mock with version 1.0.0 but signature verification needs plugin version greater than or equal to " |
| 1157 |
var policyDocument = trustpolicy.Document{ |
| 1158 |
Version: "1.0", |
| 1159 |
TrustPolicies: []trustpolicy.TrustPolicy{ |
| 1160 |
{ |
| 1161 |
Name: "wabbit-networks-images", |
| 1162 |
RegistryScopes: []string{"localhost:5000/net-monitor"}, |
| 1163 |
SignatureVerification: trustpolicy.SignatureVerification{VerificationLevel: trustpolicy.LevelStrict.Name}, |
| 1164 |
TrustStores: []string{"ca:valid-trust-store"}, |
| 1165 |
TrustedIdentities: []string{"x509.subject: CN=wabbit-networks.io,O=Notary,L=Seattle,ST=WA,C=US"}, |
| 1166 |
}, |
| 1167 |
}, |
| 1168 |
} |
| 1169 |
pluginManager := mock.PluginManager{} |
| 1170 |
pluginManager.PluginCapabilities = []proto.Capability{proto.CapabilityTrustedIdentityVerifier} |
| 1171 |
pluginManager.PluginRunnerExecuteResponse = &proto.VerifySignatureResponse{ |
| 1172 |
VerificationResults: map[proto.Capability]*proto.VerificationResult{ |
| 1173 |
proto.CapabilityTrustedIdentityVerifier: { |
| 1174 |
Success: true, |
| 1175 |
}, |
| 1176 |
}, |
| 1177 |
ProcessedAttributes: []interface{}{mock.PluginExtendedCriticalAttribute.Key}, |
| 1178 |
} |
| 1179 |
dir.UserConfigDir = "testdata" |
| 1180 |
x509TrustStore := truststore.NewX509TrustStore(dir.ConfigFS()) |
| 1181 |
revocationClient, err := revocation.New(&http.Client{Timeout: 2 * time.Second}) |
| 1182 |
if err != nil { |
| 1183 |
t.Fatalf("unexpected error while creating revocation object: %v", err) |
| 1184 |
} |
| 1185 |
v := verifier{ |
| 1186 |
trustPolicyDoc: &policyDocument, |
| 1187 |
trustStore: x509TrustStore, |
| 1188 |
pluginManager: pluginManager, |
| 1189 |
revocationClient: revocationClient, |
| 1190 |
} |
| 1191 |
opts := notation.VerifierVerifyOptions{ArtifactReference: "localhost:5000/net-monitor@sha256:fe7e9333395060c2f5e63cf36a38fba10176f183b4163a5794e081a480abba5f", SignatureMediaType: "application/jose+json"} |
| 1192 |
|
| 1193 |
tests := []struct { |
| 1194 |
minPluginVerTests []byte |
| 1195 |
wantErr string |
| 1196 |
}{ |
| 1197 |
|
| 1198 |
{mock.MockCaIncompatiblePluginVerSigEnv_1_0_9, errTemplate + "1.0.9"}, |
| 1199 |
{mock.MockCaIncompatiblePluginVerSigEnv_1_0_1, errTemplate + "1.0.1"}, |
| 1200 |
{mock.MockCaIncompatiblePluginVerSigEnv_1_2_3, errTemplate + "1.2.3"}, |
| 1201 |
{mock.MockCaIncompatiblePluginVerSigEnv_1_1_0_alpha, errTemplate + "1.1.0-alpha"}, |
| 1202 |
{mock.MockCaCompatiblePluginVerSigEnv_0_0_9, ""}, |
| 1203 |
{mock.MockCaCompatiblePluginVerSigEnv_1_0_0_alpha, ""}, |
| 1204 |
{mock.MockCaCompatiblePluginVerSigEnv_1_0_0_alpha_beta, ""}, |
| 1205 |
{mock.MockCaCompatiblePluginVerSigEnv_1_0_0, ""}, |
| 1206 |
} |
| 1207 |
for _, tt := range tests { |
| 1208 |
|
| 1209 |
if _, err := v.Verify(context.Background(), mock.TestImageDescriptor, tt.minPluginVerTests, opts); err != nil && tt.wantErr != "" { |
| 1210 |
if err.Error() != tt.wantErr { |
| 1211 |
t.Errorf("TestPluginVersionCompatibility Error: %s, WantErr: %s ", err.Error(), tt.wantErr) |
| 1212 |
} |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
func TestIsRequiredVerificationPluginVer(t *testing.T) { |
| 1218 |
testPlugVer := "1.0.0" |
| 1219 |
|
| 1220 |
tests := []struct { |
| 1221 |
minVerTests []string |
| 1222 |
expectedVal bool |
| 1223 |
}{ |
| 1224 |
{[]string{"0.0.9"}, true}, |
| 1225 |
{[]string{"1.0.0"}, true}, |
| 1226 |
{[]string{"1.0.0-alpha"}, true}, |
| 1227 |
{[]string{"1-pre+meta"}, true}, |
| 1228 |
{[]string{"1.0.1"}, false}, |
| 1229 |
{[]string{"1.1.0"}, false}, |
| 1230 |
{[]string{"1.2.0"}, false}, |
| 1231 |
{[]string{"1.1.0-alpha"}, false}, |
| 1232 |
} |
| 1233 |
for _, tt := range tests { |
| 1234 |
funcVal := isRequiredVerificationPluginVer(testPlugVer, tt.minVerTests[0]) |
| 1235 |
if funcVal != tt.expectedVal { |
| 1236 |
t.Errorf("TestIsRequiredVerificationPluginVer Error: version comparison mismatch between plugin with version %s and min verification plugin version %s, function output: %v, expected output: %v", testPlugVer, tt.minVerTests[0], funcVal, tt.expectedVal) |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
func TestRevocationFinalResult(t *testing.T) { |
| 1242 |
certResult := []*result.CertRevocationResult{ |
| 1243 |
{ |
| 1244 |
// update leaf cert result in each sub-test |
| 1245 |
}, |
| 1246 |
{ |
| 1247 |
Result: result.ResultNonRevokable, |
| 1248 |
ServerResults: []*result.ServerResult{ |
| 1249 |
{ |
| 1250 |
Result: result.ResultNonRevokable, |
| 1251 |
}, |
| 1252 |
}, |
| 1253 |
}, |
| 1254 |
} |
| 1255 |
certChain := []*x509.Certificate{ |
| 1256 |
{ |
| 1257 |
Subject: pkix.Name{ |
| 1258 |
CommonName: "leafCert", |
| 1259 |
}, |
| 1260 |
}, |
| 1261 |
{ |
| 1262 |
Subject: pkix.Name{ |
| 1263 |
CommonName: "rootCert", |
| 1264 |
}, |
| 1265 |
}, |
| 1266 |
} |
| 1267 |
t.Run("OCSP error without fallback", func(t *testing.T) { |
| 1268 |
certResult[0] = &result.CertRevocationResult{ |
| 1269 |
Result: result.ResultUnknown, |
| 1270 |
ServerResults: []*result.ServerResult{ |
| 1271 |
{ |
| 1272 |
Server: "http://ocsp.example.com", |
| 1273 |
Result: result.ResultUnknown, |
| 1274 |
Error: errors.New("ocsp error"), |
| 1275 |
RevocationMethod: result.RevocationMethodOCSP, |
| 1276 |
}, |
| 1277 |
}, |
| 1278 |
} |
| 1279 |
|
| 1280 |
finalResult, problematicCertSubject := revocationFinalResult(certResult, certChain, log.Discard) |
| 1281 |
if finalResult != result.ResultUnknown || problematicCertSubject != "CN=leafCert" { |
| 1282 |
t.Fatalf("unexpected final result: %v, problematic cert subject: %s", finalResult, problematicCertSubject) |
| 1283 |
} |
| 1284 |
}) |
| 1285 |
|
| 1286 |
t.Run("OCSP error with fallback", func(t *testing.T) { |
| 1287 |
certResult[0] = &result.CertRevocationResult{ |
| 1288 |
Result: result.ResultOK, |
| 1289 |
ServerResults: []*result.ServerResult{ |
| 1290 |
{ |
| 1291 |
Server: "http://ocsp.example.com", |
| 1292 |
Result: result.ResultUnknown, |
| 1293 |
Error: errors.New("ocsp error"), |
| 1294 |
RevocationMethod: result.RevocationMethodOCSP, |
| 1295 |
}, |
| 1296 |
{ |
| 1297 |
Result: result.ResultOK, |
| 1298 |
Server: "http://crl.example.com", |
| 1299 |
RevocationMethod: result.RevocationMethodCRL, |
| 1300 |
}, |
| 1301 |
}, |
| 1302 |
RevocationMethod: result.RevocationMethodOCSPFallbackCRL, |
| 1303 |
} |
| 1304 |
|
| 1305 |
finalResult, problematicCertSubject := revocationFinalResult(certResult, certChain, log.Discard) |
| 1306 |
if finalResult != result.ResultOK || problematicCertSubject != "" { |
| 1307 |
t.Fatalf("unexpected final result: %v, problematic cert subject: %s", finalResult, problematicCertSubject) |
| 1308 |
} |
| 1309 |
}) |
| 1310 |
|
| 1311 |
t.Run("OCSP error with fallback and CRL error", func(t *testing.T) { |
| 1312 |
certResult[0] = &result.CertRevocationResult{ |
| 1313 |
Result: result.ResultUnknown, |
| 1314 |
ServerResults: []*result.ServerResult{ |
| 1315 |
{ |
| 1316 |
Server: "http://ocsp.example.com", |
| 1317 |
Result: result.ResultUnknown, |
| 1318 |
Error: errors.New("ocsp error"), |
| 1319 |
RevocationMethod: result.RevocationMethodOCSP, |
| 1320 |
}, |
| 1321 |
{ |
| 1322 |
Result: result.ResultUnknown, |
| 1323 |
Error: errors.New("crl error"), |
| 1324 |
RevocationMethod: result.RevocationMethodCRL, |
| 1325 |
}, |
| 1326 |
}, |
| 1327 |
RevocationMethod: result.RevocationMethodOCSPFallbackCRL, |
| 1328 |
} |
| 1329 |
|
| 1330 |
finalResult, problematicCertSubject := revocationFinalResult(certResult, certChain, log.Discard) |
| 1331 |
if finalResult != result.ResultUnknown || problematicCertSubject != "CN=leafCert" { |
| 1332 |
t.Fatalf("unexpected final result: %v, problematic cert subject: %s", finalResult, problematicCertSubject) |
| 1333 |
} |
| 1334 |
}) |
| 1335 |
|
| 1336 |
t.Run("revocation method unknown error(should never reach here)", func(t *testing.T) { |
| 1337 |
certResult[0] = &result.CertRevocationResult{ |
| 1338 |
Result: result.ResultUnknown, |
| 1339 |
ServerResults: []*result.ServerResult{ |
| 1340 |
{ |
| 1341 |
Result: result.ResultUnknown, |
| 1342 |
Error: errors.New("unknown error"), |
| 1343 |
RevocationMethod: result.RevocationMethodUnknown, |
| 1344 |
}, |
| 1345 |
}, |
| 1346 |
} |
| 1347 |
|
| 1348 |
finalResult, problematicCertSubject := revocationFinalResult(certResult, certChain, log.Discard) |
| 1349 |
if finalResult != result.ResultUnknown || problematicCertSubject != "CN=leafCert" { |
| 1350 |
t.Fatalf("unexpected final result: %v, problematic cert subject: %s", finalResult, problematicCertSubject) |
| 1351 |
} |
| 1352 |
}) |
| 1353 |
} |
| 1354 |
|
| 1355 |
func verifyResult(outcome *notation.VerificationOutcome, expectedResult notation.ValidationResult, expectedErr error, t *testing.T) { |
| 1356 |
var actualResult *notation.ValidationResult |
| 1357 |
for _, r := range outcome.VerificationResults { |
| 1358 |
if r.Type == expectedResult.Type { |
| 1359 |
if actualResult == nil { |
| 1360 |
actualResult = r |
| 1361 |
} else { |
| 1362 |
t.Fatalf("expected only one VerificatiionResult for %q but found one more. first: %+v second: %+v", r.Type, actualResult, r) |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
if actualResult == nil || |
| 1368 |
(expectedResult.Error != nil && expectedResult.Error.Error() != actualResult.Error.Error()) || |
| 1369 |
expectedResult.Action != actualResult.Action { |
| 1370 |
t.Fatalf("assertion failed. expected : %+v got : %+v", expectedResult, actualResult) |
| 1371 |
} |
| 1372 |
|
| 1373 |
if expectedResult.Action == trustpolicy.ActionEnforce && expectedErr != nil && outcome.Error.Error() != expectedErr.Error() { |
| 1374 |
t.Fatalf("assertion failed. expected : %v got : %v", expectedErr, outcome.Error) |
| 1375 |
} |
| 1376 |
} |