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 dc af 6e e4 92 af b4 15 c6 f0 19 75 2d 16 7a 5a 89 19 d9 4d 0c e0 d3
[51] 6b 63 31 3c 2a 4d 10 2f ca cd 89 b9 21 88 b7 91 c5 c8 27 a9 4e 49 52 67 8f
[76] a1 ab 49 4a 19 18 e9 7f ac da a3 82 cd 16 52 f5
To read a DER key use read_key or
read_pubkey with der = TRUE.
[256-bit ecdsa public key]
md5: 09db7f883b3966e5f843157d7e92b468
sha256: 1044bcb7b5eb0aa542c6a4f66d0f96c1378d1abe4157f0dba6c8826030b52592
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3K9u5JKvtBXG8Bl1LRZ6WokZ2U0M
4NNrYzE8Kk0QL8rNibkhiLeRxcgnqU5JUmePoatJShkY6X+s2qOCzRZS9Q==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgDzzwNu0WUsfCxrqT
wYQ2qUXb4q87VYVSnFoSh1iSchShRANCAATcr27kkq+0FcbwGXUtFnpaiRnZTQzg
02tjMTwqTRAvys2JuSGIt5HFyCepTklSZ4+hq0lKGRjpf6zao4LNFlL1
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAjjGc+TlvAYugICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIbb3ausJogzUEgZAx/tJuIPrzz/f/
dmEJFLHuMQvyhaMomA9yKKEzfGKh7QicqaVk+6smCMf3Po9k99u8jeW0U4zySmec
SBNWkwDS79K9hzZl4FoHNT6oiBMYob3l4jQuHE4FhVzF+GIvvLIV+mtjtNnaMpPU
CAEcTQhLK5Tb43U3cMersTtx7GpfT1dtEF+lYjXvTxSxXFmDIPU=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNyvbuSSr7QVxvAZdS0WelqJGdlNDODTa2MxPCpNEC/KzYm5IYi3kcXIJ6lOSVJnj6GrSUoZGOl/rNqjgs0WUvU="
The read_pubkey function will automatically detect if a
file contains a PEM or SSH key.
[256-bit ecdsa public key]
md5: 09db7f883b3966e5f843157d7e92b468
sha256: 1044bcb7b5eb0aa542c6a4f66d0f96c1378d1abe4157f0dba6c8826030b52592
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": "3K9u5JKvtBXG8Bl1LRZ6WokZ2U0M4NNrYzE8Kk0QL8o",
"y": "zYm5IYi3kcXIJ6lOSVJnj6GrSUoZGOl_rNqjgs0WUvU"
}
Keys from jose and openssl are the
same.
[1] TRUE
[256-bit ecdsa public key]
md5: 09db7f883b3966e5f843157d7e92b468
sha256: 1044bcb7b5eb0aa542c6a4f66d0f96c1378d1abe4157f0dba6c8826030b52592