PEM Wraps DER
A PEM file is the DER bytes of a certificate in base64, 64 characters to a line, between a BEGIN and an END line; the number of bytes is not the number of characters times three quarters.
The question
A certificate usually travels as a text file that starts with -----BEGIN CERTIFICATE-----. What is in the lines between the BEGIN line and the END line, and how many bytes of certificate do they stand for? The rest of this book reads the bytes, so the first step is to get from the file to the bytes.
The lines are the certificate's DER encoding written in base64. This lesson takes a test certificate apart that way. It is test material with a 512-bit RSA key, a toy size far too small to use.
Work it by hand
RFC 7468, section 2, says a generator wraps the base64 text so that each line has exactly 64 characters except the last. The leaf test certificate has 668 base64 characters: ten full lines and a last line of 28.
Base64 writes 3 bytes as 4 characters of 6 bits each. The alphabet is in RFC 4648, section 4, Table 1: A to Z are 0 to 25, a to z are 26 to 51, 0 to 9 are 52 to 61. The first four characters of the first line are MIIB. M is 12, I is 8 and B is 1, so the bits are 001100 001000 001000 000001, which regroup as 00110000 10000010 00000001: the bytes 30 82 01.
The next four characters, 8DCC, give f0 30 82. The certificate starts 30 82 01 f0 30 82. Every four characters are three bytes, so 668 characters would be 501 bytes; the one = at the end of the text means one byte of padding, and the DER has 500 bytes.
Run it
The demo is one command (line 3): x509 -in in/cert.txt -outform DER -out out.der reads the PEM text and writes the DER to the file out.der. The picture shows the first lines of the text and the first 16 bytes of the file, and the number of bytes written.
You can put another test certificate in in/cert.txt, any PEM certificate of at most 2048 characters. Text that is not a certificate stops the command: measured on this runtime, OpenSSL says Could not find or decode certificate from in/cert.txt and writes no file.

Public test material, generated for this lesson. Never use it for anything real.
# Writes the DER bytes of the certificate to out.der.
# Check runs it with other certificates in in/cert.txt.
openssl x509 -in in/cert.txt -outform DER -out out.der
{
"answer": "308201f03082019aa003020102020103300d06092a864886f70d01010b0500304231193017060355040a0c104578616d706c652054657374204c61623125302306035504030c1c4578616d706c65205465737420496e7465726d656469617465204341301e170d3236303130313030303030305a170d3336303130313030303030305a301b3119301706035504030c107777772e6578616d706c652e74657374305c300d06092a864886f70d0101010500034b003048024100ba5ca7906dfc2cb549cf8e7d35d727cf313a057ad7186b088dbb2fdc17ff855208eaeff4b6ee2ff00689be340c4a6b18371d5d1c41c690f5c153f0cdcfaccf030203010001a381a130819e300c0603551d130101ff04023000300e0603551d0f0101ff04040302078030130603551d25040c300a06082b0601050507030130290603551d110422302082107777772e6578616d706c652e74657374820c6578616d706c652e74657374301d0603551d0e04160414bc10b24aecaed03a1cbb88b9a8e5f7939d1e4e25301f0603551d2304183016801462d8f166c49655c2d84aae71641bcf0f9e8b2894300d06092a864886f70d01010b0500034100423acc4e1892b8c577f9d97c048c75178d84d731940d40ebb0be631272f714bba42fd8489cdd5758f466ea3f7f790a14db3ecbfe3b08b2f45fa018023ad02dfd",
"file": "out.der",
"bytes": 500
}Your turn
The program below reads a PEM block from the text block. The block may have lines of text before the BEGIN line. It is meant to return two texts: the number of DER bytes that the base64 lines decode to, and the first four bytes of that DER as eight lowercase hex characters. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on seven others.
![The first 2 lines of a PEM block of the test certificate wild: the BEGIN line and the first base64 line of 64 characters, cut at 56, and the list of two texts that the shown program returns, ["48","30820189"].](../assets/nc/pem-wraps-der--exercise.png)
Public test material, generated for this lesson. Never use it for anything real.
let lines = str_split(block, "\n");
let body = "";
let inside = false;
for (i in range(0, lines.len())) {
let line = str_trim(lines.at(i));
if (str_starts_with(line, "-----END")) { inside = false; }
if (inside && body == "") { body = line; }
if (str_starts_with(line, "-----BEGIN")) { inside = true; }
}
let der = str_base64_decode(body);
let answer = str_list();
answer.add(str(str_len(der)));
answer.add(str_hex_encode(str_substr(der, 0, 4)));
{
"answer": [
"48",
"30820189"
]
}block is a text with one PEM block, possibly with lines of text before the BEGIN line. The body is the lines between the BEGIN line and the END line. Return a list of two texts: the number of bytes that the base64 body decodes to, written in decimal, and the first four decoded bytes as eight lowercase hex characters.
Read the result carefully
The page's text boxes keep printable characters and line feeds only, so a block with CR LF line ends cannot be typed here. The programs trim every line, which is what they would need to do with such a file; this is stated here, not run. RFC 7468 says that parsers MAY accept other line sizes, and these programs do.
LMCode's str_base64_decode is strict, measured: a text that still has a newline, a text that still has the BEGIN line and a text whose length is not a multiple of four each stop the run with str_base64_decode: invalid encoding. The command base64 -d is not used here. The lesson shows what PEM is, and does not check that bytes are a valid certificate.
Recap
- A PEM certificate is the DER bytes in base64 between a BEGIN and an END line.
- Generators wrap the lines at 64 characters; the last line is shorter (RFC 7468, section 2).
- Four characters are three bytes, and each
=removes one byte, so the length is not characters times three quarters. - The leaf certificate of this book is 500 bytes and starts 30 82 01 f0: test material, toy size.