asn1_mac.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* crypto/asn1/asn1_mac.h */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #ifndef HEADER_ASN1_MAC_H
  59. # define HEADER_ASN1_MAC_H
  60. # include <openssl/asn1.h>
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. # ifndef ASN1_MAC_ERR_LIB
  65. # define ASN1_MAC_ERR_LIB ERR_LIB_ASN1
  66. # endif
  67. # define ASN1_MAC_H_err(f,r,line) \
  68. ERR_PUT_error(ASN1_MAC_ERR_LIB,(f),(r),__FILE__,(line))
  69. # define M_ASN1_D2I_vars(a,type,func) \
  70. ASN1_const_CTX c; \
  71. type ret=NULL; \
  72. \
  73. c.pp=(const unsigned char **)pp; \
  74. c.q= *(const unsigned char **)pp; \
  75. c.error=ERR_R_NESTED_ASN1_ERROR; \
  76. if ((a == NULL) || ((*a) == NULL)) \
  77. { if ((ret=(type)func()) == NULL) \
  78. { c.line=__LINE__; goto err; } } \
  79. else ret=(*a);
  80. # define M_ASN1_D2I_Init() \
  81. c.p= *(const unsigned char **)pp; \
  82. c.max=(length == 0)?0:(c.p+length);
  83. # define M_ASN1_D2I_Finish_2(a) \
  84. if (!asn1_const_Finish(&c)) \
  85. { c.line=__LINE__; goto err; } \
  86. *(const unsigned char **)pp=c.p; \
  87. if (a != NULL) (*a)=ret; \
  88. return(ret);
  89. # define M_ASN1_D2I_Finish(a,func,e) \
  90. M_ASN1_D2I_Finish_2(a); \
  91. err:\
  92. ASN1_MAC_H_err((e),c.error,c.line); \
  93. asn1_add_error(*(const unsigned char **)pp,(int)(c.q- *pp)); \
  94. if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
  95. return(NULL)
  96. # define M_ASN1_D2I_start_sequence() \
  97. if (!asn1_GetSequence(&c,&length)) \
  98. { c.line=__LINE__; goto err; }
  99. /* Begin reading ASN1 without a surrounding sequence */
  100. # define M_ASN1_D2I_begin() \
  101. c.slen = length;
  102. /* End reading ASN1 with no check on length */
  103. # define M_ASN1_D2I_Finish_nolen(a, func, e) \
  104. *pp=c.p; \
  105. if (a != NULL) (*a)=ret; \
  106. return(ret); \
  107. err:\
  108. ASN1_MAC_H_err((e),c.error,c.line); \
  109. asn1_add_error(*pp,(int)(c.q- *pp)); \
  110. if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
  111. return(NULL)
  112. # define M_ASN1_D2I_end_sequence() \
  113. (((c.inf&1) == 0)?(c.slen <= 0): \
  114. (c.eos=ASN1_const_check_infinite_end(&c.p,c.slen)))
  115. /* Don't use this with d2i_ASN1_BOOLEAN() */
  116. # define M_ASN1_D2I_get(b, func) \
  117. c.q=c.p; \
  118. if (func(&(b),&c.p,c.slen) == NULL) \
  119. {c.line=__LINE__; goto err; } \
  120. c.slen-=(c.p-c.q);
  121. /* Don't use this with d2i_ASN1_BOOLEAN() */
  122. # define M_ASN1_D2I_get_x(type,b,func) \
  123. c.q=c.p; \
  124. if (((D2I_OF(type))func)(&(b),&c.p,c.slen) == NULL) \
  125. {c.line=__LINE__; goto err; } \
  126. c.slen-=(c.p-c.q);
  127. /* use this instead () */
  128. # define M_ASN1_D2I_get_int(b,func) \
  129. c.q=c.p; \
  130. if (func(&(b),&c.p,c.slen) < 0) \
  131. {c.line=__LINE__; goto err; } \
  132. c.slen-=(c.p-c.q);
  133. # define M_ASN1_D2I_get_opt(b,func,type) \
  134. if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
  135. == (V_ASN1_UNIVERSAL|(type)))) \
  136. { \
  137. M_ASN1_D2I_get(b,func); \
  138. }
  139. # define M_ASN1_D2I_get_imp(b,func, type) \
  140. M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \
  141. c.q=c.p; \
  142. if (func(&(b),&c.p,c.slen) == NULL) \
  143. {c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \
  144. c.slen-=(c.p-c.q);\
  145. M_ASN1_next_prev=_tmp;
  146. # define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
  147. if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
  148. (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
  149. { \
  150. unsigned char _tmp = M_ASN1_next; \
  151. M_ASN1_D2I_get_imp(b,func, type);\
  152. }
  153. # define M_ASN1_D2I_get_set(r,func,free_func) \
  154. M_ASN1_D2I_get_imp_set(r,func,free_func, \
  155. V_ASN1_SET,V_ASN1_UNIVERSAL);
  156. # define M_ASN1_D2I_get_set_type(type,r,func,free_func) \
  157. M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \
  158. V_ASN1_SET,V_ASN1_UNIVERSAL);
  159. # define M_ASN1_D2I_get_set_opt(r,func,free_func) \
  160. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  161. V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
  162. { M_ASN1_D2I_get_set(r,func,free_func); }
  163. # define M_ASN1_D2I_get_set_opt_type(type,r,func,free_func) \
  164. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  165. V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
  166. { M_ASN1_D2I_get_set_type(type,r,func,free_func); }
  167. # define M_ASN1_I2D_len_SET_opt(a,f) \
  168. if ((a != NULL) && (sk_num(a) != 0)) \
  169. M_ASN1_I2D_len_SET(a,f);
  170. # define M_ASN1_I2D_put_SET_opt(a,f) \
  171. if ((a != NULL) && (sk_num(a) != 0)) \
  172. M_ASN1_I2D_put_SET(a,f);
  173. # define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
  174. if ((a != NULL) && (sk_num(a) != 0)) \
  175. M_ASN1_I2D_put_SEQUENCE(a,f);
  176. # define M_ASN1_I2D_put_SEQUENCE_opt_type(type,a,f) \
  177. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  178. M_ASN1_I2D_put_SEQUENCE_type(type,a,f);
  179. # define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \
  180. if ((c.slen != 0) && \
  181. (M_ASN1_next == \
  182. (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
  183. { \
  184. M_ASN1_D2I_get_imp_set(b,func,free_func,\
  185. tag,V_ASN1_CONTEXT_SPECIFIC); \
  186. }
  187. # define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \
  188. if ((c.slen != 0) && \
  189. (M_ASN1_next == \
  190. (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
  191. { \
  192. M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\
  193. tag,V_ASN1_CONTEXT_SPECIFIC); \
  194. }
  195. # define M_ASN1_D2I_get_seq(r,func,free_func) \
  196. M_ASN1_D2I_get_imp_set(r,func,free_func,\
  197. V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
  198. # define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \
  199. M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
  200. V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
  201. # define M_ASN1_D2I_get_seq_opt(r,func,free_func) \
  202. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  203. V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
  204. { M_ASN1_D2I_get_seq(r,func,free_func); }
  205. # define M_ASN1_D2I_get_seq_opt_type(type,r,func,free_func) \
  206. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  207. V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
  208. { M_ASN1_D2I_get_seq_type(type,r,func,free_func); }
  209. # define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \
  210. M_ASN1_D2I_get_imp_set(r,func,free_func,\
  211. x,V_ASN1_CONTEXT_SPECIFIC);
  212. # define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \
  213. M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
  214. x,V_ASN1_CONTEXT_SPECIFIC);
  215. # define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \
  216. c.q=c.p; \
  217. if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,\
  218. (void (*)())free_func,a,b) == NULL) \
  219. { c.line=__LINE__; goto err; } \
  220. c.slen-=(c.p-c.q);
  221. # define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \
  222. c.q=c.p; \
  223. if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\
  224. free_func,a,b) == NULL) \
  225. { c.line=__LINE__; goto err; } \
  226. c.slen-=(c.p-c.q);
  227. # define M_ASN1_D2I_get_set_strings(r,func,a,b) \
  228. c.q=c.p; \
  229. if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
  230. { c.line=__LINE__; goto err; } \
  231. c.slen-=(c.p-c.q);
  232. # define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
  233. if ((c.slen != 0L) && (M_ASN1_next == \
  234. (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
  235. { \
  236. int Tinf,Ttag,Tclass; \
  237. long Tlen; \
  238. \
  239. c.q=c.p; \
  240. Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
  241. if (Tinf & 0x80) \
  242. { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
  243. c.line=__LINE__; goto err; } \
  244. if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
  245. Tlen = c.slen - (c.p - c.q) - 2; \
  246. if (func(&(r),&c.p,Tlen) == NULL) \
  247. { c.line=__LINE__; goto err; } \
  248. if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
  249. Tlen = c.slen - (c.p - c.q); \
  250. if(!ASN1_const_check_infinite_end(&c.p, Tlen)) \
  251. { c.error=ERR_R_MISSING_ASN1_EOS; \
  252. c.line=__LINE__; goto err; } \
  253. }\
  254. c.slen-=(c.p-c.q); \
  255. }
  256. # define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \
  257. if ((c.slen != 0) && (M_ASN1_next == \
  258. (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
  259. { \
  260. int Tinf,Ttag,Tclass; \
  261. long Tlen; \
  262. \
  263. c.q=c.p; \
  264. Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
  265. if (Tinf & 0x80) \
  266. { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
  267. c.line=__LINE__; goto err; } \
  268. if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
  269. Tlen = c.slen - (c.p - c.q) - 2; \
  270. if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
  271. (void (*)())free_func, \
  272. b,V_ASN1_UNIVERSAL) == NULL) \
  273. { c.line=__LINE__; goto err; } \
  274. if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
  275. Tlen = c.slen - (c.p - c.q); \
  276. if(!ASN1_check_infinite_end(&c.p, Tlen)) \
  277. { c.error=ERR_R_MISSING_ASN1_EOS; \
  278. c.line=__LINE__; goto err; } \
  279. }\
  280. c.slen-=(c.p-c.q); \
  281. }
  282. # define M_ASN1_D2I_get_EXP_set_opt_type(type,r,func,free_func,tag,b) \
  283. if ((c.slen != 0) && (M_ASN1_next == \
  284. (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
  285. { \
  286. int Tinf,Ttag,Tclass; \
  287. long Tlen; \
  288. \
  289. c.q=c.p; \
  290. Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
  291. if (Tinf & 0x80) \
  292. { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
  293. c.line=__LINE__; goto err; } \
  294. if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
  295. Tlen = c.slen - (c.p - c.q) - 2; \
  296. if (d2i_ASN1_SET_OF_##type(&(r),&c.p,Tlen,func, \
  297. free_func,b,V_ASN1_UNIVERSAL) == NULL) \
  298. { c.line=__LINE__; goto err; } \
  299. if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
  300. Tlen = c.slen - (c.p - c.q); \
  301. if(!ASN1_check_infinite_end(&c.p, Tlen)) \
  302. { c.error=ERR_R_MISSING_ASN1_EOS; \
  303. c.line=__LINE__; goto err; } \
  304. }\
  305. c.slen-=(c.p-c.q); \
  306. }
  307. /* New macros */
  308. # define M_ASN1_New_Malloc(ret,type) \
  309. if ((ret=(type *)OPENSSL_malloc(sizeof(type))) == NULL) \
  310. { c.line=__LINE__; goto err2; }
  311. # define M_ASN1_New(arg,func) \
  312. if (((arg)=func()) == NULL) return(NULL)
  313. # define M_ASN1_New_Error(a) \
  314. /*- err: ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
  315. return(NULL);*/ \
  316. err2: ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
  317. return(NULL)
  318. /*
  319. * BIG UGLY WARNING! This is so damn ugly I wanna puke. Unfortunately, some
  320. * macros that use ASN1_const_CTX still insist on writing in the input
  321. * stream. ARGH! ARGH! ARGH! Let's get rid of this macro package. Please? --
  322. * Richard Levitte
  323. */
  324. # define M_ASN1_next (*((unsigned char *)(c.p)))
  325. # define M_ASN1_next_prev (*((unsigned char *)(c.q)))
  326. /*************************************************/
  327. # define M_ASN1_I2D_vars(a) int r=0,ret=0; \
  328. unsigned char *p; \
  329. if (a == NULL) return(0)
  330. /* Length Macros */
  331. # define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
  332. # define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
  333. # define M_ASN1_I2D_len_SET(a,f) \
  334. ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
  335. # define M_ASN1_I2D_len_SET_type(type,a,f) \
  336. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \
  337. V_ASN1_UNIVERSAL,IS_SET);
  338. # define M_ASN1_I2D_len_SEQUENCE(a,f) \
  339. ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
  340. IS_SEQUENCE);
  341. # define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
  342. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
  343. V_ASN1_UNIVERSAL,IS_SEQUENCE)
  344. # define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
  345. if ((a != NULL) && (sk_num(a) != 0)) \
  346. M_ASN1_I2D_len_SEQUENCE(a,f);
  347. # define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \
  348. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  349. M_ASN1_I2D_len_SEQUENCE_type(type,a,f);
  350. # define M_ASN1_I2D_len_IMP_SET(a,f,x) \
  351. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  352. # define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
  353. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  354. V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  355. # define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
  356. if ((a != NULL) && (sk_num(a) != 0)) \
  357. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  358. IS_SET);
  359. # define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
  360. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  361. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  362. V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  363. # define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
  364. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  365. IS_SEQUENCE);
  366. # define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \
  367. if ((a != NULL) && (sk_num(a) != 0)) \
  368. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  369. IS_SEQUENCE);
  370. # define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
  371. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  372. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  373. V_ASN1_CONTEXT_SPECIFIC, \
  374. IS_SEQUENCE);
  375. # define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
  376. if (a != NULL)\
  377. { \
  378. v=f(a,NULL); \
  379. ret+=ASN1_object_size(1,v,mtag); \
  380. }
  381. # define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
  382. if ((a != NULL) && (sk_num(a) != 0))\
  383. { \
  384. v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
  385. ret+=ASN1_object_size(1,v,mtag); \
  386. }
  387. # define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
  388. if ((a != NULL) && (sk_num(a) != 0))\
  389. { \
  390. v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \
  391. IS_SEQUENCE); \
  392. ret+=ASN1_object_size(1,v,mtag); \
  393. }
  394. # define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
  395. if ((a != NULL) && (sk_##type##_num(a) != 0))\
  396. { \
  397. v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \
  398. V_ASN1_UNIVERSAL, \
  399. IS_SEQUENCE); \
  400. ret+=ASN1_object_size(1,v,mtag); \
  401. }
  402. /* Put Macros */
  403. # define M_ASN1_I2D_put(a,f) f(a,&p)
  404. # define M_ASN1_I2D_put_IMP_opt(a,f,t) \
  405. if (a != NULL) \
  406. { \
  407. unsigned char *q=p; \
  408. f(a,&p); \
  409. *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
  410. }
  411. # define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
  412. V_ASN1_UNIVERSAL,IS_SET)
  413. # define M_ASN1_I2D_put_SET_type(type,a,f) \
  414. i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET)
  415. # define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
  416. V_ASN1_CONTEXT_SPECIFIC,IS_SET)
  417. # define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
  418. i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
  419. # define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
  420. V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
  421. # define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
  422. V_ASN1_UNIVERSAL,IS_SEQUENCE)
  423. # define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
  424. i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
  425. IS_SEQUENCE)
  426. # define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
  427. if ((a != NULL) && (sk_num(a) != 0)) \
  428. M_ASN1_I2D_put_SEQUENCE(a,f);
  429. # define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
  430. if ((a != NULL) && (sk_num(a) != 0)) \
  431. { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  432. IS_SET); }
  433. # define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
  434. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  435. { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
  436. V_ASN1_CONTEXT_SPECIFIC, \
  437. IS_SET); }
  438. # define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
  439. if ((a != NULL) && (sk_num(a) != 0)) \
  440. { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  441. IS_SEQUENCE); }
  442. # define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
  443. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  444. { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
  445. V_ASN1_CONTEXT_SPECIFIC, \
  446. IS_SEQUENCE); }
  447. # define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
  448. if (a != NULL) \
  449. { \
  450. ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
  451. f(a,&p); \
  452. }
  453. # define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
  454. if ((a != NULL) && (sk_num(a) != 0)) \
  455. { \
  456. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  457. i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
  458. }
  459. # define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
  460. if ((a != NULL) && (sk_num(a) != 0)) \
  461. { \
  462. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  463. i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \
  464. }
  465. # define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
  466. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  467. { \
  468. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  469. i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \
  470. IS_SEQUENCE); \
  471. }
  472. # define M_ASN1_I2D_seq_total() \
  473. r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
  474. if (pp == NULL) return(r); \
  475. p= *pp; \
  476. ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
  477. # define M_ASN1_I2D_INF_seq_start(tag,ctx) \
  478. *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
  479. *(p++)=0x80
  480. # define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
  481. # define M_ASN1_I2D_finish() *pp=p; \
  482. return(r);
  483. int asn1_GetSequence(ASN1_const_CTX *c, long *length);
  484. void asn1_add_error(const unsigned char *address, int offset);
  485. #ifdef __cplusplus
  486. }
  487. #endif
  488. #endif