-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcert_linux.go
More file actions
46 lines (41 loc) · 957 Bytes
/
Copy pathcert_linux.go
File metadata and controls
46 lines (41 loc) · 957 Bytes
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
//go:build linux
package main
import (
"crypto/x509"
"encoding/pem"
"os"
"path/filepath"
)
// enableAnsi is a no-op on Linux.
func enableAnsi() {}
// GetHostCertificates scans standard Linux directories for CA certificates.
func GetHostCertificates() ([]*x509.Certificate, error) {
var certs []*x509.Certificate
dirs := []string{"/etc/ssl/certs", "/etc/pki/tls/certs"}
for _, dir := range dirs {
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil
}
for len(data) > 0 {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
break
}
if block.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(block.Bytes)
if err == nil && cert.IsCA {
certs = append(certs, cert)
}
}
}
return nil
})
}
return certs, nil
}