asn1t.h 29 KB

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