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 c9 95 c0 9d 2a da f8 04 11 45 f0 8e 36 00 31 60 22 b0 6b 15 90 01 94
[51] 3f 39 e5 f0 b2 9e 86 39 3c 3d 65 9a 08 88 11 43 79 4f 8a 0e 55 9c f2 39 82
[76] 7c 0a 86 0e d2 45 d1 a5 4b d0 20 0a a6 ac 6b 8d
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: 04f4ba7171c4fdececdb3394126e2bc6
sha256: bbaa337111faf05ff088595133ee5e70f0faa93198f71654f0754c5355c9ee67
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEyZXAnSra+AQRRfCONgAxYCKwaxWQ
AZQ/OeXwsp6GOTw9ZZoIiBFDeU+KDlWc8jmCfAqGDtJF0aVL0CAKpqxrjQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghi7OheWhnPt3Wx/z
44bYM+A7DZwvfSHZ01vN6H0D2pyhRANCAATJlcCdKtr4BBFF8I42ADFgIrBrFZAB
lD855fCynoY5PD1lmgiIEUN5T4oOVZzyOYJ8CoYO0kXRpUvQIAqmrGuN
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAjXEaVpwNuwrAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIqwx1R0SAz7UEgZBHtB+nbHGZkIip
X92y6d5jlLhwJA6ALdoUmsW2f9XKW1ySXjwniv5QwXZYsf/LBIhKNHULIkQ9bk2C
WGVbdFYp1H2v96MpAbczpvP0S/vc3v+xCLLr1B7IXIgvtAMmdt3y/QRkeCvxrc2A
6odv2wGAFzjxHMZZsgqrEEVF6Dzkj20/7UFRTlbCa9yhlZ5E/Ok=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMmVwJ0q2vgEEUXwjjYAMWAisGsVkAGUPznl8LKehjk8PWWaCIgRQ3lPig5VnPI5gnwKhg7SRdGlS9AgCqasa40="
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: 04f4ba7171c4fdececdb3394126e2bc6
sha256: bbaa337111faf05ff088595133ee5e70f0faa93198f71654f0754c5355c9ee67
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": "yZXAnSra-AQRRfCONgAxYCKwaxWQAZQ_OeXwsp6GOTw",
"y": "PWWaCIgRQ3lPig5VnPI5gnwKhg7SRdGlS9AgCqasa40"
}
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: 04f4ba7171c4fdececdb3394126e2bc6
sha256: bbaa337111faf05ff088595133ee5e70f0faa93198f71654f0754c5355c9ee67