Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save weshouman/3fd7dc5e7001fa1dbf38d333858bb6e0 to your computer and use it in GitHub Desktop.
Save weshouman/3fd7dc5e7001fa1dbf38d333858bb6e0 to your computer and use it in GitHub Desktop.
How to generate & use private keys using the OpenSSL command line tool

How to Generate & Use Private Keys using OpenSSL's Command Line Tool

These commands generate and use private keys in unencrypted binary (not Base64 “PEM”) PKCS#8 format. The PKCS#8 format is used here because it is the most interoperable format when dealing with software that isn't based on OpenSSL.

OpenSSL has a variety of commands that can be used to operate on private key files, some of which are specific to RSA (e.g. openssl rsa and openssl genrsa) or which have other limitations. Here we always use openssl pkey, openssl genpkey, and openssl pkcs8, regardless of the type of key.

The first section describes how to generate private keys. The second and third sections describe how to extract the public key from the generated private key. The last section describes how to inspect a private key's metadata.

Generating Private Keys

Ed25519 isn't listed here because OpenSSL's command line utilities do not support Ed25519 keys yet.


The key will use the named curve form, i.e. the only correct form, which unfortunately isn't the default form in all versions of OpenSSL.

The algorithm identifier will be id-ecPublicKey (1.2.840.10045.2.1), which is what software for signing and verifying ECDSA signatures expects.

ECC P-256

openssl genpkey -algorithm EC \
    -pkeyopt ec_paramgen_curve:P-256 \
    -pkeyopt ec_param_enc:named_curve | \
  openssl pkcs8 -topk8 -nocrypt -outform der > p256-private-key.p8

ECC P-384

openssl genpkey -algorithm EC \
    -pkeyopt ec_paramgen_curve:P-384 \
    -pkeyopt ec_param_enc:named_curve |
  openssl pkcs8 -topk8 -nocrypt -outform der > p384-private-key.p8


The key will have two primes (i.e. it will not be a multi-prime key), and public exponent 65537, which are by far the most interoperable parameters. Unless you have special requirements, generate a 2048-bit key.

The key's algorithm identifier is rsaEncryption (1.2.840.113549.1.1.1), which is the most interoperable form. Almost all software will accept keys marked as such for use in RSA encryption and for RSA PKCS#1 1.5 signatures and RSA-PSS signatures.

2048-bit RSA

openssl genpkey -algorithm RSA \
    -pkeyopt rsa_keygen_bits:2048 \
    -pkeyopt rsa_keygen_pubexp:65537 | \
  openssl pkcs8 -topk8 -nocrypt -outform der > rsa-2048-private-key.p8

3072-bit RSA

openssl genpkey -algorithm RSA \
    -pkeyopt rsa_keygen_bits:3072 \
    -pkeyopt rsa_keygen_pubexp:65537 | \
  openssl pkcs8 -topk8 -nocrypt -outform der > rsa-3072-private-key.p8

Extracting the Public Key from the Private Key as a SubjectPublicKeyInfo.

PKCS#8 files are self-describing, and PKCS#8 private key files contain the public key, so the public key can be extracted from the private key file:

openssl pkey -pubout -inform der -outform der \
    -in <filename> \
    -out <another-filename>


openssl pkey -pubout -inform der -outform der \
    -in p256-private-key.p8 \
    -out p256-public-key.spki
openssl pkey -pubout -inform der -outform der \
    -in p384-private-key.p8 \
    -out p384-public-key.spki
openssl pkey -pubout -inform der -outform der \
    -in rsa-2048-private-key.p8 \
    -out rsa-2048-public-key.spki
openssl pkey -pubout -inform der -outform der \
    -in rsa-3072-private-key.p8 \
    -out rsa-3072-public-key.spki

Extracting an RSA Public Key from the Private Key Without the SubjectPublicKeyInfo Metadata

Above, we said we would only need openssl pkey, openssl genpkey, and openssl pkcs8, but that's only true if you don't need to output the legacy form of the public key. If you need the legacy form in binary (“DER”) format then can do the conversion following this example:

openssl pkey -pubout -inform der -outform der \
    -in rsa-2048-private-key.p8 | \
  openssl rsa -pubin -RSAPublicKey_out -inform DER -outform DER \
    -out rsa-2048-public-key-legacy-form.der

The Base64 (“PEM”) form can be output following this example:

openssl pkey -pubout -inform der -outform der \
    -in rsa-2048-private-key.p8 | \
  openssl rsa -pubin -RSAPublicKey_out -inform DER -outform PEM \
    -out rsa-2048-public-key-legacy-form.pem

Inspecting a Key's Public Metadata

PKCS#8 files are self-describing, and PKCS#8 private key files contain the public key, so a single command can output all the public properties for any private key.

WARNING: By default OpenSSL's command line tool will output the value of the private key, even when you ask for it to output the public metadata; the -noout parameter suppresses this.

openssl pkey -noout -text_pub -inform der -in <filename>

For example, if you generated p256-private-key.p8 as described in the in the section on generating private keys, then given the command:

openssl pkey -noout -text_pub -inform der -in p256-private-key.p8

you'd see output like this (with a different value for pub, the public key):

Public-Key: (256 bit)
ASN1 OID: prime256v1

As another example, if you generated rsa-2048-private-key.p8 as described in the section on generating private keys, then this:

openssl pkey -noout -text_pub -inform der -in rsa-2048-private-key.p8

would output something like this (with a different modulus value):

Public-Key: (2048 bit)
Exponent: 65537 (0x10001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment