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 35 43 ce 0a 75 c5 75 e3 8d f5 04 8f 2d a4 cd ac 2f 9f 45 a4 da ba cd
[51] 76 e5 68 07 51 30 f1 8d 94 06 4f 9b 5c 5f aa 31 cc 5e 5c 5a ba c9 48 65 c9
[76] 8b 2b 4a 60 50 03 7c 3b ca 89 43 cc 94 61 9b d4
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: 85d6415d6957da1c7baded580a4f5500
sha256: 9df1c93d2209e01f3d8bbcf6d9c4ff0b29cbb02947a710fa770163eec2322a90
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENUPOCnXFdeON9QSPLaTNrC+fRaTa
us125WgHUTDxjZQGT5tcX6oxzF5cWrrJSGXJiytKYFADfDvKiUPMlGGb1A==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtYVEXyMu3PD7gv1s
nFvXyzmW4q+f5BYWXvy0sWu2K/WhRANCAAQ1Q84KdcV14431BI8tpM2sL59FpNq6
zXblaAdRMPGNlAZPm1xfqjHMXlxauslIZcmLK0pgUAN8O8qJQ8yUYZvU
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBDJDZTNVHOrSw2RY3qK
La+zAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAgsARMH8PkXdwSBkBk2
NGFoMLdYLHY4ca0vxlHet4BJK9arltNTP7DF6OhKRaIRIBbkaQD/FXTvkEOU91yH
u5JiFl3PKrhcYUIJGYMG7V0oxF10MiwUsCVk0jTmKGtDYaLobC41n/Ai/fOYjAmH
MUQ4S7Kyc1QCCWKswjWF6hsy0QpMburfVDRmzVg5JNae5l0OzoUO0H7+VKBS5g==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDVDzgp1xXXjjfUEjy2kzawvn0Wk2rrNduVoB1Ew8Y2UBk+bXF+qMcxeXFq6yUhlyYsrSmBQA3w7yolDzJRhm9Q="
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: 85d6415d6957da1c7baded580a4f5500
sha256: 9df1c93d2209e01f3d8bbcf6d9c4ff0b29cbb02947a710fa770163eec2322a90
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": "NUPOCnXFdeON9QSPLaTNrC-fRaTaus125WgHUTDxjZQ",
"y": "Bk-bXF-qMcxeXFq6yUhlyYsrSmBQA3w7yolDzJRhm9Q"
}
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: 85d6415d6957da1c7baded580a4f5500
sha256: 9df1c93d2209e01f3d8bbcf6d9c4ff0b29cbb02947a710fa770163eec2322a90