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