secasn1t.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * Types for encoding/decoding of ASN.1 using BER/DER (Basic/Distinguished
  6. * Encoding Rules).
  7. */
  8. #ifndef _SECASN1T_H_
  9. #define _SECASN1T_H_
  10. #include "utilrename.h"
  11. /*
  12. ** An array of these structures defines a BER/DER encoding for an object.
  13. **
  14. ** The array usually starts with a dummy entry whose kind is SEC_ASN1_SEQUENCE;
  15. ** such an array is terminated with an entry where kind == 0. (An array
  16. ** which consists of a single component does not require a second dummy
  17. ** entry -- the array is only searched as long as previous component(s)
  18. ** instruct it.)
  19. */
  20. typedef struct sec_ASN1Template_struct {
  21. /*
  22. ** Kind of item being decoded/encoded, including tags and modifiers.
  23. */
  24. unsigned long kind;
  25. /*
  26. ** The value is the offset from the base of the structure to the
  27. ** field that holds the value being decoded/encoded.
  28. */
  29. unsigned long offset;
  30. /*
  31. ** When kind suggests it (SEC_ASN1_POINTER, SEC_ASN1_GROUP, SEC_ASN1_INLINE,
  32. ** or a component that is *not* a SEC_ASN1_UNIVERSAL), this points to
  33. ** a sub-template for nested encoding/decoding,
  34. ** OR, iff SEC_ASN1_DYNAMIC is set, then this is a pointer to a pointer
  35. ** to a function which will return the appropriate template when called
  36. ** at runtime. NOTE! that explicit level of indirection, which is
  37. ** necessary because ANSI does not allow you to store a function
  38. ** pointer directly as a "void *" so we must store it separately and
  39. ** dereference it to get at the function pointer itself.
  40. */
  41. const void *sub;
  42. /*
  43. ** In the first element of a template array, the value is the size
  44. ** of the structure to allocate when this template is being referenced
  45. ** by another template via SEC_ASN1_POINTER or SEC_ASN1_GROUP.
  46. ** In all other cases, the value is ignored.
  47. */
  48. unsigned int size;
  49. } SEC_ASN1Template;
  50. /* default size used for allocation of encoding/decoding stuff */
  51. /* XXX what is the best value here? */
  52. #define SEC_ASN1_DEFAULT_ARENA_SIZE (2048)
  53. /*
  54. ** BER/DER values for ASN.1 identifier octets.
  55. */
  56. #define SEC_ASN1_TAG_MASK 0xff
  57. /*
  58. * BER/DER universal type tag numbers.
  59. * The values are defined by the X.208 standard; do not change them!
  60. * NOTE: if you add anything to this list, you must add code to secasn1d.c
  61. * to accept the tag, and probably also to secasn1e.c to encode it.
  62. * XXX It appears some have been added recently without being added to
  63. * the code; so need to go through the list now and double-check them all.
  64. * (Look especially at those added in revision 1.10.)
  65. */
  66. #define SEC_ASN1_TAGNUM_MASK 0x1f
  67. #define SEC_ASN1_BOOLEAN 0x01
  68. #define SEC_ASN1_INTEGER 0x02
  69. #define SEC_ASN1_BIT_STRING 0x03
  70. #define SEC_ASN1_OCTET_STRING 0x04
  71. #define SEC_ASN1_NULL 0x05
  72. #define SEC_ASN1_OBJECT_ID 0x06
  73. #define SEC_ASN1_OBJECT_DESCRIPTOR 0x07
  74. /* External type and instance-of type 0x08 */
  75. #define SEC_ASN1_REAL 0x09
  76. #define SEC_ASN1_ENUMERATED 0x0a
  77. #define SEC_ASN1_EMBEDDED_PDV 0x0b
  78. #define SEC_ASN1_UTF8_STRING 0x0c
  79. /* 0x0d */
  80. /* 0x0e */
  81. /* 0x0f */
  82. #define SEC_ASN1_SEQUENCE 0x10
  83. #define SEC_ASN1_SET 0x11
  84. #define SEC_ASN1_NUMERIC_STRING 0x12
  85. #define SEC_ASN1_PRINTABLE_STRING 0x13
  86. #define SEC_ASN1_T61_STRING 0x14
  87. #define SEC_ASN1_VIDEOTEX_STRING 0x15
  88. #define SEC_ASN1_IA5_STRING 0x16
  89. #define SEC_ASN1_UTC_TIME 0x17
  90. #define SEC_ASN1_GENERALIZED_TIME 0x18
  91. #define SEC_ASN1_GRAPHIC_STRING 0x19
  92. #define SEC_ASN1_VISIBLE_STRING 0x1a
  93. #define SEC_ASN1_GENERAL_STRING 0x1b
  94. #define SEC_ASN1_UNIVERSAL_STRING 0x1c
  95. /* 0x1d */
  96. #define SEC_ASN1_BMP_STRING 0x1e
  97. #define SEC_ASN1_HIGH_TAG_NUMBER 0x1f
  98. #define SEC_ASN1_TELETEX_STRING SEC_ASN1_T61_STRING
  99. /*
  100. ** Modifiers to type tags. These are also specified by a/the
  101. ** standard, and must not be changed.
  102. */
  103. #define SEC_ASN1_METHOD_MASK 0x20
  104. #define SEC_ASN1_PRIMITIVE 0x00
  105. #define SEC_ASN1_CONSTRUCTED 0x20
  106. #define SEC_ASN1_CLASS_MASK 0xc0
  107. #define SEC_ASN1_UNIVERSAL 0x00
  108. #define SEC_ASN1_APPLICATION 0x40
  109. #define SEC_ASN1_CONTEXT_SPECIFIC 0x80
  110. #define SEC_ASN1_PRIVATE 0xc0
  111. /*
  112. ** Our additions, used for templates.
  113. ** These are not defined by any standard; the values are used internally only.
  114. ** Just be careful to keep them out of the low 8 bits.
  115. ** XXX finish comments
  116. */
  117. #define SEC_ASN1_OPTIONAL 0x00100
  118. #define SEC_ASN1_EXPLICIT 0x00200
  119. #define SEC_ASN1_ANY 0x00400
  120. #define SEC_ASN1_INLINE 0x00800
  121. #define SEC_ASN1_POINTER 0x01000
  122. #define SEC_ASN1_GROUP 0x02000 /* with SET or SEQUENCE means \
  123. * SET OF or SEQUENCE OF */
  124. #define SEC_ASN1_DYNAMIC 0x04000 /* subtemplate is found by calling \
  125. * a function at runtime */
  126. #define SEC_ASN1_SKIP 0x08000 /* skip a field; only for decoding */
  127. #define SEC_ASN1_INNER 0x10000 /* with ANY means capture the \
  128. * contents only (not the id, len, \
  129. * or eoc); only for decoding */
  130. #define SEC_ASN1_SAVE 0x20000 /* stash away the encoded bytes first; \
  131. * only for decoding */
  132. #define SEC_ASN1_MAY_STREAM 0x40000 /* field or one of its sub-fields may \
  133. * stream in and so should encode as \
  134. * indefinite-length when streaming \
  135. * has been indicated; only for \
  136. * encoding */
  137. #define SEC_ASN1_SKIP_REST 0x80000 /* skip all following fields; \
  138. only for decoding */
  139. #define SEC_ASN1_CHOICE 0x100000 /* pick one from a template */
  140. #define SEC_ASN1_NO_STREAM 0X200000 /* This entry will not stream \
  141. even if the sub-template says \
  142. streaming is possible. Helps \
  143. to solve ambiguities with potential \
  144. streaming entries that are \
  145. optional */
  146. #define SEC_ASN1_DEBUG_BREAK 0X400000 /* put this in your template and the \
  147. decoder will assert when it \
  148. processes it. Only for use with \
  149. SEC_QuickDERDecodeItem */
  150. /* Shorthand/Aliases */
  151. #define SEC_ASN1_SEQUENCE_OF (SEC_ASN1_GROUP | SEC_ASN1_SEQUENCE)
  152. #define SEC_ASN1_SET_OF (SEC_ASN1_GROUP | SEC_ASN1_SET)
  153. #define SEC_ASN1_ANY_CONTENTS (SEC_ASN1_ANY | SEC_ASN1_INNER)
  154. /* Maximum depth of nested SEQUENCEs and SETs */
  155. #define SEC_ASN1D_MAX_DEPTH 32
  156. /*
  157. ** Function used for SEC_ASN1_DYNAMIC.
  158. ** "arg" is a pointer to the structure being encoded/decoded
  159. ** "enc", when true, means that we are encoding (false means decoding)
  160. */
  161. typedef const SEC_ASN1Template *SEC_ASN1TemplateChooser(void *arg, PRBool enc);
  162. typedef SEC_ASN1TemplateChooser *SEC_ASN1TemplateChooserPtr;
  163. #if defined(_WIN32) || defined(ANDROID)
  164. #define SEC_ASN1_GET(x) NSS_Get_##x(NULL, PR_FALSE)
  165. #define SEC_ASN1_SUB(x) &p_NSS_Get_##x
  166. #define SEC_ASN1_XTRN SEC_ASN1_DYNAMIC
  167. #define SEC_ASN1_MKSUB(x) \
  168. static const SEC_ASN1TemplateChooserPtr p_NSS_Get_##x = &NSS_Get_##x;
  169. #else
  170. #define SEC_ASN1_GET(x) x
  171. #define SEC_ASN1_SUB(x) x
  172. #define SEC_ASN1_XTRN 0
  173. #define SEC_ASN1_MKSUB(x)
  174. #endif
  175. #define SEC_ASN1_CHOOSER_DECLARE(x) \
  176. extern const SEC_ASN1Template *NSS_Get_##x(void *arg, PRBool enc);
  177. #define SEC_ASN1_CHOOSER_IMPLEMENT(x) \
  178. const SEC_ASN1Template *NSS_Get_##x(void *arg, PRBool enc) \
  179. { \
  180. return x; \
  181. }
  182. /*
  183. ** Opaque object used by the decoder to store state.
  184. */
  185. typedef struct sec_DecoderContext_struct SEC_ASN1DecoderContext;
  186. /*
  187. ** Opaque object used by the encoder to store state.
  188. */
  189. typedef struct sec_EncoderContext_struct SEC_ASN1EncoderContext;
  190. /*
  191. * This is used to describe to a filter function the bytes that are
  192. * being passed to it. This is only useful when the filter is an "outer"
  193. * one, meaning it expects to get *all* of the bytes not just the
  194. * contents octets.
  195. */
  196. typedef enum {
  197. SEC_ASN1_Identifier = 0,
  198. SEC_ASN1_Length = 1,
  199. SEC_ASN1_Contents = 2,
  200. SEC_ASN1_EndOfContents = 3
  201. } SEC_ASN1EncodingPart;
  202. /*
  203. * Type of the function pointer used either for decoding or encoding,
  204. * when doing anything "funny" (e.g. manipulating the data stream)
  205. */
  206. typedef void (*SEC_ASN1NotifyProc)(void *arg, PRBool before,
  207. void *dest, int real_depth);
  208. /*
  209. * Type of the function pointer used for grabbing encoded bytes.
  210. * This can be used during either encoding or decoding, as follows...
  211. *
  212. * When decoding, this can be used to filter the encoded bytes as they
  213. * are parsed. This is what you would do if you wanted to process the data
  214. * along the way (like to decrypt it, or to perform a hash on it in order
  215. * to do a signature check later). See SEC_ASN1DecoderSetFilterProc().
  216. * When processing only part of the encoded bytes is desired, you "watch"
  217. * for the field(s) you are interested in with a "notify proc" (see
  218. * SEC_ASN1DecoderSetNotifyProc()) and for even finer granularity (e.g. to
  219. * ignore all by the contents bytes) you pay attention to the "data_kind"
  220. * parameter.
  221. *
  222. * When encoding, this is the specification for the output function which
  223. * will receive the bytes as they are encoded. The output function can
  224. * perform any postprocessing necessary (like hashing (some of) the data
  225. * to create a digest that gets included at the end) as well as shoving
  226. * the data off wherever it needs to go. (In order to "tune" any processing,
  227. * you can set a "notify proc" as described above in the decoding case.)
  228. *
  229. * The parameters:
  230. * - "arg" is an opaque pointer that you provided at the same time you
  231. * specified a function of this type
  232. * - "data" is a buffer of length "len", containing the encoded bytes
  233. * - "depth" is how deep in a nested encoding we are (it is not usually
  234. * valuable, but can be useful sometimes so I included it)
  235. * - "data_kind" tells you if these bytes are part of the ASN.1 encoded
  236. * octets for identifier, length, contents, or end-of-contents
  237. */
  238. typedef void (*SEC_ASN1WriteProc)(void *arg,
  239. const char *data, unsigned long len,
  240. int depth, SEC_ASN1EncodingPart data_kind);
  241. #endif /* _SECASN1T_H_ */