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 31 a5 80 d7 c9 44 8e 36 fc dd 3b 44 71 c7 66 52 ce 8e de 02 65 15 71
[51] 47 35 1e 0a 19 80 34 34 a4 b3 b6 20 7b 10 15 8e b0 8c d8 79 5f e4 ca 38 dd
[76] c7 1a ef 88 62 f8 b9 d7 68 ca dd c3 ca 24 f7 5d
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: 67e90fa7ba9933a1968083dde6f35d9c
sha256: a78ceb122f0e6fe77987d06300928d2cd96771808da1274dc47d10f7f1d32c5d
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEMaWA18lEjjb83TtEccdmUs6O3gJl
FXFHNR4KGYA0NKSztiB7EBWOsIzYeV/kyjjdxxrviGL4uddoyt3DyiT3XQ==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgh5qEDGT82bjze8ZV
rU/aqnSjalJWn9wGx1EhajA0t9ShRANCAAQxpYDXyUSONvzdO0Rxx2ZSzo7eAmUV
cUc1HgoZgDQ0pLO2IHsQFY6wjNh5X+TKON3HGu+IYvi512jK3cPKJPdd
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAgUEpKSET43QgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQINRNNTydwfIsEgZBfJ7zgOHRjY6Tr
+u2oYGGjxfhqhWzdIOBgU95wcgI9EUjxEsy5uYhC+NfAb8u28LkC/doZR1WYjaMl
ZjbMpjfURs625z4mXZbRhA34v71gpEHQanunI5irD32NGqTf31mbRjrG0mhncIH9
tgB7wugXR2XiF+/Q+gZlfRdLCGINl9bgDaQBZyHNFjgrpHhr31Q=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDGlgNfJRI42/N07RHHHZlLOjt4CZRVxRzUeChmANDSks7YgexAVjrCM2Hlf5Mo43cca74hi+LnXaMrdw8ok910="
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: 67e90fa7ba9933a1968083dde6f35d9c
sha256: a78ceb122f0e6fe77987d06300928d2cd96771808da1274dc47d10f7f1d32c5d
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": "MaWA18lEjjb83TtEccdmUs6O3gJlFXFHNR4KGYA0NKQ",
"y": "s7YgexAVjrCM2Hlf5Mo43cca74hi-LnXaMrdw8ok910"
}
Keys from jose
and openssl
are the
same.
[1] TRUE
print(mykey)
[256-bit ecdsa public key]
md5: 67e90fa7ba9933a1968083dde6f35d9c
sha256: a78ceb122f0e6fe77987d06300928d2cd96771808da1274dc47d10f7f1d32c5d