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 c6 ce 8e f1 26 11 7b 07 ad d3 77 f9 45 f6 4c 6a 28 13 00 5f eb f6 61
[51] 61 6b ea 0d b9 51 b3 6d be 24 4d cd df d0 7e ac 5f a1 5e 19 8a e8 fd 1f 62
[76] cf ae 37 15 3a ef 28 75 25 c2 52 cb 2a 7a ef 61
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: f4b140b8f5c1a3271cecd2cb35bbadee
sha256: fe0e1ad0f144cc5feb86441cc69372fa59311cd27d2130c3f6eb0479e4325a9b
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExs6O8SYRewet03f5RfZMaigTAF/r
9mFha+oNuVGzbb4kTc3f0H6sX6FeGYro/R9iz643FTrvKHUlwlLLKnrvYQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgTskGRqQhvBnykBA+
bfEV0U3PnEJ0o/BEB42gAU3Q9ayhRANCAATGzo7xJhF7B63Td/lF9kxqKBMAX+v2
YWFr6g25UbNtviRNzd/QfqxfoV4Ziuj9H2LPrjcVOu8odSXCUssqeu9h
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBD5I+tS/D13ZaKyyzWi
dK0iAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAi5n8U64RmXRwSBkIkK
dI6vqjBDnlevPEhmYqgZNmCzw1PB2DTRir+QCmcAF/WRlqbai2fuF+pBhVEnrOX4
mqgdH/eaRf1OLoiwy/pkVzbgYsnKSAcAn1rk/8YS9VVfcWACovSDwKijxOJEB4xA
Vnzc5KR1PscYQ3CH27Z8yNGFxrf8UErMH+vAA1NohRQ/rLvA1LJfFDO1RIAe8w==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMbOjvEmEXsHrdN3+UX2TGooEwBf6/ZhYWvqDblRs22+JE3N39B+rF+hXhmK6P0fYs+uNxU67yh1JcJSyyp672E="
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: f4b140b8f5c1a3271cecd2cb35bbadee
sha256: fe0e1ad0f144cc5feb86441cc69372fa59311cd27d2130c3f6eb0479e4325a9b
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": "xs6O8SYRewet03f5RfZMaigTAF_r9mFha-oNuVGzbb4",
"y": "JE3N39B-rF-hXhmK6P0fYs-uNxU67yh1JcJSyyp672E"
}
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: f4b140b8f5c1a3271cecd2cb35bbadee
sha256: fe0e1ad0f144cc5feb86441cc69372fa59311cd27d2130c3f6eb0479e4325a9b