go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/network/resources/certificates/parse.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package certificates 5 6 import ( 7 "bytes" 8 "crypto/x509" 9 "encoding/pem" 10 "io" 11 12 "k8s.io/client-go/util/cert" 13 ) 14 15 func ParseCertsFromPEM(r io.Reader) ([]*x509.Certificate, error) { 16 data, err := io.ReadAll(r) 17 if err != nil { 18 return nil, err 19 } 20 21 certs, err := cert.ParseCertsPEM(data) 22 if err != nil { 23 return nil, err 24 } 25 26 return certs, nil 27 } 28 29 func EncodeCertAsPEM(cert *x509.Certificate) ([]byte, error) { 30 certBuffer := bytes.Buffer{} 31 if err := pem.Encode(&certBuffer, &pem.Block{Type: CertificateBlockType, Bytes: cert.Raw}); err != nil { 32 return nil, err 33 } 34 return certBuffer.Bytes(), nil 35 } 36 37 const ( 38 // CertificateBlockType is a possible value for pem.Block.Type. 39 CertificateBlockType = "CERTIFICATE" 40 )