Correction
[quick-der] / USING.MD
1 # Quick DER parsing support
2
3 > *Quick DER parsing aims to get you started with the parsing of DER (and most
4 > BER) encoded ASN.1 data really quickly.  It also aims to makes quick parsers,
5 > by using shared data structures whenever possible.*
6
7 **Note: This is a preview of features to come; for now, it is no promise, but merely a rough sketch of what is to come.**
8
9 ## Outline of using this library
10
11 Working with the quick DER library is really quick to learn.
12
13 ### Preparing your build environment
14
15 The first thing you do, is parse an ASN.1 specification that you may have gotten
16 from any source -- for example, from an RFC.  You map it to a header file and a
17 parser specification file:
18
19     qder_from_asn1 myspec.asn1 myderparser.c myderparser.h
20
21 Your source code dealing with DER should read the entire block, and pass it to
22 the DER parser.  Initially, it would include:
23
24     #include <der/qparse.h>
25     #include "myderparser.h"
26
27 and builing should include:
28
29     gcc -o myparser.o myparser.c
30     gcc ... myderparser.o -lqder
31
32 You may be lucky, and find your favourite spec precompiled.  In that case, you
33 can limit yourself to things like:
34
35     #define RFC5280_PREFIX pkix_
36     #include <der/qparse.h>
37     #include <der/rfc5280.h>
38
39 and link with simply:
40
41     gcc ... -lqder
42
43
44 ### Parsing DER structures
45
46 Before you get to parse DER-encoded structures that match the ASN.1 syntax,
47 you should read the entire data into memory.  The parser output will not
48 clone bits and pieces of data, but instead point into it with cursors; these
49 are little structures with a pointer and a length.  Note that this means that
50 strings are not NUL-terminated; printing them may be different than what you
51 are accustomed to:
52
53     printf ("%s\n", derelem->ptr);                  // Unbounded string
54     printf ("%.*s\n", derelem->len, derelem->ptr);  // Perfect printing
55
56 Now, to invoke the parser, you setup a cursor describing the entire content,
57
58    dercursor_t thelot;
59    thelot.derptr = ...pointer-to-data...;
60    thelot.derlen = ...length-of-data...;
61
62 then you invoke the parser, providing it with storage space and the
63 precompiled structure to follow while parsing:
64
65    struct pkix_Certificate crt;
66    int prsok = der_unpack (&thelot, asn1_pkix_Certificate, &crt, 1);
67
68 This will parse the DER-encoded data in `thelot` and store the various fields
69 in `crt`, so it becomes available as individual cursor structures such as
70 `crt.tbsCertificate.validity.notAfter`.  This follows the structure of the
71 ASN.1 syntax, and uses field names defined in it, to gradually move into
72 the structure.  The header file defines those names as part of the
73 `asn1_pkix_Certificate`.
74
75 Something else that can now be done, is switch behaviour based on the the
76 various fields that contain an `OBJECT IDENTIFIER` for that purposes.  These
77 can usually be treated as binary settings to be compared as binaries.  The
78 `dercmp()` utility does this by looking at the length as well as binary
79 contents of such fields, as in
80
81     if (dercmp (&crt.signatureAlgorith.algorithm, RSA_WITH_SHA1) == 0) {
82        ...the OIDs matched...
83     } else ...other cases...
84
85
86 ## Iterating over repeating structures
87
88 Many structures in ASN.1 are variable in the sizes of primitive data types, but
89 have a fixed composition structure.  And `OPTIONAL` parts can be parsed and their
90 respective structure fields set to NULL when they are absent.  This also happens
91 to values setup with a `DEFAULT` value in ASN.1 (note that their default value
92 is not filled in by the parser).
93
94 But some structures are not parsed immediately, because they might have a
95 repeated structure, and thus won't fit into a structure; not if the assumption
96 is that dynamic allocation should not be done by the parser.  For such
97 repeating structure, there are two options, namely iterating over their
98 contents or allocating memory and having them filled by the parser.
99
100 To iterate over a repeated structure, simply use the two routines setup for
101 that in quick DER, as in:
102
103     if (der_iterate_first (&crt.tbsCertificate.extensions.packed, &iter)) do {
104        ...treat extension pointed to by iter...
105     } while (der_iterate_next (&crt.tbsCertificate.extensions.packed, &iter));
106
107 This requires no dynamic allocation, and simply handles each of the extensions
108 in a certificate one by one.
109
110 ## Allocating space for a repeating structure
111
112 The structures that repeat are limited to the ASN.1 constructs
113 `SEQUENCE OF` and `SET OF`.  When these occur, the parser will not unfold
114 the contained structure, but simply store the whole structure.  We will
115 refer to that as "packed" representation, meaning the binary DER format.
116
117 It is possible to replace packed notation by unpacked, by assigning to it
118 an array of suitable size to contain the required number of elements,
119 and then unfold the repeated structure into it:
120
121     size_t count = der_countelements (&crt.tbsCertificate.extensions.packed);
122     pkix_Extensions *exts = calloc (count, sizeof (pkcix_Extensions));
123     if (exts === NULL) {
124        ...handle error...
125     }
126     prsok = der_unpack (&crt.tbsCertificate.extensions.packed, asn1_pkix_Extension, exts, count);
127
128 This will unpack `count` times the structure described by `asn1_pkix_Extension` and place the output in `count` structures in the array `exts`; note that the earlier
129 call the `der_unpack` had a parameter `1` in the position of `count`.
130
131 When successful, the `der_unpack()` routine replaces the `extensions.packed`
132 structure, which is a plain `dercursor_t`, with an unpacked structure
133 `unpacked` which has elements `derray` pointing to an array of cursors and
134 an element `dercnt` with the number of cursors in that array.  When this
135 is setup, the `.packed` version of the data is destroyed; the `.packed` and
136 `.unpacked` versions are in fact labels of a union.
137
138 Note that structures such as `crt` may hold a lot of useful naming, but they
139 are just a cleverly constructed overlay form for an array of `dercursor_t` fields,
140 which is exactly how `der_unpack` treats them.  The ASN.1 parsing instructions
141 are matched to the structures so that no data will be sticking out of these
142 array-like structures.
143
144 ## Composing DER output
145
146 The composition of DER output uses the same ASN.1 structural descriptions as
147 the unpacking process.  It is possible to use `.packed` structures, but once
148 they are unpacked it becomes necessary to prepare repeating structures for
149 repackaging.  This uses the `der_prepack()` function:
150
151     int prsok = der_prepack (TODO);
152
153 This sets up a third flavour of the repeated structure, namely `.prepacked`.
154 In this form, the `derlen` value has been set to the eventual length of
155 the to-be-formed DER structure, but the `derray` value still points to the
156 array of `dercursor_t` holding the to-be-filled data.  This `derlen` field
157 can subsequently be used during the future packing process.
158
159 TODO: How to distinguish packed, unpacked and prepacked lengths?  Tag or size bits?
160