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 76 59 53 4e c5 d8 94 6d 71 60 29 c8 97 4b 37 1e cf cb a3 04 df 99 40
[51] 29 ca a6 30 26 6f 0a 1f 9a 74 7b a0 5c 49 4b be 70 78 d7 5a 22 0e 08 67 ec
[76] ee c0 c9 bb 28 8a 05 53 31 12 e9 2f 16 30 76 b9

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: 8e544aa5523d1c75ac3a0a1ad81e3532

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdllTTsXYlG1xYCnIl0s3Hs/LowTf
mUApyqYwJm8KH5p0e6BcSUu+cHjXWiIOCGfs7sDJuyiKBVMxEukvFjB2uQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg31fdTU7KjREpu2ln
s/WI1gky4ycDfPcGp8z6EjHFDbOhRANCAAR2WVNOxdiUbXFgKciXSzcez8ujBN+Z
QCnKpjAmbwofmnR7oFxJS75weNdaIg4IZ+zuwMm7KIoFUzES6S8WMHa5
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAhX4uUUXm/IOAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIcNber2DzLtkEgZAVw+sVo/SrdDZQ
ci8S2WiAjT2KawSloefBv8KEvvG/LxGhuUOC6lv+gDjThkSMMw0K7p4u8YzJjxo1
9EMOI5H+W9LNNUNoGwTDRZ4vRgnkpiNSWUV/A0NUvKPfFIf04jBN7ZsfKtIjANzC
aP1rOGkwuzshLPTc84D8B7hzly2EHqQX3ZUWR0d1gEhKX+Rc5lg=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHZZU07F2JRtcWApyJdLNx7Py6ME35lAKcqmMCZvCh+adHugXElLvnB411oiDghn7O7AybsoigVTMRLpLxYwdrk="

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: 8e544aa5523d1c75ac3a0a1ad81e3532

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": "dllTTsXYlG1xYCnIl0s3Hs_LowTfmUApyqYwJm8KH5o",
    "y": "dHugXElLvnB411oiDghn7O7AybsoigVTMRLpLxYwdrk"
}
 

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: 8e544aa5523d1c75ac3a0a1ad81e3532