Debian Codegraph @

pkg/x509/x509.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2022 The Sigstore Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package x509
16
17 import (
18 "crypto"
19 "crypto/x509"
20 "encoding/asn1"
21 "errors"
22
23 "github.com/sigstore/sigstore/pkg/cryptoutils"
24 "github.com/sigstore/sigstore/pkg/cryptoutils/goodkey"
25 )
26
27 var (
28 // EKUOID is the Extended Key Usage OID, per RFC 5280
29 EKUOID = asn1.ObjectIdentifier{2, 5, 29, 37}
30 EKUTimestampingOID = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 8}
31 )
32
33 // VerifyCertChain verifies that the certificate chain is valid for issuing
34 // timestamping certificates. The chain should start with a leaf certificate,
35 // followed by any number of intermediates, and end with the root certificate.
36 func VerifyCertChain(certs []*x509.Certificate, signer crypto.Signer, enforceIntermediateEku bool) error {
37 // Chain must contain at least one CA certificate and a leaf certificate
38 if len(certs) == 0 || certs[0] == nil {
39 return errors.New("certificate chain must contain a leaf certificate")
40 }
41 leaf := certs[0]
42
43 if len(certs) < 2 {
44 return errors.New("certificate chain must contain at least two certificates")
45 }
46
47 if signer == nil {
48 return errors.New("signer must not be nil")
49 }
50
51 roots := x509.NewCertPool()
52 roots.AddCert(certs[len(certs)-1])
53
54 intermediates := x509.NewCertPool()
55 if len(certs) > 2 {
56 for _, intermediate := range certs[1 : len(certs)-1] {
57 intermediates.AddCert(intermediate)
58 }
59 }
60
61 // Verify the certificate chain
62 opts := x509.VerifyOptions{
63 Roots: roots,
64 Intermediates: intermediates,
65 KeyUsages: []x509.ExtKeyUsage{
66 x509.ExtKeyUsageTimeStamping,
67 },
68 }
69 if _, err := leaf.Verify(opts); err != nil {
70 return err
71 }
72
73 // Verify that all certificates but the leaf are CA certificates
74 for _, c := range certs[1:] {
75 if !c.IsCA {
76 return errors.New("certificate is not a CA certificate")
77 }
78 }
79
80 // Verify leaf has only a single EKU for timestamping, per RFC 3161 2.3
81 // This should be enforced by Verify already
82 leafEKU := leaf.ExtKeyUsage
83 if len(leafEKU) != 1 {
84 return errors.New("certificate should only contain one EKU")
85 }
86
87 // Verify leaf's EKU is set to critical, per RFC 3161 2.3
88 var criticalEKU bool
89 for _, ext := range leaf.Extensions {
90 if ext.Id.Equal(EKUOID) {
91 criticalEKU = ext.Critical
92 break
93 }
94 }
95 if !criticalEKU {
96 return errors.New("certificate must set EKU to critical")
97 }
98
99 if enforceIntermediateEku && len(certs) > 2 {
100 // If the chain contains intermediates, verify that the extended key
101 // usage includes the extended key usage timestamping for EKU chaining
102 for _, c := range certs[1 : len(certs)-1] {
103 var hasExtKeyUsageTimeStamping bool
104 for _, extKeyUsage := range c.ExtKeyUsage {
105 if extKeyUsage == x509.ExtKeyUsageTimeStamping {
106 hasExtKeyUsageTimeStamping = true
107 break
108 }
109 }
110 if !hasExtKeyUsageTimeStamping {
111 return errors.New(`certificate must have extended key usage timestamping set to sign timestamping certificates`)
112 }
113 }
114 }
115
116 // Verify the signer's public key matches the leaf certificate
117 if err := cryptoutils.EqualKeys(leaf.PublicKey, signer.Public()); err != nil {
118 return err
119 }
120
121 // Verify the key's strength
122 return goodkey.ValidatePubKey(signer.Public())
123 }