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 c2 2c ed 68 ab a0 7d 07 b7 61 fe e4 23 9e 80 40 85 2d b5 6f 52 6e dc
[51] 94 b0 1f a1 24 35 62 09 3b 21 1a 69 be 42 a3 5c 4a eb 8c ff 96 f7 c8 9c 3b
[76] 39 01 ca 8f 82 55 ad 6c 25 ba 16 8c b7 e2 4e 05

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: b360bc08a8eaedaea93e616ecc832548
sha256: f66f63766ce3744b5375052310b264606908e8fb247584a34d4564a8002c6bf0

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwiztaKugfQe3Yf7kI56AQIUttW9S
btyUsB+hJDViCTshGmm+QqNcSuuM/5b3yJw7OQHKj4JVrWwluhaMt+JOBQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQggUEFURVOLmU2QxTh
9gl1QOH2V0QuKt+wV8BZSHXErbyhRANCAATCLO1oq6B9B7dh/uQjnoBAhS21b1Ju
3JSwH6EkNWIJOyEaab5Co1xK64z/lvfInDs5AcqPglWtbCW6Foy34k4F
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAg6406AETZH2wICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIZggOq9fnjSsEgZCfNvup15XZf2fR
JtWf4gsIlFgRUHQYNZ1ae5h8+S4d/54BVRaykv8Ym0wBVr8flyXwrfn4CU907TAf
Jc9XUoSWqXm/F9/t1VpTcPQ+dwXFJ75eWLTLj6uPIu5QASXcWI28OozT7gMEebx7
txEYmS90jIghilqV9hvxiCBE8OxGzsDTdkRPSagQhEZkGVohc7o=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMIs7WiroH0Ht2H+5COegECFLbVvUm7clLAfoSQ1Ygk7IRppvkKjXErrjP+W98icOzkByo+CVa1sJboWjLfiTgU="

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: b360bc08a8eaedaea93e616ecc832548
sha256: f66f63766ce3744b5375052310b264606908e8fb247584a34d4564a8002c6bf0

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": "wiztaKugfQe3Yf7kI56AQIUttW9SbtyUsB-hJDViCTs",
    "y": "IRppvkKjXErrjP-W98icOzkByo-CVa1sJboWjLfiTgU"
}
 

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: b360bc08a8eaedaea93e616ecc832548
sha256: f66f63766ce3744b5375052310b264606908e8fb247584a34d4564a8002c6bf0