2009-11-21 23:53:03 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2009-11-21 23:53:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A CASet is a set of certificates.
|
|
|
|
type CASet struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
bySubjectKeyId map[string]*x509.Certificate
|
|
|
|
byName map[string]*x509.Certificate
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCASet() *CASet {
|
|
|
|
return &CASet{
|
|
|
|
make(map[string]*x509.Certificate),
|
|
|
|
make(map[string]*x509.Certificate),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nameToKey(name *x509.Name) string {
|
|
|
|
return name.Country + "/" + name.OrganizationalUnit + "/" + name.OrganizationalUnit + "/" + name.CommonName
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindParent attempts to find the certificate in s which signs the given
|
|
|
|
// certificate. If no such certificate can be found, it returns nil.
|
|
|
|
func (s *CASet) FindParent(cert *x509.Certificate) (parent *x509.Certificate) {
|
2009-12-15 23:33:31 +00:00
|
|
|
var ok bool
|
2009-11-21 23:53:03 +00:00
|
|
|
|
|
|
|
if len(cert.AuthorityKeyId) > 0 {
|
|
|
|
parent, ok = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
|
|
|
|
} else {
|
|
|
|
parent, ok = s.byName[nameToKey(&cert.Issuer)]
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
return parent
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFromPEM attempts to parse a series of PEM encoded root certificates. It
|
|
|
|
// appends any certificates found to s and returns true if any certificates
|
|
|
|
// were successfully parsed. On many Linux systems, /etc/ssl/cert.pem will
|
|
|
|
// contains the system wide set of root CAs in a format suitable for this
|
|
|
|
// function.
|
|
|
|
func (s *CASet) SetFromPEM(pemCerts []byte) (ok bool) {
|
|
|
|
for len(pemCerts) > 0 {
|
2009-12-15 23:33:31 +00:00
|
|
|
var block *pem.Block
|
|
|
|
block, pemCerts = pem.Decode(pemCerts)
|
2009-11-21 23:53:03 +00:00
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
2009-11-21 23:53:03 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cert.SubjectKeyId) > 0 {
|
|
|
|
s.bySubjectKeyId[string(cert.SubjectKeyId)] = cert
|
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
s.byName[nameToKey(&cert.Subject)] = cert
|
|
|
|
ok = true
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 23:33:31 +00:00
|
|
|
return
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|