Skip to content

Commit fec0068

Browse files
committed
Revert "remove sugar/certificates.go"
This reverts commit a443c9d.
1 parent a526b45 commit fec0068

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

sugar/certificates.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

0 commit comments

Comments
 (0)