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