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 fc 0d 0a bd 1b 2b 3e fe 54 3e 82 e8 c7 68 67 5f e1 a2 d1 c8 b6 66 e9
[51] 76 1d 60 f6 13 7b 78 99 70 83 70 1b 41 48 11 43 6f eb 9e 78 79 fa 19 d0 b8
[76] 13 e2 5b 00 22 a5 7e 9f c4 28 67 75 bc d2 a0 2c

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: da701303ee3f9d75466a6d73412712eb
sha256: e1784f8be9cee2a6174e55db5c96ca9a4e4197a66c0a4920566b5e34bbefbdbe

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/A0KvRsrPv5UPoLox2hnX+Gi0ci2
Zul2HWD2E3t4mXCDcBtBSBFDb+ueeHn6GdC4E+JbACKlfp/EKGd1vNKgLA==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgcU0D9HkEHedIHBYh
uP761UW3SEJvw16yHd71oNxXyOyhRANCAAT8DQq9Gys+/lQ+gujHaGdf4aLRyLZm
6XYdYPYTe3iZcINwG0FIEUNv6554efoZ0LgT4lsAIqV+n8QoZ3W80qAs
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAhXCzTMJH36uAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIPPXF8lHYN8cEgZCUenc2voGoEMza
YsFrZarWgcUm4M8Z4sWizHC0T1hh4tjRAn0SfrcHFQq8V2aKogGWXp9tLICVtQEU
oyvMvtNYba2ec2q7r+IFJVch5Bi74IOWiwPRjynwAe1UiSG2D4OUPcsVYP5VQRxw
0ewYIioqRI1wRSihgAjYqDvzLAYDQgcEe4sRmFPLKhsRjgHpMIE=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPwNCr0bKz7+VD6C6MdoZ1/hotHItmbpdh1g9hN7eJlwg3AbQUgRQ2/rnnh5+hnQuBPiWwAipX6fxChndbzSoCw="

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: da701303ee3f9d75466a6d73412712eb
sha256: e1784f8be9cee2a6174e55db5c96ca9a4e4197a66c0a4920566b5e34bbefbdbe

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": "_A0KvRsrPv5UPoLox2hnX-Gi0ci2Zul2HWD2E3t4mXA",
    "y": "g3AbQUgRQ2_rnnh5-hnQuBPiWwAipX6fxChndbzSoCw"
}
 

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: da701303ee3f9d75466a6d73412712eb
sha256: e1784f8be9cee2a6174e55db5c96ca9a4e4197a66c0a4920566b5e34bbefbdbe