Names Are Sequences of Sets
A distinguished name is a SEQUENCE of SETs, each holding an attribute type and a value, and the order of the bytes is the order of the name; the printers of OpenSSL put spaces in different places.
The question
A certificate names its subject and its issuer, and the readable text, CN=www.example.test, is not what is in the bytes. How is a name written in DER, in which order do its parts come, and why do two programs print the same name with different spaces?
This lesson reads the names of the test certificates of this book. They are test material, made for the lessons, with 512-bit RSA keys, a toy size far too small to use.
Work it by hand
The subject of the leaf certificate is 30 1b 31 19 30 17 06 03 55 04 03 0c 10 followed by 16 text bytes. Read from the left: 30 1b is a SEQUENCE of 27 bytes, the name. 31 19 is a SET of 25 bytes, one relative distinguished name. 30 17 is a SEQUENCE of 23 bytes: an attribute with its type and value. 06 03 55 04 03 is an object identifier of 3 bytes; asn1parse prints 55 04 03 as commonName.
0c 10 is a UTF8String of 16 bytes: 77 77 77 2e 65 78 61 6d 70 6c 65 2e 74 65 73 74, the text www.example.test.
So the name is CN=www.example.test. A name with several parts repeats the SET once for each, in the order of the bytes: the issuer of the leaf certificate has an organization first and then a common name.
Run it
The demo is one command (line 3): x509 -in in/cert.txt -noout -subject -issuer -serial. It prints the three lines subject, issuer and serial of the leaf: subject=CN=www.example.test, issuer=O=Example Test Lab, CN=Example Test Intermediate CA and serial=03.
You can change the flags or the certificate, any PEM certificate of at most 2048 characters. Another certificate gives other names; a text that is not a certificate stops the command as in the first lesson.

Public test material, generated for this lesson. Never use it for anything real.
# Prints the subject, the issuer and the serial number of the certificate.
# Check runs it with other certificates in in/cert.txt.
openssl x509 -in in/cert.txt -noout -subject -issuer -serial
{
"answer": "subject=CN=www.example.test\nissuer=O=Example Test Lab, CN=Example Test Intermediate CA\nserial=03"
}Your turn
The program below should read a name given as DER hex in name and return the text of the attributes it knows, in the order of the bytes, each as the short name, an equals sign and the value. It knows CN, O, C and OU. Judge it, and find the candidates that are right for every input. Check runs each candidate on the data shown and on six others. Two cases are constructed names, not names of certificates.

Public test material, generated for this lesson. Never use it for anything real.
let top = 2 + bits_of(hex_u8(name, 1), 0, 7) * (hex_u8(name, 1) / 128);
let at = top;
let n = hex_len(name);
let answer = str_list();
for (k in range(0, 8)) {
if (at < n) {
let oid = hex_slice(name, at + 6, 3);
let size = hex_u8(name, at + 10);
let text = str_hex_decode(hex_slice(name, at + 11, size));
let tag = "";
if (oid == "550403") { tag = "CN"; }
if (oid == "55040a") { tag = "O"; }
if (oid == "550406") { tag = "C"; }
if (tag != "") { answer.add(tag + "=" + text); }
at = at + 2 + hex_u8(name, at + 1);
}
}
{
"answer": [
"C=ZZ",
"O=Example Test Lab",
"CN=names.example.test"
]
}name is the DER hex of a Name: a SEQUENCE of SETs, each with one attribute (object identifier 3 bytes, then a text value shorter than 128 bytes). Return the list of texts, one for each attribute of type commonName (55 04 03), organizationName (55 04 0a), countryName (55 04 06) or organizationalUnitName (55 04 0b), in the order of the bytes, written CN=text, O=text, C=text and OU=text. Skip attributes of other types.
Read the result carefully
The printers of a name disagree about spaces, measured on the leaf certificate: the command above prints the issuer as O=Example Test Lab, CN=Example Test Intermediate CA, with a space after the comma, and the engine's own Cert.issuer() prints O=Example Test Lab,CN=Example Test Intermediate CA without one. This exercise's answer has no commas at all: one text for each attribute.
The program reads names whose parts are shorter than 128 bytes, one attribute to a SET, type values of three bytes, and ASCII text; other attributes are skipped. It does not decode a name with several attributes in one SET or other string types. The values 27, 25, 23 and 16 belong to the leaf's subject.
Recap
- A name is a SEQUENCE of SETs; each SET holds a SEQUENCE of an object identifier and a value.
- The object identifiers 55 04 03, 55 04 0a, 55 04 06 and 55 04 0b are CN, O, C and OU; each was checked against
asn1parsefor these certificates. - The order of the parts is the order of the bytes.
- The spacing of a printed name depends on the program that prints it.