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 93 04 4b 6a 4f 75 4d e7 9a e7 3e 20 c0 d9 ca 6d 81 d9 d1 b6 a6 12 56
[51] ca 75 5d a8 c0 dc 6c 52 8f de 73 6a 00 7e 35 28 ea 8d d2 d7 21 fb d5 90 a0
[76] 93 e0 97 4a 1b e0 54 96 99 a3 20 78 68 19 9f 8c
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: 94dbb428d2f4ffa0eb2b770342320ac6
sha256: 1a8023668f0b482eab6ca62fc4a284b45d322da0e86a84bca4a8b1d1dff9209d
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEkwRLak91Teea5z4gwNnKbYHZ0bam
ElbKdV2owNxsUo/ec2oAfjUo6o3S1yH71ZCgk+CXShvgVJaZoyB4aBmfjA==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgpRi4aF78w+kpxPcM
zi41PrOnk095LJ90HjdCYsLnjEKhRANCAASTBEtqT3VN55rnPiDA2cptgdnRtqYS
Vsp1XajA3GxSj95zagB+NSjqjdLXIfvVkKCT4JdKG+BUlpmjIHhoGZ+M
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAj0VzCxt3RezgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQI06s1QQhSAlAEgZC7vNyDuI+7MdQf
NRuRN1nHScVzkUXzYg2AX7hgv4s58VX6gLB3JGCdwo7k0gFW3re0P2LT9uQFBpEv
C82pHHNBIXgi17FBpzvBcqZYBwAsfy7jHe1v64K782tfnQVDLBwPNwWIhrhUKSQM
j5YtKbGyMccE/71t1DYa+2t7AS7lVhlY5PRyeb0S1JVYRw1ee6E=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJMES2pPdU3nmuc+IMDZym2B2dG2phJWynVdqMDcbFKP3nNqAH41KOqN0tch+9WQoJPgl0ob4FSWmaMgeGgZn4w="
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: 94dbb428d2f4ffa0eb2b770342320ac6
sha256: 1a8023668f0b482eab6ca62fc4a284b45d322da0e86a84bca4a8b1d1dff9209d
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.
Warning: package 'jose' was built under R version 4.5.3
{
"kty": "EC",
"crv": "P-256",
"x": "kwRLak91Teea5z4gwNnKbYHZ0bamElbKdV2owNxsUo8",
"y": "3nNqAH41KOqN0tch-9WQoJPgl0ob4FSWmaMgeGgZn4w"
}
Keys from jose and openssl are the
same.
[1] TRUE
print(mykey)[256-bit ecdsa public key]
md5: 94dbb428d2f4ffa0eb2b770342320ac6
sha256: 1a8023668f0b482eab6ca62fc4a284b45d322da0e86a84bca4a8b1d1dff9209d