Importing and exporting RSA/DSA/EC keys

The openssl package implements a modern interface to libssl and libcrypto for R. It builds on the new EVP api which was introduced in OpenSSL 1.0 and provides a unified API to the various methods and formats. OpenSSL supports three major public key crypto systems:

For each type there are several common formats for storing keys and certificates:

The openssl package automatically detects the format when possible. However being able to recognize the various formats can be useful.

The DER format

DER is the standard binary format using by protocols for storing and exchanging keys and certificates. It consists of a serialized ASN.1 structure which hold the key’s (very large) prime numbers.

key <- ec_keygen()
pubkey <- key$pubkey
bin <- write_der(pubkey)
print(bin)
 [1] 30 59 30 13 06 07 2a 86 48 ce 3d 02 01 06 08 2a 86 48 ce 3d 03 01 07 03 42
[26] 00 04 d1 38 91 59 ce a9 62 a1 2b d8 7d e3 e7 ca 9f f9 76 bc b1 eb c4 23 4b
[51] 74 38 7e 9e 06 aa e2 81 2d e0 53 ba fc f5 92 45 a0 ef fd 86 7e af 0f 57 db
[76] 85 e1 ad a4 cc 04 83 79 55 97 0d ab 90 0e ae d7

To read a DER key use read_key or read_pubkey with der = TRUE.

read_pubkey(bin, der = TRUE)
[256-bit ecdsa public key]
md5: 267e35a0d55ab8b7e90b97387273d9b5
sha256: 24c707b5f6326e69c50c2d49c36e72adc4f88b06cc4ddf4d8798135e56b347c5

Users typically don’t need to worry about the key’s underlying primes, but have a look at key$data if you are curious.

The PEM format

In practice the user rarely encounters DER because it is mainly for internal use. When humans exchange keys and certificates they typically use the PEM format. PEM is simply base64 encoded DER data, plus a header. The header identifies the key (and possibly encryption) type.

cat(write_pem(pubkey))
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE0TiRWc6pYqEr2H3j58qf+Xa8sevE
I0t0OH6eBqrigS3gU7r89ZJFoO/9hn6vD1fbheGtpMwEg3lVlw2rkA6u1w==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgpqqa3Lu4B5NTIp2Z
FCBnteMJ7ABJ3y1Flkw2jV3D1oWhRANCAATROJFZzqlioSvYfePnyp/5dryx68Qj
S3Q4fp4GquKBLeBTuvz1kkWg7/2Gfq8PV9uF4a2kzASDeVWXDauQDq7X
-----END PRIVATE KEY-----

The PEM format allows for protecting private keys with a password. R will prompt you for the password when reading such a protected key.

cat(write_pem(key, password = "supersecret"))
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBCfqodwVJtKf2fjzy2l
hX7xAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAjijiSDXXAdZgSBkDTw
rfX6nRf1kIDHl+Y1+7ckySzJVj24G+OigbhCRudRCy0tYa6tGqVM+TQWnzU7jlhe
HCEzCS9FBq6Y27kPdOevxLJB3O4RrXIMlXvovbVck5bbQkG6gAbJzGrzJoLSGzR0
JNvg5yE5XdHDMU5pH3SZE9DSXCc81gLeUBQadho+VqftNNy7kItHUtOThWfcgQ==
-----END ENCRYPTED PRIVATE KEY-----

The OpenSSH format

For better or worse, OpenSSH uses a custom format for public keys. The advantage of this format is that it fits on a single line which is nice for e.g. your ~/.ssh/known_hosts file. There is no special format for private keys, OpenSSH uses PEM as well.

str <- write_ssh(pubkey)
print(str)
[1] "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNE4kVnOqWKhK9h94+fKn/l2vLHrxCNLdDh+ngaq4oEt4FO6/PWSRaDv/YZ+rw9X24XhraTMBIN5VZcNq5AOrtc="

The read_pubkey function will automatically detect if a file contains a PEM or SSH key.

read_pubkey(str)
[256-bit ecdsa public key]
md5: 267e35a0d55ab8b7e90b97387273d9b5
sha256: 24c707b5f6326e69c50c2d49c36e72adc4f88b06cc4ddf4d8798135e56b347c5

The JSON Web Key (JWK) format

Yet another recent format to store RSA or EC keys are JSON Web Keys (JWK). JWK is part of the Javascript Object Signing and Encryption (JOSE) specification. The write_jwk and read_jwk functions are implemented in a separate package which uses the openssl package.

library(jose)
json <- write_jwk(pubkey)
jsonlite::prettify(json)
{
    "kty": "EC",
    "crv": "P-256",
    "x": "0TiRWc6pYqEr2H3j58qf-Xa8sevEI0t0OH6eBqrigS0",
    "y": "4FO6_PWSRaDv_YZ-rw9X24XhraTMBIN5VZcNq5AOrtc"
}
 

Keys from jose and openssl are the same.

mykey <- read_jwk(json)
identical(mykey, pubkey)
[1] TRUE
print(mykey)
[256-bit ecdsa public key]
md5: 267e35a0d55ab8b7e90b97387273d9b5
sha256: 24c707b5f6326e69c50c2d49c36e72adc4f88b06cc4ddf4d8798135e56b347c5