Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
areed committed Oct 26, 2023
1 parent a3c0c0b commit 8b54772
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pemutil/pem.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ func BundleCertificate(bundlePEM []byte, certsPEM ...[]byte) ([]byte, bool, erro
// UnbundleCertificate removes PEM-encoded certificates from a PEM-encoded
// certificate bundle.
func UnbundleCertificate(bundlePEM []byte, certsPEM ...[]byte) ([]byte, bool, error) {
if len(certsPEM) == 0 {
return bundlePEM, false, nil
}
drop := make(map[[sha256.Size224]byte]bool, len(certsPEM))
for i := range certsPEM {
cert, err := ParseCertificate(certsPEM[i])
Expand All @@ -766,11 +769,10 @@ func UnbundleCertificate(bundlePEM []byte, certsPEM ...[]byte) ([]byte, bool, er
modified = true
continue
}
block, err := Serialize(cert)
if err != nil {
return nil, false, err
}
keep = append(keep, pem.EncodeToMemory(block)...)
keep = append(keep, pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
})...)
}

return keep, modified, nil
Expand Down
1 change: 1 addition & 0 deletions pemutil/pem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ func TestUnbundleCertificate(t *testing.T) {
{"remove one leave one", "testdata/bundle.crt", []string{"testdata/bundle-1st.crt"}, "testdata/bundle-2nd.crt", true, nil},
{"remove two leave none", "testdata/bundle.crt", []string{"testdata/bundle-1st.crt", "testdata/bundle-2nd.crt"}, "", true, nil},
{"remove none", "testdata/bundle.crt", []string{"testdata/ca.crt"}, "testdata/bundle.crt", false, nil},
{"none to remove", "testdata/bundle.crt", []string{}, "testdata/bundle.crt", false, nil},
{"bad cert", "testdata/bundle.crt", []string{"testdata/badca.crt"}, "", false, errors.New("invalid certificate 0")},
{"bad bundle", "testdata/badca.crt", []string{"testdata/ca.crt"}, "", false, errors.New("invalid bundle")},
}
Expand Down

0 comments on commit 8b54772

Please sign in to comment.