asn1_mac.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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_int_opt(b,func,type) \
  140. if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
  141. == (V_ASN1_UNIVERSAL|(type)))) \
  142. { \
  143. M_ASN1_D2I_get_int(b,func); \
  144. }
  145. #define M_ASN1_D2I_get_imp(b,func, type) \
  146. M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \
  147. c.q=c.p; \
  148. if (func(&(b),&c.p,c.slen) == NULL) \
  149. {c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \
  150. c.slen-=(c.p-c.q);\
  151. M_ASN1_next_prev=_tmp;
  152. #define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
  153. if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
  154. (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
  155. { \
  156. unsigned char _tmp = M_ASN1_next; \
  157. M_ASN1_D2I_get_imp(b,func, type);\
  158. }
  159. #define M_ASN1_D2I_get_set(r,func,free_func) \
  160. M_ASN1_D2I_get_imp_set(r,func,free_func, \
  161. V_ASN1_SET,V_ASN1_UNIVERSAL);
  162. #define M_ASN1_D2I_get_set_type(type,r,func,free_func) \
  163. M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \
  164. V_ASN1_SET,V_ASN1_UNIVERSAL);
  165. #define M_ASN1_D2I_get_set_opt(r,func,free_func) \
  166. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  167. V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
  168. { M_ASN1_D2I_get_set(r,func,free_func); }
  169. #define M_ASN1_D2I_get_set_opt_type(type,r,func,free_func) \
  170. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  171. V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
  172. { M_ASN1_D2I_get_set_type(type,r,func,free_func); }
  173. #define M_ASN1_I2D_len_SET_opt(a,f) \
  174. if ((a != NULL) && (sk_num(a) != 0)) \
  175. M_ASN1_I2D_len_SET(a,f);
  176. #define M_ASN1_I2D_put_SET_opt(a,f) \
  177. if ((a != NULL) && (sk_num(a) != 0)) \
  178. M_ASN1_I2D_put_SET(a,f);
  179. #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
  180. if ((a != NULL) && (sk_num(a) != 0)) \
  181. M_ASN1_I2D_put_SEQUENCE(a,f);
  182. #define M_ASN1_I2D_put_SEQUENCE_opt_type(type,a,f) \
  183. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  184. M_ASN1_I2D_put_SEQUENCE_type(type,a,f);
  185. #define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \
  186. if ((c.slen != 0) && \
  187. (M_ASN1_next == \
  188. (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
  189. { \
  190. M_ASN1_D2I_get_imp_set(b,func,free_func,\
  191. tag,V_ASN1_CONTEXT_SPECIFIC); \
  192. }
  193. #define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \
  194. if ((c.slen != 0) && \
  195. (M_ASN1_next == \
  196. (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
  197. { \
  198. M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\
  199. tag,V_ASN1_CONTEXT_SPECIFIC); \
  200. }
  201. #define M_ASN1_D2I_get_seq(r,func,free_func) \
  202. M_ASN1_D2I_get_imp_set(r,func,free_func,\
  203. V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
  204. #define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \
  205. M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
  206. V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
  207. #define M_ASN1_D2I_get_seq_opt(r,func,free_func) \
  208. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  209. V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
  210. { M_ASN1_D2I_get_seq(r,func,free_func); }
  211. #define M_ASN1_D2I_get_seq_opt_type(type,r,func,free_func) \
  212. if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
  213. V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
  214. { M_ASN1_D2I_get_seq_type(type,r,func,free_func); }
  215. #define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \
  216. M_ASN1_D2I_get_imp_set(r,func,free_func,\
  217. x,V_ASN1_CONTEXT_SPECIFIC);
  218. #define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \
  219. M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
  220. x,V_ASN1_CONTEXT_SPECIFIC);
  221. #define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \
  222. c.q=c.p; \
  223. if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,\
  224. (void (*)())free_func,a,b) == NULL) \
  225. { c.line=__LINE__; goto err; } \
  226. c.slen-=(c.p-c.q);
  227. #define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \
  228. c.q=c.p; \
  229. if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\
  230. free_func,a,b) == NULL) \
  231. { c.line=__LINE__; goto err; } \
  232. c.slen-=(c.p-c.q);
  233. #define M_ASN1_D2I_get_set_strings(r,func,a,b) \
  234. c.q=c.p; \
  235. if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
  236. { c.line=__LINE__; goto err; } \
  237. c.slen-=(c.p-c.q);
  238. #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
  239. if ((c.slen != 0L) && (M_ASN1_next == \
  240. (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
  241. { \
  242. int Tinf,Ttag,Tclass; \
  243. long Tlen; \
  244. \
  245. c.q=c.p; \
  246. Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
  247. if (Tinf & 0x80) \
  248. { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
  249. c.line=__LINE__; goto err; } \
  250. if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
  251. Tlen = c.slen - (c.p - c.q) - 2; \
  252. if (func(&(r),&c.p,Tlen) == NULL) \
  253. { c.line=__LINE__; goto err; } \
  254. if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
  255. Tlen = c.slen - (c.p - c.q); \
  256. if(!ASN1_const_check_infinite_end(&c.p, Tlen)) \
  257. { c.error=ERR_R_MISSING_ASN1_EOS; \
  258. c.line=__LINE__; goto err; } \
  259. }\
  260. c.slen-=(c.p-c.q); \
  261. }
  262. #define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \
  263. if ((c.slen != 0) && (M_ASN1_next == \
  264. (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
  265. { \
  266. int Tinf,Ttag,Tclass; \
  267. long Tlen; \
  268. \
  269. c.q=c.p; \
  270. Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
  271. if (Tinf & 0x80) \
  272. { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
  273. c.line=__LINE__; goto err; } \
  274. if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
  275. Tlen = c.slen - (c.p - c.q) - 2; \
  276. if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
  277. (void (*)())free_func, \
  278. b,V_ASN1_UNIVERSAL) == NULL) \
  279. { c.line=__LINE__; goto err; } \
  280. if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
  281. Tlen = c.slen - (c.p - c.q); \
  282. if(!ASN1_check_infinite_end(&c.p, Tlen)) \
  283. { c.error=ERR_R_MISSING_ASN1_EOS; \
  284. c.line=__LINE__; goto err; } \
  285. }\
  286. c.slen-=(c.p-c.q); \
  287. }
  288. #define M_ASN1_D2I_get_EXP_set_opt_type(type,r,func,free_func,tag,b) \
  289. if ((c.slen != 0) && (M_ASN1_next == \
  290. (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
  291. { \
  292. int Tinf,Ttag,Tclass; \
  293. long Tlen; \
  294. \
  295. c.q=c.p; \
  296. Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
  297. if (Tinf & 0x80) \
  298. { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
  299. c.line=__LINE__; goto err; } \
  300. if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
  301. Tlen = c.slen - (c.p - c.q) - 2; \
  302. if (d2i_ASN1_SET_OF_##type(&(r),&c.p,Tlen,func, \
  303. free_func,b,V_ASN1_UNIVERSAL) == NULL) \
  304. { c.line=__LINE__; goto err; } \
  305. if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
  306. Tlen = c.slen - (c.p - c.q); \
  307. if(!ASN1_check_infinite_end(&c.p, Tlen)) \
  308. { c.error=ERR_R_MISSING_ASN1_EOS; \
  309. c.line=__LINE__; goto err; } \
  310. }\
  311. c.slen-=(c.p-c.q); \
  312. }
  313. /* New macros */
  314. #define M_ASN1_New_Malloc(ret,type) \
  315. if ((ret=(type *)OPENSSL_malloc(sizeof(type))) == NULL) \
  316. { c.line=__LINE__; goto err2; }
  317. #define M_ASN1_New(arg,func) \
  318. if (((arg)=func()) == NULL) return(NULL)
  319. #define M_ASN1_New_Error(a) \
  320. /* err: ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
  321. return(NULL);*/ \
  322. err2: ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
  323. return(NULL)
  324. /* BIG UGLY WARNING! This is so damn ugly I wanna puke. Unfortunately,
  325. some macros that use ASN1_const_CTX still insist on writing in the input
  326. stream. ARGH! ARGH! ARGH! Let's get rid of this macro package.
  327. Please? -- Richard Levitte */
  328. #define M_ASN1_next (*((unsigned char *)(c.p)))
  329. #define M_ASN1_next_prev (*((unsigned char *)(c.q)))
  330. /*************************************************/
  331. #define M_ASN1_I2D_vars(a) int r=0,ret=0; \
  332. unsigned char *p; \
  333. if (a == NULL) return(0)
  334. /* Length Macros */
  335. #define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
  336. #define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
  337. #define M_ASN1_I2D_len_SET(a,f) \
  338. ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
  339. #define M_ASN1_I2D_len_SET_type(type,a,f) \
  340. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \
  341. V_ASN1_UNIVERSAL,IS_SET);
  342. #define M_ASN1_I2D_len_SEQUENCE(a,f) \
  343. ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
  344. IS_SEQUENCE);
  345. #define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
  346. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
  347. V_ASN1_UNIVERSAL,IS_SEQUENCE)
  348. #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
  349. if ((a != NULL) && (sk_num(a) != 0)) \
  350. M_ASN1_I2D_len_SEQUENCE(a,f);
  351. #define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \
  352. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  353. M_ASN1_I2D_len_SEQUENCE_type(type,a,f);
  354. #define M_ASN1_I2D_len_IMP_SET(a,f,x) \
  355. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  356. #define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
  357. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  358. V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  359. #define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
  360. if ((a != NULL) && (sk_num(a) != 0)) \
  361. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  362. IS_SET);
  363. #define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
  364. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  365. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  366. V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  367. #define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
  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(a,f,x) \
  371. if ((a != NULL) && (sk_num(a) != 0)) \
  372. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  373. IS_SEQUENCE);
  374. #define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
  375. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  376. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  377. V_ASN1_CONTEXT_SPECIFIC, \
  378. IS_SEQUENCE);
  379. #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
  380. if (a != NULL)\
  381. { \
  382. v=f(a,NULL); \
  383. ret+=ASN1_object_size(1,v,mtag); \
  384. }
  385. #define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
  386. if ((a != NULL) && (sk_num(a) != 0))\
  387. { \
  388. v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
  389. ret+=ASN1_object_size(1,v,mtag); \
  390. }
  391. #define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
  392. if ((a != NULL) && (sk_num(a) != 0))\
  393. { \
  394. v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \
  395. IS_SEQUENCE); \
  396. ret+=ASN1_object_size(1,v,mtag); \
  397. }
  398. #define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
  399. if ((a != NULL) && (sk_##type##_num(a) != 0))\
  400. { \
  401. v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \
  402. V_ASN1_UNIVERSAL, \
  403. IS_SEQUENCE); \
  404. ret+=ASN1_object_size(1,v,mtag); \
  405. }
  406. /* Put Macros */
  407. #define M_ASN1_I2D_put(a,f) f(a,&p)
  408. #define M_ASN1_I2D_put_IMP_opt(a,f,t) \
  409. if (a != NULL) \
  410. { \
  411. unsigned char *q=p; \
  412. f(a,&p); \
  413. *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
  414. }
  415. #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
  416. V_ASN1_UNIVERSAL,IS_SET)
  417. #define M_ASN1_I2D_put_SET_type(type,a,f) \
  418. i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET)
  419. #define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
  420. V_ASN1_CONTEXT_SPECIFIC,IS_SET)
  421. #define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
  422. i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
  423. #define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
  424. V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
  425. #define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
  426. V_ASN1_UNIVERSAL,IS_SEQUENCE)
  427. #define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
  428. i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
  429. IS_SEQUENCE)
  430. #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
  431. if ((a != NULL) && (sk_num(a) != 0)) \
  432. M_ASN1_I2D_put_SEQUENCE(a,f);
  433. #define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
  434. if ((a != NULL) && (sk_num(a) != 0)) \
  435. { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  436. IS_SET); }
  437. #define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
  438. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  439. { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
  440. V_ASN1_CONTEXT_SPECIFIC, \
  441. IS_SET); }
  442. #define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
  443. if ((a != NULL) && (sk_num(a) != 0)) \
  444. { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  445. IS_SEQUENCE); }
  446. #define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
  447. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  448. { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
  449. V_ASN1_CONTEXT_SPECIFIC, \
  450. IS_SEQUENCE); }
  451. #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
  452. if (a != NULL) \
  453. { \
  454. ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
  455. f(a,&p); \
  456. }
  457. #define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
  458. if ((a != NULL) && (sk_num(a) != 0)) \
  459. { \
  460. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  461. i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
  462. }
  463. #define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
  464. if ((a != NULL) && (sk_num(a) != 0)) \
  465. { \
  466. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  467. i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \
  468. }
  469. #define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
  470. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  471. { \
  472. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  473. i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \
  474. IS_SEQUENCE); \
  475. }
  476. #define M_ASN1_I2D_seq_total() \
  477. r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
  478. if (pp == NULL) return(r); \
  479. p= *pp; \
  480. ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
  481. #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
  482. *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
  483. *(p++)=0x80
  484. #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
  485. #define M_ASN1_I2D_finish() *pp=p; \
  486. return(r);
  487. int asn1_GetSequence(ASN1_const_CTX *c, long *length);
  488. void asn1_add_error(const unsigned char *address,int offset);
  489. #ifdef __cplusplus
  490. }
  491. #endif
  492. #endif