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 f4 70 7d c9 18 5f 02 97 db 15 0e 7e bc 6b d8 e2 fb d8 9f 33 7b d7 dd
[51] f7 70 e4 77 d2 13 13 ed 7a 2c ac 65 15 90 3a 1a 0b e1 b2 f4 b1 45 37 18 0c
[76] 66 54 73 6d 56 16 15 df 08 2c 7a 26 9d 45 17 42

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: 60cff0fc85149d53fbcb0c443823c16b
sha256: bce8e5c9925f71d85157cb8c3b171116b0d8f67cebca7c1d865303e74bac1e5c

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE9HB9yRhfApfbFQ5+vGvY4vvYnzN7
1933cOR30hMT7XosrGUVkDoaC+Gy9LFFNxgMZlRzbVYWFd8ILHomnUUXQg==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgQUEJMHEZXONaRAFM
oEyKBOP7Gb6Y1xUJBsq5R5vENjqhRANCAAT0cH3JGF8Cl9sVDn68a9ji+9ifM3vX
3fdw5HfSExPteiysZRWQOhoL4bL0sUU3GAxmVHNtVhYV3wgseiadRRdC
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAhV3u9RT33m7QICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIb35bxztnz28EgZAF5dCPzXhSGOLv
PXtgmmm9GEg9oqaXFGQUS7Powt17cjW0xcbrooPPvdLDgWafHy7ro2+Dz0Cf78Kt
meS1DSx21POmbNB/Swdjr5MuRbeHxr8jioTzgCGlSjDKc/AgNa3zXQk3CHbGiZnZ
HE7vdrF1jdXrpXracvaXAIzzgP/+RD9BicCThl88UrIGmFcyy9c=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPRwfckYXwKX2xUOfrxr2OL72J8ze9fd93Dkd9ITE+16LKxlFZA6GgvhsvSxRTcYDGZUc21WFhXfCCx6Jp1FF0I="

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: 60cff0fc85149d53fbcb0c443823c16b
sha256: bce8e5c9925f71d85157cb8c3b171116b0d8f67cebca7c1d865303e74bac1e5c

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": "9HB9yRhfApfbFQ5-vGvY4vvYnzN71933cOR30hMT7Xo",
    "y": "LKxlFZA6GgvhsvSxRTcYDGZUc21WFhXfCCx6Jp1FF0I"
}
 

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: 60cff0fc85149d53fbcb0c443823c16b
sha256: bce8e5c9925f71d85157cb8c3b171116b0d8f67cebca7c1d865303e74bac1e5c