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:
- RSA : Most popular method. Supports both encryption and signatures.
- DSA : Digital Signature Algorithm. Mostly for signatures, not very popular anymore.
- ECDSA : Elliptic Curve DSA. Supports signatures and encryption via Diffie Hellman. Gaining popularity.
For each type there are several common formats for storing keys and certificates:
- DER: binary format, serialized ASN.1 structure
-
PEM: base64 encoded
DER + header wrapped in
=== - SSH: single line of base64 with a header. Only for pubkeys.
- JWK: JSON Web key. Stores key data in a JSON object
The openssl package automatically detects the format when possible. However being able to recognize the various formats can be useful.
The DER format
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.
[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 b4 bf 49 09 da 99 c7 11 02 72 a1 7c da 7d 51 cf cc 49 0a 91 27 46 76
[51] 41 2a db c8 6a 7c ff f3 85 cc 7d ce 3a d5 0c 0d 79 b0 7b 31 81 4e d8 a2 55
[76] 5c 14 d0 b8 f9 bb ff 35 f5 fa ac fb 20 c0 24 fb
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: b1d2fbaf4cdcb32cd696ecce1f89b995
sha256: 50a87912dae274d4bcf058b916c71902d66e18986b65be40bb687dcd2709df5c
Users typically don’t need to worry about the key’s underlying
primes, but have a look at key$data if you are curious.
The PEM format
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.
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEtL9JCdqZxxECcqF82n1Rz8xJCpEn
RnZBKtvIanz/84XMfc461QwNebB7MYFO2KJVXBTQuPm7/zX1+qz7IMAk+w==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgITzhFb7vCuKQsSBm
kgyCOzuFDEsN9qFbtQtM9oDVqTihRANCAAS0v0kJ2pnHEQJyoXzafVHPzEkKkSdG
dkEq28hqfP/zhcx9zjrVDA15sHsxgU7YolVcFNC4+bv/NfX6rPsgwCT7
-----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.
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAg+Bd1jmdobYQICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQI4+ybQvWa3eUEgZD/14m3byfq6DgF
pQDsjJzK7TEs3DKVB5fUm4eWObdLkdUejXTOt2fNAzoC3L4cqS7aQTJziyLYcjcP
0zyk3Jp5cv0f7CDgG50CIeBG9L2Qr9Pa0wG0BdO/PgrCMBLiAi/dOWnV0G2lBBkk
tQNC5+UeXWeobS/zp76DPVManCsjbxr5METff35+3sNK+gIZqMI=
-----END ENCRYPTED PRIVATE KEY-----
The OpenSSH format
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.
[1] "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLS/SQnamccRAnKhfNp9Uc/MSQqRJ0Z2QSrbyGp8//OFzH3OOtUMDXmwezGBTtiiVVwU0Lj5u/819fqs+yDAJPs="
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: b1d2fbaf4cdcb32cd696ecce1f89b995
sha256: 50a87912dae274d4bcf058b916c71902d66e18986b65be40bb687dcd2709df5c
The JSON Web Key (JWK) format
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.
{
"kty": "EC",
"crv": "P-256",
"x": "tL9JCdqZxxECcqF82n1Rz8xJCpEnRnZBKtvIanz_84U",
"y": "zH3OOtUMDXmwezGBTtiiVVwU0Lj5u_819fqs-yDAJPs"
}
Keys from jose and openssl are the
same.
[1] TRUE
print(mykey)[256-bit ecdsa public key]
md5: b1d2fbaf4cdcb32cd696ecce1f89b995
sha256: 50a87912dae274d4bcf058b916c71902d66e18986b65be40bb687dcd2709df5c