OLD | NEW |
(Empty) | |
| 1 // Copyright 2009 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 tls |
| 6 |
| 7 import ( |
| 8 "crypto/x509"; |
| 9 "encoding/pem"; |
| 10 ) |
| 11 |
| 12 // A CASet is a set of certificates. |
| 13 type CASet struct { |
| 14 bySubjectKeyId map[string]*x509.Certificate; |
| 15 byName map[string]*x509.Certificate; |
| 16 } |
| 17 |
| 18 func NewCASet() *CASet { |
| 19 return &CASet{ |
| 20 make(map[string]*x509.Certificate), |
| 21 make(map[string]*x509.Certificate), |
| 22 } |
| 23 } |
| 24 |
| 25 func nameToKey(name *x509.Name) string { |
| 26 return name.Country + "/" + name.OrganizationalUnit + "/" + name.Organiz
ationalUnit + "/" + name.CommonName |
| 27 } |
| 28 |
| 29 // FindParent attempts to find the certificate in s which signs the given |
| 30 // certificate. If no such certificate can be found, it returns nil. |
| 31 func (s *CASet) FindParent(cert *x509.Certificate) (parent *x509.Certificate) { |
| 32 var ok bool; |
| 33 |
| 34 if len(cert.AuthorityKeyId) > 0 { |
| 35 parent, ok = s.bySubjectKeyId[string(cert.AuthorityKeyId)] |
| 36 } else { |
| 37 parent, ok = s.byName[nameToKey(&cert.Issuer)] |
| 38 } |
| 39 |
| 40 if !ok { |
| 41 return nil |
| 42 } |
| 43 return parent; |
| 44 } |
| 45 |
| 46 // SetFromPEM attempts to parse a series of PEM encoded root certificates. It |
| 47 // appends any certificates found to s and returns true if any certificates |
| 48 // were successfully parsed. On many Linux systems, /etc/ssl/cert.pem will |
| 49 // contains the system wide set of root CAs in a format suitable for this |
| 50 // function. |
| 51 func (s *CASet) SetFromPEM(pemCerts []byte) (ok bool) { |
| 52 for len(pemCerts) > 0 { |
| 53 var block *pem.Block; |
| 54 block, pemCerts = pem.Decode(pemCerts); |
| 55 if block == nil { |
| 56 break |
| 57 } |
| 58 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { |
| 59 continue |
| 60 } |
| 61 |
| 62 cert, err := x509.ParseCertificate(block.Bytes); |
| 63 if err != nil { |
| 64 continue |
| 65 } |
| 66 |
| 67 if len(cert.SubjectKeyId) > 0 { |
| 68 s.bySubjectKeyId[string(cert.SubjectKeyId)] = cert |
| 69 } |
| 70 s.byName[nameToKey(&cert.Subject)] = cert; |
| 71 ok = true; |
| 72 } |
| 73 |
| 74 return; |
| 75 } |
OLD | NEW |