asn1t.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2000.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #ifndef HEADER_ASN1T_H
  58. #define HEADER_ASN1T_H
  59. #include <openssl/base.h>
  60. #include <openssl/asn1.h>
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /* Legacy ASN.1 library template definitions.
  65. *
  66. * This header is used to define new types in OpenSSL's ASN.1 implementation. It
  67. * is deprecated and will be unexported from the library. Use the new |CBS| and
  68. * |CBB| library in <openssl/bytestring.h> instead. */
  69. typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE;
  70. typedef struct ASN1_TLC_st ASN1_TLC;
  71. /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
  72. #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
  73. /* Macros for start and end of ASN1_ITEM definition */
  74. #define ASN1_ITEM_start(itname) \
  75. const ASN1_ITEM itname##_it = {
  76. #define ASN1_ITEM_end(itname) \
  77. };
  78. /* Macros to aid ASN1 template writing */
  79. #define ASN1_ITEM_TEMPLATE(tname) \
  80. static const ASN1_TEMPLATE tname##_item_tt
  81. #define ASN1_ITEM_TEMPLATE_END(tname) \
  82. ;\
  83. ASN1_ITEM_start(tname) \
  84. ASN1_ITYPE_PRIMITIVE,\
  85. -1,\
  86. &tname##_item_tt,\
  87. 0,\
  88. NULL,\
  89. 0,\
  90. #tname \
  91. ASN1_ITEM_end(tname)
  92. /* This is a ASN1 type which just embeds a template */
  93. /* This pair helps declare a SEQUENCE. We can do:
  94. *
  95. * ASN1_SEQUENCE(stname) = {
  96. * ... SEQUENCE components ...
  97. * } ASN1_SEQUENCE_END(stname)
  98. *
  99. * This will produce an ASN1_ITEM called stname_it
  100. * for a structure called stname.
  101. *
  102. * If you want the same structure but a different
  103. * name then use:
  104. *
  105. * ASN1_SEQUENCE(itname) = {
  106. * ... SEQUENCE components ...
  107. * } ASN1_SEQUENCE_END_name(stname, itname)
  108. *
  109. * This will create an item called itname_it using
  110. * a structure called stname.
  111. */
  112. #define ASN1_SEQUENCE(tname) \
  113. static const ASN1_TEMPLATE tname##_seq_tt[]
  114. #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
  115. #define ASN1_SEQUENCE_END_name(stname, tname) \
  116. ;\
  117. ASN1_ITEM_start(tname) \
  118. ASN1_ITYPE_SEQUENCE,\
  119. V_ASN1_SEQUENCE,\
  120. tname##_seq_tt,\
  121. sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
  122. NULL,\
  123. sizeof(stname),\
  124. #stname \
  125. ASN1_ITEM_end(tname)
  126. #define ASN1_SEQUENCE_cb(tname, cb) \
  127. static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
  128. ASN1_SEQUENCE(tname)
  129. #define ASN1_SEQUENCE_ref(tname, cb) \
  130. static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), cb, 0}; \
  131. ASN1_SEQUENCE(tname)
  132. #define ASN1_SEQUENCE_enc(tname, enc, cb) \
  133. static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, cb, offsetof(tname, enc)}; \
  134. ASN1_SEQUENCE(tname)
  135. #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
  136. #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
  137. #define ASN1_SEQUENCE_END_ref(stname, tname) \
  138. ;\
  139. ASN1_ITEM_start(tname) \
  140. ASN1_ITYPE_SEQUENCE,\
  141. V_ASN1_SEQUENCE,\
  142. tname##_seq_tt,\
  143. sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
  144. &tname##_aux,\
  145. sizeof(stname),\
  146. #stname \
  147. ASN1_ITEM_end(tname)
  148. /* This pair helps declare a CHOICE type. We can do:
  149. *
  150. * ASN1_CHOICE(chname) = {
  151. * ... CHOICE options ...
  152. * ASN1_CHOICE_END(chname)
  153. *
  154. * This will produce an ASN1_ITEM called chname_it
  155. * for a structure called chname. The structure
  156. * definition must look like this:
  157. * typedef struct {
  158. * int type;
  159. * union {
  160. * ASN1_SOMETHING *opt1;
  161. * ASN1_SOMEOTHER *opt2;
  162. * } value;
  163. * } chname;
  164. *
  165. * the name of the selector must be 'type'.
  166. * to use an alternative selector name use the
  167. * ASN1_CHOICE_END_selector() version.
  168. */
  169. #define ASN1_CHOICE(tname) \
  170. static const ASN1_TEMPLATE tname##_ch_tt[]
  171. #define ASN1_CHOICE_cb(tname, cb) \
  172. static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
  173. ASN1_CHOICE(tname)
  174. #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
  175. #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
  176. #define ASN1_CHOICE_END_selector(stname, tname, selname) \
  177. ;\
  178. ASN1_ITEM_start(tname) \
  179. ASN1_ITYPE_CHOICE,\
  180. offsetof(stname,selname) ,\
  181. tname##_ch_tt,\
  182. sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
  183. NULL,\
  184. sizeof(stname),\
  185. #stname \
  186. ASN1_ITEM_end(tname)
  187. #define ASN1_CHOICE_END_cb(stname, tname, selname) \
  188. ;\
  189. ASN1_ITEM_start(tname) \
  190. ASN1_ITYPE_CHOICE,\
  191. offsetof(stname,selname) ,\
  192. tname##_ch_tt,\
  193. sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
  194. &tname##_aux,\
  195. sizeof(stname),\
  196. #stname \
  197. ASN1_ITEM_end(tname)
  198. /* This helps with the template wrapper form of ASN1_ITEM */
  199. #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
  200. (flags), (tag), 0,\
  201. #name, ASN1_ITEM_ref(type) }
  202. /* These help with SEQUENCE or CHOICE components */
  203. /* used to declare other types */
  204. #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
  205. (flags), (tag), offsetof(stname, field),\
  206. #field, ASN1_ITEM_ref(type) }
  207. /* used when the structure is combined with the parent */
  208. #define ASN1_EX_COMBINE(flags, tag, type) { \
  209. (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
  210. /* implicit and explicit helper macros */
  211. #define ASN1_IMP_EX(stname, field, type, tag, ex) \
  212. ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
  213. #define ASN1_EXP_EX(stname, field, type, tag, ex) \
  214. ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
  215. /* Any defined by macros: the field used is in the table itself */
  216. #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
  217. /* Plain simple type */
  218. #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
  219. /* OPTIONAL simple type */
  220. #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  221. /* IMPLICIT tagged simple type */
  222. #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
  223. /* IMPLICIT tagged OPTIONAL simple type */
  224. #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
  225. /* Same as above but EXPLICIT */
  226. #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
  227. #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
  228. /* SEQUENCE OF type */
  229. #define ASN1_SEQUENCE_OF(stname, field, type) \
  230. ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
  231. /* OPTIONAL SEQUENCE OF */
  232. #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
  233. ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  234. /* Same as above but for SET OF */
  235. #define ASN1_SET_OF(stname, field, type) \
  236. ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
  237. #define ASN1_SET_OF_OPT(stname, field, type) \
  238. ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  239. /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
  240. #define ASN1_IMP_SET_OF(stname, field, type, tag) \
  241. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
  242. #define ASN1_EXP_SET_OF(stname, field, type, tag) \
  243. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
  244. #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
  245. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
  246. #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
  247. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
  248. #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
  249. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
  250. #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
  251. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
  252. #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
  253. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
  254. #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
  255. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
  256. /* Macros for the ASN1_ADB structure */
  257. #define ASN1_ADB(name) \
  258. static const ASN1_ADB_TABLE name##_adbtbl[]
  259. #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
  260. ;\
  261. static const ASN1_ADB name##_adb = {\
  262. flags,\
  263. offsetof(name, field),\
  264. app_table,\
  265. name##_adbtbl,\
  266. sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
  267. def,\
  268. none\
  269. }
  270. #define ADB_ENTRY(val, template) {val, template}
  271. #define ASN1_ADB_TEMPLATE(name) \
  272. static const ASN1_TEMPLATE name##_tt
  273. /* This is the ASN1 template structure that defines
  274. * a wrapper round the actual type. It determines the
  275. * actual position of the field in the value structure,
  276. * various flags such as OPTIONAL and the field name.
  277. */
  278. struct ASN1_TEMPLATE_st {
  279. unsigned long flags; /* Various flags */
  280. long tag; /* tag, not used if no tagging */
  281. unsigned long offset; /* Offset of this field in structure */
  282. const char *field_name; /* Field name */
  283. ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
  284. };
  285. /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
  286. #define ASN1_TEMPLATE_item(t) (t->item_ptr)
  287. #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
  288. typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
  289. typedef struct ASN1_ADB_st ASN1_ADB;
  290. typedef struct asn1_must_be_null_st ASN1_MUST_BE_NULL;
  291. struct ASN1_ADB_st {
  292. unsigned long flags; /* Various flags */
  293. unsigned long offset; /* Offset of selector field */
  294. ASN1_MUST_BE_NULL *unused;
  295. const ASN1_ADB_TABLE *tbl; /* Table of possible types */
  296. long tblcount; /* Number of entries in tbl */
  297. const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
  298. const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
  299. };
  300. struct ASN1_ADB_TABLE_st {
  301. int value; /* NID for an object */
  302. const ASN1_TEMPLATE tt; /* item for this value */
  303. };
  304. /* template flags */
  305. /* Field is optional */
  306. #define ASN1_TFLG_OPTIONAL (0x1)
  307. /* Field is a SET OF */
  308. #define ASN1_TFLG_SET_OF (0x1 << 1)
  309. /* Field is a SEQUENCE OF */
  310. #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
  311. /* Mask for SET OF or SEQUENCE OF */
  312. #define ASN1_TFLG_SK_MASK (0x3 << 1)
  313. /* These flags mean the tag should be taken from the
  314. * tag field. If EXPLICIT then the underlying type
  315. * is used for the inner tag.
  316. */
  317. /* IMPLICIT tagging */
  318. #define ASN1_TFLG_IMPTAG (0x1 << 3)
  319. /* EXPLICIT tagging, inner tag from underlying type */
  320. #define ASN1_TFLG_EXPTAG (0x2 << 3)
  321. #define ASN1_TFLG_TAG_MASK (0x3 << 3)
  322. /* context specific IMPLICIT */
  323. #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
  324. /* context specific EXPLICIT */
  325. #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
  326. /* If tagging is in force these determine the
  327. * type of tag to use. Otherwise the tag is
  328. * determined by the underlying type. These
  329. * values reflect the actual octet format.
  330. */
  331. /* Universal tag */
  332. #define ASN1_TFLG_UNIVERSAL (0x0<<6)
  333. /* Application tag */
  334. #define ASN1_TFLG_APPLICATION (0x1<<6)
  335. /* Context specific tag */
  336. #define ASN1_TFLG_CONTEXT (0x2<<6)
  337. /* Private tag */
  338. #define ASN1_TFLG_PRIVATE (0x3<<6)
  339. #define ASN1_TFLG_TAG_CLASS (0x3<<6)
  340. /* These are for ANY DEFINED BY type. In this case
  341. * the 'item' field points to an ASN1_ADB structure
  342. * which contains a table of values to decode the
  343. * relevant type
  344. */
  345. #define ASN1_TFLG_ADB_MASK (0x3<<8)
  346. #define ASN1_TFLG_ADB_OID (0x1<<8)
  347. /* This flag means a parent structure is passed
  348. * instead of the field: this is useful is a
  349. * SEQUENCE is being combined with a CHOICE for
  350. * example. Since this means the structure and
  351. * item name will differ we need to use the
  352. * ASN1_CHOICE_END_name() macro for example.
  353. */
  354. #define ASN1_TFLG_COMBINE (0x1<<10)
  355. /* This is the actual ASN1 item itself */
  356. struct ASN1_ITEM_st {
  357. char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */
  358. long utype; /* underlying type */
  359. const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
  360. long tcount; /* Number of templates if SEQUENCE or CHOICE */
  361. const void *funcs; /* functions that handle this type */
  362. long size; /* Structure size (usually)*/
  363. const char *sname; /* Structure name */
  364. };
  365. /* These are values for the itype field and
  366. * determine how the type is interpreted.
  367. *
  368. * For PRIMITIVE types the underlying type
  369. * determines the behaviour if items is NULL.
  370. *
  371. * Otherwise templates must contain a single
  372. * template and the type is treated in the
  373. * same way as the type specified in the template.
  374. *
  375. * For SEQUENCE types the templates field points
  376. * to the members, the size field is the
  377. * structure size.
  378. *
  379. * For CHOICE types the templates field points
  380. * to each possible member (typically a union)
  381. * and the 'size' field is the offset of the
  382. * selector.
  383. *
  384. * The 'funcs' field is used for application
  385. * specific functions.
  386. *
  387. * The EXTERN type uses a new style d2i/i2d.
  388. * The new style should be used where possible
  389. * because it avoids things like the d2i IMPLICIT
  390. * hack.
  391. *
  392. * MSTRING is a multiple string type, it is used
  393. * for a CHOICE of character strings where the
  394. * actual strings all occupy an ASN1_STRING
  395. * structure. In this case the 'utype' field
  396. * has a special meaning, it is used as a mask
  397. * of acceptable types using the B_ASN1 constants.
  398. *
  399. */
  400. #define ASN1_ITYPE_PRIMITIVE 0x0
  401. #define ASN1_ITYPE_SEQUENCE 0x1
  402. #define ASN1_ITYPE_CHOICE 0x2
  403. #define ASN1_ITYPE_EXTERN 0x4
  404. #define ASN1_ITYPE_MSTRING 0x5
  405. /* Deprecated tag and length cache */
  406. struct ASN1_TLC_st;
  407. /* Typedefs for ASN1 function pointers */
  408. typedef ASN1_VALUE * ASN1_new_func(void);
  409. typedef void ASN1_free_func(ASN1_VALUE *a);
  410. typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
  411. typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
  412. typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
  413. int tag, int aclass, char opt, ASN1_TLC *ctx);
  414. typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
  415. typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
  416. typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
  417. typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
  418. int indent, const char *fname,
  419. const ASN1_PCTX *pctx);
  420. typedef struct ASN1_EXTERN_FUNCS_st {
  421. void *app_data;
  422. ASN1_ex_new_func *asn1_ex_new;
  423. ASN1_ex_free_func *asn1_ex_free;
  424. ASN1_ex_free_func *asn1_ex_clear;
  425. ASN1_ex_d2i *asn1_ex_d2i;
  426. ASN1_ex_i2d *asn1_ex_i2d;
  427. /* asn1_ex_print is unused. */
  428. ASN1_ex_print_func *asn1_ex_print;
  429. } ASN1_EXTERN_FUNCS;
  430. /* This is the ASN1_AUX structure: it handles various
  431. * miscellaneous requirements. For example the use of
  432. * reference counts and an informational callback.
  433. *
  434. * The "informational callback" is called at various
  435. * points during the ASN1 encoding and decoding. It can
  436. * be used to provide minor customisation of the structures
  437. * used. This is most useful where the supplied routines
  438. * *almost* do the right thing but need some extra help
  439. * at a few points. If the callback returns zero then
  440. * it is assumed a fatal error has occurred and the
  441. * main operation should be abandoned.
  442. *
  443. * If major changes in the default behaviour are required
  444. * then an external type is more appropriate.
  445. */
  446. typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
  447. void *exarg);
  448. typedef struct ASN1_AUX_st {
  449. void *app_data;
  450. int flags;
  451. int ref_offset; /* Offset of reference value */
  452. ASN1_aux_cb *asn1_cb;
  453. int enc_offset; /* Offset of ASN1_ENCODING structure */
  454. } ASN1_AUX;
  455. /* Flags in ASN1_AUX */
  456. /* Use a reference count */
  457. #define ASN1_AFLG_REFCOUNT 1
  458. /* Save the encoding of structure (useful for signatures) */
  459. #define ASN1_AFLG_ENCODING 2
  460. /* operation values for asn1_cb */
  461. #define ASN1_OP_NEW_PRE 0
  462. #define ASN1_OP_NEW_POST 1
  463. #define ASN1_OP_FREE_PRE 2
  464. #define ASN1_OP_FREE_POST 3
  465. #define ASN1_OP_D2I_PRE 4
  466. #define ASN1_OP_D2I_POST 5
  467. /* ASN1_OP_I2D_PRE and ASN1_OP_I2D_POST are not supported. We leave the
  468. * constants undefined so code relying on them does not accidentally compile. */
  469. #define ASN1_OP_PRINT_PRE 8
  470. #define ASN1_OP_PRINT_POST 9
  471. #define ASN1_OP_STREAM_PRE 10
  472. #define ASN1_OP_STREAM_POST 11
  473. #define ASN1_OP_DETACHED_PRE 12
  474. #define ASN1_OP_DETACHED_POST 13
  475. /* Macro to implement a primitive type */
  476. #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
  477. #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
  478. ASN1_ITEM_start(itname) \
  479. ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
  480. ASN1_ITEM_end(itname)
  481. /* Macro to implement a multi string type */
  482. #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
  483. ASN1_ITEM_start(itname) \
  484. ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
  485. ASN1_ITEM_end(itname)
  486. #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
  487. ASN1_ITEM_start(sname) \
  488. ASN1_ITYPE_EXTERN, \
  489. tag, \
  490. NULL, \
  491. 0, \
  492. &fptrs, \
  493. 0, \
  494. #sname \
  495. ASN1_ITEM_end(sname)
  496. /* Macro to implement standard functions in terms of ASN1_ITEM structures */
  497. #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
  498. #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
  499. #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
  500. IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
  501. #define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
  502. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
  503. #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
  504. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
  505. #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
  506. pre stname *fname##_new(void) \
  507. { \
  508. return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
  509. } \
  510. pre void fname##_free(stname *a) \
  511. { \
  512. ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
  513. }
  514. #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
  515. stname *fname##_new(void) \
  516. { \
  517. return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
  518. } \
  519. void fname##_free(stname *a) \
  520. { \
  521. ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
  522. }
  523. #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
  524. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
  525. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
  526. #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
  527. stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
  528. { \
  529. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
  530. } \
  531. int i2d_##fname(stname *a, unsigned char **out) \
  532. { \
  533. return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
  534. }
  535. /* This includes evil casts to remove const: they will go away when full
  536. * ASN1 constification is done.
  537. */
  538. #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
  539. stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
  540. { \
  541. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
  542. } \
  543. int i2d_##fname(const stname *a, unsigned char **out) \
  544. { \
  545. return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
  546. }
  547. #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
  548. stname * stname##_dup(stname *x) \
  549. { \
  550. return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
  551. }
  552. #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
  553. IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
  554. #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
  555. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
  556. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
  557. /* external definitions for primitive types */
  558. DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
  559. DEFINE_STACK_OF(ASN1_VALUE)
  560. #ifdef __cplusplus
  561. }
  562. #endif
  563. #endif