2011-10-13 13:59:13 -04:00
|
|
|
// Copyright 2011 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.
|
|
|
|
|
|
2019-10-08 19:19:13 +00:00
|
|
|
// +build aix dragonfly freebsd js,wasm linux netbsd openbsd solaris
|
2011-12-14 10:25:48 -05:00
|
|
|
|
2012-03-07 13:12:35 -05:00
|
|
|
package x509
|
2011-10-13 13:59:13 -04:00
|
|
|
|
2016-03-30 16:41:18 +11:00
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2019-11-04 09:19:59 -08:00
|
|
|
"strings"
|
2016-03-30 16:41:18 +11:00
|
|
|
)
|
2011-10-13 13:59:13 -04:00
|
|
|
|
2014-09-30 09:51:49 +10:00
|
|
|
// Possible directories with certificate files; stop after successfully
|
|
|
|
|
// reading at least one file from a directory.
|
|
|
|
|
var certDirectories = []string{
|
2015-11-04 21:20:33 -05:00
|
|
|
"/etc/ssl/certs", // SLES10/SLES11, https://golang.org/issue/12139
|
2014-09-30 09:51:49 +10:00
|
|
|
"/system/etc/security/cacerts", // Android
|
2017-02-02 00:34:06 +00:00
|
|
|
"/usr/local/share/certs", // FreeBSD
|
|
|
|
|
"/etc/pki/tls/certs", // Fedora/RHEL
|
|
|
|
|
"/etc/openssl/certs", // NetBSD
|
2018-09-28 15:42:19 +02:00
|
|
|
"/var/ssl/certs", // AIX
|
2014-09-30 09:51:49 +10:00
|
|
|
}
|
|
|
|
|
|
2017-02-02 00:34:06 +00:00
|
|
|
const (
|
|
|
|
|
// certFileEnv is the environment variable which identifies where to locate
|
|
|
|
|
// the SSL certificate file. If set this overrides the system default.
|
|
|
|
|
certFileEnv = "SSL_CERT_FILE"
|
|
|
|
|
|
|
|
|
|
// certDirEnv is the environment variable which identifies which directory
|
|
|
|
|
// to check for SSL certificate files. If set this overrides the system default.
|
2019-11-04 09:19:59 -08:00
|
|
|
// It is a colon separated list of directories.
|
|
|
|
|
// See https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html.
|
2017-02-02 00:34:06 +00:00
|
|
|
certDirEnv = "SSL_CERT_DIR"
|
|
|
|
|
)
|
|
|
|
|
|
2012-03-07 13:12:35 -05:00
|
|
|
func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 16:41:18 +11:00
|
|
|
func loadSystemRoots() (*CertPool, error) {
|
2012-03-07 13:12:35 -05:00
|
|
|
roots := NewCertPool()
|
2017-02-02 00:34:06 +00:00
|
|
|
|
|
|
|
|
files := certFiles
|
|
|
|
|
if f := os.Getenv(certFileEnv); f != "" {
|
|
|
|
|
files = []string{f}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 16:41:18 +11:00
|
|
|
var firstErr error
|
2017-02-02 00:34:06 +00:00
|
|
|
for _, file := range files {
|
2011-10-13 13:59:13 -04:00
|
|
|
data, err := ioutil.ReadFile(file)
|
|
|
|
|
if err == nil {
|
|
|
|
|
roots.AppendCertsFromPEM(data)
|
2017-02-02 00:34:06 +00:00
|
|
|
break
|
2016-03-30 16:41:18 +11:00
|
|
|
}
|
|
|
|
|
if firstErr == nil && !os.IsNotExist(err) {
|
|
|
|
|
firstErr = err
|
2011-10-13 13:59:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2012-03-07 13:12:35 -05:00
|
|
|
|
2017-02-02 00:34:06 +00:00
|
|
|
dirs := certDirectories
|
|
|
|
|
if d := os.Getenv(certDirEnv); d != "" {
|
2019-11-04 09:19:59 -08:00
|
|
|
// OpenSSL and BoringSSL both use ":" as the SSL_CERT_DIR separator.
|
|
|
|
|
// See:
|
|
|
|
|
// * https://golang.org/issue/35325
|
|
|
|
|
// * https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html
|
|
|
|
|
dirs = strings.Split(d, ":")
|
2017-02-02 00:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, directory := range dirs {
|
2014-09-30 09:51:49 +10:00
|
|
|
fis, err := ioutil.ReadDir(directory)
|
|
|
|
|
if err != nil {
|
2016-03-30 16:41:18 +11:00
|
|
|
if firstErr == nil && !os.IsNotExist(err) {
|
|
|
|
|
firstErr = err
|
|
|
|
|
}
|
2014-09-30 09:51:49 +10:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
|
data, err := ioutil.ReadFile(directory + "/" + fi.Name())
|
2019-11-04 09:19:59 -08:00
|
|
|
if err == nil {
|
|
|
|
|
roots.AppendCertsFromPEM(data)
|
2014-09-30 09:51:49 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-29 17:55:59 -07:00
|
|
|
if len(roots.certs) > 0 || firstErr == nil {
|
2017-02-02 00:34:06 +00:00
|
|
|
return roots, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 16:41:18 +11:00
|
|
|
return nil, firstErr
|
2011-10-13 13:59:13 -04:00
|
|
|
}
|