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 6e 27 2e 60 41 a9 6e 5a 53 40 ec cc 75 45 4e 06 3f c5 3c bb 82 05 75
[51] cf 89 a9 f1 b4 cd f9 57 d1 1d 93 49 6e 0a f2 61 d6 a7 77 33 b2 af 76 7b d5
[76] ce 5e 00 6d e8 40 05 4b 8a ef c3 2e da c5 2c bb
To read a DER key use read_key or
read_pubkey with der = TRUE.
[256-bit ecdsa public key]
md5: bd672d985d598cbe2b789cbc83899e49
sha256: 0daf305aaec3a3fc6649e3b1ed93cda48c53c73bc1532f0093584b429237334a
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEbicuYEGpblpTQOzMdUVOBj/FPLuC
BXXPianxtM35V9Edk0luCvJh1qd3M7KvdnvVzl4AbehABUuK78Mu2sUsuw==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYQNl+HwrUMI9n/HR
HgUOhTt9S7jOvRrcaCH3ecMp8WChRANCAARuJy5gQaluWlNA7Mx1RU4GP8U8u4IF
dc+JqfG0zflX0R2TSW4K8mHWp3czsq92e9XOXgBt6EAFS4rvwy7axSy7
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAhcZn0/e1na0AICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIl4NMVK4MGAwEgZBUnIBn0fWfhTGZ
lnVqynRru+GnWmO9obUA+Zq60MLy2T5kV6SRpP5VurccomHc9MocJg5cmetkHS5v
VJPuRPsk6IhMr1W+5bSr51YkNaoLfHLdazVysmvmpw2Nv02w06wAOMdXPOx5oIUs
YYgNbAxuzYd6d5efTi4/errsWmjOHln0rR2lNLm99iqkbZVFog4=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBG4nLmBBqW5aU0DszHVFTgY/xTy7ggV1z4mp8bTN+VfRHZNJbgryYdandzOyr3Z71c5eAG3oQAVLiu/DLtrFLLs="
The read_pubkey function will automatically detect if a
file contains a PEM or SSH key.
[256-bit ecdsa public key]
md5: bd672d985d598cbe2b789cbc83899e49
sha256: 0daf305aaec3a3fc6649e3b1ed93cda48c53c73bc1532f0093584b429237334a
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": "bicuYEGpblpTQOzMdUVOBj_FPLuCBXXPianxtM35V9E",
"y": "HZNJbgryYdandzOyr3Z71c5eAG3oQAVLiu_DLtrFLLs"
}
Keys from jose and openssl are the
same.
[1] TRUE
[256-bit ecdsa public key]
md5: bd672d985d598cbe2b789cbc83899e49
sha256: 0daf305aaec3a3fc6649e3b1ed93cda48c53c73bc1532f0093584b429237334a