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 <- as.list(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
[24] 03 42 00 04 a4 8c e4 2f ba 35 bb 51 fb dc 54 e4 23 2f ad 1e 33 1b d4
[47] 34 ec f6 e5 9a 01 c0 8a 71 a4 2b d7 26 9a f6 61 f7 45 16 bf 79 8f 49
[70] 4c b3 e6 fa 4d 60 16 79 79 e5 97 d6 48 d6 46 b8 d7 5c df 5f b5 ef
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: d5:51:98:98:c6:ef:22:93:cb:98:9e:02:77:a4:0f:33
Users typically don’t need to worry about the key’s underlying primes, but have a look at as.list(key)
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEpIzkL7o1u1H73FTkIy+tHjMb1DTs
9uWaAcCKcaQr1yaa9mH3RRa/eY9JTLPm+k1gFnl55ZfWSNZGuNdc31+17w==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQggUgDSn/6Gi27GfNi
SEvmG+TASp0GWBQm7Zp8QjkmMOahRANCAASkjOQvujW7UfvcVOQjL60eMxvUNOz2
5ZoBwIpxpCvXJpr2YfdFFr95j0lMs+b6TWAWeXnll9ZI1ka411zfX7Xv
-----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.
str <- write_pem(key, password = "supersecret")
cat(str)
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHVMEAGCSqGSIb3DQEFDTAzMBsGCSqGSIb3DQEFDDAOBAhR+BNhsPjI2QICCAAw
FAYIKoZIhvcNAwcECAa8lcpv7lk7BIGQ93hq0WEhS8zNNCfXyMBYcLf6kbqnSF2v
WWUGoR37gkmrm3xDiySG48VQAe2WD9M1z1DIxUQUrD38wumj7g3e8OWeDcpc+Rrl
SPpgKLUKy1tn9KmvRDLBGcsvtLKaB5YzmSNnWRTDkwZuSky/DJ8G+vuQOWoJCr4e
MxD8THFquBNTlvY2j1Z5OUFbmez+s9AX
-----END ENCRYPTED PRIVATE KEY-----
read_key(str, password = "supersecret")
[256-bit ecdsa private key]
md5: d5:51:98:98:c6:ef:22:93:cb:98:9e:02:77:a4:0f:33
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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKSM5C+6NbtR+9xU5CMvrR4zG9Q07PblmgHAinGkK9cmmvZh90UWv3mPSUyz5vpNYBZ5eeWX1kjWRrjXXN9fte8="
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: d5:51:98:98:c6:ef:22:93:cb:98:9e:02:77:a4:0f:33
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 jwk_write
and jwk_read
functions are implemented in a separate package which uses the openssl
package.
library(jose)
json <- jose::jwk_write(pubkey)
jsonlite::prettify(json)
{
"kty": "EC",
"crv": "P-256",
"x": "pIzkL7o1u1H73FTkIy-tHjMb1DTs9uWaAcCKcaQr1yY",
"y": "mvZh90UWv3mPSUyz5vpNYBZ5eeWX1kjWRrjXXN9fte8"
}
The jwk_read
function parses the json blob back into the openssl key:
mykey <- jose::jwk_read(json)
print(mykey)
[256-bit ecdsa public key]
md5: d5:51:98:98:c6:ef:22:93:cb:98:9e:02:77:a4:0f:33