Skip to content

Commit

Permalink
optimize paser cert chain func (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
shipengqi committed Jun 30, 2022
1 parent 9f9b7e9 commit c26ffa5
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions crtutil/crt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,16 @@ func ParseCertChainFile(fpath string) ([]*x509.Certificate, error) {
// The data is expected to be PEM Encoded and contain one of more certificates
// with PEM type "CERTIFICATE".
func ParseCertChainBytes(data []byte) ([]*x509.Certificate, error) {
var certs []*x509.Certificate
var cert *x509.Certificate
var block *pem.Block
var rest []byte
var err error
var (
certs []*x509.Certificate
cert *x509.Certificate
block *pem.Block
err error
)

block, rest = pem.Decode(data)
if block == nil {
return nil, ErrNoPEMData
}
cert, err = x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certs = append(certs, cert)
for {
rest = bytes.TrimSpace(rest)
// This loop terminates because there is no more content
if len(rest) == 0 {
break
}
block, rest = pem.Decode(rest)
for len(data) > 0 {
data = bytes.TrimSpace(data)
block, data = pem.Decode(data)
// No PEM data is found
if block == nil {
break
Expand All @@ -91,6 +79,10 @@ func ParseCertChainBytes(data []byte) ([]*x509.Certificate, error) {
certs = append(certs, cert)
}

if len(certs) == 0 {
return nil, ErrNoPEMData
}

return certs, nil
}

Expand Down

0 comments on commit c26ffa5

Please sign in to comment.