...
Source file
src/crypto/x509/root_unix.go
1
2
3
4
5
6
7
8 package x509
9
10 import (
11 "io/fs"
12 "os"
13 "path/filepath"
14 "strings"
15 )
16
17 const (
18
19
20 certFileEnv = "SSL_CERT_FILE"
21
22
23
24
25
26 certDirEnv = "SSL_CERT_DIR"
27 )
28
29 func (c *Certificate) systemVerify(opts *VerifyOptions) (chains [][]*Certificate, err error) {
30 return nil, nil
31 }
32
33 func loadSystemRoots() (*CertPool, error) {
34 roots := NewCertPool()
35
36 files := certFiles
37 if f := os.Getenv(certFileEnv); f != "" {
38 files = []string{f}
39 }
40
41 var firstErr error
42 for _, file := range files {
43 data, err := os.ReadFile(file)
44 if err == nil {
45 roots.AppendCertsFromPEM(data)
46 break
47 }
48 if firstErr == nil && !os.IsNotExist(err) {
49 firstErr = err
50 }
51 }
52
53 dirs := certDirectories
54 if d := os.Getenv(certDirEnv); d != "" {
55
56
57
58
59 dirs = strings.Split(d, ":")
60 }
61
62 for _, directory := range dirs {
63 fis, err := readUniqueDirectoryEntries(directory)
64 if err != nil {
65 if firstErr == nil && !os.IsNotExist(err) {
66 firstErr = err
67 }
68 continue
69 }
70 for _, fi := range fis {
71 data, err := os.ReadFile(directory + "/" + fi.Name())
72 if err == nil {
73 roots.AppendCertsFromPEM(data)
74 }
75 }
76 }
77
78 if roots.len() > 0 || firstErr == nil {
79 return roots, nil
80 }
81
82 return nil, firstErr
83 }
84
85
86
87 func readUniqueDirectoryEntries(dir string) ([]fs.DirEntry, error) {
88 files, err := os.ReadDir(dir)
89 if err != nil {
90 return nil, err
91 }
92 uniq := files[:0]
93 for _, f := range files {
94 if !isSameDirSymlink(f, dir) {
95 uniq = append(uniq, f)
96 }
97 }
98 return uniq, nil
99 }
100
101
102
103 func isSameDirSymlink(f fs.DirEntry, dir string) bool {
104 if f.Type()&fs.ModeSymlink == 0 {
105 return false
106 }
107 target, err := os.Readlink(filepath.Join(dir, f.Name()))
108 return err == nil && !strings.Contains(target, "/")
109 }
110
View as plain text