-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509.go
52 lines (40 loc) · 860 Bytes
/
x509.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package crypto
import (
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
// Create a certificate pool containing all the certificates found in the specified directory.
func NewCertPoolFromCertificatesInDirectory(dir string) (*x509.CertPool, error) {
file, err := os.Open(dir)
if err != nil {
return nil, err
}
names, err := file.Readdirnames(1024)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
for _, e := range names {
switch strings.ToLower(path.Ext(e)) {
case ".crt", ".cert", ".pem": // ok
default:
continue // skip
}
file, err = os.Open(path.Join(dir, e))
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
if !pool.AppendCertsFromPEM(data) {
return nil, fmt.Errorf("No certificates found")
}
}
return pool, nil
}