libgo: update to go1.9
[official-gcc.git] / libgo / go / crypto / x509 / root_unix.go
blob0547460437b92a2a0418c287915ad0af23d89249
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.
5 // +build aix dragonfly freebsd linux nacl netbsd openbsd solaris
7 package x509
9 import (
10 "io/ioutil"
11 "os"
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 "/var/ssl/certs", // AIX
25 const (
26 // certFileEnv is the environment variable which identifies where to locate
27 // the SSL certificate file. If set this overrides the system default.
28 certFileEnv = "SSL_CERT_FILE"
30 // certDirEnv is the environment variable which identifies which directory
31 // to check for SSL certificate files. If set this overrides the system default.
32 certDirEnv = "SSL_CERT_DIR"
35 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
36 return nil, nil
39 func loadSystemRoots() (*CertPool, error) {
40 roots := NewCertPool()
42 files := certFiles
43 if f := os.Getenv(certFileEnv); f != "" {
44 files = []string{f}
47 var firstErr error
48 for _, file := range files {
49 data, err := ioutil.ReadFile(file)
50 if err == nil {
51 roots.AppendCertsFromPEM(data)
52 break
54 if firstErr == nil && !os.IsNotExist(err) {
55 firstErr = err
59 dirs := certDirectories
60 if d := os.Getenv(certDirEnv); d != "" {
61 dirs = []string{d}
64 for _, directory := range dirs {
65 fis, err := ioutil.ReadDir(directory)
66 if err != nil {
67 if firstErr == nil && !os.IsNotExist(err) {
68 firstErr = err
70 continue
72 rootsAdded := false
73 for _, fi := range fis {
74 data, err := ioutil.ReadFile(directory + "/" + fi.Name())
75 if err == nil && roots.AppendCertsFromPEM(data) {
76 rootsAdded = true
79 if rootsAdded {
80 return roots, nil
84 if len(roots.certs) > 0 {
85 return roots, nil
88 return nil, firstErr