asn1t.h 32 KB

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