-
Notifications
You must be signed in to change notification settings - Fork 36
/
x509.go
39 lines (29 loc) · 882 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
package crypto
import (
"crypto/x509"
"encoding/pem"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
func CertPoolFromPEM(pemCerts []byte) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
for pemCertsIdx := 1; len(pemCerts) > 0; pemCertsIdx++ {
var block *pem.Block
block, pemCerts = pem.Decode(pemCerts)
if block == nil {
if strings.TrimSpace(string(pemCerts)) != "" {
return nil, bosherr.Errorf("Parsing certificate %d: Missing PEM block", pemCertsIdx)
}
break
}
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
return nil, bosherr.Errorf("Parsing certificate %d: Not a certificate", pemCertsIdx)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Parsing certificate %d", pemCertsIdx)
}
certPool.AddCert(cert)
}
return certPool, nil
}