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.
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
[24] 03 42 00 04 4e 2c 93 c7 55 8e 3e a4 78 92 97 0b dd 12 2f 22 8e 70 a1
[47] 2d c6 e4 e8 bf 96 66 04 04 45 b3 40 60 ab 13 ee 17 5c a6 00 71 b7 29
[70] 72 22 6e 5b 96 d9 6d 10 f8 6f 00 27 02 6c fa 4a c8 6e 00 8b 95 d0
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: 10537c3b3dd0cffda041ec298472c4a6
Users typically don’t need to worry about the key’s underlying primes, but have a look at key$data
if you are curious.
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAETiyTx1WOPqR4kpcL3RIvIo5woS3G
5Oi/lmYEBEWzQGCrE+4XXKYAcbcpciJuW5bZbRD4bwAnAmz6SshuAIuV0A==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgJucLrfmwzFZjoeTk
uolBt5fh2QVQMHkmYYclG7GV2BehRANCAAROLJPHVY4+pHiSlwvdEi8ijnChLcbk
6L+WZgQERbNAYKsT7hdcpgBxtylyIm5bltltEPhvACcCbPpKyG4Ai5XQ
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAgmgJNuq/5YwQICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIqHBk/g7LHawEgZAe/7qnYigSOVDY
74tPEuUh0+7Qj+rjJqZmsEcyyvW/fTXX3yYILXRRnJhNblzNF9cDajO5DjO3OJZU
s7qt9Dvc2Lm/xI1/0e/OJiMIigVpMPLyTel4j1ps4Ip4aXsSmK82YqsHSAMzLZ3R
MdutHwm9HjGkN4/rnQgVEcycJ+8qTX+IHOzgcoEMUzZxDn4eHak=
-----END ENCRYPTED PRIVATE KEY-----
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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE4sk8dVjj6keJKXC90SLyKOcKEtxuTov5ZmBARFs0BgqxPuF1ymAHG3KXIibluW2W0Q+G8AJwJs+krIbgCLldA="
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: 10537c3b3dd0cffda041ec298472c4a6
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": "TiyTx1WOPqR4kpcL3RIvIo5woS3G5Oi_lmYEBEWzQGA",
"y": "qxPuF1ymAHG3KXIibluW2W0Q-G8AJwJs-krIbgCLldA"
}
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: 10537c3b3dd0cffda041ec298472c4a6