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 6f 34 ed 46 b4 82 34 19 76 85 cd 93 64 65 81 4e 5b 37 c8 26 3d 99 34
[51] 99 a7 c4 59 4a ca df ab d7 59 10 2f 87 d9 de fd bf 74 d6 5c 68 4d cb f6 a1
[76] 75 25 15 97 2d c3 36 f9 f9 72 88 8c 86 a0 48 6c

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: 62a7f6fda53ba22e2f645ca4f90c5f74
sha256: 16d1d7068ce8b38bc8e8dc711a13c77a2842cb162e8d2d97d8ff75287e6ae368

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEbzTtRrSCNBl2hc2TZGWBTls3yCY9
mTSZp8RZSsrfq9dZEC+H2d79v3TWXGhNy/ahdSUVly3DNvn5coiMhqBIbA==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgXzENQpzYZlZUeoIQ
qmji9suRJctPvc6xD0r9e80c8g6hRANCAARvNO1GtII0GXaFzZNkZYFOWzfIJj2Z
NJmnxFlKyt+r11kQL4fZ3v2/dNZcaE3L9qF1JRWXLcM2+flyiIyGoEhs
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAiJ4loju347LgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIo2BJRz+fPCMEgZDpbanobkt0qEz8
h+F4DeCQMsaZ+n2DPFP2DamxutY5xxV7i+DH6UVWr2ZACqxp0YxPnYgxJvvCoV7h
thE+APSWoQq0RkxTbNGPCv8j81hzUEK16AnjnN3oDQ1sOoH20y6jYDo4gfWlilNy
PPPOU+wq/ROOo0OIiHfV8UUuO1dPuapNyxr32jDlnVlwIWrC27U=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBG807Ua0gjQZdoXNk2RlgU5bN8gmPZk0mafEWUrK36vXWRAvh9ne/b901lxoTcv2oXUlFZctwzb5+XKIjIagSGw="

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: 62a7f6fda53ba22e2f645ca4f90c5f74
sha256: 16d1d7068ce8b38bc8e8dc711a13c77a2842cb162e8d2d97d8ff75287e6ae368

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": "bzTtRrSCNBl2hc2TZGWBTls3yCY9mTSZp8RZSsrfq9c",
    "y": "WRAvh9ne_b901lxoTcv2oXUlFZctwzb5-XKIjIagSGw"
}
 

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: 62a7f6fda53ba22e2f645ca4f90c5f74
sha256: 16d1d7068ce8b38bc8e8dc711a13c77a2842cb162e8d2d97d8ff75287e6ae368