Importing and exporting RSA/DSA/EC keys

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.

The DER format

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 d7 69 75 e1 af 83 56 06 4f 00 ac 2e d8 bb 85 56 e6 44 6f 7c 99 06 9a
[51] 0f 68 e2 a0 0a 2a 3b 0b b7 f8 d4 c1 86 c6 a0 5c 50 b1 83 d4 b2 a4 95 84 2d
[76] 90 68 14 47 d1 bd 02 3a 52 24 34 bf 95 ff 69 53

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: 88253f86d682e25211184d7fb8b45c71
sha256: e83de4df80eb50c8134979d57fbe3940d0e32f3d1df94836261655aa9744936b

Users typically don’t need to worry about the key’s underlying primes, but have a look at key$data if you are curious.

The PEM format

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-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE12l14a+DVgZPAKwu2LuFVuZEb3yZ
BpoPaOKgCio7C7f41MGGxqBcULGD1LKklYQtkGgUR9G9AjpSJDS/lf9pUw==
-----END PUBLIC KEY-----
cat(write_pem(key, password = NULL))
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgHpBzy07eznm0O7ko
ST/KGr9jRn/ZNNR8BHZpedZTO16hRANCAATXaXXhr4NWBk8ArC7Yu4VW5kRvfJkG
mg9o4qAKKjsLt/jUwYbGoFxQsYPUsqSVhC2QaBRH0b0COlIkNL+V/2lT
-----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-----
MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBAj4kwUiIbHVta4GBHE
lDQCAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAgAWSP9esfk/wSBkOWt
JKJsna+yLMs7zVJDL9h+65Qk1+fG3WBeag3uSWRceFXRfZrZdF8vKMIk5L5J5PxA
ytzgWflkVli82ige7E+gbDafXI78gBoXxJ8aTDf6h+7imkwz+Fqn94/gqrldhP73
HFuxOOL3uzNjouu2e0GjYAuvdoL06HqYcom5YYFPJy9Y5f1JDlH4woZ+2lb+rw==
-----END ENCRYPTED PRIVATE KEY-----

The OpenSSH format

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 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNdpdeGvg1YGTwCsLti7hVbmRG98mQaaD2jioAoqOwu3+NTBhsagXFCxg9SypJWELZBoFEfRvQI6UiQ0v5X/aVM="

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: 88253f86d682e25211184d7fb8b45c71
sha256: e83de4df80eb50c8134979d57fbe3940d0e32f3d1df94836261655aa9744936b

The JSON Web Key (JWK) format

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": "12l14a-DVgZPAKwu2LuFVuZEb3yZBpoPaOKgCio7C7c",
    "y": "-NTBhsagXFCxg9SypJWELZBoFEfRvQI6UiQ0v5X_aVM"
}
 

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: 88253f86d682e25211184d7fb8b45c71
sha256: e83de4df80eb50c8134979d57fbe3940d0e32f3d1df94836261655aa9744936b