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 d2 48 22 69 b2 99 d5 35 e7 68 94 ac 7e d1 d0 3c 49 c1 78 45 54 68 18
[51] 9d 06 14 f3 8d df c7 f4 5c 43 5e b0 17 fe b1 13 c2 78 f8 83 6a 5c 5f a7 bb
[76] ac 91 57 46 f5 49 ed 74 20 92 03 58 b4 4b 7d f1
To read a DER key use read_key
or
read_pubkey
with der = TRUE
.
[256-bit ecdsa public key]
md5: 7934607821b3502710136ee6f91fc0e8
sha256: de1c3d0a180daeb1c212c210276f0217b29e66b5902ab348fe207e057682ce50
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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE0kgiabKZ1TXnaJSsftHQPEnBeEVU
aBidBhTzjd/H9FxDXrAX/rETwnj4g2pcX6e7rJFXRvVJ7XQgkgNYtEt98Q==
-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgHWUQxjSFXkVuy1qg
oI9OGg/dLL+mtn6l6BNTUKdTvlKhRANCAATSSCJpspnVNedolKx+0dA8ScF4RVRo
GJ0GFPON38f0XENesBf+sRPCePiDalxfp7uskVdG9UntdCCSA1i0S33x
-----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-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAgsZJbgKnIT+wICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIKSWIZtgFUb4EgZB7zNd9SS6qRKxn
36bLQuJavFc/GAGq4R4/9RbnnMqSMmcecRBhoKrAbOMoXifKQ00BpivRl3N+AqNc
D+nwezMfJn9vSDuLOgDWAiKsi+XuvLp2dTt+55d311EhSDZvKYC1hqz1HcHILV14
P2InzVwKfTK+glpDMynM2HtFxXkmKWRZE3sHhIpA+xXBph+yQrc=
-----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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNJIImmymdU152iUrH7R0DxJwXhFVGgYnQYU843fx/RcQ16wF/6xE8J4+INqXF+nu6yRV0b1Se10IJIDWLRLffE="
The read_pubkey
function will automatically detect if a
file contains a PEM
or SSH
key.
[256-bit ecdsa public key]
md5: 7934607821b3502710136ee6f91fc0e8
sha256: de1c3d0a180daeb1c212c210276f0217b29e66b5902ab348fe207e057682ce50
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": "0kgiabKZ1TXnaJSsftHQPEnBeEVUaBidBhTzjd_H9Fw",
"y": "Q16wF_6xE8J4-INqXF-nu6yRV0b1Se10IJIDWLRLffE"
}
Keys from jose
and openssl
are the
same.
[1] TRUE
[256-bit ecdsa public key]
md5: 7934607821b3502710136ee6f91fc0e8
sha256: de1c3d0a180daeb1c212c210276f0217b29e66b5902ab348fe207e057682ce50