-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcert.go
More file actions
47 lines (43 loc) · 1.04 KB
/
cert.go
File metadata and controls
47 lines (43 loc) · 1.04 KB
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
package accelerator
import (
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/pkg/errors"
)
// parseCertificatePEM is used to parse certificate from the PEM data.
func parseCertificatePEM(pb []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(pb)
if block == nil {
return nil, errors.New("invalid PEM block")
}
if block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("invalid PEM block type: %s", block.Type)
}
return x509.ParseCertificate(block.Bytes)
}
// parseCertificatesPEM is used to parse certificates from the PEM data.
func parseCertificatesPEM(pb []byte) ([]*x509.Certificate, error) {
var (
certs []*x509.Certificate
block *pem.Block
)
for {
block, pb = pem.Decode(pb)
if block == nil {
return nil, errors.New("invalid PEM block")
}
if block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("invalid PEM block type: %s", block.Type)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certs = append(certs, cert)
if len(pb) == 0 {
break
}
}
return certs, nil
}