github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/sugar/certificates.go (about) 1 package sugar 2 3 import ( 4 "crypto/x509" 5 "encoding/pem" 6 "os" 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 := os.ReadFile(filepath.Clean(caFile)) 15 if err != nil { 16 return nil, xerrors.WithStackTrace(err) 17 } 18 19 return LoadCertificatesFromPem(bytes), nil 20 } 21 22 // LoadCertificatesFromPem parse bytes and returns certificates 23 func LoadCertificatesFromPem(bytes []byte) (certs []*x509.Certificate) { 24 var ( 25 cert *x509.Certificate 26 err error 27 ) 28 for len(bytes) > 0 { 29 var block *pem.Block 30 block, bytes = pem.Decode(bytes) 31 if block == nil { 32 break 33 } 34 if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { 35 continue 36 } 37 certBytes := block.Bytes 38 cert, err = x509.ParseCertificate(certBytes) 39 if err != nil { 40 continue 41 } 42 certs = append(certs, cert) 43 } 44 45 return 46 }