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
|
package stfe
import (
"fmt"
"crypto/x509"
"encoding/pem"
"io/ioutil"
)
// LoadTrustAnchors loads a list of PEM-encoded certificates from file
func LoadTrustAnchors(path string) ([]*x509.Certificate, *x509.CertPool, error) {
rest, err := ioutil.ReadFile(path)
if err != nil {
return nil, nil, fmt.Errorf("failed reading trust anchors: %v", err)
}
pool := x509.NewCertPool()
var anchors []*x509.Certificate
for len(rest) > 0 {
var block *pem.Block
block, rest = pem.Decode(rest)
if block == nil {
break
}
if block.Type != "CERTIFICATE" {
return nil, nil, fmt.Errorf("unexpected PEM block type: %s", block.Type)
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, nil, fmt.Errorf("invalid trust anchor before rest(%s): %v", rest, err)
}
anchors = append(anchors, certificate)
pool.AddCert(certificate)
}
if len(anchors) == 0 {
return nil, nil, fmt.Errorf("found no valid trust anchor in: %s", path)
}
return anchors, pool, nil
}
|