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 81 4b ac 3d 61 3e 7d 11 b3 77 9a 25 5d c7 96 68 4e 63 e1 1d 0e de ab
[51] f1 42 e3 cc 26 66 82 3f af cd 09 0f 0f ed 72 77 5c 71 d2 15 ec ae 15 85 b6
[76] e5 95 90 a6 7c d3 4c 4d 7a 24 a2 10 2b e9 e8 db

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: 5b86f293273ca13f2690ebf979b4d5b3
sha256: 1dc96a092238c1eca684cc9ef2e6dd2b5d8c72838a5de2c3860eb8031bc52554

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgUusPWE+fRGzd5olXceWaE5j4R0O
3qvxQuPMJmaCP6/NCQ8P7XJ3XHHSFeyuFYW25ZWQpnzTTE16JKIQK+no2w==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgGIYbpbM4qOju5I8c
eouiD/aSB4WzBcocoeZHAiH0B1ehRANCAASBS6w9YT59EbN3miVdx5ZoTmPhHQ7e
q/FC48wmZoI/r80JDw/tcndccdIV7K4VhbbllZCmfNNMTXokohAr6ejb
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBBkRn1rakylVyK9yOkr
L9mAAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAg//Kb4LwvhEwSBkGyN
8IJl6v4SFiUooF9Y+z8SiW2o3ydMAu5feQrVR60w0QiUiNdMeOG5Syw/VERJXiAv
qfcqGzhXpdNdnPv9ALd/VRsr5xQfX2NWC22GS+FgDGCqUASGen3urzS7yJ2oTOvy
9DjPE/UTNWvpL8eo4dOx50bsMoAoIZ81wAM2RNZEIOEoKBpbkGMqz8kptyY8sA==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIFLrD1hPn0Rs3eaJV3HlmhOY+EdDt6r8ULjzCZmgj+vzQkPD+1yd1xx0hXsrhWFtuWVkKZ800xNeiSiECvp6Ns="

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: 5b86f293273ca13f2690ebf979b4d5b3
sha256: 1dc96a092238c1eca684cc9ef2e6dd2b5d8c72838a5de2c3860eb8031bc52554

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": "gUusPWE-fRGzd5olXceWaE5j4R0O3qvxQuPMJmaCP68",
    "y": "zQkPD-1yd1xx0hXsrhWFtuWVkKZ800xNeiSiECvp6Ns"
}
 

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: 5b86f293273ca13f2690ebf979b4d5b3
sha256: 1dc96a092238c1eca684cc9ef2e6dd2b5d8c72838a5de2c3860eb8031bc52554