Importing and exporting RSA/DSA/EC keys

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 96 9a 54 cc 36 e5 c5 b2 ef d9 86 f5 b7 d4 02 64 e3 52 0a 6e 8a d9 3a
[51] 9c 05 d4 05 52 fd af 7d 01 12 d0 2c 7d 60 00 b8 d9 11 6b 90 1a a6 82 b3 4b
[76] af 17 bd d3 6f 88 ad 04 95 18 83 8a cb 51 79 19

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: 69e6ccaafed2cb6686f7729841e8d4b2
sha256: 9038223ba81e7b5a95589846fe95f84b400b1345cc324408784119cd48efb711

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAElppUzDblxbLv2Yb1t9QCZONSCm6K
2TqcBdQFUv2vfQES0Cx9YAC42RFrkBqmgrNLrxe902+IrQSVGIOKy1F5GQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgxyUChCnCy+rVkikO
HLCiRgHO94oyyHdUaKeMLPkxKQShRANCAASWmlTMNuXFsu/ZhvW31AJk41IKborZ
OpwF1AVS/a99ARLQLH1gALjZEWuQGqaCs0uvF73Tb4itBJUYg4rLUXkZ
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBAj8ArG7g26NqfrrdSV
SQNNAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAh9btYiB/K5qASBkNxz
Tw4l03SpZeqcDH1fMeYBPuGVqZX278FxGbgaJ2oPmnaeg3sSyLgipiz1nOS1ZJTr
FozU4ePZMlBdA3rIbxmHHNMDDmOLVrTfkZpd5Hl9Kj0CB1nIKw1JlRv29OyoUhw2
SXhpkal25lqnImzJJoDYXau4p03SxuEU6DaywV42Pp96GSEwoV+GsW3XzVXJjA==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJaaVMw25cWy79mG9bfUAmTjUgpuitk6nAXUBVL9r30BEtAsfWAAuNkRa5AapoKzS68XvdNviK0ElRiDistReRk="

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: 69e6ccaafed2cb6686f7729841e8d4b2
sha256: 9038223ba81e7b5a95589846fe95f84b400b1345cc324408784119cd48efb711

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": "lppUzDblxbLv2Yb1t9QCZONSCm6K2TqcBdQFUv2vfQE",
    "y": "EtAsfWAAuNkRa5AapoKzS68XvdNviK0ElRiDistReRk"
}
 

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: 69e6ccaafed2cb6686f7729841e8d4b2
sha256: 9038223ba81e7b5a95589846fe95f84b400b1345cc324408784119cd48efb711