PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/gecko_api/include/secasn1t.h

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