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 0c 0a ec 6f e0 6b f2 db e2 13 d1 cb 25 85 a4 7b 21 84 dd 9d 6a a5 a2
[51] 0d e9 c5 49 4d 7a 51 b0 93 93 3b 35 87 43 46 b5 1e 48 d5 7c a6 ad 22 81 91
[76] 3e e2 b1 8a e3 6c 76 66 5c 7f 3e 93 38 ea f0 15

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: 5bfb7f49576ca9a9a1919d84321153c3
sha256: a95b0213eb228c12c1262e9e6d2e18b804d9179b92d3fa1045f3f269dfb0f433

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEDArsb+Br8tviE9HLJYWkeyGE3Z1q
paIN6cVJTXpRsJOTOzWHQ0a1HkjVfKatIoGRPuKxiuNsdmZcfz6TOOrwFQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgJtkb6SuDVRN2rT/D
29k1IcFjvlaWR/xLTC/I5VjwLeuhRANCAAQMCuxv4Gvy2+IT0cslhaR7IYTdnWql
og3pxUlNelGwk5M7NYdDRrUeSNV8pq0igZE+4rGK42x2Zlx/PpM46vAV
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAjzhKJJ42lSpwICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIwMWe9gl4ElsEgZCyAuHT6b8mxhDk
zDqA3xOJF/vfWl9TCy/f+N7yN4deFTZl0FubQJRvGaUzyNPiCucFGHfS2/pRDVbs
JmGbSGSxDOAxW1hhl6XnCGlwAFKhSw9fGjEn9X/f9nEy6HvvpypzqdeiAWh1LLd7
JHZGQ9wj9WGvHGvT/USMsPkHmhqhIV5bp4Q7nIiZP44YkSPRlmQ=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAwK7G/ga/Lb4hPRyyWFpHshhN2daqWiDenFSU16UbCTkzs1h0NGtR5I1XymrSKBkT7isYrjbHZmXH8+kzjq8BU="

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: 5bfb7f49576ca9a9a1919d84321153c3
sha256: a95b0213eb228c12c1262e9e6d2e18b804d9179b92d3fa1045f3f269dfb0f433

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": "DArsb-Br8tviE9HLJYWkeyGE3Z1qpaIN6cVJTXpRsJM",
    "y": "kzs1h0NGtR5I1XymrSKBkT7isYrjbHZmXH8-kzjq8BU"
}
 

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: 5bfb7f49576ca9a9a1919d84321153c3
sha256: a95b0213eb228c12c1262e9e6d2e18b804d9179b92d3fa1045f3f269dfb0f433