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 3b ac 36 1e 0d 0d af 65 62 78 0c 92 9d 7b 03 71 f9 82 16 da 19 cc 77
[51] d2 b7 c2 a4 41 bd 63 72 10 e3 ab 2e 8f c4 4a 16 79 ac f9 cb 71 b4 a7 2c 21
[76] c6 3d af 15 cd 61 da cf 20 6f 51 e5 88 6d 0e c9
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: a4e96e7a674c99c5ec304e610e508c30
sha256: 8073b47463af1b4221aceb675ec513aa90f1775d9eabd6af3d0a02bd941d8e55
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEO6w2Hg0Nr2VieAySnXsDcfmCFtoZ
zHfSt8KkQb1jchDjqy6PxEoWeaz5y3G0pywhxj2vFc1h2s8gb1HliG0OyQ==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg1KRJo5ZRZEfZqMvO
hOcSdKTw2KobyoTV6PdL2GL+I9mhRANCAAQ7rDYeDQ2vZWJ4DJKdewNx+YIW2hnM
d9K3wqRBvWNyEOOrLo/EShZ5rPnLcbSnLCHGPa8VzWHazyBvUeWIbQ7J
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAjJs5Mvw3CxPAICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQI3aLNafc+TmQEgZD4m84XkfLTWbW0
4bqA1j8n+/aZwA0PhEIVasG80JNUcWx0MpU22YiobO+YXkzPXWqIaSGwpVF2vwOV
O0BJMrvCY6WKguDdE43NFAEFJn+0SX4dW0elBAczl7vYmZKee7CyNIh+oMOI0VtR
X+NuX1omvv9hR+2iBOhMhqz4MYsDYrLeTdiLjc9uWW8GvNVdkPw=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDusNh4NDa9lYngMkp17A3H5ghbaGcx30rfCpEG9Y3IQ46suj8RKFnms+ctxtKcsIcY9rxXNYdrPIG9R5YhtDsk="
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: a4e96e7a674c99c5ec304e610e508c30
sha256: 8073b47463af1b4221aceb675ec513aa90f1775d9eabd6af3d0a02bd941d8e55
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": "O6w2Hg0Nr2VieAySnXsDcfmCFtoZzHfSt8KkQb1jchA",
"y": "46suj8RKFnms-ctxtKcsIcY9rxXNYdrPIG9R5YhtDsk"
}
Keys from jose and openssl are the
same.
[1] TRUE
print(mykey)[256-bit ecdsa public key]
md5: a4e96e7a674c99c5ec304e610e508c30
sha256: 8073b47463af1b4221aceb675ec513aa90f1775d9eabd6af3d0a02bd941d8e55