Skip to contents

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:

  • RSA : Most popular method. Supports both encryption and signatures.
  • DSA : Digital Signature Algorithm. Mostly for signatures, not very popular anymore.
  • ECDSA : Elliptic Curve DSA. Supports signatures and encryption via Diffie Hellman. Gaining popularity.

For each type there are several common formats for storing keys and certificates:

  • DER: binary format, serialized ASN.1 structure
  • PEM: base64 encoded DER + header wrapped in ===
  • SSH: single line of base64 with a header. Only for pubkeys.
  • JWK: JSON Web key. Stores key data in a JSON object

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 79 bd a0 57 01 66 9b 5b 96 a7 bd bb c4 68 56 b7 c8 dc e8 e6 50 19 22
[51] 6b 40 79 01 49 2b 34 88 71 be 0a 18 36 30 5a 3e 01 7f 3a bd 8c 33 59 f2 8c
[76] d3 40 af 84 c6 05 c6 15 d9 09 f1 da 3f 76 f6 1a

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: 65768a018391a8a9d3a3e233c9d79977
sha256: d4631f4fd6f06a8de1357c49ab63f2d5aeb5d8640d3565e45ca5456abdac369e

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEeb2gVwFmm1uWp727xGhWt8jc6OZQ
GSJrQHkBSSs0iHG+Chg2MFo+AX86vYwzWfKM00CvhMYFxhXZCfHaP3b2Gg==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgP/2lUQzAx/wJSLLF
6UHQpZFHROXHqsX31miu7uDYNdGhRANCAAR5vaBXAWabW5anvbvEaFa3yNzo5lAZ
ImtAeQFJKzSIcb4KGDYwWj4Bfzq9jDNZ8ozTQK+ExgXGFdkJ8do/dvYa
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAgw1jsPU1nOnAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIQ3eVJUhT06AEgZBt300qNicDLA8a
MdrYwAmoF6py7/YsoXxq7IY+Eg2PnQ9azWCzyK8NS6Y0EnFMDAx66fOH2JBk8GYk
Zew0U1NY3ZFPspB/GRJgTcLZVmvvA4kQ9Z6VPpWaCtqodDQ2Rd17kyqeO5XUWunr
GqgJ+CGOi+BCG7bs2rY0mZSm/IOd+laKjPnkESvpsbNt6eVHM0M=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHm9oFcBZptblqe9u8RoVrfI3OjmUBkia0B5AUkrNIhxvgoYNjBaPgF/Or2MM1nyjNNAr4TGBcYV2Qnx2j929ho="

The read_pubkey function will automatically detect if a file contains a PEM or SSH key.

[256-bit ecdsa public key]
md5: 65768a018391a8a9d3a3e233c9d79977
sha256: d4631f4fd6f06a8de1357c49ab63f2d5aeb5d8640d3565e45ca5456abdac369e

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": "eb2gVwFmm1uWp727xGhWt8jc6OZQGSJrQHkBSSs0iHE",
    "y": "vgoYNjBaPgF_Or2MM1nyjNNAr4TGBcYV2Qnx2j929ho"
}
 

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: 65768a018391a8a9d3a3e233c9d79977
sha256: d4631f4fd6f06a8de1357c49ab63f2d5aeb5d8640d3565e45ca5456abdac369e