|
| 1 | +// Source: https://github.com/golang/go/blob/master/src/crypto/x509/root_unix.go |
| 2 | +// TODO: Check on every minor version release if there is a better way to do it, or, at least, update it. |
| 3 | + |
| 4 | +//go:build aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1 |
| 5 | + |
| 6 | +package certStore |
| 7 | + |
| 8 | +import ( |
| 9 | + "crypto/x509" |
| 10 | + "io/fs" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "reflect" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + // certFileEnv is the environment variable which identifies where to locate |
| 19 | + // the SSL certificate file. If set this overrides the system default. |
| 20 | + certFileEnv = "SSL_CERT_FILE" |
| 21 | + |
| 22 | + // certDirEnv is the environment variable which identifies which directory |
| 23 | + // to check for SSL certificate files. If set this overrides the system default. |
| 24 | + // It is a colon separated list of directories. |
| 25 | + // See https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html. |
| 26 | + certDirEnv = "SSL_CERT_DIR" |
| 27 | +) |
| 28 | + |
| 29 | +func loadSystemRoots() (*x509.CertPool, error) { |
| 30 | + roots := x509.NewCertPool() |
| 31 | + |
| 32 | + files := certFiles |
| 33 | + if f := os.Getenv(certFileEnv); f != "" { |
| 34 | + files = []string{f} |
| 35 | + } |
| 36 | + |
| 37 | + var firstErr error |
| 38 | + for _, file := range files { |
| 39 | + data, err := os.ReadFile(file) |
| 40 | + if err == nil { |
| 41 | + roots.AppendCertsFromPEM(data) |
| 42 | + break |
| 43 | + } |
| 44 | + if firstErr == nil && !os.IsNotExist(err) { |
| 45 | + firstErr = err |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + dirs := certDirectories |
| 50 | + if d := os.Getenv(certDirEnv); d != "" { |
| 51 | + // OpenSSL and BoringSSL both use ":" as the SSL_CERT_DIR separator. |
| 52 | + // See: |
| 53 | + // * https://golang.org/issue/35325 |
| 54 | + // * https://www.openssl.org/docs/man1.0.2/man1/c_rehash.html |
| 55 | + dirs = strings.Split(d, ":") |
| 56 | + } |
| 57 | + |
| 58 | + for _, directory := range dirs { |
| 59 | + fis, err := readUniqueDirectoryEntries(directory) |
| 60 | + if err != nil { |
| 61 | + if firstErr == nil && !os.IsNotExist(err) { |
| 62 | + firstErr = err |
| 63 | + } |
| 64 | + continue |
| 65 | + } |
| 66 | + for _, fi := range fis { |
| 67 | + data, err := os.ReadFile(directory + "/" + fi.Name()) |
| 68 | + if err == nil { |
| 69 | + roots.AppendCertsFromPEM(data) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if firstErr != nil { |
| 75 | + return roots, nil |
| 76 | + } |
| 77 | + |
| 78 | + rootsValue := reflect.ValueOf(roots) |
| 79 | + lenMethod := rootsValue.MethodByName("len") |
| 80 | + results := lenMethod.Call(nil) |
| 81 | + |
| 82 | + if results[0].Int() > 0 { |
| 83 | + return roots, nil |
| 84 | + } |
| 85 | + |
| 86 | + return nil, firstErr |
| 87 | +} |
| 88 | + |
| 89 | +// readUniqueDirectoryEntries is like os.ReadDir but omits |
| 90 | +// symlinks that point within the directory. |
| 91 | +func readUniqueDirectoryEntries(dir string) ([]fs.DirEntry, error) { |
| 92 | + files, err := os.ReadDir(dir) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + uniq := files[:0] |
| 97 | + for _, f := range files { |
| 98 | + if !isSameDirSymlink(f, dir) { |
| 99 | + uniq = append(uniq, f) |
| 100 | + } |
| 101 | + } |
| 102 | + return uniq, nil |
| 103 | +} |
| 104 | + |
| 105 | +// isSameDirSymlink reports whether fi in dir is a symlink with a |
| 106 | +// target not containing a slash. |
| 107 | +func isSameDirSymlink(f fs.DirEntry, dir string) bool { |
| 108 | + if f.Type()&fs.ModeSymlink == 0 { |
| 109 | + return false |
| 110 | + } |
| 111 | + target, err := os.Readlink(filepath.Join(dir, f.Name())) |
| 112 | + return err == nil && !strings.Contains(target, "/") |
| 113 | +} |
0 commit comments