Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pki/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type TLSScheme interface {

func UnmarshalPEMPublicKey(data []byte) (sign.PublicKey, error) {
block, rest := pem.Decode(data)
if block == nil {
return nil, errors.New("no pem block found")
}
if len(rest) != 0 {
return nil, errors.New("trailing data")
}
Expand Down Expand Up @@ -87,6 +90,9 @@ func UnmarshalPKIXPublicKey(data []byte) (sign.PublicKey, error) {

func UnmarshalPEMPrivateKey(data []byte) (sign.PrivateKey, error) {
block, rest := pem.Decode(data)
if block == nil {
return nil, errors.New("no pem block found")
}
if len(rest) != 0 {
return nil, errors.New("trailing")
}
Expand Down
11 changes: 11 additions & 0 deletions pki/pki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@ func TestMLDSA(t *testing.T) {

testMLDSASad(t, "bad-ML-DSA-44-1.priv.gz")
}

func TestUnmarshalPEMNoBlock(t *testing.T) {
for _, in := range [][]byte{nil, {}, []byte("not pem"), []byte("-----BEGIN GARBAGE")} {
if _, err := pki.UnmarshalPEMPublicKey(in); err == nil {
t.Errorf("UnmarshalPEMPublicKey(%q) expected error, got nil", in)
}
if _, err := pki.UnmarshalPEMPrivateKey(in); err == nil {
t.Errorf("UnmarshalPEMPrivateKey(%q) expected error, got nil", in)
}
}
}
Loading