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

FIXIT: Generate a better error for incorrectly generated private key #438

Merged
merged 1 commit into from
Jun 8, 2020
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
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