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

Add PKCS8 rsa key format #6

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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ Named elliptic curve to use to generate a key. Valid values are P256, P384, P521

Number of bits to use when generating RSA or octet keys

**--rsa-format="PKCS8"**

RSA private key format. Valid values are PKCS1 (PKCS #1) or PKCS8 (PKCS #8)

**--pem**

Output only PEM format (useful for pipelining results and shell scripting)
Expand Down
3 changes: 3 additions & 0 deletions jwkgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var (
bits = kingpin.
Flag("bits", "Number of bits to use for RSA or octet keys").
Short('b').Default("2048").Int()
rsaKeyFormat = kingpin.
Flag("rsa-format", "RSA private key format. Valid values are PKCS1 (PKCS #1) or PKCS8 (PKCS #8)").
Default("PKCS8").String()
onlyPEM = kingpin.Flag("pem", "Print only PEM format").Bool()
onlyJWK = kingpin.Flag("jwk", "Print only JWK format").Bool()
keyType = kingpin.Arg("key type", "Key type: oct, rsa, ec").Default("ec").Enum("oct", "rsa", "ec")
Expand Down
13 changes: 12 additions & 1 deletion pem_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ func pemBlockFor(obj interface{}) (*pem.Block, error) {
var der []byte
switch o := obj.(type) {
case *rsa.PrivateKey:
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(o)}, nil
switch *rsaKeyFormat {
case "PKCS1":
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(o)}, nil
case "PKCS8":
der, err := x509.MarshalPKCS8PrivateKey(o)
if err != nil {
return nil, errors.Wrap(err, "Unable to marshal RSA private key")
}
return &pem.Block{Type: "PRIVATE KEY", Bytes: der}, nil
default:
return nil, errors.Errorf("Unknown key format: %v", reflect.TypeOf(obj))
}
case *ecdsa.PrivateKey:
der, err = x509.MarshalECPrivateKey(o)
if err != nil {
Expand Down