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 03 42
[26] 00 04 06 2b 4d f2 7d ac af 06 56 66 01 01 1d 84 e9 f7 4b 23 39 12 43 a4 2f
[51] 19 65 f8 09 35 b6 cb 46 99 6c 18 89 d1 91 55 6b c4 88 2a b3 f7 24 09 e3 a6
[76] cd 43 9f 7d cf 48 7e 88 7a fa 61 46 89 7b d3 13
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: 9179758f5580c621e680de424bcd679e
sha256: 28a20597aecf9c7832aa53b24bd672c73792e335e83804ff82b22a3a6980691c
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBitN8n2srwZWZgEBHYTp90sjORJD
pC8ZZfgJNbbLRplsGInRkVVrxIgqs/ckCeOmzUOffc9Ifoh6+mFGiXvTEw==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgvc0fpo+BuKLfGxt6
GiuzBpJ4/mXkgWKOpCrPOCqEMI+hRANCAAQGK03yfayvBlZmAQEdhOn3SyM5EkOk
Lxll+Ak1tstGmWwYidGRVWvEiCqz9yQJ46bNQ599z0h+iHr6YUaJe9MT
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAgKPy8pOJ5jyQICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIrwBFvQnsSP4EgZAZS/j3FVfxdpn3
W4kwTB/TnHTJ1RHhZJ5My1Cs8y6pQ0kNOlKkfg2dz8MDrKNy5pBJ+CX2rnj1Qt0j
kxgSzN5+B05W4WvK4Q55QqAoLZxnE+6imXQuA4i8/cNQIZ/BwOMylbuG7Q5a5Hjt
9/0cFX6OKnzY+yXpBFHCfTh32e6ESUJSwT+ki7jET2ezW8/RHBo=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAYrTfJ9rK8GVmYBAR2E6fdLIzkSQ6QvGWX4CTW2y0aZbBiJ0ZFVa8SIKrP3JAnjps1Dn33PSH6IevphRol70xM="
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: 9179758f5580c621e680de424bcd679e
sha256: 28a20597aecf9c7832aa53b24bd672c73792e335e83804ff82b22a3a6980691c
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": "BitN8n2srwZWZgEBHYTp90sjORJDpC8ZZfgJNbbLRpk",
"y": "bBiJ0ZFVa8SIKrP3JAnjps1Dn33PSH6IevphRol70xM"
}
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: 9179758f5580c621e680de424bcd679e
sha256: 28a20597aecf9c7832aa53b24bd672c73792e335e83804ff82b22a3a6980691c