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 dc 73 c3 2f 09 9c 54 4e f6 3b 18 9b 3f d2 7c 5b 9a 85 df ad 6f 84 72
[51] ba 5b dc fc f6 bf af 06 93 39 05 ce 71 0e b2 b0 33 5f f7 08 2c 04 02 d1 93
[76] 10 5c c0 3f e0 27 08 b5 4d a5 b7 92 15 3c cc fc
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: ce510e6db33c4b67f648ba3e8c392101
sha256: 82f76aa2e06952148b4df5eac754454b18bfb9f2e774c0fbf304198a86414755
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3HPDLwmcVE72OxibP9J8W5qF361v
hHK6W9z89r+vBpM5Bc5xDrKwM1/3CCwEAtGTEFzAP+AnCLVNpbeSFTzM/A==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgSoAXxIMbHP/om4yg
fQF8fbN7rqhLReNUBsHSE2lciw6hRANCAATcc8MvCZxUTvY7GJs/0nxbmoXfrW+E
crpb3Pz2v68GkzkFznEOsrAzX/cILAQC0ZMQXMA/4CcItU2lt5IVPMz8
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAiDTO5CnY1zgAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIdALDUtBssOEEgZBfCFvMLEelbOny
YlpEsHzpVVRNHEABMvyVwlCuj9tjOP1f4CW02jrg8TC0FsvwAG1cSHxfCTyCZS3s
TsnvpcdRCemE5RClImD8sTwJscz5AJPqUf2ba5sJzJ1JVjbb/qEQeFx6V41XHLvi
PUdTq6m9JjEx3VmHNo2Z+wf0GslTSqTTqqgwcXahy48ekVJzrfo=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNxzwy8JnFRO9jsYmz/SfFuahd+tb4Ryulvc/Pa/rwaTOQXOcQ6ysDNf9wgsBALRkxBcwD/gJwi1TaW3khU8zPw="
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: ce510e6db33c4b67f648ba3e8c392101
sha256: 82f76aa2e06952148b4df5eac754454b18bfb9f2e774c0fbf304198a86414755
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": "3HPDLwmcVE72OxibP9J8W5qF361vhHK6W9z89r-vBpM",
"y": "OQXOcQ6ysDNf9wgsBALRkxBcwD_gJwi1TaW3khU8zPw"
}
Keys from jose and openssl are the
same.
[1] TRUE
print(mykey)[256-bit ecdsa public key]
md5: ce510e6db33c4b67f648ba3e8c392101
sha256: 82f76aa2e06952148b4df5eac754454b18bfb9f2e774c0fbf304198a86414755