Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential panic with DecodePublicKey #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 4 additions & 2 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
// DecodePublicKey decodes a PEM-encoded ECDSA public key.
func DecodePublicKey(encodedKey []byte) (*ecdsa.PublicKey, error) {
block, _ := pem.Decode(encodedKey)
if block == nil || block.Type != "PUBLIC KEY" {
if block == nil {
return nil, fmt.Errorf("marshal: decoded nil PEM block")
}
if block.Type != "PUBLIC KEY" {
return nil, fmt.Errorf("marshal: could not decode PEM block type %s", block.Type)

}

pub, err := x509.ParsePKIXPublicKey(block.Bytes)
Expand Down
11 changes: 11 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,18 @@ func TestPublicKeyMarshaling(t *testing.T) {
if !bytes.Equal(pemBytes, []byte(pemECPublicKeyP256)) {
t.Fatal("public key encoding did not match")
}
}

func TestPublicKeyDecodeInvalid(t *testing.T) {
_, err := DecodePublicKey(nil)
if err == nil {
t.Fatal("expected error")
}

_, err = DecodePublicKey([]byte("-----BEGIN PUBLIC KEY-----\n-----END PUBLIC KEY-----"))
if err == nil {
t.Fatal("expected error")
}
}

func TestPrivateKeyBadDecode(t *testing.T) {
Expand Down