-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootca.go
66 lines (60 loc) · 1.25 KB
/
rootca.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dane
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/http"
"os"
)
func Load(data []byte) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
for block, rest := pem.Decode(data); block != nil; block, rest = pem.Decode(rest) {
switch block.Type {
case "CERTIFICATE":
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certs = append(certs, cert)
default:
}
}
return certs, nil
}
func CreateCertPool(certs []*x509.Certificate) *x509.CertPool {
pool := x509.NewCertPool()
for _, cert := range certs {
pool.AddCert(cert)
}
return pool
}
func MozillaCertPool() (*x509.CertPool, error) {
resp, err := http.Get("https://curl.se/ca/cacert.pem")
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad http response: %d", resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
certs, err := Load(data)
if err != nil {
return nil, err
}
return CreateCertPool(certs), nil
}
func AcmeCertPool() (*x509.CertPool, error) {
data, err := os.ReadFile("acme.ca")
if err != nil {
return nil, err
}
certs, err := Load(data)
if err != nil {
return nil, err
}
return CreateCertPool(certs), nil
}