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 13 8a 29 1b 02 73 ba c2 98 38 cf 00 b0 94 47 2e 7c 3c 1b 4e 6b 26 84
[51] fa 48 63 e3 e6 2a 16 e6 dd db c4 c3 3e 2e d5 db 74 98 9f 03 78 70 b4 4c 2a
[76] 5c 5b ca d5 a8 a0 4f 19 60 ac 94 0f 42 bd 38 94
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: c5122b636367453db85dc277c878602e
sha256: 04b82a8f242c8f6d283493ca38bf66a07a3ccd8c7231a1556858b358bb1b45c0
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEE4opGwJzusKYOM8AsJRHLnw8G05r
JoT6SGPj5ioW5t3bxMM+LtXbdJifA3hwtEwqXFvK1aigTxlgrJQPQr04lA==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgQZNCmXQJ/G6AHuR+
H1QNxlHZHU967wjyQXC183pCvVShRANCAAQTiikbAnO6wpg4zwCwlEcufDwbTmsm
hPpIY+PmKhbm3dvEwz4u1dt0mJ8DeHC0TCpcW8rVqKBPGWCslA9CvTiU
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAhBrAVCfJsCSgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQI45HgnzYDXP0EgZCwe0nva7oinNbM
MwfVxYMwxMbD0RgN58C2r0QQDojT9UABDZLPhVDDqD0zzFIYaxjLGqYXAnXbUfYW
sSplNSwBKtTE0lmW5w3oRK4iU0KS4UJfCG3Cg6/MKmgn7tx13fqZxU3W94mzOTC9
5ySHAjxh0yZ3+jehu0zhij9UE1dguM0kfWXqnzZDQGUUTfm0P0A=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBOKKRsCc7rCmDjPALCURy58PBtOayaE+khj4+YqFubd28TDPi7V23SYnwN4cLRMKlxbytWooE8ZYKyUD0K9OJQ="
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: c5122b636367453db85dc277c878602e
sha256: 04b82a8f242c8f6d283493ca38bf66a07a3ccd8c7231a1556858b358bb1b45c0
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": "E4opGwJzusKYOM8AsJRHLnw8G05rJoT6SGPj5ioW5t0",
"y": "28TDPi7V23SYnwN4cLRMKlxbytWooE8ZYKyUD0K9OJQ"
}
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: c5122b636367453db85dc277c878602e
sha256: 04b82a8f242c8f6d283493ca38bf66a07a3ccd8c7231a1556858b358bb1b45c0