in calnex/cert/cert.go [83:135]
func Parse(data []byte) (*Bundle, error) {
bundle := &Bundle{}
for len(data) > 0 {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
return nil, ErrFailedToParsePEM
}
if block.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
bundle.Certs = append(bundle.Certs, cert)
} else if strings.Contains(block.Type, "PRIVATE KEY") {
if bundle.PrivKey != nil {
return nil, ErrMultiplePrivKeys
}
var key *rsa.PrivateKey
var err error
if block.Type == "RSA PRIVATE KEY" {
key, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
} else if block.Type == "PRIVATE KEY" {
tmpKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
ok := false
key, ok = tmpKey.(*rsa.PrivateKey)
if !ok {
return nil, ErrOnlyRSA
}
} else {
return nil, ErrUnsupportedPEMBlock
}
bundle.PrivKey = key
} else {
return nil, ErrUnsupportedPEMBlock
}
}
return bundle, nil
}