25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

960 lines
29 KiB

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