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 2f 55 75 aa e8 38 02 f0 2b ac 69 43 7e 2a 82 bd fd d6 65 a2 7f 49 58
[51] 78 13 e6 13 45 fe da 70 55 5e 2c 91 76 31 4c 46 90 74 ea 2d 1f 12 81 88 33
[76] 46 01 ac e1 23 b5 5e 0a d4 56 a0 30 45 20 08 38

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: 6a6984efb641ccb67ac1067c3bca9c54
sha256: 472da87af313a6d0a9d55b5662935fdde218f8279bab6492ad93f88a9a6edf51

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEL1V1qug4AvArrGlDfiqCvf3WZaJ/
SVh4E+YTRf7acFVeLJF2MUxGkHTqLR8SgYgzRgGs4SO1XgrUVqAwRSAIOA==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgVk4tlCrBLji94waG
E/ud9NRRffqQF3p0AdyDqveCMbShRANCAAQvVXWq6DgC8CusaUN+KoK9/dZlon9J
WHgT5hNF/tpwVV4skXYxTEaQdOotHxKBiDNGAazhI7VeCtRWoDBFIAg4
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBALSPtDgbMiQwOIWTtz
G4wWAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAglJNel3AMCwwSBkD7X
xrqQYZBWp4E9h9612VHVTVl+JPeWwbsDo5jgy/PlIVcCDj4rv4KF3dlgyOZrBqnQ
tpDlELvDjjc8gJlrOJP6On6ggoS8Kg6E15s4pvY+1KnuBC2EL5JGl2Mn7D1LP13I
xN6oQeAsNYIkA9nxwAOfwQiTB3tXbIFJSbn6B9q2G3u8osZzr/G97Pne2tVT6Q==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC9VdaroOALwK6xpQ34qgr391mWif0lYeBPmE0X+2nBVXiyRdjFMRpB06i0fEoGIM0YBrOEjtV4K1FagMEUgCDg="

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: 6a6984efb641ccb67ac1067c3bca9c54
sha256: 472da87af313a6d0a9d55b5662935fdde218f8279bab6492ad93f88a9a6edf51

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": "L1V1qug4AvArrGlDfiqCvf3WZaJ_SVh4E-YTRf7acFU",
    "y": "XiyRdjFMRpB06i0fEoGIM0YBrOEjtV4K1FagMEUgCDg"
}
 

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: 6a6984efb641ccb67ac1067c3bca9c54
sha256: 472da87af313a6d0a9d55b5662935fdde218f8279bab6492ad93f88a9a6edf51