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