File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ package sugar
2+
3+ import (
4+ "crypto/x509"
5+ "encoding/pem"
6+ "io/ioutil"
7+ "path/filepath"
8+
9+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
10+ )
11+
12+ // LoadCertificatesFromFile read and parse caFile and returns certificates
13+ func LoadCertificatesFromFile (caFile string ) ([]* x509.Certificate , error ) {
14+ bytes , err := ioutil .ReadFile (filepath .Clean (caFile ))
15+ if err != nil {
16+ return nil , xerrors .WithStackTrace (err )
17+ }
18+ return LoadCertificatesFromPem (bytes ), nil
19+ }
20+
21+ // LoadCertificatesFromPem parse bytes and returns certificates
22+ func LoadCertificatesFromPem (bytes []byte ) (certs []* x509.Certificate ) {
23+ var (
24+ cert * x509.Certificate
25+ err error
26+ )
27+ for len (bytes ) > 0 {
28+ var block * pem.Block
29+ block , bytes = pem .Decode (bytes )
30+ if block == nil {
31+ break
32+ }
33+ if block .Type != "CERTIFICATE" || len (block .Headers ) != 0 {
34+ continue
35+ }
36+ certBytes := block .Bytes
37+ cert , err = x509 .ParseCertificate (certBytes )
38+ if err != nil {
39+ continue
40+ }
41+ certs = append (certs , cert )
42+ }
43+ return
44+ }
You can’t perform that action at this time.
0 commit comments