asn1t.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. /* IMPLICIT tagged simple type */
  289. # define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
  290. /* IMPLICIT tagged OPTIONAL simple type */
  291. # define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
  292. /* Same as above but EXPLICIT */
  293. # define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
  294. # define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
  295. /* SEQUENCE OF type */
  296. # define ASN1_SEQUENCE_OF(stname, field, type) \
  297. ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
  298. /* OPTIONAL SEQUENCE OF */
  299. # define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
  300. ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  301. /* Same as above but for SET OF */
  302. # define ASN1_SET_OF(stname, field, type) \
  303. ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
  304. # define ASN1_SET_OF_OPT(stname, field, type) \
  305. ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
  306. /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
  307. # define ASN1_IMP_SET_OF(stname, field, type, tag) \
  308. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
  309. # define ASN1_EXP_SET_OF(stname, field, type, tag) \
  310. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
  311. # define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
  312. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
  313. # define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
  314. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
  315. # define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
  316. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
  317. # define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
  318. ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
  319. # define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
  320. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
  321. # define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
  322. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
  323. /* EXPLICIT using indefinite length constructed form */
  324. # define ASN1_NDEF_EXP(stname, field, type, tag) \
  325. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
  326. /* EXPLICIT OPTIONAL using indefinite length constructed form */
  327. # define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
  328. ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
  329. /* Macros for the ASN1_ADB structure */
  330. # define ASN1_ADB(name) \
  331. static const ASN1_ADB_TABLE name##_adbtbl[]
  332. # ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
  333. # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
  334. ;\
  335. static const ASN1_ADB name##_adb = {\
  336. flags,\
  337. offsetof(name, field),\
  338. adb_cb,\
  339. name##_adbtbl,\
  340. sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
  341. def,\
  342. none\
  343. }
  344. # else
  345. # define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
  346. ;\
  347. static const ASN1_ITEM *name##_adb(void) \
  348. { \
  349. static const ASN1_ADB internal_adb = \
  350. {\
  351. flags,\
  352. offsetof(name, field),\
  353. adb_cb,\
  354. name##_adbtbl,\
  355. sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
  356. def,\
  357. none\
  358. }; \
  359. return (const ASN1_ITEM *) &internal_adb; \
  360. } \
  361. void dummy_function(void)
  362. # endif
  363. # define ADB_ENTRY(val, template) {val, template}
  364. # define ASN1_ADB_TEMPLATE(name) \
  365. static const ASN1_TEMPLATE name##_tt
  366. /*
  367. * This is the ASN1 template structure that defines a wrapper round the
  368. * actual type. It determines the actual position of the field in the value
  369. * structure, various flags such as OPTIONAL and the field name.
  370. */
  371. struct ASN1_TEMPLATE_st {
  372. unsigned long flags; /* Various flags */
  373. long tag; /* tag, not used if no tagging */
  374. unsigned long offset; /* Offset of this field in structure */
  375. const char *field_name; /* Field name */
  376. ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
  377. };
  378. /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
  379. # define ASN1_TEMPLATE_item(t) (t->item_ptr)
  380. # define ASN1_TEMPLATE_adb(t) (t->item_ptr)
  381. typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
  382. typedef struct ASN1_ADB_st ASN1_ADB;
  383. struct ASN1_ADB_st {
  384. unsigned long flags; /* Various flags */
  385. unsigned long offset; /* Offset of selector field */
  386. int (*adb_cb)(long *psel); /* Application callback */
  387. const ASN1_ADB_TABLE *tbl; /* Table of possible types */
  388. long tblcount; /* Number of entries in tbl */
  389. const ASN1_TEMPLATE *default_tt; /* Type to use if no match */
  390. const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */
  391. };
  392. struct ASN1_ADB_TABLE_st {
  393. long value; /* NID for an object or value for an int */
  394. const ASN1_TEMPLATE tt; /* item for this value */
  395. };
  396. /* template flags */
  397. /* Field is optional */
  398. # define ASN1_TFLG_OPTIONAL (0x1)
  399. /* Field is a SET OF */
  400. # define ASN1_TFLG_SET_OF (0x1 << 1)
  401. /* Field is a SEQUENCE OF */
  402. # define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
  403. /*
  404. * Special case: this refers to a SET OF that will be sorted into DER order
  405. * when encoded *and* the corresponding STACK will be modified to match the
  406. * new order.
  407. */
  408. # define ASN1_TFLG_SET_ORDER (0x3 << 1)
  409. /* Mask for SET OF or SEQUENCE OF */
  410. # define ASN1_TFLG_SK_MASK (0x3 << 1)
  411. /*
  412. * These flags mean the tag should be taken from the tag field. If EXPLICIT
  413. * then the underlying type is used for the inner tag.
  414. */
  415. /* IMPLICIT tagging */
  416. # define ASN1_TFLG_IMPTAG (0x1 << 3)
  417. /* EXPLICIT tagging, inner tag from underlying type */
  418. # define ASN1_TFLG_EXPTAG (0x2 << 3)
  419. # define ASN1_TFLG_TAG_MASK (0x3 << 3)
  420. /* context specific IMPLICIT */
  421. # define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
  422. /* context specific EXPLICIT */
  423. # define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
  424. /*
  425. * If tagging is in force these determine the type of tag to use. Otherwise
  426. * the tag is determined by the underlying type. These values reflect the
  427. * actual octet format.
  428. */
  429. /* Universal tag */
  430. # define ASN1_TFLG_UNIVERSAL (0x0<<6)
  431. /* Application tag */
  432. # define ASN1_TFLG_APPLICATION (0x1<<6)
  433. /* Context specific tag */
  434. # define ASN1_TFLG_CONTEXT (0x2<<6)
  435. /* Private tag */
  436. # define ASN1_TFLG_PRIVATE (0x3<<6)
  437. # define ASN1_TFLG_TAG_CLASS (0x3<<6)
  438. /*
  439. * These are for ANY DEFINED BY type. In this case the 'item' field points to
  440. * an ASN1_ADB structure which contains a table of values to decode the
  441. * relevant type
  442. */
  443. # define ASN1_TFLG_ADB_MASK (0x3<<8)
  444. # define ASN1_TFLG_ADB_OID (0x1<<8)
  445. # define ASN1_TFLG_ADB_INT (0x1<<9)
  446. /*
  447. * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes
  448. * indefinite length constructed encoding to be used if required.
  449. */
  450. # define ASN1_TFLG_NDEF (0x1<<11)
  451. /* Field is embedded and not a pointer */
  452. # define ASN1_TFLG_EMBED (0x1 << 12)
  453. /* This is the actual ASN1 item itself */
  454. struct ASN1_ITEM_st {
  455. char itype; /* The item type, primitive, SEQUENCE, CHOICE
  456. * or extern */
  457. long utype; /* underlying type */
  458. const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains
  459. * the contents */
  460. long tcount; /* Number of templates if SEQUENCE or CHOICE */
  461. const void *funcs; /* functions that handle this type */
  462. long size; /* Structure size (usually) */
  463. const char *sname; /* Structure name */
  464. };
  465. /*-
  466. * These are values for the itype field and
  467. * determine how the type is interpreted.
  468. *
  469. * For PRIMITIVE types the underlying type
  470. * determines the behaviour if items is NULL.
  471. *
  472. * Otherwise templates must contain a single
  473. * template and the type is treated in the
  474. * same way as the type specified in the template.
  475. *
  476. * For SEQUENCE types the templates field points
  477. * to the members, the size field is the
  478. * structure size.
  479. *
  480. * For CHOICE types the templates field points
  481. * to each possible member (typically a union)
  482. * and the 'size' field is the offset of the
  483. * selector.
  484. *
  485. * The 'funcs' field is used for application
  486. * specific functions.
  487. *
  488. * The EXTERN type uses a new style d2i/i2d.
  489. * The new style should be used where possible
  490. * because it avoids things like the d2i IMPLICIT
  491. * hack.
  492. *
  493. * MSTRING is a multiple string type, it is used
  494. * for a CHOICE of character strings where the
  495. * actual strings all occupy an ASN1_STRING
  496. * structure. In this case the 'utype' field
  497. * has a special meaning, it is used as a mask
  498. * of acceptable types using the B_ASN1 constants.
  499. *
  500. * NDEF_SEQUENCE is the same as SEQUENCE except
  501. * that it will use indefinite length constructed
  502. * encoding if requested.
  503. *
  504. */
  505. # define ASN1_ITYPE_PRIMITIVE 0x0
  506. # define ASN1_ITYPE_SEQUENCE 0x1
  507. # define ASN1_ITYPE_CHOICE 0x2
  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 int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
  525. const ASN1_ITEM *it, int tag, int aclass, char opt,
  526. ASN1_TLC *ctx);
  527. typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  528. const ASN1_ITEM *it, int tag, int aclass);
  529. typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
  530. typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
  531. typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
  532. int indent, const char *fname,
  533. const ASN1_PCTX *pctx);
  534. typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont,
  535. int *putype, const ASN1_ITEM *it);
  536. typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
  537. int len, int utype, char *free_cont,
  538. const ASN1_ITEM *it);
  539. typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval,
  540. const ASN1_ITEM *it, int indent,
  541. const ASN1_PCTX *pctx);
  542. typedef struct ASN1_EXTERN_FUNCS_st {
  543. void *app_data;
  544. ASN1_ex_new_func *asn1_ex_new;
  545. ASN1_ex_free_func *asn1_ex_free;
  546. ASN1_ex_free_func *asn1_ex_clear;
  547. ASN1_ex_d2i *asn1_ex_d2i;
  548. ASN1_ex_i2d *asn1_ex_i2d;
  549. ASN1_ex_print_func *asn1_ex_print;
  550. } ASN1_EXTERN_FUNCS;
  551. typedef struct ASN1_PRIMITIVE_FUNCS_st {
  552. void *app_data;
  553. unsigned long flags;
  554. ASN1_ex_new_func *prim_new;
  555. ASN1_ex_free_func *prim_free;
  556. ASN1_ex_free_func *prim_clear;
  557. ASN1_primitive_c2i *prim_c2i;
  558. ASN1_primitive_i2c *prim_i2c;
  559. ASN1_primitive_print *prim_print;
  560. } ASN1_PRIMITIVE_FUNCS;
  561. /*
  562. * This is the ASN1_AUX structure: it handles various miscellaneous
  563. * requirements. For example the use of reference counts and an informational
  564. * callback. The "informational callback" is called at various points during
  565. * the ASN1 encoding and decoding. It can be used to provide minor
  566. * customisation of the structures used. This is most useful where the
  567. * supplied routines *almost* do the right thing but need some extra help at
  568. * a few points. If the callback returns zero then it is assumed a fatal
  569. * error has occurred and the main operation should be abandoned. If major
  570. * changes in the default behaviour are required then an external type is
  571. * more appropriate.
  572. */
  573. typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
  574. void *exarg);
  575. typedef struct ASN1_AUX_st {
  576. void *app_data;
  577. int flags;
  578. int ref_offset; /* Offset of reference value */
  579. int ref_lock; /* Lock type to use */
  580. ASN1_aux_cb *asn1_cb;
  581. int enc_offset; /* Offset of ASN1_ENCODING structure */
  582. } ASN1_AUX;
  583. /* For print related callbacks exarg points to this structure */
  584. typedef struct ASN1_PRINT_ARG_st {
  585. BIO *out;
  586. int indent;
  587. const ASN1_PCTX *pctx;
  588. } ASN1_PRINT_ARG;
  589. /* For streaming related callbacks exarg points to this structure */
  590. typedef struct ASN1_STREAM_ARG_st {
  591. /* BIO to stream through */
  592. BIO *out;
  593. /* BIO with filters appended */
  594. BIO *ndef_bio;
  595. /* Streaming I/O boundary */
  596. unsigned char **boundary;
  597. } ASN1_STREAM_ARG;
  598. /* Flags in ASN1_AUX */
  599. /* Use a reference count */
  600. # define ASN1_AFLG_REFCOUNT 1
  601. /* Save the encoding of structure (useful for signatures) */
  602. # define ASN1_AFLG_ENCODING 2
  603. /* The Sequence length is invalid */
  604. # define ASN1_AFLG_BROKEN 4
  605. /* operation values for asn1_cb */
  606. # define ASN1_OP_NEW_PRE 0
  607. # define ASN1_OP_NEW_POST 1
  608. # define ASN1_OP_FREE_PRE 2
  609. # define ASN1_OP_FREE_POST 3
  610. # define ASN1_OP_D2I_PRE 4
  611. # define ASN1_OP_D2I_POST 5
  612. # define ASN1_OP_I2D_PRE 6
  613. # define ASN1_OP_I2D_POST 7
  614. # define ASN1_OP_PRINT_PRE 8
  615. # define ASN1_OP_PRINT_POST 9
  616. # define ASN1_OP_STREAM_PRE 10
  617. # define ASN1_OP_STREAM_POST 11
  618. # define ASN1_OP_DETACHED_PRE 12
  619. # define ASN1_OP_DETACHED_POST 13
  620. /* Macro to implement a primitive type */
  621. # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
  622. # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
  623. ASN1_ITEM_start(itname) \
  624. ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
  625. ASN1_ITEM_end(itname)
  626. /* Macro to implement a multi string type */
  627. # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
  628. ASN1_ITEM_start(itname) \
  629. ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
  630. ASN1_ITEM_end(itname)
  631. # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
  632. ASN1_ITEM_start(sname) \
  633. ASN1_ITYPE_EXTERN, \
  634. tag, \
  635. NULL, \
  636. 0, \
  637. &fptrs, \
  638. 0, \
  639. #sname \
  640. ASN1_ITEM_end(sname)
  641. /* Macro to implement standard functions in terms of ASN1_ITEM structures */
  642. # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
  643. # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
  644. # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
  645. IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
  646. # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
  647. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
  648. # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
  649. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
  650. # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
  651. pre stname *fname##_new(void) \
  652. { \
  653. return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
  654. } \
  655. pre void fname##_free(stname *a) \
  656. { \
  657. ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
  658. }
  659. # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
  660. stname *fname##_new(void) \
  661. { \
  662. return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
  663. } \
  664. void fname##_free(stname *a) \
  665. { \
  666. ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
  667. }
  668. # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
  669. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
  670. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
  671. # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
  672. stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
  673. { \
  674. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
  675. } \
  676. int i2d_##fname(stname *a, unsigned char **out) \
  677. { \
  678. return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
  679. }
  680. # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
  681. int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
  682. { \
  683. return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
  684. }
  685. # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
  686. static stname *d2i_##stname(stname **a, \
  687. const unsigned char **in, long len) \
  688. { \
  689. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
  690. ASN1_ITEM_rptr(stname)); \
  691. } \
  692. static int i2d_##stname(stname *a, unsigned char **out) \
  693. { \
  694. return ASN1_item_i2d((ASN1_VALUE *)a, out, \
  695. ASN1_ITEM_rptr(stname)); \
  696. }
  697. /*
  698. * This includes evil casts to remove const: they will go away when full ASN1
  699. * constification is done.
  700. */
  701. # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
  702. stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
  703. { \
  704. return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
  705. } \
  706. int i2d_##fname(const stname *a, unsigned char **out) \
  707. { \
  708. return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
  709. }
  710. # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
  711. stname * stname##_dup(stname *x) \
  712. { \
  713. return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
  714. }
  715. # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
  716. IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
  717. # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
  718. int fname##_print_ctx(BIO *out, stname *x, int indent, \
  719. const ASN1_PCTX *pctx) \
  720. { \
  721. return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
  722. ASN1_ITEM_rptr(itname), pctx); \
  723. }
  724. # define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
  725. IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
  726. # define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
  727. IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
  728. IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
  729. /* external definitions for primitive types */
  730. DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
  731. DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
  732. DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
  733. DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
  734. DECLARE_ASN1_ITEM(CBIGNUM)
  735. DECLARE_ASN1_ITEM(BIGNUM)
  736. DECLARE_ASN1_ITEM(LONG)
  737. DECLARE_ASN1_ITEM(ZLONG)
  738. DEFINE_STACK_OF(ASN1_VALUE)
  739. /* Functions used internally by the ASN1 code */
  740. int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  741. void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  742. int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
  743. const ASN1_ITEM *it, int tag, int aclass, char opt,
  744. ASN1_TLC *ctx);
  745. int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  746. const ASN1_ITEM *it, int tag, int aclass);
  747. #ifdef __cplusplus
  748. }
  749. #endif
  750. #endif