README.ASN1 7.5 KB

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