asn1_mac.h 24 KB

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