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 b9 bf 9f 08 db 6c 66 10 e6 a9 91 01 91 45 f1 7a 60 a3 1d 5b 4c 30 29
[51] fb 4d f2 2f e1 32 7d 0b ef 55 df e5 81 27 be c4 9c 55 ac a9 69 03 d7 e4 23
[76] a1 a6 33 6f 93 15 d4 00 95 ac 32 e4 ca 1c 17 0f
To read a DER key use read_key
or
read_pubkey
with der = TRUE
.
[256-bit ecdsa public key]
md5: 48780e4dc76e0f69653681c020c36b3f
sha256: 32962d36728af3baf83d16d32fda643697559e96c5964b5f21d457d0cc1ede42
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEub+fCNtsZhDmqZEBkUXxemCjHVtM
MCn7TfIv4TJ9C+9V3+WBJ77EnFWsqWkD1+QjoaYzb5MV1ACVrDLkyhwXDw==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrQBya+eX6TcZN/bh
uwvUpyqFKTD0PlvS7YJO/BpDIM2hRANCAAS5v58I22xmEOapkQGRRfF6YKMdW0ww
KftN8i/hMn0L71Xf5YEnvsScVaypaQPX5COhpjNvkxXUAJWsMuTKHBcP
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAinWRQBLs1wIAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQI87na3WBE0sQEgZDBTpxWgmeqJwcV
vekxyyE7FAkoMaViTCVZIiukZc3ZR0GxpxHoHLTpvUxD75V2cHQl/5PQNrNXUCKK
PX7eDh+fkjZuiA9BAlD3XZXBUMWb6FDQ/Sp++MtXMmGAIADeAQxy+BASVkQXs+J4
s7SdmmXdYaA0JgMC5Y2kk/TsTzmn94asZJ+mQMdJMAdkzJ4l+Rk=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLm/nwjbbGYQ5qmRAZFF8Xpgox1bTDAp+03yL+EyfQvvVd/lgSe+xJxVrKlpA9fkI6GmM2+TFdQAlawy5MocFw8="
The read_pubkey
function will automatically detect if a
file contains a PEM
or SSH
key.
[256-bit ecdsa public key]
md5: 48780e4dc76e0f69653681c020c36b3f
sha256: 32962d36728af3baf83d16d32fda643697559e96c5964b5f21d457d0cc1ede42
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": "ub-fCNtsZhDmqZEBkUXxemCjHVtMMCn7TfIv4TJ9C-8",
"y": "Vd_lgSe-xJxVrKlpA9fkI6GmM2-TFdQAlawy5MocFw8"
}
Keys from jose
and openssl
are the
same.
[1] TRUE
[256-bit ecdsa public key]
md5: 48780e4dc76e0f69653681c020c36b3f
sha256: 32962d36728af3baf83d16d32fda643697559e96c5964b5f21d457d0cc1ede42