Skip to content

Commit

Permalink
FIXIT: Generate a better error for incorrectly generated private keys.
Browse files Browse the repository at this point in the history
…Fixes #354
  • Loading branch information
banaag committed Jun 8, 2020
1 parent 72682de commit fdc06a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packager/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func ParsePrivateKey(keyPem []byte) (crypto.PrivateKey, error) {
var pemBlock *pem.Block
pemBlock, keyPem = pem.Decode(keyPem)
if pemBlock == nil {
return nil, errors.New("invalid PEM block in private key file")
return nil, errors.New("invalid PEM block in private key file, make sure to use the right key type. See: https://github.com/WICG/webpackage/tree/master/go/signedexchange#creating-our-first-signed-exchange")

}

var err error
Expand Down
12 changes: 12 additions & 0 deletions packager/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package util_test
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"testing"
"time"

Expand Down Expand Up @@ -56,6 +59,15 @@ func TestParsePrivateKey(t *testing.T) {
assert.Equal(t, elliptic.P256(), pkgt.B3Key.(*ecdsa.PrivateKey).PublicKey.Curve)
}

func TestParsePrivateKeyWithInvalidType(t *testing.T) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err, "Could not generate test key")

key, err := util.ParsePrivateKey(x509.MarshalPKCS1PrivateKey(privateKey))
assert.Nil(t, key)
assert.EqualError(t, err, "invalid PEM block in private key file, make sure to use the right key type. See: https://github.com/WICG/webpackage/tree/master/go/signedexchange#creating-our-first-signed-exchange")
}

func TestCanSignHttpExchangesExtension(t *testing.T) {
// Leaf node has the extension.
assert.Nil(t, util.CanSignHttpExchanges(pkgt.B3Certs[0]))
Expand Down

0 comments on commit fdc06a5

Please sign in to comment.