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.
key <- ec_keygen()
pubkey <- key$pubkey
bin <- write_der(pubkey)
print(bin)
[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 43 2e 33 93 87 3a 48 ef 6d 4b e4 93 89 c9 da 58 60 e8 38 68 8e ec 18
[51] fe 82 79 05 f6 01 db bb 67 71 b9 78 4b 22 de 14 56 60 d8 4d 91 9a 7c 21 2f
[76] 59 37 23 f8 fa 55 4f 4c f2 6d 8a 84 29 27 a9 3a
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: 9bfaeb843b5d9b6f1cdb536420dd9d7d
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.
cat(write_pem(pubkey))
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEQy4zk4c6SO9tS+STicnaWGDoOGiO
7Bj+gnkF9gHbu2dxuXhLIt4UVmDYTZGafCEvWTcj+PpVT0zybYqEKSepOg==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgsHCpNh0FQLmcqJ55
z7SXSLsRvUNQ+1IHjbgVUt1whsahRANCAARDLjOThzpI721L5JOJydpYYOg4aI7s
GP6CeQX2Adu7Z3G5eEsi3hRWYNhNkZp8IS9ZNyP4+lVPTPJtioQpJ6k6
-----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.
cat(write_pem(key, password = "supersecret"))
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIHjME4GCSqGSIb3DQEFDTBBMCkGCSqGSIb3DQEFDDAcBAibNL5w3cWdrgICCAAw
DAYIKoZIhvcNAgkFADAUBggqhkiG9w0DBwQIPq9O8i7HTAQEgZCHp4z/Rut4jrgs
zdJ2i80ld6PeSSpXkn40ted9Ye5R5Ia0u8zAwhIzEvs0G0RKdqIEFNpcFzbIpOGr
YkOx1qcN0Asrd+AsxYYqVxDEIKiTRanOS8eUyZwDiRLBWbJCRi0HcSoXiXrs7ODL
/Uwklwaf0LiOlE4lW3KRY/Rgcvh6zQdiT4xlTvw2lhCbQ9vd07c=
-----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.
str <- write_ssh(pubkey)
print(str)
[1] "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEMuM5OHOkjvbUvkk4nJ2lhg6DhojuwY/oJ5BfYB27tncbl4SyLeFFZg2E2RmnwhL1k3I/j6VU9M8m2KhCknqTo="
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: 9bfaeb843b5d9b6f1cdb536420dd9d7d
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.
library(jose)
json <- write_jwk(pubkey)
jsonlite::prettify(json)
{
"kty": "EC",
"crv": "P-256",
"x": "Qy4zk4c6SO9tS-STicnaWGDoOGiO7Bj-gnkF9gHbu2c",
"y": "cbl4SyLeFFZg2E2RmnwhL1k3I_j6VU9M8m2KhCknqTo"
}
Keys from jose
and openssl
are the same.
mykey <- read_jwk(json)
identical(mykey, pubkey)
[1] TRUE
print(mykey)
[256-bit ecdsa public key]
md5: 9bfaeb843b5d9b6f1cdb536420dd9d7d