This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / asn1 / asn1_mac.h
1 /* crypto/asn1/asn1_mac.h */
2 /* Copyright (C) 1995-1997 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 "asn1.h"
67 #include "x509.h"
68 #include "pkcs7.h"
69
70 #define M_ASN1_D2I_vars(a,type,func) \
71         ASN1_CTX c; \
72         type ret=NULL; \
73         \
74         c.pp=pp; \
75         c.error=ASN1_R_ERROR_STACK; \
76         if ((a == NULL) || ((*a) == NULL)) \
77                 { if ((ret=(type)func()) == NULL) goto err; } \
78         else    ret=(*a);
79
80 #define M_ASN1_D2I_Init() \
81         c.p= *pp; \
82         c.max=(length == 0)?0:(c.p+length);
83
84 #define M_ASN1_D2I_Finish_2(a) \
85         if (!asn1_Finish(&c)) goto err; \
86         *pp=c.p; \
87         if (a != NULL) (*a)=ret; \
88         return(ret);
89
90 #define M_ASN1_D2I_Finish(a,func,e) \
91         M_ASN1_D2I_Finish_2(a); \
92 err:\
93         ASN1err((e),c.error); \
94         if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
95         return(NULL)
96
97 #define M_ASN1_D2I_start_sequence() \
98         if (!asn1_GetSequence(&c,&length)) goto err;
99
100 #define M_ASN1_D2I_end_sequence() \
101         (((c.inf&1) == 0)?(c.slen <= 0): \
102                 (c.eos=ASN1_check_infinite_end(&c.p,c.slen)))
103
104 #define M_ASN1_D2I_get(b,func) \
105         c.q=c.p; \
106         if (func(&(b),&c.p,c.slen) == NULL) goto err; \
107         c.slen-=(c.p-c.q);
108
109 #define M_ASN1_D2I_get_opt(b,func,type) \
110         if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
111                 == (V_ASN1_UNIVERSAL|(type)))) \
112                 { \
113                 M_ASN1_D2I_get(b,func); \
114                 }
115
116 #define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
117         if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
118                 (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
119                 { \
120                 unsigned char tmp; \
121                 tmp=M_ASN1_next; \
122                 M_ASN1_next=(tmp& ~V_ASN1_PRIMATIVE_TAG)|type; \
123                 M_ASN1_D2I_get(b,func); \
124                 M_ASN1_next_prev=tmp; \
125                 }
126
127 #define M_ASN1_D2I_get_set(r,func) \
128                 M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SET,V_ASN1_UNIVERSAL);
129
130 #define M_ASN1_D2I_get_IMP_set_opt(b,func,tag) \
131         if ((c.slen != 0) && \
132                 (M_ASN1_next == \
133                 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
134                 { \
135                 M_ASN1_D2I_get_imp_set(b,func,tag,V_ASN1_CONTEXT_SPECIFIC); \
136                 }
137
138 #define M_ASN1_D2I_get_seq(r,func) \
139                 M_ASN1_D2I_get_imp_set(r,func,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
140
141 #define M_ASN1_D2I_get_seq_opt(r,func) \
142         if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
143                 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
144                 { M_ASN1_D2I_get_seq(r,func); }
145
146 #define M_ASN1_D2I_get_IMP_set(r,func,x) \
147                 M_ASN1_D2I_get_imp_set(r,func,x,V_ASN1_CONTEXT_SPECIFIC);
148
149 #define M_ASN1_D2I_get_imp_set(r,func,a,b) \
150         c.q=c.p; \
151         if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,a,b) == NULL) \
152                 goto err; \
153         c.slen-=(c.p-c.q);
154
155 #define M_ASN1_D2I_get_set_strings(r,func,a,b) \
156         c.q=c.p; \
157         if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
158                 goto err; \
159         c.slen-=(c.p-c.q);
160
161 #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
162         if ((c.slen != 0L) && (M_ASN1_next == \
163                 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
164                 { \
165                 int Tinf,Ttag,Tclass; \
166                 long Tlen; \
167                 \
168                 c.q=c.p; \
169                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
170                 if (Tinf & 0x80) \
171                         { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \
172                 if (func(&(r),&c.p,Tlen) == NULL) \
173                         goto err; \
174                 c.slen-=(c.p-c.q); \
175                 }
176
177 #define M_ASN1_D2I_get_EXP_set_opt(r,func,tag,b) \
178         if ((c.slen != 0) && (M_ASN1_next == \
179                 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
180                 { \
181                 int Tinf,Ttag,Tclass; \
182                 long Tlen; \
183                 \
184                 c.q=c.p; \
185                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
186                 if (Tinf & 0x80) \
187                         { c.error=ASN1_R_BAD_OBJECT_HEADER; goto err; } \
188                 if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
189                         b,V_ASN1_UNIVERSAL) == NULL) \
190                         goto err; \
191                 c.slen-=(c.p-c.q); \
192                 }
193
194 /* New macros */
195 #define M_ASN1_New_Malloc(ret,type) \
196         if ((ret=(type *)Malloc(sizeof(type))) == NULL) goto err2;
197
198 #define M_ASN1_New(arg,func) \
199         if (((arg)=func()) == NULL) return(NULL)
200
201 #define M_ASN1_New_Error(a) \
202 /*      err:    ASN1err((a),ASN1_R_ERROR_STACK); \
203                 return(NULL);*/ \
204         err2:   ASN1err((a),ERR_R_MALLOC_FAILURE); \
205                 return(NULL)
206
207
208 #define M_ASN1_next             (*c.p)
209 #define M_ASN1_next_prev        (*c.q)
210
211 /*************************************************/
212
213 #define M_ASN1_I2D_vars(a)      int r=0,ret=0; \
214                                 unsigned char *p; \
215                                 if (a == NULL) return(0)
216
217 /* Length Macros */
218 #define M_ASN1_I2D_len(a,f)     ret+=f(a,NULL)
219 #define M_ASN1_I2D_len_IMP_opt(a,f)     if (a != NULL) M_ASN1_I2D_len(a,f)
220
221 #define M_ASN1_I2D_len_SET(a,f) \
222                 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL);
223
224 #define M_ASN1_I2D_len_SEQ(a,f) \
225                 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
226
227 #define M_ASN1_I2D_len_SEQ_opt(a,f) \
228                 if ((a != NULL) && (sk_num(a) != 0)) \
229                         M_ASN1_I2D_len_SEQ(a,f);
230
231 #define M_ASN1_I2D_len_IMP_set(a,f,x) \
232                 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC);
233
234 #define M_ASN1_I2D_len_IMP_set_opt(a,f,x) \
235                 if ((a != NULL) && (sk_num(a) != 0)) \
236                         ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC);
237
238 #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
239                 if (a != NULL)\
240                         { \
241                         v=f(a,NULL); \
242                         ret+=ASN1_object_size(1,v,mtag); \
243                         }
244
245 #define M_ASN1_I2D_len_EXP_set_opt(a,f,mtag,tag,v) \
246                 if ((a != NULL) && (sk_num(a) != 0))\
247                         { \
248                         v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL); \
249                         ret+=ASN1_object_size(1,v,mtag); \
250                         }
251
252 /* Put Macros */
253 #define M_ASN1_I2D_put(a,f)     f(a,&p)
254
255 #define M_ASN1_I2D_put_IMP_opt(a,f,t)   \
256                 if (a != NULL) \
257                         { \
258                         unsigned char *q=p; \
259                         f(a,&p); \
260                         *q=(V_ASN1_CONTEXT_SPECIFIC|t); \
261                         }
262
263 #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
264                         V_ASN1_UNIVERSAL)
265 #define M_ASN1_I2D_put_IMP_set(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
266                         V_ASN1_CONTEXT_SPECIFIC)
267
268 #define M_ASN1_I2D_put_SEQ(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
269                         V_ASN1_UNIVERSAL)
270
271 #define M_ASN1_I2D_put_SEQ_opt(a,f) \
272                 if ((a != NULL) && (sk_num(a) != 0)) \
273                         M_ASN1_I2D_put_SEQ(a,f);
274
275 #define M_ASN1_I2D_put_IMP_set_opt(a,f,x) \
276                 if ((a != NULL) && (sk_num(a) != 0)) \
277                         { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC); }
278
279 #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
280                 if (a != NULL) \
281                         { \
282                         ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
283                         f(a,&p); \
284                         }
285
286 #define M_ASN1_I2D_put_EXP_set_opt(a,f,mtag,tag,v) \
287                 if ((a != NULL) && (sk_num(a) != 0)) \
288                         { \
289                         ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
290                         i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL); \
291                         }
292
293 #define M_ASN1_I2D_seq_total() \
294                 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
295                 if (pp == NULL) return(r); \
296                 p= *pp; \
297                 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
298
299 #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
300                 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
301                 *(p++)=0x80
302
303 #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
304
305 #define M_ASN1_I2D_finish()     *pp=p; \
306                                 return(r);
307
308 #ifndef NOPROTO
309 int asn1_GetSequence(ASN1_CTX *c, long *length);
310 #else 
311 int asn1_GetSequence();
312 #endif
313
314 #ifdef  __cplusplus
315 }
316 #endif
317
318 #endif