You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

895 regels
28 KiB

  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. /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
  70. #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
  71. /* Macros for start and end of ASN1_ITEM definition */
  72. #define ASN1_ITEM_start(itname) \
  73. const ASN1_ITEM itname##_it = {
  74. #define ASN1_ITEM_end(itname) \
  75. };
  76. /* Macros to aid ASN1 template writing */
  77. #define ASN1_ITEM_TEMPLATE(tname) \
  78. static const ASN1_TEMPLATE tname##_item_tt
  79. #define ASN1_ITEM_TEMPLATE_END(tname) \
  80. ;\
  81. ASN1_ITEM_start(tname) \
  82. ASN1_ITYPE_PRIMITIVE,\
  83. -1,\
  84. &tname##_item_tt,\
  85. 0,\
  86. NULL,\
  87. 0,\
  88. #tname \
  89. ASN1_ITEM_end(tname)
  90. /* This is a ASN1 type which just embeds a template */
  91. /* This pair helps declare a SEQUENCE. We can do:
  92. *
  93. * ASN1_SEQUENCE(stname) = {
  94. * ... SEQUENCE components ...
  95. * } ASN1_SEQUENCE_END(stname)
  96. *
  97. * This will produce an ASN1_ITEM called stname_it
  98. * for a structure called stname.
  99. *
  100. * If you want the same structure but a different
  101. * name then use:
  102. *
  103. * ASN1_SEQUENCE(itname) = {
  104. * ... SEQUENCE components ...
  105. * } ASN1_SEQUENCE_END_name(stname, itname)
  106. *
  107. * This will create an item called itname_it using
  108. * a structure called stname.
  109. */
  110. #define ASN1_SEQUENCE(tname) \
  111. static const ASN1_TEMPLATE tname##_seq_tt[]
  112. #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
  113. #define ASN1_SEQUENCE_END_name(stname, tname) \
  114. ;\
  115. ASN1_ITEM_start(tname) \
  116. ASN1_ITYPE_SEQUENCE,\
  117. V_ASN1_SEQUENCE,\
  118. tname##_seq_tt,\
  119. sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
  120. NULL,\
  121. sizeof(stname),\
  122. #stname \
  123. ASN1_ITEM_end(tname)
  124. #define ASN1_NDEF_SEQUENCE(tname) \
  125. ASN1_SEQUENCE(tname)
  126. #define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
  127. ASN1_SEQUENCE_cb(tname, cb)
  128. #define ASN1_SEQUENCE_cb(tname, cb) \
  129. static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
  130. ASN1_SEQUENCE(tname)
  131. #define ASN1_BROKEN_SEQUENCE(tname) \
  132. static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0}; \
  133. ASN1_SEQUENCE(tname)
  134. #define ASN1_SEQUENCE_ref(tname, cb) \
  135. static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), cb, 0}; \
  136. ASN1_SEQUENCE(tname)
  137. #define ASN1_SEQUENCE_enc(tname, enc, cb) \
  138. static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, cb, offsetof(tname, enc)}; \
  139. ASN1_SEQUENCE(tname)
  140. #define ASN1_NDEF_SEQUENCE_END(tname) \
  141. ;\
  142. ASN1_ITEM_start(tname) \
  143. ASN1_ITYPE_NDEF_SEQUENCE,\
  144. V_ASN1_SEQUENCE,\
  145. tname##_seq_tt,\
  146. sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
  147. NULL,\
  148. sizeof(tname),\
  149. #tname \
  150. ASN1_ITEM_end(tname)
  151. #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
  152. #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
  153. #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
  154. #define ASN1_SEQUENCE_END_ref(stname, tname) \
  155. ;\
  156. ASN1_ITEM_start(tname) \
  157. ASN1_ITYPE_SEQUENCE,\
  158. V_ASN1_SEQUENCE,\
  159. tname##_seq_tt,\
  160. sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
  161. &tname##_aux,\
  162. sizeof(stname),\
  163. #stname \
  164. ASN1_ITEM_end(tname)
  165. #define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
  166. ;\
  167. ASN1_ITEM_start(tname) \
  168. ASN1_ITYPE_NDEF_SEQUENCE,\
  169. V_ASN1_SEQUENCE,\
  170. tname##_seq_tt,\
  171. sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
  172. &tname##_aux,\
  173. sizeof(stname),\
  174. #stname \
  175. ASN1_ITEM_end(tname)
  176. /* This pair helps declare a CHOICE type. We can do:
  177. *
  178. * ASN1_CHOICE(chname) = {
  179. * ... CHOICE options ...
  180. * ASN1_CHOICE_END(chname)
  181. *
  182. * This will produce an ASN1_ITEM called chname_it
  183. * for a structure called chname. The structure
  184. * definition must look like this:
  185. * typedef struct {
  186. * int type;
  187. * union {
  188. * ASN1_SOMETHING *opt1;
  189. * ASN1_SOMEOTHER *opt2;
  190. * } value;
  191. * } chname;
  192. *
  193. * the name of the selector must be 'type'.
  194. * to use an alternative selector name use the
  195. * ASN1_CHOICE_END_selector() version.
  196. */
  197. #define ASN1_CHOICE(tname) \
  198. static const ASN1_TEMPLATE tname##_ch_tt[]
  199. #define ASN1_CHOICE_cb(tname, cb) \
  200. static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
  201. ASN1_CHOICE(tname)
  202. #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
  203. #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
  204. #define ASN1_CHOICE_END_selector(stname, tname, selname) \
  205. ;\
  206. ASN1_ITEM_start(tname) \
  207. ASN1_ITYPE_CHOICE,\
  208. offsetof(stname,selname) ,\
  209. tname##_ch_tt,\
  210. sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
  211. NULL,\
  212. sizeof(stname),\
  213. #stname \
  214. ASN1_ITEM_end(tname)
  215. #define ASN1_CHOICE_END_cb(stname, tname, selname) \
  216. ;\
  217. ASN1_ITEM_start(tname) \
  218. ASN1_ITYPE_CHOICE,\
  219. offsetof(stname,selname) ,\
  220. tname##_ch_tt,\
  221. sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
  222. &tname##_aux,\
  223. sizeof(stname),\
  224. #stname \
  225. ASN1_ITEM_end(tname)
  226. /* This helps with the template wrapper form of ASN1_ITEM */
  227. #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
  228. (flags), (tag), 0,\
  229. #name, ASN1_ITEM_ref(type) }
  230. /* These help with SEQUENCE or CHOICE components */
  231. /* used to declare other types */
  232. #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
  233. (flags), (tag), offsetof(stname, field),\
  234. #field, ASN1_ITEM_ref(type) }
  235. /* used when the structure is combined with the parent */
  236. #define ASN1_EX_COMBINE(flags, tag, type) { \
  237. (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
  238. /* implicit and explicit helper macros */
  239. #define ASN1_IMP_EX(stname, field, type, tag, ex) \
  240. ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
  241. #define ASN1_EXP_EX(stname, field, type, tag, ex) \
  242. ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
  243. /* Any defined by macros: the field used is in the table itself */
  244. #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
  245. #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
  246. /* Plain simple type */
  247. #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
  248. /* OPTIONAL simple type */
  249. #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  250. /* IMPLICIT tagged simple type */
  251. #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
  252. /* IMPLICIT tagged OPTIONAL simple type */
  253. #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
  254. /* Same as above but EXPLICIT */
  255. #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
  256. #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
  257. /* SEQUENCE OF type */
  258. #define ASN1_SEQUENCE_OF(stname, field, type) \
  259. ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
  260. /* OPTIONAL SEQUENCE OF */
  261. #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
  262. ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  263. /* Same as above but for SET OF */
  264. #define ASN1_SET_OF(stname, field, type) \
  265. ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
  266. #define ASN1_SET_OF_OPT(stname, field, type) \
  267. ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  268. /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
  269. #define ASN1_IMP_SET_OF(stname, field, type, tag) \
  270. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
  271. #define ASN1_EXP_SET_OF(stname, field, type, tag) \
  272. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
  273. #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
  274. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
  275. #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
  276. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
  277. #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
  278. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
  279. #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
  280. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
  281. #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
  282. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
  283. #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
  284. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
  285. /* EXPLICIT using indefinite length constructed form */
  286. #define ASN1_NDEF_EXP(stname, field, type, tag) \
  287. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
  288. /* EXPLICIT OPTIONAL using indefinite length constructed form */
  289. #define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
  290. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
  291. /* Macros for the ASN1_ADB structure */
  292. #define ASN1_ADB(name) \
  293. static const ASN1_ADB_TABLE name##_adbtbl[]
  294. #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
  295. ;\
  296. static const ASN1_ADB name##_adb = {\
  297. flags,\
  298. offsetof(name, field),\
  299. app_table,\
  300. name##_adbtbl,\
  301. sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
  302. def,\
  303. none\
  304. }
  305. #define ADB_ENTRY(val, template) {val, template}
  306. #define ASN1_ADB_TEMPLATE(name) \
  307. static const ASN1_TEMPLATE name##_tt
  308. /* This is the ASN1 template structure that defines
  309. * a wrapper round the actual type. It determines the
  310. * actual position of the field in the value structure,
  311. * various flags such as OPTIONAL and the field name.
  312. */
  313. struct ASN1_TEMPLATE_st {
  314. unsigned long flags; /* Various flags */
  315. long tag; /* tag, not used if no tagging */
  316. unsigned long offset; /* Offset of this field in structure */
  317. #ifndef NO_ASN1_FIELD_NAMES
  318. const char *field_name; /* Field name */
  319. #endif
  320. ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
  321. };
  322. /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
  323. #define ASN1_TEMPLATE_item(t) (t->item_ptr)
  324. #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
  325. typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
  326. typedef struct ASN1_ADB_st ASN1_ADB;
  327. typedef struct asn1_must_be_null_st ASN1_MUST_BE_NULL;
  328. struct ASN1_ADB_st {
  329. unsigned long flags; /* Various flags */
  330. unsigned long offset; /* Offset of selector field */
  331. ASN1_MUST_BE_NULL *unused;
  332. const ASN1_ADB_TABLE *tbl; /* Table of possible types */
  333. long tblcount; /* Number of entries in tbl */
  334. const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
  335. const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
  336. };
  337. struct ASN1_ADB_TABLE_st {
  338. long value; /* NID for an object or value for an int */
  339. const ASN1_TEMPLATE tt; /* item for this value */
  340. };
  341. /* template flags */
  342. /* Field is optional */
  343. #define ASN1_TFLG_OPTIONAL (0x1)
  344. /* Field is a SET OF */
  345. #define ASN1_TFLG_SET_OF (0x1 << 1)
  346. /* Field is a SEQUENCE OF */
  347. #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
  348. /* Special case: this refers to a SET OF that
  349. * will be sorted into DER order when encoded *and*
  350. * the corresponding STACK will be modified to match
  351. * the new order.
  352. */
  353. #define ASN1_TFLG_SET_ORDER (0x3 << 1)
  354. /* Mask for SET OF or SEQUENCE OF */
  355. #define ASN1_TFLG_SK_MASK (0x3 << 1)
  356. /* These flags mean the tag should be taken from the
  357. * tag field. If EXPLICIT then the underlying type
  358. * is used for the inner tag.
  359. */
  360. /* IMPLICIT tagging */
  361. #define ASN1_TFLG_IMPTAG (0x1 << 3)
  362. /* EXPLICIT tagging, inner tag from underlying type */
  363. #define ASN1_TFLG_EXPTAG (0x2 << 3)
  364. #define ASN1_TFLG_TAG_MASK (0x3 << 3)
  365. /* context specific IMPLICIT */
  366. #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
  367. /* context specific EXPLICIT */
  368. #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
  369. /* If tagging is in force these determine the
  370. * type of tag to use. Otherwise the tag is
  371. * determined by the underlying type. These
  372. * values reflect the actual octet format.
  373. */
  374. /* Universal tag */
  375. #define ASN1_TFLG_UNIVERSAL (0x0<<6)
  376. /* Application tag */
  377. #define ASN1_TFLG_APPLICATION (0x1<<6)
  378. /* Context specific tag */
  379. #define ASN1_TFLG_CONTEXT (0x2<<6)
  380. /* Private tag */
  381. #define ASN1_TFLG_PRIVATE (0x3<<6)
  382. #define ASN1_TFLG_TAG_CLASS (0x3<<6)
  383. /* These are for ANY DEFINED BY type. In this case
  384. * the 'item' field points to an ASN1_ADB structure
  385. * which contains a table of values to decode the
  386. * relevant type
  387. */
  388. #define ASN1_TFLG_ADB_MASK (0x3<<8)
  389. #define ASN1_TFLG_ADB_OID (0x1<<8)
  390. #define ASN1_TFLG_ADB_INT (0x1<<9)
  391. /* This flag means a parent structure is passed
  392. * instead of the field: this is useful is a
  393. * SEQUENCE is being combined with a CHOICE for
  394. * example. Since this means the structure and
  395. * item name will differ we need to use the
  396. * ASN1_CHOICE_END_name() macro for example.
  397. */
  398. #define ASN1_TFLG_COMBINE (0x1<<10)
  399. /* This flag when present in a SEQUENCE OF, SET OF
  400. * or EXPLICIT causes indefinite length constructed
  401. * encoding to be used if required.
  402. */
  403. #define ASN1_TFLG_NDEF (0x1<<11)
  404. /* This is the actual ASN1 item itself */
  405. struct ASN1_ITEM_st {
  406. char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */
  407. long utype; /* underlying type */
  408. const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
  409. long tcount; /* Number of templates if SEQUENCE or CHOICE */
  410. const void *funcs; /* functions that handle this type */
  411. long size; /* Structure size (usually)*/
  412. #ifndef NO_ASN1_FIELD_NAMES
  413. const char *sname; /* Structure name */
  414. #endif
  415. };
  416. /* These are values for the itype field and
  417. * determine how the type is interpreted.
  418. *
  419. * For PRIMITIVE types the underlying type
  420. * determines the behaviour if items is NULL.
  421. *
  422. * Otherwise templates must contain a single
  423. * template and the type is treated in the
  424. * same way as the type specified in the template.
  425. *
  426. * For SEQUENCE types the templates field points
  427. * to the members, the size field is the
  428. * structure size.
  429. *
  430. * For CHOICE types the templates field points
  431. * to each possible member (typically a union)
  432. * and the 'size' field is the offset of the
  433. * selector.
  434. *
  435. * The 'funcs' field is used for application
  436. * specific functions.
  437. *
  438. * For COMPAT types the funcs field gives a
  439. * set of functions that handle this type, this
  440. * supports the old d2i, i2d convention.
  441. *
  442. * The EXTERN type uses a new style d2i/i2d.
  443. * The new style should be used where possible
  444. * because it avoids things like the d2i IMPLICIT
  445. * hack.
  446. *
  447. * MSTRING is a multiple string type, it is used
  448. * for a CHOICE of character strings where the
  449. * actual strings all occupy an ASN1_STRING
  450. * structure. In this case the 'utype' field
  451. * has a special meaning, it is used as a mask
  452. * of acceptable types using the B_ASN1 constants.
  453. *
  454. * NDEF_SEQUENCE is the same as SEQUENCE except
  455. * that it will use indefinite length constructed
  456. * encoding if requested.
  457. *
  458. */
  459. #define ASN1_ITYPE_PRIMITIVE 0x0
  460. #define ASN1_ITYPE_SEQUENCE 0x1
  461. #define ASN1_ITYPE_CHOICE 0x2
  462. #define ASN1_ITYPE_COMPAT 0x3
  463. #define ASN1_ITYPE_EXTERN 0x4
  464. #define ASN1_ITYPE_MSTRING 0x5
  465. #define ASN1_ITYPE_NDEF_SEQUENCE 0x6
  466. /* Cache for ASN1 tag and length, so we
  467. * don't keep re-reading it for things
  468. * like CHOICE
  469. */
  470. struct ASN1_TLC_st{
  471. char valid; /* Values below are valid */
  472. int ret; /* return value */
  473. long plen; /* length */
  474. int ptag; /* class value */
  475. int pclass; /* class value */
  476. int hdrlen; /* header length */
  477. };
  478. /* Typedefs for ASN1 function pointers */
  479. typedef ASN1_VALUE * ASN1_new_func(void);
  480. typedef void ASN1_free_func(ASN1_VALUE *a);
  481. typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
  482. typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
  483. typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
  484. int tag, int aclass, char opt, ASN1_TLC *ctx);
  485. typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
  486. typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
  487. typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
  488. typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
  489. int indent, const char *fname,
  490. const ASN1_PCTX *pctx);
  491. typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
  492. typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
  493. typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, const ASN1_PCTX *pctx);
  494. typedef struct ASN1_COMPAT_FUNCS_st {
  495. ASN1_new_func *asn1_new;
  496. ASN1_free_func *asn1_free;
  497. ASN1_d2i_func *asn1_d2i;
  498. ASN1_i2d_func *asn1_i2d;
  499. } ASN1_COMPAT_FUNCS;
  500. typedef struct ASN1_EXTERN_FUNCS_st {
  501. void *app_data;
  502. ASN1_ex_new_func *asn1_ex_new;
  503. ASN1_ex_free_func *asn1_ex_free;
  504. ASN1_ex_free_func *asn1_ex_clear;
  505. ASN1_ex_d2i *asn1_ex_d2i;
  506. ASN1_ex_i2d *asn1_ex_i2d;
  507. /* asn1_ex_print is unused. */
  508. ASN1_ex_print_func *asn1_ex_print;
  509. } ASN1_EXTERN_FUNCS;
  510. typedef struct ASN1_PRIMITIVE_FUNCS_st {
  511. void *app_data;
  512. unsigned long flags;
  513. ASN1_ex_new_func *prim_new;
  514. ASN1_ex_free_func *prim_free;
  515. ASN1_ex_free_func *prim_clear;
  516. ASN1_primitive_c2i *prim_c2i;
  517. ASN1_primitive_i2c *prim_i2c;
  518. ASN1_primitive_print *prim_print;
  519. } ASN1_PRIMITIVE_FUNCS;
  520. /* This is the ASN1_AUX structure: it handles various
  521. * miscellaneous requirements. For example the use of
  522. * reference counts and an informational callback.
  523. *
  524. * The "informational callback" is called at various
  525. * points during the ASN1 encoding and decoding. It can
  526. * be used to provide minor customisation of the structures
  527. * used. This is most useful where the supplied routines
  528. * *almost* do the right thing but need some extra help
  529. * at a few points. If the callback returns zero then
  530. * it is assumed a fatal error has occurred and the
  531. * main operation should be abandoned.
  532. *
  533. * If major changes in the default behaviour are required
  534. * then an external type is more appropriate.
  535. */
  536. typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
  537. void *exarg);
  538. typedef struct ASN1_AUX_st {
  539. void *app_data;
  540. int flags;
  541. int ref_offset; /* Offset of reference value */
  542. ASN1_aux_cb *asn1_cb;
  543. int enc_offset; /* Offset of ASN1_ENCODING structure */
  544. } ASN1_AUX;
  545. /* For print related callbacks exarg points to this structure */
  546. typedef struct ASN1_PRINT_ARG_st {
  547. BIO *out;
  548. int indent;
  549. const ASN1_PCTX *pctx;
  550. } ASN1_PRINT_ARG;
  551. /* For streaming related callbacks exarg points to this structure */
  552. typedef struct ASN1_STREAM_ARG_st {
  553. /* BIO to stream through */
  554. BIO *out;
  555. /* BIO with filters appended */
  556. BIO *ndef_bio;
  557. /* Streaming I/O boundary */
  558. unsigned char **boundary;
  559. } ASN1_STREAM_ARG;
  560. /* Flags in ASN1_AUX */
  561. /* Use a reference count */
  562. #define ASN1_AFLG_REFCOUNT 1
  563. /* Save the encoding of structure (useful for signatures) */
  564. #define ASN1_AFLG_ENCODING 2
  565. /* The Sequence length is invalid */
  566. #define ASN1_AFLG_BROKEN 4
  567. /* operation values for asn1_cb */
  568. #define ASN1_OP_NEW_PRE 0
  569. #define ASN1_OP_NEW_POST 1
  570. #define ASN1_OP_FREE_PRE 2
  571. #define ASN1_OP_FREE_POST 3
  572. #define ASN1_OP_D2I_PRE 4
  573. #define ASN1_OP_D2I_POST 5
  574. #define ASN1_OP_I2D_PRE 6
  575. #define ASN1_OP_I2D_POST 7
  576. #define ASN1_OP_PRINT_PRE 8
  577. #define ASN1_OP_PRINT_POST 9
  578. #define ASN1_OP_STREAM_PRE 10
  579. #define ASN1_OP_STREAM_POST 11
  580. #define ASN1_OP_DETACHED_PRE 12
  581. #define ASN1_OP_DETACHED_POST 13
  582. /* Macro to implement a primitive type */
  583. #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
  584. #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
  585. ASN1_ITEM_start(itname) \
  586. ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
  587. ASN1_ITEM_end(itname)
  588. /* Macro to implement a multi string type */
  589. #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
  590. ASN1_ITEM_start(itname) \
  591. ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
  592. ASN1_ITEM_end(itname)
  593. /* Macro to implement an ASN1_ITEM in terms of old style funcs */
  594. #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
  595. #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
  596. static const ASN1_COMPAT_FUNCS sname##_ff = { \
  597. (ASN1_new_func *)sname##_new, \
  598. (ASN1_free_func *)sname##_free, \
  599. (ASN1_d2i_func *)d2i_##sname, \
  600. (ASN1_i2d_func *)i2d_##sname, \
  601. }; \
  602. ASN1_ITEM_start(sname) \
  603. ASN1_ITYPE_COMPAT, \
  604. tag, \
  605. NULL, \
  606. 0, \
  607. &sname##_ff, \
  608. 0, \
  609. #sname \
  610. ASN1_ITEM_end(sname)
  611. #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
  612. ASN1_ITEM_start(sname) \
  613. ASN1_ITYPE_EXTERN, \
  614. tag, \
  615. NULL, \
  616. 0, \
  617. &fptrs, \
  618. 0, \
  619. #sname \
  620. ASN1_ITEM_end(sname)
  621. /* Macro to implement standard functions in terms of ASN1_ITEM structures */
  622. #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
  623. #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
  624. #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
  625. IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
  626. #define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
  627. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
  628. #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
  629. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
  630. #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
  631. pre stname *fname##_new(void) \
  632. { \
  633. return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
  634. } \
  635. pre void fname##_free(stname *a) \
  636. { \
  637. ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
  638. }
  639. #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
  640. stname *fname##_new(void) \
  641. { \
  642. return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
  643. } \
  644. void fname##_free(stname *a) \
  645. { \
  646. ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
  647. }
  648. #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
  649. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
  650. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
  651. #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
  652. stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
  653. { \
  654. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
  655. } \
  656. int i2d_##fname(stname *a, unsigned char **out) \
  657. { \
  658. return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
  659. }
  660. #define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
  661. int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
  662. { \
  663. return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
  664. }
  665. /* This includes evil casts to remove const: they will go away when full
  666. * ASN1 constification is done.
  667. */
  668. #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
  669. stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
  670. { \
  671. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
  672. } \
  673. int i2d_##fname(const stname *a, unsigned char **out) \
  674. { \
  675. return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
  676. }
  677. #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
  678. stname * stname##_dup(stname *x) \
  679. { \
  680. return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
  681. }
  682. #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
  683. IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
  684. #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
  685. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
  686. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
  687. /* external definitions for primitive types */
  688. DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
  689. DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
  690. DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
  691. DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
  692. DEFINE_STACK_OF(ASN1_VALUE)
  693. /* Functions used internally by the ASN1 code */
  694. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  695. void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  696. int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  697. int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  698. void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  699. int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
  700. int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
  701. int tag, int aclass, char opt, ASN1_TLC *ctx);
  702. int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
  703. int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
  704. void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  705. int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
  706. int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
  707. int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
  708. int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
  709. ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
  710. const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
  711. void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it);
  712. int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
  713. void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
  714. void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  715. int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
  716. int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
  717. #ifdef __cplusplus
  718. }
  719. #endif
  720. #endif