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 78 9b be dd 8d 7f d2 84 17 98 89 eb c2 c3 06 01 d1 f1 7f 02 d5 0a aa
[51] 29 12 d2 68 66 4a 19 ee 70 7b d0 82 6b 75 ed fc 7b 1b e7 3f 3f a5 90 ae 81
[76] d7 bd c6 9e b9 52 f1 87 0c d5 78 09 a2 1f ba ee

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: 993f231370050df0204fd6169e7abd73
sha256: e1dd56f33f76faa9cece8c1082034a482d1b01abb1b0dd96caf3935727200bbc

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEeJu+3Y1/0oQXmInrwsMGAdHxfwLV
CqopEtJoZkoZ7nB70IJrde38exvnPz+lkK6B173GnrlS8YcM1XgJoh+67g==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8M3XHQQY7QFWwBpO
NyIAD+iyFjcaj+CW2DqZen26rXWhRANCAAR4m77djX/ShBeYievCwwYB0fF/AtUK
qikS0mhmShnucHvQgmt17fx7G+c/P6WQroHXvcaeuVLxhwzVeAmiH7ru
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBDKFzOn5ZzjDUiIETZm
ISW+AgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAhNeep+qUBiSQSBkP26
9+17ocC2xPl7YqqMtcWzFSwAN4gBO32ClRkODglHfOnIpjPkGA/T6u3UbwKT4/KS
G6lRnfNROsbOfxza3rVb78gc3qabXb8Kkizdb7sLnuZ9Anx3qDNP6P+cDKJxoTLt
nUi0Yv4EfGI62IZxJuHCZBGmAg3cAijvuneaDC+MtWz7DGlnVUlvguY8V2Pw4Q==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHibvt2Nf9KEF5iJ68LDBgHR8X8C1QqqKRLSaGZKGe5we9CCa3Xt/Hsb5z8/pZCugde9xp65UvGHDNV4CaIfuu4="

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: 993f231370050df0204fd6169e7abd73
sha256: e1dd56f33f76faa9cece8c1082034a482d1b01abb1b0dd96caf3935727200bbc

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": "eJu-3Y1_0oQXmInrwsMGAdHxfwLVCqopEtJoZkoZ7nA",
    "y": "e9CCa3Xt_Hsb5z8_pZCugde9xp65UvGHDNV4CaIfuu4"
}
 

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: 993f231370050df0204fd6169e7abd73
sha256: e1dd56f33f76faa9cece8c1082034a482d1b01abb1b0dd96caf3935727200bbc