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 f2 f6 4f 0b 5f bd cb 2d 1e 1a ab 11 ed 92 9c 26 ed 77 c8 4a 7e 93 7d
[51] d4 f7 cc d9 fc 9c ed de 65 cb 8b cc 3e 4a 40 54 86 20 65 d9 5d 3a 93 9d 5d
[76] 50 a4 55 74 e2 b6 12 94 31 12 b8 4c 06 33 2d 19
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: e65e5c6cf2e1efa9152b3d9be2b14af2
sha256: 52230f92c47a2b9011d6748af68d874de350db55224a323eb7b93e67eee72384
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE8vZPC1+9yy0eGqsR7ZKcJu13yEp+
k33U98zZ/Jzt3mXLi8w+SkBUhiBl2V06k51dUKRVdOK2EpQxErhMBjMtGQ==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgDXEy+6oWIzPSqSGF
C1ZEhu/yu7dXkpS3tEyoMuIDdF6hRANCAATy9k8LX73LLR4aqxHtkpwm7XfISn6T
fdT3zNn8nO3eZcuLzD5KQFSGIGXZXTqTnV1QpFV04rYSlDESuEwGMy0Z
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAjEW2h4ysXuwgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIItue7mf8HtkEgZDaNg12J22ABwMb
wgb5ckUrLXRevYaQd4vHcQ2lqCGkBPpHvug0tpkDUxMUDewzuPqWfwjfrIqqEc5Y
co6wrBGbm+RPo9d48QoxZ6/VbpNFZViecJhloUL+iOpW1c9ryUX3wOxMv/LI38Mx
/njnon3PkNeRvRDSXHHHBcuEwHZcsOL7Uh1af6Q8N/UwLsUXn7s=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPL2TwtfvcstHhqrEe2SnCbtd8hKfpN91PfM2fyc7d5ly4vMPkpAVIYgZdldOpOdXVCkVXTithKUMRK4TAYzLRk="
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: e65e5c6cf2e1efa9152b3d9be2b14af2
sha256: 52230f92c47a2b9011d6748af68d874de350db55224a323eb7b93e67eee72384
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": "8vZPC1-9yy0eGqsR7ZKcJu13yEp-k33U98zZ_Jzt3mU",
"y": "y4vMPkpAVIYgZdldOpOdXVCkVXTithKUMRK4TAYzLRk"
}
Keys from jose and openssl are the
same.
[1] TRUE
print(mykey)[256-bit ecdsa public key]
md5: e65e5c6cf2e1efa9152b3d9be2b14af2
sha256: 52230f92c47a2b9011d6748af68d874de350db55224a323eb7b93e67eee72384