asn1t.h 34 KB

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