Debian Codegraph @

ct/x509/verify.go

raw symbols SCIP
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package x509scip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/"
9 "netscip-go gomod github.com/golang/go/src go1.16 net/"
10 "runtimescip-go gomod github.com/golang/go/src go1.16 runtime/"
11 "stringsscip-go gomod github.com/golang/go/src go1.16 strings/"
12 "timescip-go gomod github.com/golang/go/src go1.16 time/"
13 "unicode/utf8scip-go gomod github.com/golang/go/src go1.16 `unicode/utf8`/"
14 )
15
16 type InvalidReasonscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/InvalidReason# int
17
18 const (
19 // NotAuthorizedToSign results when a certificate is signed by another
20 // which isn't marked as a CA certificate.
21 NotAuthorizedToSignscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/NotAuthorizedToSign. InvalidReasonscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/InvalidReason# = iota
22 // Expired results when a certificate has expired, based on the time
23 // given in the VerifyOptions.
24 Expiredscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Expired.
25 // CANotAuthorizedForThisName results when an intermediate or root
26 // certificate has a name constraint which doesn't include the name
27 // being checked.
28 CANotAuthorizedForThisNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CANotAuthorizedForThisName.
29 // TooManyIntermediates results when a path length constraint is
30 // violated.
31 TooManyIntermediatesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/TooManyIntermediates.
32 // IncompatibleUsage results when the certificate's key usage indicates
33 // that it may only be used for a different purpose.
34 IncompatibleUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/IncompatibleUsage.
35 )
36
37 // CertificateInvalidError results when an odd error occurs. Users of this
38 // library probably want to handle all these errors uniformly.
39 type CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError# struct {
40 Certscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#Cert. *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#
41 Reasonscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#Reason. InvalidReasonscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/InvalidReason#
42 }
43
44 func (e CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#) Errorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#Error().() string {
45 switch e.Reasonscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#Reason. {
46 case NotAuthorizedToSignscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/NotAuthorizedToSign.:
47 return "x509: certificate is not authorized to sign other certificates"
48 case Expiredscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Expired.:
49 return "x509: certificate has expired or is not yet valid"
50 case CANotAuthorizedForThisNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CANotAuthorizedForThisName.:
51 return "x509: a root or intermediate certificate is not authorized to sign in this domain"
52 case TooManyIntermediatesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/TooManyIntermediates.:
53 return "x509: too many intermediates for path length constraint"
54 case IncompatibleUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/IncompatibleUsage.:
55 return "x509: certificate specifies an incompatible key usage"
56 }
57 return "x509: unknown error"
58 }
59
60 // HostnameError results when the set of authorized names doesn't match the
61 // requested name.
62 type HostnameErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError# struct {
63 Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Certificate. *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#
64 Hostscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Host. string
65 }
66
67 func (h HostnameErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#) Errorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Error().() string {
68 c := h.Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Certificate.
69
70 var valid string
71 if ip := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseIPscip-go gomod github.com/golang/go/src go1.16 net/ParseIP().(h.Hostscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Host.); ip != nil {
72 // Trying to validate an IP
73 if len(c.IPAddressesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#IPAddresses.) == 0 {
74 return "x509: cannot validate certificate for " + h.Hostscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Host. + " because it doesn't contain any IP SANs"
75 }
76 for _, san := range c.IPAddressesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#IPAddresses. {
77 if len(valid) > 0 {
78 valid += ", "
79 }
80 valid += san.Stringscip-go gomod github.com/golang/go/src go1.16 net/IP#String().()
81 }
82 } else {
83 if len(c.DNSNamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#DNSNames.) > 0 {
84 valid = stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Joinscip-go gomod github.com/golang/go/src go1.16 strings/Join().(c.DNSNamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#DNSNames., ", ")
85 } else {
86 valid = c.Subjectscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#Subject..CommonNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509/pkix`/Name#CommonName.
87 }
88 }
89 return "x509: certificate is valid for " + valid + ", not " + h.Hostscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#Host.
90 }
91
92 // UnknownAuthorityError results when the certificate issuer is unknown
93 type UnknownAuthorityErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError# struct {
94 certscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#cert. *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#
95 // hintErr contains an error that may be helpful in determining why an
96 // authority wasn't found.
97 hintErrscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintErr. error
98 // hintCert contains a possible authority certificate that was rejected
99 // because of the error in hintErr.
100 hintCertscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintCert. *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#
101 }
102
103 func (e UnknownAuthorityErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#) Errorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#Error().() string {
104 s := "x509: certificate signed by unknown authority"
105 if e.hintErrscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintErr. != nil {
106 certName := e.hintCertscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintCert..Subjectscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#Subject..CommonNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509/pkix`/Name#CommonName.
107 if len(certName) == 0 {
108 if len(e.hintCertscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintCert..Subjectscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#Subject..Organizationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509/pkix`/Name#Organization.) > 0 {
109 certName = e.hintCertscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintCert..Subjectscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#Subject..Organizationscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509/pkix`/Name#Organization.[0]
110 }
111 certName = "serial:" + e.hintCertscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintCert..SerialNumberscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#SerialNumber..Stringscip-go gomod github.com/golang/go/src go1.16 `math/big`/Int#String().()
112 }
113 s += fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().(" (possibly because of %q while trying to verify candidate authority certificate %q)", e.hintErrscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#hintErr., certName)
114 }
115 return s
116 }
117
118 // SystemRootsError results when we fail to load the system root certificates.
119 type SystemRootsErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/SystemRootsError# struct {
120 }
121
122 func (e SystemRootsErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/SystemRootsError#) Errorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/SystemRootsError#Error().() string {
123 return "x509: failed to load system roots and no roots provided"
124 }
125
126 // VerifyOptions contains parameters for Certificate.Verify. It's a structure
127 // because other PKIX verification APIs have ended up needing many options.
128 type VerifyOptionsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions# struct {
129 DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName. string
130 Intermediatesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Intermediates. *CertPoolscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertPool#
131 Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots. *CertPoolscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertPool# // if nil, the system roots are used
132 CurrentTimescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#CurrentTime. timescip-go gomod github.com/golang/go/src go1.16 time/.Timescip-go gomod github.com/golang/go/src go1.16 time/Time# // if zero, the current time is used
133 DisableTimeChecksscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DisableTimeChecks. bool
134 // KeyUsage specifies which Extended Key Usage values are acceptable.
135 // An empty list means ExtKeyUsageServerAuth. Key usage is considered a
136 // constraint down the chain which mirrors Windows CryptoAPI behaviour,
137 // but not the spec. To accept any key usage, include ExtKeyUsageAny.
138 KeyUsagesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#KeyUsages. []ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsage#
139 }
140
141 const (
142 leafCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/leafCertificate. = iota
143 intermediateCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/intermediateCertificate.
144 rootCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/rootCertificate.
145 )
146
147 // isValid performs validity checks on the c.
148 func (c *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#) isValidscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#isValid().(certType int, currentChain []*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, opts *VerifyOptionsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#) error {
149 if !opts.DisableTimeChecksscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DisableTimeChecks. {
150 now := opts.CurrentTimescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#CurrentTime.
151 if now.IsZeroscip-go gomod github.com/golang/go/src go1.16 time/Time#IsZero().() {
152 now = timescip-go gomod github.com/golang/go/src go1.16 time/.Nowscip-go gomod github.com/golang/go/src go1.16 time/Now().()
153 }
154 if now.Beforescip-go gomod github.com/golang/go/src go1.16 time/Time#Before().(c.NotBeforescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#NotBefore.) || now.Afterscip-go gomod github.com/golang/go/src go1.16 time/Time#After().(c.NotAfterscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#NotAfter.) {
155 return CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#{c, Expiredscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Expired.}
156 }
157 }
158
159 if len(c.PermittedDNSDomainsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#PermittedDNSDomains.) > 0 {
160 ok := false
161 for _, domain := range c.PermittedDNSDomainsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#PermittedDNSDomains. {
162 if opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName. == domain ||
163 (stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.16 strings/HasSuffix().(opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName., domain) &&
164 len(opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName.) >= 1+len(domain) &&
165 opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName.[len(opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName.)-len(domain)-1] == '.') {
166 ok = true
167 break
168 }
169 }
170
171 if !ok {
172 return CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#{c, CANotAuthorizedForThisNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CANotAuthorizedForThisName.}
173 }
174 }
175
176 // KeyUsage status flags are ignored. From Engineering Security, Peter
177 // Gutmann: A European government CA marked its signing certificates as
178 // being valid for encryption only, but no-one noticed. Another
179 // European CA marked its signature keys as not being valid for
180 // signatures. A different CA marked its own trusted root certificate
181 // as being invalid for certificate signing. Another national CA
182 // distributed a certificate to be used to encrypt data for the
183 // country’s tax authority that was marked as only being usable for
184 // digital signatures but not for encryption. Yet another CA reversed
185 // the order of the bit flags in the keyUsage due to confusion over
186 // encoding endianness, essentially setting a random keyUsage in
187 // certificates that it issued. Another CA created a self-invalidating
188 // certificate by adding a certificate policy statement stipulating
189 // that the certificate had to be used strictly as specified in the
190 // keyUsage, and a keyUsage containing a flag indicating that the RSA
191 // encryption key could only be used for Diffie-Hellman key agreement.
192
193 if certType == intermediateCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/intermediateCertificate. && (!c.BasicConstraintsValidscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#BasicConstraintsValid. || !c.IsCAscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#IsCA.) {
194 return CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#{c, NotAuthorizedToSignscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/NotAuthorizedToSign.}
195 }
196
197 if c.BasicConstraintsValidscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#BasicConstraintsValid. && c.MaxPathLenscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#MaxPathLen. >= 0 {
198 numIntermediates := len(currentChain) - 1
199 if numIntermediates > c.MaxPathLenscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#MaxPathLen. {
200 return CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#{c, TooManyIntermediatesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/TooManyIntermediates.}
201 }
202 }
203
204 return nil
205 }
206
207 // Verify attempts to verify c by building one or more chains from c to a
208 // certificate in opts.Roots, using certificates in opts.Intermediates if
209 // needed. If successful, it returns one or more chains where the first
210 // element of the chain is c and the last element is from opts.Roots.
211 //
212 // WARNING: this doesn't do any revocation checking.
213 func (c *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#) Verifyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#Verify().(opts VerifyOptionsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#) (chains [][]*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, err error) {
214 // Use Windows's own verification and chain building.
215 if opts.Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots. == nil && runtimescip-go gomod github.com/golang/go/src go1.16 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.16 runtime/GOOS. == "windows" {
216 return c.systemVerifyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#systemVerify().(&opts)
217 }
218
219 if opts.Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots. == nil {
220 opts.Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots. = systemRootsPoolscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/systemRootsPool().()
221 if opts.Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots. == nil {
222 return nil, SystemRootsErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/SystemRootsError#{}
223 }
224 }
225
226 err = c.isValidscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#isValid().(leafCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/leafCertificate., nil, &opts)
227 if err != nil {
228 return
229 }
230
231 if len(opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName.) > 0 {
232 err = c.VerifyHostnamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#VerifyHostname().(opts.DNSNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#DNSName.)
233 if err != nil {
234 return
235 }
236 }
237
238 candidateChains, err := c.buildChainsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#buildChains().(make(map[int][][]*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#), []*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#{c}, &opts)
239 if err != nil {
240 return
241 }
242
243 keyUsages := opts.KeyUsagesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#KeyUsages.
244 if len(keyUsages) == 0 {
245 keyUsages = []ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsage#{ExtKeyUsageServerAuthscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsageServerAuth.}
246 }
247
248 // If any key usage is acceptable then we're done.
249 for _, usage := range keyUsages {
250 if usage == ExtKeyUsageAnyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsageAny. {
251 chains = candidateChains
252 return
253 }
254 }
255
256 for _, candidate := range candidateChains {
257 if checkChainForKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/checkChainForKeyUsage().(candidate, keyUsages) {
258 chains = append(chains, candidate)
259 }
260 }
261
262 if len(chains) == 0 {
263 err = CertificateInvalidErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertificateInvalidError#{c, IncompatibleUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/IncompatibleUsage.}
264 }
265
266 return
267 }
268
269 func appendToFreshChainscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/appendToFreshChain().(chain []*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, cert *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#) []*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate# {
270 n := make([]*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, len(chain)+1)
271 copy(n, chain)
272 n[len(chain)] = cert
273 return n
274 }
275
276 func (c *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#) buildChainsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#buildChains().(cache map[int][][]*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, currentChain []*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, opts *VerifyOptionsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#) (chains [][]*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, err error) {
277 possibleRoots, failedRoot, rootErr := opts.Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots..findVerifiedParentsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertPool#findVerifiedParents().(c)
278 for _, rootNum := range possibleRoots {
279 root := opts.Rootsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Roots..certsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertPool#certs.[rootNum]
280 err = root.isValidscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#isValid().(rootCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/rootCertificate., currentChain, opts)
281 if err != nil {
282 continue
283 }
284 chains = append(chains, appendToFreshChainscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/appendToFreshChain().(currentChain, root))
285 }
286
287 possibleIntermediates, failedIntermediate, intermediateErr := opts.Intermediatesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Intermediates..findVerifiedParentsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertPool#findVerifiedParents().(c)
288 nextIntermediate:
289 for _, intermediateNum := range possibleIntermediates {
290 intermediate := opts.Intermediatesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/VerifyOptions#Intermediates..certsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/CertPool#certs.[intermediateNum]
291 for _, cert := range currentChain {
292 if cert == intermediate {
293 continue nextIntermediate
294 }
295 }
296 err = intermediate.isValidscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#isValid().(intermediateCertificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/intermediateCertificate., currentChain, opts)
297 if err != nil {
298 continue
299 }
300 var childChains [][]*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#
301 childChains, ok := cache[intermediateNum]
302 if !ok {
303 childChains, err = intermediate.buildChainsscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#buildChains().(cache, appendToFreshChainscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/appendToFreshChain().(currentChain, intermediate), opts)
304 cache[intermediateNum] = childChains
305 }
306 chains = append(chains, childChains...)
307 }
308
309 if len(chains) > 0 {
310 err = nil
311 }
312
313 if len(chains) == 0 && err == nil {
314 hintErr := rootErr
315 hintCert := failedRoot
316 if hintErr == nil {
317 hintErr = intermediateErr
318 hintCert = failedIntermediate
319 }
320 err = UnknownAuthorityErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/UnknownAuthorityError#{c, hintErr, hintCert}
321 }
322
323 return
324 }
325
326 func matchHostnamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/matchHostnames().(pattern, host string) bool {
327 if len(pattern) == 0 || len(host) == 0 {
328 return false
329 }
330
331 patternParts := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Splitscip-go gomod github.com/golang/go/src go1.16 strings/Split().(pattern, ".")
332 hostParts := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Splitscip-go gomod github.com/golang/go/src go1.16 strings/Split().(host, ".")
333
334 if len(patternParts) != len(hostParts) {
335 return false
336 }
337
338 for i, patternPart := range patternParts {
339 if patternPart == "*" {
340 continue
341 }
342 if patternPart != hostParts[i] {
343 return false
344 }
345 }
346
347 return true
348 }
349
350 // toLowerCaseASCII returns a lower-case version of in. See RFC 6125 6.4.1. We use
351 // an explicitly ASCII function to avoid any sharp corners resulting from
352 // performing Unicode operations on DNS labels.
353 func toLowerCaseASCIIscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/toLowerCaseASCII().(in string) string {
354 // If the string is already lower-case then there's nothing to do.
355 isAlreadyLowerCase := true
356 for _, c := range in {
357 if c == utf8scip-go gomod github.com/golang/go/src go1.16 `unicode/utf8`/.RuneErrorscip-go gomod github.com/golang/go/src go1.16 `unicode/utf8`/RuneError. {
358 // If we get a UTF-8 error then there might be
359 // upper-case ASCII bytes in the invalid sequence.
360 isAlreadyLowerCase = false
361 break
362 }
363 if 'A' <= c && c <= 'Z' {
364 isAlreadyLowerCase = false
365 break
366 }
367 }
368
369 if isAlreadyLowerCase {
370 return in
371 }
372
373 out := []byte(in)
374 for i, c := range out {
375 if 'A' <= c && c <= 'Z' {
376 out[i] += 'a' - 'A'
377 }
378 }
379 return string(out)
380 }
381
382 // VerifyHostname returns nil if c is a valid certificate for the named host.
383 // Otherwise it returns an error describing the mismatch.
384 func (c *Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#) VerifyHostnamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#VerifyHostname().(h string) error {
385 // IP addresses may be written in [ ].
386 candidateIP := h
387 if len(h) >= 3 && h[0] == '[' && h[len(h)-1] == ']' {
388 candidateIP = h[1 : len(h)-1]
389 }
390 if ip := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseIPscip-go gomod github.com/golang/go/src go1.16 net/ParseIP().(candidateIP); ip != nil {
391 // We only match IP addresses against IP SANs.
392 // https://tools.ietf.org/html/rfc6125#appendix-B.2
393 for _, candidate := range c.IPAddressesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#IPAddresses. {
394 if ip.Equalscip-go gomod github.com/golang/go/src go1.16 net/IP#Equal().(candidate) {
395 return nil
396 }
397 }
398 return HostnameErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#{c, candidateIP}
399 }
400
401 lowered := toLowerCaseASCIIscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/toLowerCaseASCII().(h)
402
403 if len(c.DNSNamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#DNSNames.) > 0 {
404 for _, match := range c.DNSNamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#DNSNames. {
405 if matchHostnamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/matchHostnames().(toLowerCaseASCIIscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/toLowerCaseASCII().(match), lowered) {
406 return nil
407 }
408 }
409 // If Subject Alt Name is given, we ignore the common name.
410 } else if matchHostnamesscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/matchHostnames().(toLowerCaseASCIIscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/toLowerCaseASCII().(c.Subjectscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#Subject..CommonNamescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509/pkix`/Name#CommonName.), lowered) {
411 return nil
412 }
413
414 return HostnameErrorscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/HostnameError#{c, h}
415 }
416
417 func checkChainForKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/checkChainForKeyUsage().(chain []*Certificatescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#, keyUsages []ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsage#) bool {
418 usages := make([]ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsage#, len(keyUsages))
419 copy(usages, keyUsages)
420
421 if len(chain) == 0 {
422 return false
423 }
424
425 usagesRemaining := len(usages)
426
427 // We walk down the list and cross out any usages that aren't supported
428 // by each certificate. If we cross out all the usages, then the chain
429 // is unacceptable.
430
431 for i := len(chain) - 1; i >= 0; i-- {
432 cert := chain[i]
433 if len(cert.ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#ExtKeyUsage.) == 0 && len(cert.UnknownExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#UnknownExtKeyUsage.) == 0 {
434 // The certificate doesn't have any extended key usage specified.
435 continue
436 }
437
438 for _, usage := range cert.ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#ExtKeyUsage. {
439 if usage == ExtKeyUsageAnyscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsageAny. {
440 // The certificate is explicitly good for any usage.
441 continue
442 }
443 }
444
445 const invalidUsage ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsage# = -1
446
447 NextRequestedUsage:
448 for i, requestedUsage := range usages {
449 if requestedUsage == invalidUsage {
450 continue
451 }
452
453 for _, usage := range cert.ExtKeyUsagescip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/Certificate#ExtKeyUsage. {
454 if requestedUsage == usage {
455 continue NextRequestedUsage
456 } else if requestedUsage == ExtKeyUsageServerAuthscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsageServerAuth. &&
457 (usage == ExtKeyUsageNetscapeServerGatedCryptoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsageNetscapeServerGatedCrypto. ||
458 usage == ExtKeyUsageMicrosoftServerGatedCryptoscip-go gomod github.com/zmap/zcrypto . `github.com/zmap/zcrypto/ct/x509`/ExtKeyUsageMicrosoftServerGatedCrypto.) {
459 // In order to support COMODO
460 // certificate chains, we have to
461 // accept Netscape or Microsoft SGC
462 // usages as equal to ServerAuth.
463 continue NextRequestedUsage
464 }
465 }
466
467 usages[i] = invalidUsage
468 usagesRemaining--
469 if usagesRemaining == 0 {
470 return false
471 }
472 }
473 }
474
475 return true
476 }