github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/crypto/x509/root_unix.go (about) 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 // +build dragonfly freebsd linux nacl netbsd openbsd solaris 6 7 package x509 8 9 import ( 10 "io/ioutil" 11 "os" 12 ) 13 14 // Possible directories with certificate files; stop after successfully 15 // reading at least one file from a directory. 16 var certDirectories = []string{ 17 "/etc/ssl/certs", // SLES10/SLES11, https://golang.org/issue/12139 18 "/system/etc/security/cacerts", // Android 19 "/usr/local/share/certs", // FreeBSD 20 "/etc/pki/tls/certs", // Fedora/RHEL 21 "/etc/openssl/certs", // NetBSD 22 } 23 24 const ( 25 // certFileEnv is the environment variable which identifies where to locate 26 // the SSL certificate file. If set this overrides the system default. 27 certFileEnv = "SSL_CERT_FILE" 28 29 // certDirEnv is the environment variable which identifies which directory 30 // to check for SSL certificate files. If set this overrides the system default. 31 certDirEnv = "SSL_CERT_DIR" 32 ) 33 34 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) { 35 return nil, nil 36 } 37 38 func loadSystemRoots() (*CertPool, error) { 39 roots := NewCertPool() 40 41 files := certFiles 42 if f := os.Getenv(certFileEnv); f != "" { 43 files = []string{f} 44 } 45 46 var firstErr error 47 for _, file := range files { 48 data, err := ioutil.ReadFile(file) 49 if err == nil { 50 roots.AppendCertsFromPEM(data) 51 break 52 } 53 if firstErr == nil && !os.IsNotExist(err) { 54 firstErr = err 55 } 56 } 57 58 dirs := certDirectories 59 if d := os.Getenv(certDirEnv); d != "" { 60 dirs = []string{d} 61 } 62 63 for _, directory := range dirs { 64 fis, err := ioutil.ReadDir(directory) 65 if err != nil { 66 if firstErr == nil && !os.IsNotExist(err) { 67 firstErr = err 68 } 69 continue 70 } 71 rootsAdded := false 72 for _, fi := range fis { 73 data, err := ioutil.ReadFile(directory + "/" + fi.Name()) 74 if err == nil && roots.AppendCertsFromPEM(data) { 75 rootsAdded = true 76 } 77 } 78 if rootsAdded { 79 return roots, nil 80 } 81 } 82 83 if len(roots.certs) > 0 { 84 return roots, nil 85 } 86 87 return nil, firstErr 88 }