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 
59 module deimos.openssl.asn1_mac;
60 
61 import deimos.openssl._d_util;
62 
63 import deimos.openssl.err; // Needed for ERR_LIB_ASN1.
64 
65 public import deimos.openssl.asn1;
66 
67 extern (C):
68 nothrow:
69 
70 // #ifndef ASN1_MAC_ERR_LIB
71 alias ERR_LIB_ASN1 ASN1_MAC_ERR_LIB;
72 // #endif
73 
74 /+ FIXME: Not yet ported.
75 #define ASN1_MAC_H_err(f,r,line) \
76 	ERR_PUT_error(ASN1_MAC_ERR_LIB,(f),(r),__FILE__,(line))
77 
78 #define M_ASN1_D2I_vars(a,type,func) \
79 	ASN1_const_CTX c; \
80 	type ret=NULL; \
81 	\
82 	c.pp=(const(ubyte)**)pp; \
83 	c.q= *(const(ubyte)**)pp; \
84 	c.error=ERR_R_NESTED_ASN1_ERROR; \
85 	if ((a == NULL) || ((*a) == NULL)) \
86 		{ if ((ret=(type)func()) == NULL) \
87 			{ c.line=__LINE__; goto err; } } \
88 	else	ret=(*a);
89 
90 #define M_ASN1_D2I_Init() \
91 	c.p= *(const(ubyte)**)pp; \
92 	c.max=(length == 0)?0:(c.p+length);
93 
94 #define M_ASN1_D2I_Finish_2(a) \
95 	if (!asn1_const_Finish(&c)) \
96 		{ c.line=__LINE__; goto err; } \
97 	*(const(ubyte)**)pp=c.p; \
98 	if (a != NULL) (*a)=ret; \
99 	return(ret);
100 
101 #define M_ASN1_D2I_Finish(a,func,e) \
102 	M_ASN1_D2I_Finish_2(a); \
103 err:\
104 	ASN1_MAC_H_err((e),c.error,c.line); \
105 	asn1_add_error(*(const(ubyte)**)pp,(int)(c.q- *pp)); \
106 	if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
107 	return(NULL)
108 
109 #define M_ASN1_D2I_start_sequence() \
110 	if (!asn1_GetSequence(&c,&length)) \
111 		{ c.line=__LINE__; goto err; }
112 /* Begin reading ASN1 without a surrounding sequence */
113 #define M_ASN1_D2I_begin() \
114 	c.slen = length;
115 
116 /* End reading ASN1 with no check on length */
117 #define M_ASN1_D2I_Finish_nolen(a, func, e) \
118 	*pp=c.p; \
119 	if (a != NULL) (*a)=ret; \
120 	return(ret); \
121 err:\
122 	ASN1_MAC_H_err((e),c.error,c.line); \
123 	asn1_add_error(*pp,(int)(c.q- *pp)); \
124 	if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
125 	return(NULL)
126 
127 #define M_ASN1_D2I_end_sequence() \
128 	(((c.inf&1) == 0)?(c.slen <= 0): \
129 		(c.eos=ASN1_const_check_infinite_end(&c.p,c.slen)))
130 
131 /* Don't use this with d2i_ASN1_BOOLEAN() */
132 #define M_ASN1_D2I_get(b, func) \
133 	c.q=c.p; \
134 	if (func(&(b),&c.p,c.slen) == NULL) \
135 		{c.line=__LINE__; goto err; } \
136 	c.slen-=(c.p-c.q);
137 
138 /* Don't use this with d2i_ASN1_BOOLEAN() */
139 #define M_ASN1_D2I_get_x(type,b,func) \
140 	c.q=c.p; \
141 	if (((D2I_OF(type))func)(&(b),&c.p,c.slen) == NULL) \
142 		{c.line=__LINE__; goto err; } \
143 	c.slen-=(c.p-c.q);
144 
145 /* use this instead () */
146 #define M_ASN1_D2I_get_int(b,func) \
147 	c.q=c.p; \
148 	if (func(&(b),&c.p,c.slen) < 0) \
149 		{c.line=__LINE__; goto err; } \
150 	c.slen-=(c.p-c.q);
151 
152 #define M_ASN1_D2I_get_opt(b,func,type) \
153 	if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
154 		== (V_ASN1_UNIVERSAL|(type)))) \
155 		{ \
156 		M_ASN1_D2I_get(b,func); \
157 		}
158 
159 #define M_ASN1_D2I_get_int_opt(b,func,type) \
160 	if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
161 		== (V_ASN1_UNIVERSAL|(type)))) \
162 		{ \
163 		M_ASN1_D2I_get_int(b,func); \
164 		}
165 
166 #define M_ASN1_D2I_get_imp(b,func, type) \
167 	M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \
168 	c.q=c.p; \
169 	if (func(&(b),&c.p,c.slen) == NULL) \
170 		{c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \
171 	c.slen-=(c.p-c.q);\
172 	M_ASN1_next_prev=_tmp;
173 
174 #define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
175 	if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
176 		(V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
177 		{ \
178 		ubyte _tmp = M_ASN1_next; \
179 		M_ASN1_D2I_get_imp(b,func, type);\
180 		}
181 
182 #define M_ASN1_D2I_get_set(r,func,free_func) \
183 		M_ASN1_D2I_get_imp_set(r,func,free_func, \
184 			V_ASN1_SET,V_ASN1_UNIVERSAL);
185 
186 #define M_ASN1_D2I_get_set_type(type,r,func,free_func) \
187 		M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \
188 			V_ASN1_SET,V_ASN1_UNIVERSAL);
189 
190 #define M_ASN1_D2I_get_set_opt(r,func,free_func) \
191 	if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
192 		V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
193 		{ M_ASN1_D2I_get_set(r,func,free_func); }
194 
195 #define M_ASN1_D2I_get_set_opt_type(type,r,func,free_func) \
196 	if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
197 		V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
198 		{ M_ASN1_D2I_get_set_type(type,r,func,free_func); }
199 
200 #define M_ASN1_I2D_len_SET_opt(a,f) \
201 	if ((a != NULL) && (sk_num(a) != 0)) \
202 		M_ASN1_I2D_len_SET(a,f);
203 
204 #define M_ASN1_I2D_put_SET_opt(a,f) \
205 	if ((a != NULL) && (sk_num(a) != 0)) \
206 		M_ASN1_I2D_put_SET(a,f);
207 
208 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
209 	if ((a != NULL) && (sk_num(a) != 0)) \
210 		M_ASN1_I2D_put_SEQUENCE(a,f);
211 
212 #define M_ASN1_I2D_put_SEQUENCE_opt_type(type,a,f) \
213 	if ((a != NULL) && (sk_##type##_num(a) != 0)) \
214 		M_ASN1_I2D_put_SEQUENCE_type(type,a,f);
215 
216 #define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \
217 	if ((c.slen != 0) && \
218 		(M_ASN1_next == \
219 		(V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
220 		{ \
221 		M_ASN1_D2I_get_imp_set(b,func,free_func,\
222 			tag,V_ASN1_CONTEXT_SPECIFIC); \
223 		}
224 
225 #define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \
226 	if ((c.slen != 0) && \
227 		(M_ASN1_next == \
228 		(V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
229 		{ \
230 		M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\
231 			tag,V_ASN1_CONTEXT_SPECIFIC); \
232 		}
233 
234 #define M_ASN1_D2I_get_seq(r,func,free_func) \
235 		M_ASN1_D2I_get_imp_set(r,func,free_func,\
236 			V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
237 
238 #define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \
239 		M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
240 					    V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
241 
242 #define M_ASN1_D2I_get_seq_opt(r,func,free_func) \
243 	if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
244 		V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
245 		{ M_ASN1_D2I_get_seq(r,func,free_func); }
246 
247 #define M_ASN1_D2I_get_seq_opt_type(type,r,func,free_func) \
248 	if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
249 		V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
250 		{ M_ASN1_D2I_get_seq_type(type,r,func,free_func); }
251 
252 #define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \
253 		M_ASN1_D2I_get_imp_set(r,func,free_func,\
254 			x,V_ASN1_CONTEXT_SPECIFIC);
255 
256 #define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \
257 		M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
258 			x,V_ASN1_CONTEXT_SPECIFIC);
259 
260 #define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \
261 	c.q=c.p; \
262 	if (d2i_ASN1_SET(&(r),&c.p,c.slen,(ExternC!(char* function()) )func,\
263 		(ExternC!(void function()) )free_func,a,b) == NULL) \
264 		{ c.line=__LINE__; goto err; } \
265 	c.slen-=(c.p-c.q);
266 
267 #define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \
268 	c.q=c.p; \
269 	if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\
270 				   free_func,a,b) == NULL) \
271 		{ c.line=__LINE__; goto err; } \
272 	c.slen-=(c.p-c.q);
273 
274 #define M_ASN1_D2I_get_set_strings(r,func,a,b) \
275 	c.q=c.p; \
276 	if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
277 		{ c.line=__LINE__; goto err; } \
278 	c.slen-=(c.p-c.q);
279 
280 #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
281 	if ((c.slen != 0L) && (M_ASN1_next == \
282 		(V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
283 		{ \
284 		int Tinf,Ttag,Tclass; \
285 		c_long Tlen; \
286 		\
287 		c.q=c.p; \
288 		Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
289 		if (Tinf & 0x80) \
290 			{ c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
291 			c.line=__LINE__; goto err; } \
292 		if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
293 					Tlen = c.slen - (c.p - c.q) - 2; \
294 		if (func(&(r),&c.p,Tlen) == NULL) \
295 			{ c.line=__LINE__; goto err; } \
296 		if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
297 			Tlen = c.slen - (c.p - c.q); \
298 			if(!ASN1_const_check_infinite_end(&c.p, Tlen)) \
299 				{ c.error=ERR_R_MISSING_ASN1_EOS; \
300 				c.line=__LINE__; goto err; } \
301 		}\
302 		c.slen-=(c.p-c.q); \
303 		}
304 
305 #define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \
306 	if ((c.slen != 0) && (M_ASN1_next == \
307 		(V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
308 		{ \
309 		int Tinf,Ttag,Tclass; \
310 		c_long Tlen; \
311 		\
312 		c.q=c.p; \
313 		Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
314 		if (Tinf & 0x80) \
315 			{ c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
316 			c.line=__LINE__; goto err; } \
317 		if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
318 					Tlen = c.slen - (c.p - c.q) - 2; \
319 		if (d2i_ASN1_SET(&(r),&c.p,Tlen,(ExternC!(char* function()) )func, \
320 			(ExternC!(void function()) )free_func, \
321 			b,V_ASN1_UNIVERSAL) == NULL) \
322 			{ c.line=__LINE__; goto err; } \
323 		if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
324 			Tlen = c.slen - (c.p - c.q); \
325 			if(!ASN1_check_infinite_end(&c.p, Tlen)) \
326 				{ c.error=ERR_R_MISSING_ASN1_EOS; \
327 				c.line=__LINE__; goto err; } \
328 		}\
329 		c.slen-=(c.p-c.q); \
330 		}
331 
332 #define M_ASN1_D2I_get_EXP_set_opt_type(type,r,func,free_func,tag,b) \
333 	if ((c.slen != 0) && (M_ASN1_next == \
334 		(V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
335 		{ \
336 		int Tinf,Ttag,Tclass; \
337 		c_long Tlen; \
338 		\
339 		c.q=c.p; \
340 		Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
341 		if (Tinf & 0x80) \
342 			{ c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
343 			c.line=__LINE__; goto err; } \
344 		if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
345 					Tlen = c.slen - (c.p - c.q) - 2; \
346 		if (d2i_ASN1_SET_OF_##type(&(r),&c.p,Tlen,func, \
347 			free_func,b,V_ASN1_UNIVERSAL) == NULL) \
348 			{ c.line=__LINE__; goto err; } \
349 		if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
350 			Tlen = c.slen - (c.p - c.q); \
351 			if(!ASN1_check_infinite_end(&c.p, Tlen)) \
352 				{ c.error=ERR_R_MISSING_ASN1_EOS; \
353 				c.line=__LINE__; goto err; } \
354 		}\
355 		c.slen-=(c.p-c.q); \
356 		}
357 
358 /* New macros */
359 #define M_ASN1_New_Malloc(ret,type) \
360 	if ((ret=(type*)OPENSSL_malloc(sizeof(type))) == NULL) \
361 		{ c.line=__LINE__; goto err2; }
362 
363 #define M_ASN1_New(arg,func) \
364 	if (((arg)=func()) == NULL) return(NULL)
365 
366 #define M_ASN1_New_Error(a) \
367 /*	err:	ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
368 		return(NULL);*/ \
369 	err2:	ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
370 		return(NULL)
371 
372 
373 /* BIG UGLY WARNING!  This is so damn ugly I wanna puke.  Unfortunately,
374    some macros that use ASN1_const_CTX still insist on writing in the input
375    stream.  ARGH!  ARGH!  ARGH!  Let's get rid of this macro package.
376    Please?						-- Richard Levitte */
377 #define M_ASN1_next		(*((ubyte*)(c.p)))
378 #define M_ASN1_next_prev	(*((ubyte*)(c.q)))
379 
380 /*************************************************/
381 
382 #define M_ASN1_I2D_vars(a)	int r=0,ret=0; \
383 				ubyte* p; \
384 				if (a == NULL) return(0)
385 
386 /* Length Macros */
387 #define M_ASN1_I2D_len(a,f)	ret+=f(a,NULL)
388 #define M_ASN1_I2D_len_IMP_opt(a,f)	if (a != NULL) M_ASN1_I2D_len(a,f)
389 
390 #define M_ASN1_I2D_len_SET(a,f) \
391 		ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
392 
393 #define M_ASN1_I2D_len_SET_type(type,a,f) \
394 		ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \
395 					    V_ASN1_UNIVERSAL,IS_SET);
396 
397 #define M_ASN1_I2D_len_SEQUENCE(a,f) \
398 		ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
399 				  IS_SEQUENCE);
400 
401 #define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
402 		ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
403 					    V_ASN1_UNIVERSAL,IS_SEQUENCE)
404 
405 #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
406 		if ((a != NULL) && (sk_num(a) != 0)) \
407 			M_ASN1_I2D_len_SEQUENCE(a,f);
408 
409 #define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \
410 		if ((a != NULL) && (sk_##type##_num(a) != 0)) \
411 			M_ASN1_I2D_len_SEQUENCE_type(type,a,f);
412 
413 #define M_ASN1_I2D_len_IMP_SET(a,f,x) \
414 		ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
415 
416 #define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
417 		ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
418 					    V_ASN1_CONTEXT_SPECIFIC,IS_SET);
419 
420 #define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
421 		if ((a != NULL) && (sk_num(a) != 0)) \
422 			ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
423 					  IS_SET);
424 
425 #define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
426 		if ((a != NULL) && (sk_##type##_num(a) != 0)) \
427 			ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
428 					       V_ASN1_CONTEXT_SPECIFIC,IS_SET);
429 
430 #define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
431 		ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
432 				  IS_SEQUENCE);
433 
434 #define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \
435 		if ((a != NULL) && (sk_num(a) != 0)) \
436 			ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
437 					  IS_SEQUENCE);
438 
439 #define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
440 		if ((a != NULL) && (sk_##type##_num(a) != 0)) \
441 			ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
442 						    V_ASN1_CONTEXT_SPECIFIC, \
443 						    IS_SEQUENCE);
444 
445 #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
446 		if (a != NULL)\
447 			{ \
448 			v=f(a,NULL); \
449 			ret+=ASN1_object_size(1,v,mtag); \
450 			}
451 
452 #define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
453 		if ((a != NULL) && (sk_num(a) != 0))\
454 			{ \
455 			v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
456 			ret+=ASN1_object_size(1,v,mtag); \
457 			}
458 
459 #define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
460 		if ((a != NULL) && (sk_num(a) != 0))\
461 			{ \
462 			v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \
463 				       IS_SEQUENCE); \
464 			ret+=ASN1_object_size(1,v,mtag); \
465 			}
466 
467 #define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
468 		if ((a != NULL) && (sk_##type##_num(a) != 0))\
469 			{ \
470 			v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \
471 						 V_ASN1_UNIVERSAL, \
472 						 IS_SEQUENCE); \
473 			ret+=ASN1_object_size(1,v,mtag); \
474 			}
475 
476 /* Put Macros */
477 #define M_ASN1_I2D_put(a,f)	f(a,&p)
478 
479 #define M_ASN1_I2D_put_IMP_opt(a,f,t)	\
480 		if (a != NULL) \
481 			{ \
482 			ubyte* q=p; \
483 			f(a,&p); \
484 			*q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
485 			}
486 
487 #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
488 			V_ASN1_UNIVERSAL,IS_SET)
489 #define M_ASN1_I2D_put_SET_type(type,a,f) \
490      i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET)
491 #define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
492 			V_ASN1_CONTEXT_SPECIFIC,IS_SET)
493 #define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
494      i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
495 #define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
496 			V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
497 
498 #define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
499 					     V_ASN1_UNIVERSAL,IS_SEQUENCE)
500 
501 #define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
502      i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
503 			    IS_SEQUENCE)
504 
505 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
506 		if ((a != NULL) && (sk_num(a) != 0)) \
507 			M_ASN1_I2D_put_SEQUENCE(a,f);
508 
509 #define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
510 		if ((a != NULL) && (sk_num(a) != 0)) \
511 			{ i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
512 				       IS_SET); }
513 
514 #define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
515 		if ((a != NULL) && (sk_##type##_num(a) != 0)) \
516 			{ i2d_ASN1_SET_OF_##type(a,&p,f,x, \
517 						 V_ASN1_CONTEXT_SPECIFIC, \
518 						 IS_SET); }
519 
520 #define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
521 		if ((a != NULL) && (sk_num(a) != 0)) \
522 			{ i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
523 				       IS_SEQUENCE); }
524 
525 #define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
526 		if ((a != NULL) && (sk_##type##_num(a) != 0)) \
527 			{ i2d_ASN1_SET_OF_##type(a,&p,f,x, \
528 						 V_ASN1_CONTEXT_SPECIFIC, \
529 						 IS_SEQUENCE); }
530 
531 #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
532 		if (a != NULL) \
533 			{ \
534 			ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
535 			f(a,&p); \
536 			}
537 
538 #define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
539 		if ((a != NULL) && (sk_num(a) != 0)) \
540 			{ \
541 			ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
542 			i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
543 			}
544 
545 #define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
546 		if ((a != NULL) && (sk_num(a) != 0)) \
547 			{ \
548 			ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
549 			i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \
550 			}
551 
552 #define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
553 		if ((a != NULL) && (sk_##type##_num(a) != 0)) \
554 			{ \
555 			ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
556 			i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \
557 					       IS_SEQUENCE); \
558 			}
559 
560 #define M_ASN1_I2D_seq_total() \
561 		r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
562 		if (pp == NULL) return(r); \
563 		p= *pp; \
564 		ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
565 
566 #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
567 		*(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
568 		*(p++)=0x80
569 
570 #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
571 
572 #define M_ASN1_I2D_finish()	*pp=p; \
573 				return(r);
574 +/
575 
576 int asn1_GetSequence(ASN1_const_CTX* c, c_long* length);
577 void asn1_add_error(const(ubyte)* address,int offset);