Merge from main trunk: lets see if this works ;-)
[OpenSSL.git] / README.ASN1
blob8f41eb60b8e059ed8152fab1204a56d6e2c9e483
2 OpenSSL ASN1 Revision
3 =====================
5 This branch will ultimately contain the revised OpenSSL ASN1 code.
7 For some time it is likely to be unstable, non-portable and may not even
8 compile at all: you have been warned! 
10 Anyone who generally avoids OpenSSL ASN1 should steer well clear of this
11 for now.
13 Current OpenSSL ASN1 problems
14 =============================
16 OK why does the OpenSSL ASN1 code need revising in the first place? Well
17 there are lots of reasons some of which are included below...
19 1. The code is difficult to read and write. For every single ASN1 structure
20 (e.g. SEQUENCE) four functions need to be written for new, free, encode and
21 decode operations. This is a very painful and error prone operation. Very few
22 people have ever written any OpenSSL ASN1 and those that have usually wish
23 they hadn't.
25 2. Partly because of 1. the code is bloated and takes up a disproportionate
26 amount of space. The SEQUENCE encoder is particularly bad: it essentially
27 contains two copies of the same operation, one to compute the SEQUENCE length
28 and the other to encode it.
30 3. The code is memory based: that is it expects to be able to read the whole
31 structure from memory. This is fine for small structures but if you have a
32 (say) 1Gb PKCS#7 signedData structure it isn't such a good idea...
34 4. The code for the ASN1 IMPLICIT tag is evil. It is handled by temporarily
35 changing the tag to the expected one, attempting to read it, then changing it
36 back again. This means that decode buffers have to be writable even though they
37 are ultimately unchanged. This gets in the way of constification.
39 5. The handling of EXPLICIT isn't much better. It adds a chunk of code into 
40 the decoder and encoder for every EXPLICIT tag.
42 6. APPLICATION and PRIVATE tags aren't even supported at all.
44 7. Even IMPLICIT isn't complete: there is no support for implicitly tagged
45 types that are not OPTIONAL.
47 8. Much of the code assumes that a tag will fit in a single octet. This is
48 only true if the tag is 30 or less (mercifully tags over 30 are rare).
50 9. The ASN1 CHOICE type has to be largely handled manually, there aren't any
51 macros that properly support it.
53 10. Encoders have no concept of OPTIONAL and have no error checking. If the
54 passed structure contains a NULL in a mandatory field it will not be encoded,
55 resulting in an invalid structure.
57 11. It is tricky to add ASN1 encoders and decoders to external applications.
59 Template model
60 ==============
62 One of the major problems with revision is the sheer volume of the ASN1 code.
63 Attempts to change (for example) the IMPLICIT behaviour would result in a
64 modification of *every* single decode function. 
66 I decided to adopt a template based approach. I'm using the term 'template'
67 in a manner similar to SNACC templates: it has nothing to do with C++
68 templates.
70 A template is a description of an ASN1 module as several constant C structures.
71 It describes in a machine readable way exactly how the ASN1 structure should
72 behave. If this template contains enough detail then it is possible to write
73 versions of new, free, encode, decode (and possibly others operations) that
74 operate on templates.
76 Instead of having to write code to handle each operation only a single
77 template needs to be written. If new operations are needed (such as a 'print'
78 operation) only a single new template based function needs to be written 
79 which will then automatically handle all existing templates.
81 Plans for revision
82 ==================
84 The revision will consist of the following steps. Other than the first two
85 these can be handled in any order.
87 o Design and write template new, free, encode and decode operations, initially
88 memory based.
90 o Convert existing ASN1 code to template form.
92 o Convert an existing ASN1 compiler (probably SNACC) to output templates
93 in OpenSSL form.
95 o Add support for BIO based ASN1 encoders and decoders to handle large
96 structures, initially blocking I/O.
98 o Add support for non blocking I/O: this is quite a bit harder than blocking
99 I/O.
101 o Add new ASN1 structures, such as OCSP, CRMF, S/MIME v3 (CMS), attribute
102 certificates etc etc.
104 Description of major changes
105 ============================
107 The BOOLEAN type now takes three values. 0xff is TRUE, 0 is FALSE and -1 is
108 absent. The meaning of absent depends on the context. If for example the
109 boolean type is DEFAULT FALSE (as in the case of the critical flag for
110 certificate extensions) then -1 is FALSE, if DEFAULT TRUE then -1 is TRUE.
111 Usually the value will only ever be read via an API which will hide this from
112 an application.
114 There is an evil bug in the old ASN1 code that mishandles OPTIONAL with
115 SEQUENCE OF or SET OF. These are both implemented as a STACK structure. The
116 old code would omit the structure if the STACK was NULL (which is fine) or if
117 it had zero elements (which is NOT OK). This causes problems because an empty
118 SEQUENCE OF or SET OF will result in an empty STACK when it is decoded but when
119 it is encoded it will be omitted resulting in different encodings. The new code
120 only omits the encoding if the STACK is NULL, if it contains zero elements it
121 is encoded and empty. There is an additional problem though: because an empty
122 STACK was omitted, sometimes the corresponding *_new() function would
123 initialize the STACK to empty so an application could immediately use it, if
124 this is done with the new code (i.e. a NULL) it wont work. Therefore a new
125 STACK should be allocated first. One instance of this is the X509_CRL list of
126 revoked certificates: a helper function X509_CRL_add0_revoked() has been added
127 for this purpose.
129 The X509_ATTRIBUTE structure used to have an element called 'set' which took
130 the value 1 if the attribute value was a SET OF or 0 if it was a single. Due
131 to the behaviour of CHOICE in the new code this has been changed to a field
132 called 'single' which is 0 for a SET OF and 1 for single. The old field has
133 been deleted to deliberately break source compatibility. Since this structure
134 is normally accessed via higher level functions this shouldn't break too much.
136 The X509_REQ_INFO certificate request info structure no longer has a field
137 called 'req_kludge'. This used to be set to 1 if the attributes field was
138 (incorrectly) omitted. You can check to see if the field is omitted now by
139 checking if the attributes field is NULL. Similarly if you need to omit
140 the field then free attributes and set it to NULL.
142 The top level 'detached' field in the PKCS7 structure is no longer set when
143 a PKCS#7 structure is read in. PKCS7_is_detached() should be called instead.
144 The behaviour of PKCS7_get_detached() is unaffected.
146 The values of 'type' in the GENERAL_NAME structure have changed. This is
147 because the old code use the ASN1 initial octet as the selector. The new
148 code uses the index in the ASN1_CHOICE template.
150 The DIST_POINT_NAME structure has changed to be a true CHOICE type.
152 typedef struct DIST_POINT_NAME_st {
153 int type;
154 union {
155         STACK_OF(GENERAL_NAME) *fullname;
156         STACK_OF(X509_NAME_ENTRY) *relativename;
157 } name;
158 } DIST_POINT_NAME;
160 This means that name.fullname or name.relativename should be set
161 and type reflects the option. That is if name.fullname is set then
162 type is 0 and if name.relativename is set type is 1.
164 With the old code using the i2d functions would typically involve:
166 unsigned char *buf, *p;
167 int len;
168 /* Find length of encoding */
169 len = i2d_SOMETHING(x, NULL);
170 /* Allocate buffer */
171 buf = OPENSSL_malloc(len);
172 if(buf == NULL) {
173         /* Malloc error */
175 /* Use temp variable because &p gets updated to point to end of
176  * encoding.
177  */
178 p = buf;
179 i2d_SOMETHING(x, &p);
182 Using the new i2d you can also do:
184 unsigned char *buf = NULL;
185 int len;
186 len = i2d_SOMETHING(x, &buf);
187 if(len < 0) {
188         /* Malloc error */
191 and it will automatically allocate and populate a buffer with the
192 encoding. After this call 'buf' will point to the start of the
193 encoding which is len bytes long.