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.
[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 88 09 b0 45 99 95 98 e3 28 81 27 f1 36 82 98 43 1d a3 1d 80 88 b8 3a
[51] 8f d8 e8 f7 4b d5 4b d7 67 65 42 aa cb d0 1d 9f 58 33 e7 52 e0 65 d6 3a f8
[76] 3a 7c 66 96 7e 42 51 50 6d ab 83 2e 24 6a d4 e8
To read a DER key use read_key or
read_pubkey with der = TRUE.
[256-bit ecdsa public key]
md5: aff7b86c9cfc0d6bd127378d267f2981
sha256: d4b609a91446f1b0804bfd46ff34123b0178759a332838f30bcc82e986d33528
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.
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiAmwRZmVmOMogSfxNoKYQx2jHYCI
uDqP2Oj3S9VL12dlQqrL0B2fWDPnUuBl1jr4Onxmln5CUVBtq4MuJGrU6A==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg2//WW3i28YUyt0CM
V7G0SQ0qx0VzaWPmL480XU2oieWhRANCAASICbBFmZWY4yiBJ/E2gphDHaMdgIi4
Oo/Y6PdL1UvXZ2VCqsvQHZ9YM+dS4GXWOvg6fGaWfkJRUG2rgy4katTo
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAg8i2N3WxJejgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIlzoF7CU9d/wEgZBMetonsM780y64
Y0kJvRdDjFjjmKZ/tG5KtZrTxsOdmeMsmsiv7V6G8bf8uH1UXRolZTkTQ2bVKMdW
XwxDpLs41BqsO4DIr0FIYMiKFxQlHFq1HDCZpSpSq9eqU9/XWWOaJAZkZZUzNdQq
gsgZntRdvRCWlE25AvFUWbEOEx5Lmu0N7FbLH+9RYIzH24qi4Nk=
-----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.
[1] "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIgJsEWZlZjjKIEn8TaCmEMdox2AiLg6j9jo90vVS9dnZUKqy9Adn1gz51LgZdY6+Dp8ZpZ+QlFQbauDLiRq1Og="
The read_pubkey function will automatically detect if a
file contains a PEM or SSH key.
[256-bit ecdsa public key]
md5: aff7b86c9cfc0d6bd127378d267f2981
sha256: d4b609a91446f1b0804bfd46ff34123b0178759a332838f30bcc82e986d33528
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": "iAmwRZmVmOMogSfxNoKYQx2jHYCIuDqP2Oj3S9VL12c",
"y": "ZUKqy9Adn1gz51LgZdY6-Dp8ZpZ-QlFQbauDLiRq1Og"
}
Keys from jose and openssl are the
same.
[1] TRUE
[256-bit ecdsa public key]
md5: aff7b86c9cfc0d6bd127378d267f2981
sha256: d4b609a91446f1b0804bfd46ff34123b0178759a332838f30bcc82e986d33528