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 9c c8 7b c5 04 ac 0f 45 83 cc f7 8c c2 0f d5 f1 63 b6 80 43 41 bd 28
[51] 16 ef ef 11 28 a7 31 45 0c 83 8b b4 be d6 a3 af b6 a7 ad da d3 68 1b 21 45
[76] 15 60 74 cb 4b c1 07 5c 20 d8 6d 1b 40 62 94 7e

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: f613b77d7fd0c1bdcefe8448c73b9897
sha256: 355a0af4f15c500ff8189e4b44a33f3c74e4ba4e8cd22260447177fc59c65e54

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEnMh7xQSsD0WDzPeMwg/V8WO2gENB
vSgW7+8RKKcxRQyDi7S+1qOvtqet2tNoGyFFFWB0y0vBB1wg2G0bQGKUfg==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgM7rjPfu2osHoui33
UPy6fMvYDZRg1/1PVxxITYSftGGhRANCAAScyHvFBKwPRYPM94zCD9XxY7aAQ0G9
KBbv7xEopzFFDIOLtL7Wo6+2p63a02gbIUUVYHTLS8EHXCDYbRtAYpR+
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAj4k+0ZEa6Y2AICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQITwqbtF8NPvcEgZC1PnuJI6IemQlG
B891/eW4loeiKFGKalPvK1aDcRzGhbzpooa8WPc3+hhKrNXfzEaNE0m2QjmbI5yc
FPuGOtrhODdT8rR/cf+NavRScRw58LMzJSrWT+xBAs2mPSdedVqnCiuPJmj7GzKe
k/OCvnkOzPhWz0ztXYhmA+2C6247Xeit7N7TKqgfWJhjT7YnP/Q=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJzIe8UErA9Fg8z3jMIP1fFjtoBDQb0oFu/vESinMUUMg4u0vtajr7anrdrTaBshRRVgdMtLwQdcINhtG0BilH4="

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: f613b77d7fd0c1bdcefe8448c73b9897
sha256: 355a0af4f15c500ff8189e4b44a33f3c74e4ba4e8cd22260447177fc59c65e54

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": "nMh7xQSsD0WDzPeMwg_V8WO2gENBvSgW7-8RKKcxRQw",
    "y": "g4u0vtajr7anrdrTaBshRRVgdMtLwQdcINhtG0BilH4"
}
 

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: f613b77d7fd0c1bdcefe8448c73b9897
sha256: 355a0af4f15c500ff8189e4b44a33f3c74e4ba4e8cd22260447177fc59c65e54