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 e1 51 f3 29 55 87 79 63 e2 b7 7e f8 b5 da cb c9 3b d7 c3 f3 41 12 a1
[51] e2 8a 3c 8c 5f a2 ba 01 8b 06 dd e8 3f ad bb 12 54 32 a4 e6 36 a8 3d 0b 5b
[76] a4 33 a3 7d dc 11 db 32 f4 b3 bd 44 fd 9d bb 2a

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: 4909210ab2a0efb703b390bd2ee555aa
sha256: 93dfdc0d67723972c9806bc0e9d2adc664fa60eeef746c3b013f62d481b71cad

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4VHzKVWHeWPit374tdrLyTvXw/NB
EqHiijyMX6K6AYsG3eg/rbsSVDKk5jaoPQtbpDOjfdwR2zL0s71E/Z27Kg==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgXI8t6tCzsqLbciho
Ez/4tf797rvNS9CGqdWXVcw+tL2hRANCAAThUfMpVYd5Y+K3fvi12svJO9fD80ES
oeKKPIxforoBiwbd6D+tuxJUMqTmNqg9C1ukM6N93BHbMvSzvUT9nbsq
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBAjEunnhanqptqvheBc
oE4JAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAih8PB4HDP9uQSBkGaE
WrmoTl9XvS1ha5AFYznDTGiOk9ds2lHs5P3Y842nWw1wSxIMY8fw0PtQA8UPgUno
HJ7BsPmvoI261ZVOLEs5oOYEPfiOpugxyVXrf7e/iG48aTC2/gMQh/4vxN2xGHbD
LIrFmWtyooN1hn92QgD2wCcWCZepSN/E+ngZdIUsKl7RfXJliqTlDsnm2jyHtw==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOFR8ylVh3lj4rd++LXay8k718PzQRKh4oo8jF+iugGLBt3oP627ElQypOY2qD0LW6Qzo33cEdsy9LO9RP2duyo="

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: 4909210ab2a0efb703b390bd2ee555aa
sha256: 93dfdc0d67723972c9806bc0e9d2adc664fa60eeef746c3b013f62d481b71cad

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": "4VHzKVWHeWPit374tdrLyTvXw_NBEqHiijyMX6K6AYs",
    "y": "Bt3oP627ElQypOY2qD0LW6Qzo33cEdsy9LO9RP2duyo"
}
 

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: 4909210ab2a0efb703b390bd2ee555aa
sha256: 93dfdc0d67723972c9806bc0e9d2adc664fa60eeef746c3b013f62d481b71cad