forked from TrueCloudLab/lego
Merge pull request #71 from xenolf/pem-decode-fix
Fix PEM decoding if file ends with multiple newlines
This commit is contained in:
commit
beac6273f6
1 changed files with 11 additions and 12 deletions
|
@ -177,23 +177,22 @@ func performECDH(priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, outLen int, label
|
||||||
// a slice of x509 certificates. This function will error if no certificates are found.
|
// a slice of x509 certificates. This function will error if no certificates are found.
|
||||||
func parsePEMBundle(bundle []byte) ([]*x509.Certificate, error) {
|
func parsePEMBundle(bundle []byte) ([]*x509.Certificate, error) {
|
||||||
var certificates []*x509.Certificate
|
var certificates []*x509.Certificate
|
||||||
|
var certDERBlock *pem.Block
|
||||||
|
|
||||||
remaining := bundle
|
for {
|
||||||
for len(remaining) != 0 {
|
certDERBlock, bundle = pem.Decode(bundle)
|
||||||
certBlock, rem := pem.Decode(remaining)
|
if certDERBlock == nil {
|
||||||
// Thanks golang for having me do this :[
|
break
|
||||||
remaining = rem
|
|
||||||
if certBlock == nil {
|
|
||||||
return nil, errors.New("Could not decode certificate.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cert, err := x509.ParseCertificate(certBlock.Bytes)
|
if certDERBlock.Type == "CERTIFICATE" {
|
||||||
|
cert, err := x509.ParseCertificate(certDERBlock.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
certificates = append(certificates, cert)
|
certificates = append(certificates, cert)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(certificates) == 0 {
|
if len(certificates) == 0 {
|
||||||
return nil, errors.New("No certificates were found while parsing the bundle.")
|
return nil, errors.New("No certificates were found while parsing the bundle.")
|
||||||
|
|
Loading…
Reference in a new issue