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 8f 84 46 c2 85 82 98 9f 51 a9 09 58 58 56 47 fd 04 02 d6 c7 44 69 de
[51] ab 67 a7 e4 f3 6a d7 ec ee 91 68 94 39 50 8b 0a 2f e3 11 dd 70 2c 54 ad 6b
[76] 99 37 78 98 a5 58 8d 50 17 56 f6 56 ed 21 92 b4
To read a DER key use read_key or
read_pubkey with der = TRUE.
[256-bit ecdsa public key]
md5: cdc80c8297467bee9180e719465809d2
sha256: 8ebbdc20f236cb0099ec3671b8ef7dd0d551ef7c2f773bd1e966c9771b7a45c4
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEj4RGwoWCmJ9RqQlYWFZH/QQC1sdE
ad6rZ6fk82rX7O6RaJQ5UIsKL+MR3XAsVK1rmTd4mKVYjVAXVvZW7SGStA==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgcJSYjd7g6oddC6Bo
ViHUsZazZucg16KeQd+Qn1tjyjWhRANCAASPhEbChYKYn1GpCVhYVkf9BALWx0Rp
3qtnp+Tzatfs7pFolDlQiwov4xHdcCxUrWuZN3iYpViNUBdW9lbtIZK0
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBA5h6lIBXJP7Cd5smIR
zlmVAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAhpT6kioanklgSBkFC4
TMWNAivjtD0Fv/gxKfKiCEZkx9SESfkmWyXvihs6SLzWjlW4Doo1oIZuzysPN9C8
e2oIORqRsyOzNI4doiQDnCXpukCFj/9Ban8DozCeh2r/6eiEOEBy7tKNyeRfWCOv
X/DLkJh07+NzDRUhR+KVVDpCuRujWddmMwXCBAQh7DyCTmjHDUapTjHQ1xKPMg==
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBI+ERsKFgpifUakJWFhWR/0EAtbHRGneq2en5PNq1+zukWiUOVCLCi/jEd1wLFSta5k3eJilWI1QF1b2Vu0hkrQ="
The read_pubkey function will automatically detect if a
file contains a PEM or SSH key.
[256-bit ecdsa public key]
md5: cdc80c8297467bee9180e719465809d2
sha256: 8ebbdc20f236cb0099ec3671b8ef7dd0d551ef7c2f773bd1e966c9771b7a45c4
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": "j4RGwoWCmJ9RqQlYWFZH_QQC1sdEad6rZ6fk82rX7O4",
"y": "kWiUOVCLCi_jEd1wLFSta5k3eJilWI1QF1b2Vu0hkrQ"
}
Keys from jose and openssl are the
same.
[1] TRUE
[256-bit ecdsa public key]
md5: cdc80c8297467bee9180e719465809d2
sha256: 8ebbdc20f236cb0099ec3671b8ef7dd0d551ef7c2f773bd1e966c9771b7a45c4