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 c0 46 dd 11 9c 32 87 85 fe 10 98 02 8e fd 39 92 6e 31 98 0d a7 b0 f7
[51] 5d bb 05 9b b4 45 6d e5 93 95 2d 43 48 71 40 d3 c1 9f 15 1d 36 ec 25 25 2b
[76] 32 5a cc ea d5 bf 3f f8 5d 0d d5 e1 a9 72 34 fc

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: ef8398a9f41fa0ef9f98aebac1fccd75
sha256: e3022927bc0ca0237a887fe39faa9839003e8028b5fb40568811ed83038764bf

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEwEbdEZwyh4X+EJgCjv05km4xmA2n
sPdduwWbtEVt5ZOVLUNIcUDTwZ8VHTbsJSUrMlrM6tW/P/hdDdXhqXI0/A==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgjykZGvNUkYBaIHLM
N/GxV5e30WAVDvEswBBOOEPo9RyhRANCAATARt0RnDKHhf4QmAKO/TmSbjGYDaew
9127BZu0RW3lk5UtQ0hxQNPBnxUdNuwlJSsyWszq1b8/+F0N1eGpcjT8
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAhvC+2pUBLLfAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIeowdVXvQApIEgZBAHB4xK0AZbwcs
n6HotBf2PZiHNGN1EkfnOzm6mgjKQaxzYnYrYR88fU6rnGJxCh+lSKgX9av8hbhe
TFmQ4/cHW66MGXc5kIhrbqzn6FGhgcOmS3awwLiVMzZfEeeVp4PgA34gAuKc+s11
E1dJQT6z5P4X07ERtm0bza4s+E7Jv9lXBgqy1JNuOeP/KLCYFSQ=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMBG3RGcMoeF/hCYAo79OZJuMZgNp7D3XbsFm7RFbeWTlS1DSHFA08GfFR027CUlKzJazOrVvz/4XQ3V4alyNPw="

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: ef8398a9f41fa0ef9f98aebac1fccd75
sha256: e3022927bc0ca0237a887fe39faa9839003e8028b5fb40568811ed83038764bf

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": "wEbdEZwyh4X-EJgCjv05km4xmA2nsPdduwWbtEVt5ZM",
    "y": "lS1DSHFA08GfFR027CUlKzJazOrVvz_4XQ3V4alyNPw"
}
 

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: ef8398a9f41fa0ef9f98aebac1fccd75
sha256: e3022927bc0ca0237a887fe39faa9839003e8028b5fb40568811ed83038764bf