Debian Codegraph @

example_verifyWithTimestamp_test.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 notation_test
15
16 import (
17 "context"
18 "fmt"
19 "os"
20
21 _ "github.com/notaryproject/notation-core-go/signature/cose"
22 _ "github.com/notaryproject/notation-core-go/signature/jws"
23 "github.com/notaryproject/notation-go"
24 "github.com/notaryproject/notation-go/dir"
25 "github.com/notaryproject/notation-go/registry"
26 "github.com/notaryproject/notation-go/verifier"
27 "github.com/notaryproject/notation-go/verifier/trustpolicy"
28 "github.com/notaryproject/notation-go/verifier/truststore"
29 "oras.land/oras-go/v2/registry/remote"
30 )
31
32 // Example_verifyWithTimestamp demonstrates how to use notation.Verify to verify
33 // signature of an artifact including RFC 3161 compliant timestamp countersignature
34 func Example_verifyWithTimestamp() {
35 // exampleArtifactReference is an example of the target artifact reference
36 exampleArtifactReference := "localhost:5000/software@sha256:60043cf45eaebc4c0867fea485a039b598f52fd09fd5b07b0b2d2f88fad9d74e"
37
38 // examplePolicyDocument is an example of a valid trust policy document with
39 // timestamping configurations.
40 // trust policy document should follow this spec:
41 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/trust-store-trust-policy.md#trust-policy
42 examplePolicyDocument := trustpolicy.Document{
43 Version: "1.0",
44 TrustPolicies: []trustpolicy.TrustPolicy{
45 {
46 Name: "test-statement-name",
47 RegistryScopes: []string{"*"},
48 SignatureVerification: trustpolicy.SignatureVerification{
49 VerificationLevel: trustpolicy.LevelStrict.Name,
50
51 // verify timestamp countersignature only if the signing
52 // certificate chain has expired.
53 // Default: trustpolicy.OptionAlways
54 VerifyTimestamp: trustpolicy.OptionAfterCertExpiry,
55 },
56
57 // `tsa` trust store type MUST be configured to enable
58 // timestamp verification
59 TrustStores: []string{"ca:valid-trust-store", "tsa:valid-tsa"},
60
61 // TrustedIdentities only contains trusted identities of `ca`
62 // and `signingAuthority`
63 TrustedIdentities: []string{"*"},
64 },
65 },
66 }
67
68 // generateTrustStoreWithTimestamp generates a trust store directory for demo purpose.
69 // Users should configure their own trust store and add trusted certificates
70 // into it following the trust store spec:
71 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/trust-store-trust-policy.md#trust-store
72 if err := generateTrustStoreWithTimestamp(); err != nil {
73 panic(err) // Handle error
74 }
75
76 // exampleVerifier is an example of notation.Verifier given
77 // trust policy document and X509 trust store.
78 exampleVerifier, err := verifier.New(&examplePolicyDocument, truststore.NewX509TrustStore(dir.ConfigFS()), nil)
79 if err != nil {
80 panic(err) // Handle error
81 }
82
83 // exampleRepo is an example of registry.Repository.
84 remoteRepo, err := remote.NewRepository(exampleArtifactReference)
85 if err != nil {
86 panic(err) // Handle error
87 }
88 exampleRepo := registry.NewRepository(remoteRepo)
89
90 // exampleVerifyOptions is an example of notation.VerifyOptions.
91 exampleVerifyOptions := notation.VerifyOptions{
92 ArtifactReference: exampleArtifactReference,
93 MaxSignatureAttempts: 50,
94 }
95
96 // remote verify core process
97 // upon successful verification, the target manifest descriptor
98 // and signature verification outcome are returned.
99 targetDesc, _, err := notation.Verify(context.Background(), exampleVerifier, exampleRepo, exampleVerifyOptions)
100 if err != nil {
101 panic(err) // Handle error
102 }
103
104 fmt.Println("Successfully verified")
105 fmt.Println("targetDesc MediaType:", targetDesc.MediaType)
106 fmt.Println("targetDesc Digest:", targetDesc.Digest)
107 fmt.Println("targetDesc Size:", targetDesc.Size)
108 }
109
110 func generateTrustStoreWithTimestamp() error {
111 // changing the path of the trust store for demo purpose.
112 // Users could keep the default value, i.e. os.UserConfigDir.
113 dir.UserConfigDir = "tmp"
114
115 // an example of a valid X509 self-signed certificate for demo purpose ONLY.
116 // Users should replace `exampleX509Certificate` with their own trusted
117 // certificate and add to the trust store, following the
118 // Notary certificate requirements:
119 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/signature-specification.md#certificate-requirements
120 exampleX509Certificate := `-----BEGIN CERTIFICATE-----
121 MIIDQDCCAiigAwIBAgIBUTANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJVUzEL
122 MAkGA1UECBMCV0ExEDAOBgNVBAcTB1NlYXR0bGUxDzANBgNVBAoTBk5vdGFyeTEP
123 MA0GA1UEAxMGYWxwaW5lMCAXDTAwMDgyOTEzNTAwMFoYDzIxMjMwODI5MTM1MDAw
124 WjBOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCV0ExEDAOBgNVBAcTB1NlYXR0bGUx
125 DzANBgNVBAoTBk5vdGFyeTEPMA0GA1UEAxMGYWxwaW5lMIIBIjANBgkqhkiG9w0B
126 AQEFAAOCAQ8AMIIBCgKCAQEAocg3qEsyNDDLfB8OHD4dhi+M1NPK1Asy5NX84c+g
127 vacZuoPLTwmpOfm6nPt7GPPB9G7S6xxhFNbRxTYfYUjK+kaCj38XjBRf5lGewbSJ
128 KVkxQ82/axU70ceSW3JpazrageN9JUTZ/Jfi4MfnipITwcmMoiij8eGrHskjyVyZ
129 bJd0WMMKRDWVhLPUiPMVWt/4d7YtZItzacaQKtXmXgsTCTWpIols3gftNYjrQoMs
130 UelUdD8vOAWN9J28/SyC+uSh/K1KfyUlbqufn4di8DEBxntP5wnXYbJL1jtjsUgE
131 xAVjQxT1zI59X36m3t3YKqCQh1cud02L5onObY6zj57N6QIDAQABoycwJTAOBgNV
132 HQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYIKwYBBQUHAwMwDQYJKoZIhvcNAQELBQAD
133 ggEBAC8AjBLy7EsRpi6oguCdFSb6nRGjvF17N+b6mDb3sARnB8T1pxvzTT26ya+A
134 yWR+jjodEwbMIS+13lV+9qT2LwqlbOUNY519Pa2GRRY72JjeowWI3iKkKaMzfZUB
135 7lRTGXdEuZApLbTO/3JVcR9ffu00N1UaAP9YGElSt4JDJYA9M+d/Qto+HiIsE0Kj
136 +jdnwIYovPPOlryKOLfFb/r1GEq7n63xFZz83iyWNaZdsJ5N3YHxdOpkbBbCalOE
137 BDJTjQKqeAYBLoANNU0OBslmqHCSBTEnhbqJHN6QKyF09ScOl5LwM1QsTl0UY5si
138 GLAfj/jSf9OH9VLTPHOS8/N0Ka4=
139 -----END CERTIFICATE-----`
140
141 // Adding the certificate into the trust store.
142 if err := os.MkdirAll("tmp/truststore/x509/ca/valid-trust-store", 0700); err != nil {
143 return err
144 }
145 if err := os.WriteFile("tmp/truststore/x509/ca/valid-trust-store/NotationExample.pem", []byte(exampleX509Certificate), 0600); err != nil {
146 return err
147 }
148
149 // an example of a valid TSA root certificate for demo purpose ONLY.
150 // Users should replace `exampleTSARootCertificate` with their own trusted
151 // TSA root certificate and add to the trust store, following the
152 // Notary certificate requirements:
153 // https://github.com/notaryproject/notaryproject/blob/v1.0.0/specs/signature-specification.md#certificate-requirements
154 exampleTSARootCertificate := `-----BEGIN CERTIFICATE-----
155 MIIFkDCCA3igAwIBAgIQBZsbV56OITLiOQe9p3d1XDANBgkqhkiG9w0BAQwFADBi
156 MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
157 d3cuZGlnaWNlcnQuY29tMSEwHwYDVQQDExhEaWdpQ2VydCBUcnVzdGVkIFJvb3Qg
158 RzQwHhcNMTMwODAxMTIwMDAwWhcNMzgwMTE1MTIwMDAwWjBiMQswCQYDVQQGEwJV
159 UzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQu
160 Y29tMSEwHwYDVQQDExhEaWdpQ2VydCBUcnVzdGVkIFJvb3QgRzQwggIiMA0GCSqG
161 SIb3DQEBAQUAA4ICDwAwggIKAoICAQC/5pBzaN675F1KPDAiMG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gcfJW5GqXb5JQbZaNaHqa
178 sjYUegbyJLkJEVDXCLG4iXqEI2FCKeWjzaIgQdfRnGTZ6iahixTXTBmyUEFxPT9N
179 cCOGDErcgdLMMpSEDQgJlxxPwO5rIHQw0uA5NBCFIRUBCOhVMt5xSdkoF1BN5r5N
180 0XWs0Mr7QbhDparTwwVETyw2m+L64kW4I1NsBm9nVX9GtUw/bihaeSbSpKhil9Ie
181 4u1Ki7wb/UdKDd9nZn6yW0HQO+T0O/QEY+nvwlQAUaCKKsnOeMzV6ocEGLPOr0mI
182 r/OSmbaz5mEP0oUA51Aa5BuVnRmhuZyxm7EAHu/QD09CbMkKvO5D+jpxpchNJqU1
183 /YldvIViHTLSoCtU7ZpXwdv6EM8Zt4tKG48BtieVU+i2iW1bvGjUI+iLUaJW+fCm
184 gKDWHrO8Dw9TdSmq6hN35N6MgSGtBxBHEa2HPQfRdbzP82Z+
185 -----END CERTIFICATE-----`
186
187 // Adding the tsa root certificate into the trust store.
188 if err := os.MkdirAll("tmp/truststore/x509/tsa/valid-tsa", 0700); err != nil {
189 return err
190 }
191 return os.WriteFile("tmp/truststore/x509/tsa/valid-tsa/NotationTSAExample.pem", []byte(exampleTSARootCertificate), 0600)
192 }