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 fd d0 1e eb 59 7a aa 06 b0 97 bb f7 27 25 e6 3d 2d 30 5d 1b ee af e8
[51] b3 60 76 25 ca a4 7c 59 69 ee 7a ff 24 96 8d a1 4b a0 bd 17 bf b7 9a aa 7f
[76] a0 52 99 58 5d ff b7 88 03 2d bf 8e 07 ef 8c 18

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: 682b7d6d7f5ea024af9bf4667ff8dd81
sha256: b12369c584aeb6ab5c1463537a6abe90e64a69adc26f82535c43a745fd0041ef

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/dAe61l6qgawl7v3JyXmPS0wXRvu
r+izYHYlyqR8WWnuev8klo2hS6C9F7+3mqp/oFKZWF3/t4gDLb+OB++MGA==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYUv6IkG2hHCBtkb2
2XRu49ICHt7C5f7wiSNYpDZrrtihRANCAAT90B7rWXqqBrCXu/cnJeY9LTBdG+6v
6LNgdiXKpHxZae56/ySWjaFLoL0Xv7eaqn+gUplYXf+3iAMtv44H74wY
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAiL6FeAcla+awICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIiLk2mUJQ2ycEgZATbr7q8XDMGlwk
djJvs52jy7A0UAIIjwc73/1IQtqMZ8Wr/IITUBer4TfmjrbQUfiltl/wb4PggAN7
h+vcQEuJzo8UgCxlWVlxVK+1Vot3OIlhMtU44tBsWv6EllquJXGvhcRT6f2v6T2P
XaTZlM5O8z0keOnbQARbena75nVu3P3o0POTbrdsiwVsZ0/4Gqk=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBP3QHutZeqoGsJe79ycl5j0tMF0b7q/os2B2JcqkfFlp7nr/JJaNoUugvRe/t5qqf6BSmVhd/7eIAy2/jgfvjBg="

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: 682b7d6d7f5ea024af9bf4667ff8dd81
sha256: b12369c584aeb6ab5c1463537a6abe90e64a69adc26f82535c43a745fd0041ef

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": "_dAe61l6qgawl7v3JyXmPS0wXRvur-izYHYlyqR8WWk",
    "y": "7nr_JJaNoUugvRe_t5qqf6BSmVhd_7eIAy2_jgfvjBg"
}
 

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: 682b7d6d7f5ea024af9bf4667ff8dd81
sha256: b12369c584aeb6ab5c1463537a6abe90e64a69adc26f82535c43a745fd0041ef