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 88 ad 1d 1a 18 17 ca be 1f ca 27 6c 4e 56 f2 6a 5b f7 48 1b 7f d2 84
[51] c2 60 6d af 4d 53 71 7e 8d 18 4d b2 a7 70 b7 6f ad 7e 1a b1 67 20 26 bb 2f
[76] f8 f1 c4 5d f9 ca 29 4e e2 c2 dc bf cd 0d 2c 54

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: 6343f376bff1f8a0b787f93d390ab959
sha256: 1405cac8cfc3a8d3ad4a8534a94307df2ac10a324380bcb0fafa8c92785836a6

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiK0dGhgXyr4fyidsTlbyalv3SBt/
0oTCYG2vTVNxfo0YTbKncLdvrX4asWcgJrsv+PHEXfnKKU7iwty/zQ0sVA==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgnJV4UXPfdtLBb9jr
213GONuT6xqOgGCV1/SCcYUaibWhRANCAASIrR0aGBfKvh/KJ2xOVvJqW/dIG3/S
hMJgba9NU3F+jRhNsqdwt2+tfhqxZyAmuy/48cRd+copTuLC3L/NDSxU
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAj6vh6dNRf6AwICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIZXF2anvOYQEEgZCb/pQvZpU0UPqP
EGK5oaCFT+ZfT6TLQvbjLo5JTIkQHHq+NEnQ5YqWKKN97p/V/tIhY+j+5Wm2NJfr
rYUh0TqVBx1hLZCwnQFwcIs1COUv4vYNWkT84KqNx7d1a6A1vlXgpF6q9PlJ7QvP
z5tNL3E601UNJHYrWXIqmY2yVIGXnvi0kmyU5F7ZGk5Ah/GALh4=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIitHRoYF8q+H8onbE5W8mpb90gbf9KEwmBtr01TcX6NGE2yp3C3b61+GrFnICa7L/jxxF35yilO4sLcv80NLFQ="

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: 6343f376bff1f8a0b787f93d390ab959
sha256: 1405cac8cfc3a8d3ad4a8534a94307df2ac10a324380bcb0fafa8c92785836a6

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": "iK0dGhgXyr4fyidsTlbyalv3SBt_0oTCYG2vTVNxfo0",
    "y": "GE2yp3C3b61-GrFnICa7L_jxxF35yilO4sLcv80NLFQ"
}
 

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: 6343f376bff1f8a0b787f93d390ab959
sha256: 1405cac8cfc3a8d3ad4a8534a94307df2ac10a324380bcb0fafa8c92785836a6