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 f5 92 55 eb e6 32 00 4e ce 6f 17 66 be bc 3e ea 8a f4 0d 0c 4f 14 b2
[51] fb 2b dd a7 50 8b 43 82 0e ba 7d 3b ed 90 5e 73 d8 dc e5 19 00 77 12 64 c2
[76] 39 20 4a 58 59 c9 34 bc 24 69 18 54 36 7c 12 05
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: 2b975f3a3fed2e7ac176857880d2ba37
sha256: a22bb1e4872b337cd5434dd24903c806177af1a826ed8452ba5d2af65141dae3
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE9ZJV6+YyAE7Obxdmvrw+6or0DQxP
FLL7K92nUItDgg66fTvtkF5z2NzlGQB3EmTCOSBKWFnJNLwkaRhUNnwSBQ==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrmX2ZL924kAb3lh2
fEEgCfTaXlsq1YBaH/DjmXSO/2OhRANCAAT1klXr5jIATs5vF2a+vD7qivQNDE8U
svsr3adQi0OCDrp9O+2QXnPY3OUZAHcSZMI5IEpYWck0vCRpGFQ2fBIF
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBCuh5oJgAcCbUf0k6EL
m9nTAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAgW8I3bNyF09wSBkKXI
N1vc9h6Iit8bCxnThVx/zdUKXBB6NnXeJmrNdWvy0wlG8ZFYi9slBSLj6q3ay3Eb
+oAYhUpww3+09PMBy2UDIwtnbKZnNjQarrhtM8ZR28kJt3TRyFKp8sDFPfuzPQQI
bIoD+c+89020ZffrMmTCTQSqA060HslndIYrxwMkPynbIqrZpqMGKRvrN9fGrw==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPWSVevmMgBOzm8XZr68PuqK9A0MTxSy+yvdp1CLQ4IOun077ZBec9jc5RkAdxJkwjkgSlhZyTS8JGkYVDZ8EgU="
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: 2b975f3a3fed2e7ac176857880d2ba37
sha256: a22bb1e4872b337cd5434dd24903c806177af1a826ed8452ba5d2af65141dae3
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": "9ZJV6-YyAE7Obxdmvrw-6or0DQxPFLL7K92nUItDgg4",
"y": "un077ZBec9jc5RkAdxJkwjkgSlhZyTS8JGkYVDZ8EgU"
}
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: 2b975f3a3fed2e7ac176857880d2ba37
sha256: a22bb1e4872b337cd5434dd24903c806177af1a826ed8452ba5d2af65141dae3