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 09 a3 6b 83 65 f4 d6 e1 ae 88 85 16 97 99 dc 44 0e 38 ae 4a 8f b6 be
[51] 29 a1 c3 61 56 24 f1 a8 ff f5 dd be 0d 5e 1b 53 6c 6d 5e e4 ab 71 b3 4d 3b
[76] e3 3b 68 0f d3 82 81 f3 c7 42 9d 9c 4a 76 e3 d3

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: 90b9c0504771ffa7d9245e153309e705
sha256: a3c85da3075f0f36e0e9355278ad1a8f6fcc5d190a524539df157368dc3f5252

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAECaNrg2X01uGuiIUWl5ncRA44rkqP
tr4pocNhViTxqP/13b4NXhtTbG1e5Ktxs0074ztoD9OCgfPHQp2cSnbj0w==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgfvpT/caF1vek3HYH
Qi8/qXtX3vvGk+0+pvso63hZfBShRANCAAQJo2uDZfTW4a6IhRaXmdxEDjiuSo+2
vimhw2FWJPGo//Xdvg1eG1NsbV7kq3GzTTvjO2gP04KB88dCnZxKduPT
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAiYfEc9jFjdzwICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIHMp8knUH+wYEgZBspUHvVtiKUB3v
hFeMb6ByDdsG1gCdKsXiMyIlSWxcF9/vZKqMsQUx0Q4GpXAVrCqqL/j3iTUYpgur
qRmVXujvo5ILxjbKSlsyL/hNm5lUnHn5IBvzJNJLOTwNftEstEjXWZ6jsbt925EO
IvlKWdAWLx+xRK54roSes0StREI4lBlhoGSZyN/rbEQ74g4QbT0=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAmja4Nl9NbhroiFFpeZ3EQOOK5Kj7a+KaHDYVYk8aj/9d2+DV4bU2xtXuSrcbNNO+M7aA/TgoHzx0KdnEp249M="

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: 90b9c0504771ffa7d9245e153309e705
sha256: a3c85da3075f0f36e0e9355278ad1a8f6fcc5d190a524539df157368dc3f5252

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": "CaNrg2X01uGuiIUWl5ncRA44rkqPtr4pocNhViTxqP8",
    "y": "9d2-DV4bU2xtXuSrcbNNO-M7aA_TgoHzx0KdnEp249M"
}
 

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: 90b9c0504771ffa7d9245e153309e705
sha256: a3c85da3075f0f36e0e9355278ad1a8f6fcc5d190a524539df157368dc3f5252