asn1_mac.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. /* BIG UGLY WARNING! This is so damn ugly I wanna puke. Unfortunately,
  319. some macros that use ASN1_const_CTX still insist on writing in the input
  320. stream. ARGH! ARGH! ARGH! Let's get rid of this macro package.
  321. Please? -- Richard Levitte */
  322. #define M_ASN1_next (*((unsigned char *)(c.p)))
  323. #define M_ASN1_next_prev (*((unsigned char *)(c.q)))
  324. /*************************************************/
  325. #define M_ASN1_I2D_vars(a) int r=0,ret=0; \
  326. unsigned char *p; \
  327. if (a == NULL) return(0)
  328. /* Length Macros */
  329. #define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
  330. #define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
  331. #define M_ASN1_I2D_len_SET(a,f) \
  332. ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
  333. #define M_ASN1_I2D_len_SET_type(type,a,f) \
  334. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \
  335. V_ASN1_UNIVERSAL,IS_SET);
  336. #define M_ASN1_I2D_len_SEQUENCE(a,f) \
  337. ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
  338. IS_SEQUENCE);
  339. #define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
  340. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
  341. V_ASN1_UNIVERSAL,IS_SEQUENCE)
  342. #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
  343. if ((a != NULL) && (sk_num(a) != 0)) \
  344. M_ASN1_I2D_len_SEQUENCE(a,f);
  345. #define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \
  346. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  347. M_ASN1_I2D_len_SEQUENCE_type(type,a,f);
  348. #define M_ASN1_I2D_len_IMP_SET(a,f,x) \
  349. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  350. #define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
  351. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  352. V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  353. #define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
  354. if ((a != NULL) && (sk_num(a) != 0)) \
  355. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  356. IS_SET);
  357. #define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
  358. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  359. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  360. V_ASN1_CONTEXT_SPECIFIC,IS_SET);
  361. #define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
  362. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  363. IS_SEQUENCE);
  364. #define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \
  365. if ((a != NULL) && (sk_num(a) != 0)) \
  366. ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  367. IS_SEQUENCE);
  368. #define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
  369. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  370. ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
  371. V_ASN1_CONTEXT_SPECIFIC, \
  372. IS_SEQUENCE);
  373. #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
  374. if (a != NULL)\
  375. { \
  376. v=f(a,NULL); \
  377. ret+=ASN1_object_size(1,v,mtag); \
  378. }
  379. #define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
  380. if ((a != NULL) && (sk_num(a) != 0))\
  381. { \
  382. v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
  383. ret+=ASN1_object_size(1,v,mtag); \
  384. }
  385. #define M_ASN1_I2D_len_EXP_SEQUENCE_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, \
  389. IS_SEQUENCE); \
  390. ret+=ASN1_object_size(1,v,mtag); \
  391. }
  392. #define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
  393. if ((a != NULL) && (sk_##type##_num(a) != 0))\
  394. { \
  395. v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \
  396. V_ASN1_UNIVERSAL, \
  397. IS_SEQUENCE); \
  398. ret+=ASN1_object_size(1,v,mtag); \
  399. }
  400. /* Put Macros */
  401. #define M_ASN1_I2D_put(a,f) f(a,&p)
  402. #define M_ASN1_I2D_put_IMP_opt(a,f,t) \
  403. if (a != NULL) \
  404. { \
  405. unsigned char *q=p; \
  406. f(a,&p); \
  407. *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
  408. }
  409. #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
  410. V_ASN1_UNIVERSAL,IS_SET)
  411. #define M_ASN1_I2D_put_SET_type(type,a,f) \
  412. i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET)
  413. #define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
  414. V_ASN1_CONTEXT_SPECIFIC,IS_SET)
  415. #define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
  416. i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
  417. #define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
  418. V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
  419. #define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
  420. V_ASN1_UNIVERSAL,IS_SEQUENCE)
  421. #define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
  422. i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
  423. IS_SEQUENCE)
  424. #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
  425. if ((a != NULL) && (sk_num(a) != 0)) \
  426. M_ASN1_I2D_put_SEQUENCE(a,f);
  427. #define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
  428. if ((a != NULL) && (sk_num(a) != 0)) \
  429. { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  430. IS_SET); }
  431. #define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
  432. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  433. { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
  434. V_ASN1_CONTEXT_SPECIFIC, \
  435. IS_SET); }
  436. #define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
  437. if ((a != NULL) && (sk_num(a) != 0)) \
  438. { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
  439. IS_SEQUENCE); }
  440. #define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
  441. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  442. { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
  443. V_ASN1_CONTEXT_SPECIFIC, \
  444. IS_SEQUENCE); }
  445. #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
  446. if (a != NULL) \
  447. { \
  448. ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
  449. f(a,&p); \
  450. }
  451. #define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
  452. if ((a != NULL) && (sk_num(a) != 0)) \
  453. { \
  454. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  455. i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
  456. }
  457. #define M_ASN1_I2D_put_EXP_SEQUENCE_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_SEQUENCE); \
  462. }
  463. #define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
  464. if ((a != NULL) && (sk_##type##_num(a) != 0)) \
  465. { \
  466. ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
  467. i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \
  468. IS_SEQUENCE); \
  469. }
  470. #define M_ASN1_I2D_seq_total() \
  471. r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
  472. if (pp == NULL) return(r); \
  473. p= *pp; \
  474. ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
  475. #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
  476. *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
  477. *(p++)=0x80
  478. #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
  479. #define M_ASN1_I2D_finish() *pp=p; \
  480. return(r);
  481. int asn1_GetSequence(ASN1_const_CTX *c, long *length);
  482. void asn1_add_error(const unsigned char *address,int offset);
  483. #ifdef __cplusplus
  484. }
  485. #endif
  486. #endif