123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package sm2
- import (
- "encoding/pem"
- "errors"
- "io/ioutil"
- "os"
- "runtime"
- "sync"
- )
- var certFiles = []string{
- "/etc/ssl/certs/ca-certificates.crt",
- "/etc/pki/tls/certs/ca-bundle.crt",
- "/etc/ssl/ca-bundle.pem",
- "/etc/pki/tls/cacert.pem",
- "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
- }
- type CertPool struct {
- bySubjectKeyId map[string][]int
- byName map[string][]int
- certs []*Certificate
- }
- func NewCertPool() *CertPool {
- return &CertPool{
- bySubjectKeyId: make(map[string][]int),
- byName: make(map[string][]int),
- }
- }
- var certDirectories = []string{
- "/etc/ssl/certs",
- "/system/etc/security/cacerts",
- }
- var (
- once sync.Once
- systemRoots *CertPool
- systemRootsErr error
- )
- func systemRootsPool() *CertPool {
- once.Do(initSystemRoots)
- return systemRoots
- }
- func initSystemRoots() {
- systemRoots, systemRootsErr = loadSystemRoots()
- }
- func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
- return nil, nil
- }
- func loadSystemRoots() (*CertPool, error) {
- roots := NewCertPool()
- var firstErr error
- for _, file := range certFiles {
- data, err := ioutil.ReadFile(file)
- if err == nil {
- roots.AppendCertsFromPEM(data)
- return roots, nil
- }
- if firstErr == nil && !os.IsNotExist(err) {
- firstErr = err
- }
- }
- for _, directory := range certDirectories {
- fis, err := ioutil.ReadDir(directory)
- if err != nil {
- if firstErr == nil && !os.IsNotExist(err) {
- firstErr = err
- }
- continue
- }
- rootsAdded := false
- for _, fi := range fis {
- data, err := ioutil.ReadFile(directory + "/" + fi.Name())
- if err == nil && roots.AppendCertsFromPEM(data) {
- rootsAdded = true
- }
- }
- if rootsAdded {
- return roots, nil
- }
- }
- return nil, firstErr
- }
- func SystemCertPool() (*CertPool, error) {
- if runtime.GOOS == "windows" {
-
- return nil, errors.New("crypto/x509: system root pool is not available on Windows")
- }
- return loadSystemRoots()
- }
- func (s *CertPool) findVerifiedParents(cert *Certificate) (parents []int, errCert *Certificate, err error) {
- if s == nil {
- return
- }
- var candidates []int
- if len(cert.AuthorityKeyId) > 0 {
- candidates = s.bySubjectKeyId[string(cert.AuthorityKeyId)]
- }
- if len(candidates) == 0 {
- candidates = s.byName[string(cert.RawIssuer)]
- }
- for _, c := range candidates {
- if err = cert.CheckSignatureFrom(s.certs[c]); err == nil {
- parents = append(parents, c)
- } else {
- errCert = s.certs[c]
- }
- }
- return
- }
- func (s *CertPool) contains(cert *Certificate) bool {
- if s == nil {
- return false
- }
- candidates := s.byName[string(cert.RawSubject)]
- for _, c := range candidates {
- if s.certs[c].Equal(cert) {
- return true
- }
- }
- return false
- }
- func (s *CertPool) AddCert(cert *Certificate) {
- if cert == nil {
- panic("adding nil Certificate to CertPool")
- }
-
- if s.contains(cert) {
- return
- }
- n := len(s.certs)
- s.certs = append(s.certs, cert)
- if len(cert.SubjectKeyId) > 0 {
- keyId := string(cert.SubjectKeyId)
- s.bySubjectKeyId[keyId] = append(s.bySubjectKeyId[keyId], n)
- }
- name := string(cert.RawSubject)
- s.byName[name] = append(s.byName[name], n)
- }
- func (s *CertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool) {
- for len(pemCerts) > 0 {
- var block *pem.Block
- block, pemCerts = pem.Decode(pemCerts)
- if block == nil {
- break
- }
- if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
- continue
- }
- cert, err := ParseCertificate(block.Bytes)
- if err != nil {
- continue
- }
- s.AddCert(cert)
- ok = true
- }
- return
- }
- func (s *CertPool) Subjects() [][]byte {
- res := make([][]byte, len(s.certs))
- for i, c := range s.certs {
- res[i] = c.RawSubject
- }
- return res
- }
|