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 de da ed 14 6e f5 c3 f4 5d f1 ad a5 19 31 77 d6 25 6e 3f f7 49 be 50
[51] 55 de 9f 27 12 64 79 76 12 13 a8 bf 06 7a 2c 6e 73 09 6c 04 d4 9b c6 7a 61
[76] 0d 18 bd 67 c2 26 70 f2 7e c0 87 76 bb 7d fa 62

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: 7b272ed6862049e57703f46fbbbb798c
sha256: b99a149353a4db3d8af26a175e3e7648bdcfa3cea58a513ad7cc1f4143a744ae

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3trtFG71w/Rd8a2lGTF31iVuP/dJ
vlBV3p8nEmR5dhITqL8GeixucwlsBNSbxnphDRi9Z8ImcPJ+wId2u336Yg==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgF6zWwCGE6bwEBDvn
jtnLQPxX+I9KPU/kJvf1NeBeywShRANCAATe2u0UbvXD9F3xraUZMXfWJW4/90m+
UFXenycSZHl2EhOovwZ6LG5zCWwE1JvGemENGL1nwiZw8n7Ah3a7ffpi
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAjDpgcrSeHofgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIjtJUlGv0n2cEgZCwYcq1Wx8/i/Tz
M6QPp0Hw85a9NXSxHrc1su2xyN7a5N402t8GxXVY7O0bhsDvP7l7h2Awlft1b5/Y
tL3zXwab+wcW44Hjtu1xgZZ86fHX715zNyfcWZycfTYqMgN4TWIDwDoQ9CU7zTh3
V1mHI8eQ8I0JBAKwaay6VZjQ2IHWVO9vw13vq0fhxp6z0pxliXI=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBN7a7RRu9cP0XfGtpRkxd9Ylbj/3Sb5QVd6fJxJkeXYSE6i/BnosbnMJbATUm8Z6YQ0YvWfCJnDyfsCHdrt9+mI="

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: 7b272ed6862049e57703f46fbbbb798c
sha256: b99a149353a4db3d8af26a175e3e7648bdcfa3cea58a513ad7cc1f4143a744ae

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": "3trtFG71w_Rd8a2lGTF31iVuP_dJvlBV3p8nEmR5dhI",
    "y": "E6i_BnosbnMJbATUm8Z6YQ0YvWfCJnDyfsCHdrt9-mI"
}
 

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: 7b272ed6862049e57703f46fbbbb798c
sha256: b99a149353a4db3d8af26a175e3e7648bdcfa3cea58a513ad7cc1f4143a744ae