| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package x509andr |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"net" |
| 12 |
"runtime" |
| 13 |
"strings" |
| 14 |
"time" |
| 15 |
"unicode/utf8" |
| 16 |
) |
| 17 |
|
| 18 |
type InvalidReason int |
| 19 |
|
| 20 |
const ( |
| 21 |
|
| 22 |
|
| 23 |
NotAuthorizedToSign InvalidReason = iota |
| 24 |
|
| 25 |
|
| 26 |
Expired |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CANotAuthorizedForThisName |
| 31 |
|
| 32 |
|
| 33 |
TooManyIntermediates |
| 34 |
|
| 35 |
|
| 36 |
IncompatibleUsage |
| 37 |
|
| 38 |
|
| 39 |
NameMismatch |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
type CertificateInvalidError struct { |
| 45 |
Cert *Certificate |
| 46 |
Reason InvalidReason |
| 47 |
} |
| 48 |
|
| 49 |
func (e CertificateInvalidError) Error() string { |
| 50 |
switch e.Reason { |
| 51 |
case NotAuthorizedToSign: |
| 52 |
return "x509: certificate is not authorized to sign other certificates" |
| 53 |
case Expired: |
| 54 |
return "x509: certificate has expired or is not yet valid" |
| 55 |
case CANotAuthorizedForThisName: |
| 56 |
return "x509: a root or intermediate certificate is not authorized to sign in this domain" |
| 57 |
case TooManyIntermediates: |
| 58 |
return "x509: too many intermediates for path length constraint" |
| 59 |
case IncompatibleUsage: |
| 60 |
return "x509: certificate specifies an incompatible key usage" |
| 61 |
case NameMismatch: |
| 62 |
return "x509: issuer name does not match subject from issuing certificate" |
| 63 |
} |
| 64 |
return "x509: unknown error" |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
type HostnameError struct { |
| 70 |
Certificate *Certificate |
| 71 |
Host string |
| 72 |
} |
| 73 |
|
| 74 |
func (h HostnameError) Error() string { |
| 75 |
c := h.Certificate |
| 76 |
|
| 77 |
var valid string |
| 78 |
if ip := net.ParseIP(h.Host); ip != nil { |
| 79 |
|
| 80 |
if len(c.IPAddresses) == 0 { |
| 81 |
return "x509: cannot validate certificate for " + h.Host + " because it doesn't contain any IP SANs" |
| 82 |
} |
| 83 |
for _, san := range c.IPAddresses { |
| 84 |
if len(valid) > 0 { |
| 85 |
valid += ", " |
| 86 |
} |
| 87 |
valid += san.String() |
| 88 |
} |
| 89 |
} else { |
| 90 |
if c.hasSANExtension() { |
| 91 |
valid = strings.Join(c.DNSNames, ", ") |
| 92 |
} else { |
| 93 |
valid = c.Subject.CommonName |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if len(valid) == 0 { |
| 98 |
return "x509: certificate is not valid for any names, but wanted to match " + h.Host |
| 99 |
} |
| 100 |
return "x509: certificate is valid for " + valid + ", not " + h.Host |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
type UnknownAuthorityError struct { |
| 105 |
Cert *Certificate |
| 106 |
|
| 107 |
|
| 108 |
hintErr error |
| 109 |
|
| 110 |
|
| 111 |
hintCert *Certificate |
| 112 |
} |
| 113 |
|
| 114 |
func (e UnknownAuthorityError) Error() string { |
| 115 |
s := "x509: certificate signed by unknown authority" |
| 116 |
if e.hintErr != nil { |
| 117 |
certName := e.hintCert.Subject.CommonName |
| 118 |
if len(certName) == 0 { |
| 119 |
if len(e.hintCert.Subject.Organization) > 0 { |
| 120 |
certName = e.hintCert.Subject.Organization[0] |
| 121 |
} else { |
| 122 |
certName = "serial:" + e.hintCert.SerialNumber.String() |
| 123 |
} |
| 124 |
} |
| 125 |
s += fmt.Sprintf(" (possibly because of %q while trying to verify candidate authority certificate %q)", e.hintErr, certName) |
| 126 |
} |
| 127 |
return s |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
type SystemRootsError struct { |
| 132 |
Err error |
| 133 |
} |
| 134 |
|
| 135 |
func (se SystemRootsError) Error() string { |
| 136 |
msg := "x509: failed to load system roots and no roots provided" |
| 137 |
if se.Err != nil { |
| 138 |
return msg + "; " + se.Err.Error() |
| 139 |
} |
| 140 |
return msg |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
var errNotParsed = errors.New("x509: missing ASN.1 contents; use ParseCertificate") |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
type VerifyOptions struct { |
| 150 |
DNSName string |
| 151 |
Intermediates *CertPool |
| 152 |
Roots *CertPool |
| 153 |
CurrentTime time.Time |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
KeyUsages []ExtKeyUsage |
| 159 |
} |
| 160 |
|
| 161 |
const ( |
| 162 |
leafCertificate = iota |
| 163 |
intermediateCertificate |
| 164 |
rootCertificate |
| 165 |
) |
| 166 |
|
| 167 |
func matchNameConstraint(domain, constraint string) bool { |
| 168 |
|
| 169 |
|
| 170 |
if len(constraint) == 0 { |
| 171 |
return true |
| 172 |
} |
| 173 |
|
| 174 |
if len(domain) < len(constraint) { |
| 175 |
return false |
| 176 |
} |
| 177 |
|
| 178 |
prefixLen := len(domain) - len(constraint) |
| 179 |
if !strings.EqualFold(domain[prefixLen:], constraint) { |
| 180 |
return false |
| 181 |
} |
| 182 |
|
| 183 |
if prefixLen == 0 { |
| 184 |
return true |
| 185 |
} |
| 186 |
|
| 187 |
isSubdomain := domain[prefixLen-1] == '.' |
| 188 |
constraintHasLeadingDot := constraint[0] == '.' |
| 189 |
return isSubdomain != constraintHasLeadingDot |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func (c *Certificate) isValid(certType int, currentChain []*Certificate, opts *VerifyOptions) error { |
| 194 |
if len(currentChain) > 0 { |
| 195 |
child := currentChain[len(currentChain)-1] |
| 196 |
if !bytes.Equal(child.RawIssuer, c.RawSubject) { |
| 197 |
return CertificateInvalidError{c, NameMismatch} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
now := opts.CurrentTime |
| 202 |
if now.IsZero() { |
| 203 |
now = time.Now() |
| 204 |
} |
| 205 |
if now.Before(c.NotBefore) || now.After(c.NotAfter) { |
| 206 |
return CertificateInvalidError{c, Expired} |
| 207 |
} |
| 208 |
|
| 209 |
if len(c.PermittedDNSDomains) > 0 { |
| 210 |
ok := false |
| 211 |
for _, constraint := range c.PermittedDNSDomains { |
| 212 |
ok = matchNameConstraint(opts.DNSName, constraint) |
| 213 |
if ok { |
| 214 |
break |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if !ok { |
| 219 |
return CertificateInvalidError{c, CANotAuthorizedForThisName} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
for _, constraint := range c.ExcludedDNSDomains { |
| 224 |
if matchNameConstraint(opts.DNSName, constraint) { |
| 225 |
return CertificateInvalidError{c, CANotAuthorizedForThisName} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
or |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
if certType == intermediateCertificate && (!c.BasicConstraintsValid || !c.IsCA) { |
| 247 |
return CertificateInvalidError{c, NotAuthorizedToSign} |
| 248 |
} |
| 249 |
|
| 250 |
if c.BasicConstraintsValid && c.MaxPathLen >= 0 { |
| 251 |
numIntermediates := len(currentChain) - 1 |
| 252 |
if numIntermediates > c.MaxPathLen { |
| 253 |
return CertificateInvalidError{c, TooManyIntermediates} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
return nil |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
func (c *Certificate) Verify(opts VerifyOptions) (chains [][]*Certificate, err error) { |
| 270 |
|
| 271 |
|
| 272 |
if len(c.Raw) == 0 { |
| 273 |
return nil, errNotParsed |
| 274 |
} |
| 275 |
if opts.Intermediates != nil { |
| 276 |
for _, intermediate := range opts.Intermediates.certs { |
| 277 |
if len(intermediate.Raw) == 0 { |
| 278 |
return nil, errNotParsed |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
if opts.Roots == nil && runtime.GOOS == "windows" { |
| 285 |
return c.systemVerify(&opts) |
| 286 |
} |
| 287 |
|
| 288 |
if len(c.UnhandledCriticalExtensions) > 0 { |
| 289 |
return nil, UnhandledCriticalExtension{} |
| 290 |
} |
| 291 |
|
| 292 |
if opts.Roots == nil { |
| 293 |
opts.Roots = systemRootsPool() |
| 294 |
if opts.Roots == nil { |
| 295 |
return nil, SystemRootsError{systemRootsErr} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
err = c.isValid(leafCertificate, nil, &opts) |
| 300 |
if err != nil { |
| 301 |
return |
| 302 |
} |
| 303 |
|
| 304 |
if len(opts.DNSName) > 0 { |
| 305 |
err = c.VerifyHostname(opts.DNSName) |
| 306 |
if err != nil { |
| 307 |
return |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
var candidateChains [][]*Certificate |
| 312 |
if opts.Roots.contains(c) { |
| 313 |
candidateChains = append(candidateChains, []*Certificate{c}) |
| 314 |
} else { |
| 315 |
if candidateChains, err = c.buildChains(make(map[int][][]*Certificate), []*Certificate{c}, &opts); err != nil { |
| 316 |
return nil, err |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
keyUsages := opts.KeyUsages |
| 321 |
if len(keyUsages) == 0 { |
| 322 |
keyUsages = []ExtKeyUsage{ExtKeyUsageServerAuth} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
for _, usage := range keyUsages { |
| 327 |
if usage == ExtKeyUsageAny { |
| 328 |
chains = candidateChains |
| 329 |
return |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
for _, candidate := range candidateChains { |
| 334 |
if checkChainForKeyUsage(candidate, keyUsages) { |
| 335 |
chains = append(chains, candidate) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
if len(chains) == 0 { |
| 340 |
err = CertificateInvalidError{c, IncompatibleUsage} |
| 341 |
} |
| 342 |
|
| 343 |
return |
| 344 |
} |
| 345 |
|
| 346 |
func appendToFreshChain(chain []*Certificate, cert *Certificate) []*Certificate { |
| 347 |
n := make([]*Certificate, len(chain)+1) |
| 348 |
copy(n, chain) |
| 349 |
n[len(chain)] = cert |
| 350 |
return n |
| 351 |
} |
| 352 |
|
| 353 |
func (c *Certificate) buildChains(cache map[int][][]*Certificate, currentChain []*Certificate, opts *VerifyOptions) (chains [][]*Certificate, err error) { |
| 354 |
possibleRoots, failedRoot, rootErr := opts.Roots.findVerifiedParents(c) |
| 355 |
nextRoot: |
| 356 |
for _, rootNum := range possibleRoots { |
| 357 |
root := opts.Roots.certs[rootNum] |
| 358 |
|
| 359 |
for _, cert := range currentChain { |
| 360 |
if cert.Equal(root) { |
| 361 |
continue nextRoot |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
err = root.isValid(rootCertificate, currentChain, opts) |
| 366 |
if err != nil { |
| 367 |
continue |
| 368 |
} |
| 369 |
chains = append(chains, appendToFreshChain(currentChain, root)) |
| 370 |
} |
| 371 |
|
| 372 |
possibleIntermediates, failedIntermediate, intermediateErr := opts.Intermediates.findVerifiedParents(c) |
| 373 |
nextIntermediate: |
| 374 |
for _, intermediateNum := range possibleIntermediates { |
| 375 |
intermediate := opts.Intermediates.certs[intermediateNum] |
| 376 |
for _, cert := range currentChain { |
| 377 |
if cert.Equal(intermediate) { |
| 378 |
continue nextIntermediate |
| 379 |
} |
| 380 |
} |
| 381 |
err = intermediate.isValid(intermediateCertificate, currentChain, opts) |
| 382 |
if err != nil { |
| 383 |
continue |
| 384 |
} |
| 385 |
var childChains [][]*Certificate |
| 386 |
childChains, ok := cache[intermediateNum] |
| 387 |
if !ok { |
| 388 |
childChains, err = intermediate.buildChains(cache, appendToFreshChain(currentChain, intermediate), opts) |
| 389 |
cache[intermediateNum] = childChains |
| 390 |
} |
| 391 |
chains = append(chains, childChains...) |
| 392 |
} |
| 393 |
|
| 394 |
if len(chains) > 0 { |
| 395 |
err = nil |
| 396 |
} |
| 397 |
|
| 398 |
if len(chains) == 0 && err == nil { |
| 399 |
hintErr := rootErr |
| 400 |
hintCert := failedRoot |
| 401 |
if hintErr == nil { |
| 402 |
hintErr = intermediateErr |
| 403 |
hintCert = failedIntermediate |
| 404 |
} |
| 405 |
err = UnknownAuthorityError{c, hintErr, hintCert} |
| 406 |
} |
| 407 |
|
| 408 |
return |
| 409 |
} |
| 410 |
|
| 411 |
func matchHostnames(pattern, host string) bool { |
| 412 |
host = strings.TrimSuffix(host, ".") |
| 413 |
pattern = strings.TrimSuffix(pattern, ".") |
| 414 |
|
| 415 |
if len(pattern) == 0 || len(host) == 0 { |
| 416 |
return false |
| 417 |
} |
| 418 |
|
| 419 |
patternParts := strings.Split(pattern, ".") |
| 420 |
hostParts := strings.Split(host, ".") |
| 421 |
|
| 422 |
if len(patternParts) != len(hostParts) { |
| 423 |
return false |
| 424 |
} |
| 425 |
|
| 426 |
for i, patternPart := range patternParts { |
| 427 |
if i == 0 && patternPart == "*" { |
| 428 |
continue |
| 429 |
} |
| 430 |
if patternPart != hostParts[i] { |
| 431 |
return false |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return true |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
func toLowerCaseASCII(in string) string { |
| 442 |
|
| 443 |
isAlreadyLowerCase := true |
| 444 |
for _, c := range in { |
| 445 |
if c == utf8.RuneError { |
| 446 |
|
| 447 |
|
| 448 |
isAlreadyLowerCase = false |
| 449 |
break |
| 450 |
} |
| 451 |
if 'A' <= c && c <= 'Z' { |
| 452 |
isAlreadyLowerCase = false |
| 453 |
break |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if isAlreadyLowerCase { |
| 458 |
return in |
| 459 |
} |
| 460 |
|
| 461 |
out := []byte(in) |
| 462 |
for i, c := range out { |
| 463 |
if 'A' <= c && c <= 'Z' { |
| 464 |
out[i] += 'a' - 'A' |
| 465 |
} |
| 466 |
} |
| 467 |
return string(out) |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
func (c *Certificate) VerifyHostname(h string) error { |
| 473 |
|
| 474 |
candidateIP := h |
| 475 |
if len(h) >= 3 && h[0] == '[' && h[len(h)-1] == ']' { |
| 476 |
candidateIP = h[1 : len(h)-1] |
| 477 |
} |
| 478 |
if ip := net.ParseIP(candidateIP); ip != nil { |
| 479 |
|
| 480 |
|
| 481 |
for _, candidate := range c.IPAddresses { |
| 482 |
if ip.Equal(candidate) { |
| 483 |
return nil |
| 484 |
} |
| 485 |
} |
| 486 |
return HostnameError{c, candidateIP} |
| 487 |
} |
| 488 |
|
| 489 |
lowered := toLowerCaseASCII(h) |
| 490 |
|
| 491 |
if c.hasSANExtension() { |
| 492 |
for _, match := range c.DNSNames { |
| 493 |
if matchHostnames(toLowerCaseASCII(match), lowered) { |
| 494 |
return nil |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
} else if matchHostnames(toLowerCaseASCII(c.Subject.CommonName), lowered) { |
| 499 |
return nil |
| 500 |
} |
| 501 |
|
| 502 |
return HostnameError{c, h} |
| 503 |
} |
| 504 |
|
| 505 |
func checkChainForKeyUsage(chain []*Certificate, keyUsages []ExtKeyUsage) bool { |
| 506 |
usages := make([]ExtKeyUsage, len(keyUsages)) |
| 507 |
copy(usages, keyUsages) |
| 508 |
|
| 509 |
if len(chain) == 0 { |
| 510 |
return false |
| 511 |
} |
| 512 |
|
| 513 |
usagesRemaining := len(usages) |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
NextCert: |
| 520 |
for i := len(chain) - 1; i >= 0; i-- { |
| 521 |
cert := chain[i] |
| 522 |
if len(cert.ExtKeyUsage) == 0 && len(cert.UnknownExtKeyUsage) == 0 { |
| 523 |
|
| 524 |
continue |
| 525 |
} |
| 526 |
|
| 527 |
for _, usage := range cert.ExtKeyUsage { |
| 528 |
if usage == ExtKeyUsageAny { |
| 529 |
|
| 530 |
continue NextCert |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
const invalidUsage ExtKeyUsage = -1 |
| 535 |
|
| 536 |
NextRequestedUsage: |
| 537 |
for i, requestedUsage := range usages { |
| 538 |
if requestedUsage == invalidUsage { |
| 539 |
continue |
| 540 |
} |
| 541 |
|
| 542 |
for _, usage := range cert.ExtKeyUsage { |
| 543 |
if requestedUsage == usage { |
| 544 |
continue NextRequestedUsage |
| 545 |
} else if requestedUsage == ExtKeyUsageServerAuth && |
| 546 |
(usage == ExtKeyUsageNetscapeServerGatedCrypto || |
| 547 |
usage == ExtKeyUsageMicrosoftServerGatedCrypto) { |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
continue NextRequestedUsage |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
usages[i] = invalidUsage |
| 557 |
usagesRemaining-- |
| 558 |
if usagesRemaining == 0 { |
| 559 |
return false |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
return true |
| 565 |
} |