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