In this article, I want to explain some file formats used to hold cryptographic keys and certificates. It is not meant to be a complete list of file formats, but just the first article of a miniseries about JWT. Hence, this article is structured to highlight the steps needed to create the keys of an asymmetric private/public key pair.
.pem
The File Format
“.pem” stands for “privacy enhanced mail” and dates back to the 1990s. It was created to format various (binary) data, including keys or certificates. To be able to send the binary data via mail, pem is Base64 encoded. Each line of content (between the first and last line) is 64 characters long, except for the last one which may be shorter.
Create a private key as .pem
The first step in working with asymmetric key pairs is to generate a private key. The example below uses a key length of only 512 bits for brevity. For production use, this should be at least 2048 bits.
This generates the following RSA private key of 512 bits (your own generated key differs from this, of course):
As stated above, .pem can hold various contents, for example PKCS, which is a set of standards for public key cryptography.
A .pem with the first line “BEGIN RSA PRIVATE KEY” is formatted in the “SSLeay format” and holds PKCS1, as opposed to “BEGIN PRIVATE KEY” indicating PKCS8 (see this SO).
PKCS1 is a format specifically for the RSA algorithm, hence the specific “RSA” in the first line. PKCS8 is a format for various algorithms.
The PKCS1 format can be converted to the PKCS8 format like this:
This generates the following content with the same private key, but in PKCS8 format:
.pub
The File Format
.pub files simply contain public keys of an asymmetric key pair.
Extract Public Key and Save it in .pub File
When using an asymmetrical encryption, the public key can be extracted from the formerly created .pem file like this:
This is the extracted key:
.csr
The File Format
A file ending with .csr is a Certificate Signing Request. After creating a private key as shown above, a signing request file can be generated to enable a certificate authority (CA) to sign the private key. To do that, the csr includes organizational data like the country, state and locality as well as the public key to be signed. The CA validates the request. After approving the request, the CA creates a new public certificate, signed by the private key of the CA, that signs the public key of the requestor.
Creating a certificate signing request
A certificate signing request (CSR) can be created from a pem file:
.crt
The File Format
A file ending with .crt is a certificate. From Stack Exchange: “A certificate contains a public key. The certificate, in addition to containing the public key, contains additional information such as issuer, what the certificate is supposed to be used for, and other types of metadata. Typically, a certificate is itself signed by a certificate authority (CA) using CA’s private key. This verifies the authenticity of the certificate.”
Creating a Certificate
A certificate is created from the certificate signing request:
Further Reading
- Here is a nice overview over the mentioned file formats as well as some additonal ones.