More extension code. Incomplete support for subject and issuer alt
[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 #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_PRIMATIVE_TAG)|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_opt(r,func,free_func) \
158         if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
159                 V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
160                 { M_ASN1_D2I_get_set(r,func,free_func); }
161
162 #define M_ASN1_I2D_len_SET_opt(a,f) \
163         if ((a != NULL) && (sk_num(a) != 0)) \
164                 M_ASN1_I2D_len_SET(a,f);
165
166 #define M_ASN1_I2D_put_SET_opt(a,f) \
167         if ((a != NULL) && (sk_num(a) != 0)) \
168                 M_ASN1_I2D_put_SET(a,f);
169
170 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
171         if ((a != NULL) && (sk_num(a) != 0)) \
172                 M_ASN1_I2D_put_SEQUENCE(a,f);
173
174 #define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \
175         if ((c.slen != 0) && \
176                 (M_ASN1_next == \
177                 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
178                 { \
179                 M_ASN1_D2I_get_imp_set(b,func,free_func,\
180                         tag,V_ASN1_CONTEXT_SPECIFIC); \
181                 }
182
183 #define M_ASN1_D2I_get_seq(r,func,free_func) \
184                 M_ASN1_D2I_get_imp_set(r,func,free_func,\
185                         V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
186
187 #define M_ASN1_D2I_get_seq_opt(r,func,free_func) \
188         if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
189                 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
190                 { M_ASN1_D2I_get_seq(r,func,free_func); }
191
192 #define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \
193                 M_ASN1_D2I_get_imp_set(r,func,free_func,\
194                         x,V_ASN1_CONTEXT_SPECIFIC);
195
196 #define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \
197         c.q=c.p; \
198         if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,\
199                 (void (*)())free_func,a,b) == NULL) \
200                 { c.line=__LINE__; goto err; } \
201         c.slen-=(c.p-c.q);
202
203 #define M_ASN1_D2I_get_set_strings(r,func,a,b) \
204         c.q=c.p; \
205         if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
206                 { c.line=__LINE__; goto err; } \
207         c.slen-=(c.p-c.q);
208
209 #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
210         if ((c.slen != 0L) && (M_ASN1_next == \
211                 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
212                 { \
213                 int Tinf,Ttag,Tclass; \
214                 long Tlen; \
215                 \
216                 c.q=c.p; \
217                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
218                 if (Tinf & 0x80) \
219                         { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
220                         c.line=__LINE__; goto err; } \
221                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
222                                         Tlen = c.slen - (c.p - c.q) - 2; \
223                 if (func(&(r),&c.p,Tlen) == NULL) \
224                         { c.line=__LINE__; goto err; } \
225                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
226                         Tlen = c.slen - (c.p - c.q); \
227                         if(!ASN1_check_infinite_end(&c.p, Tlen)) \
228                                 { c.error=ERR_R_MISSING_ASN1_EOS; \
229                                 c.line=__LINE__; goto err; } \
230                 }\
231                 c.slen-=(c.p-c.q); \
232                 }
233
234 #define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \
235         if ((c.slen != 0) && (M_ASN1_next == \
236                 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
237                 { \
238                 int Tinf,Ttag,Tclass; \
239                 long Tlen; \
240                 \
241                 c.q=c.p; \
242                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
243                 if (Tinf & 0x80) \
244                         { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
245                         c.line=__LINE__; goto err; } \
246                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
247                                         Tlen = c.slen - (c.p - c.q) - 2; \
248                 if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
249                         (void (*)())free_func, \
250                         b,V_ASN1_UNIVERSAL) == NULL) \
251                         { c.line=__LINE__; goto err; } \
252                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
253                         Tlen = c.slen - (c.p - c.q); \
254                         if(!ASN1_check_infinite_end(&c.p, Tlen)) \
255                                 { c.error=ERR_R_MISSING_ASN1_EOS; \
256                                 c.line=__LINE__; goto err; } \
257                 }\
258                 c.slen-=(c.p-c.q); \
259                 }
260
261 /* New macros */
262 #define M_ASN1_New_Malloc(ret,type) \
263         if ((ret=(type *)Malloc(sizeof(type))) == NULL) \
264                 { c.line=__LINE__; goto err2; }
265
266 #define M_ASN1_New(arg,func) \
267         if (((arg)=func()) == NULL) return(NULL)
268
269 #define M_ASN1_New_Error(a) \
270 /*      err:    ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
271                 return(NULL);*/ \
272         err2:   ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
273                 return(NULL)
274
275
276 #define M_ASN1_next             (*c.p)
277 #define M_ASN1_next_prev        (*c.q)
278
279 /*************************************************/
280
281 #define M_ASN1_I2D_vars(a)      int r=0,ret=0; \
282                                 unsigned char *p; \
283                                 if (a == NULL) return(0)
284
285 /* Length Macros */
286 #define M_ASN1_I2D_len(a,f)     ret+=f(a,NULL)
287 #define M_ASN1_I2D_len_IMP_opt(a,f)     if (a != NULL) M_ASN1_I2D_len(a,f)
288
289 #define M_ASN1_I2D_len_SET(a,f) \
290                 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
291
292 #define M_ASN1_I2D_len_SEQUENCE(a,f) \
293                 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
294                                   IS_SEQUENCE);
295
296 #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
297                 if ((a != NULL) && (sk_num(a) != 0)) \
298                         M_ASN1_I2D_len_SEQUENCE(a,f);
299
300 #define M_ASN1_I2D_len_IMP_SET(a,f,x) \
301                 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
302
303 #define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
304                 if ((a != NULL) && (sk_num(a) != 0)) \
305                         ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
306                                           IS_SET);
307
308 #define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
309                 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
310                                   IS_SEQUENCE);
311
312 #define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \
313                 if ((a != NULL) && (sk_num(a) != 0)) \
314                         ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
315                                           IS_SEQUENCE);
316
317 #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
318                 if (a != NULL)\
319                         { \
320                         v=f(a,NULL); \
321                         ret+=ASN1_object_size(1,v,mtag); \
322                         }
323
324 #define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
325                 if ((a != NULL) && (sk_num(a) != 0))\
326                         { \
327                         v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
328                         ret+=ASN1_object_size(1,v,mtag); \
329                         }
330
331 #define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
332                 if ((a != NULL) && (sk_num(a) != 0))\
333                         { \
334                         v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \
335                                        IS_SEQUENCE); \
336                         ret+=ASN1_object_size(1,v,mtag); \
337                         }
338
339 /* Put Macros */
340 #define M_ASN1_I2D_put(a,f)     f(a,&p)
341
342 #define M_ASN1_I2D_put_IMP_opt(a,f,t)   \
343                 if (a != NULL) \
344                         { \
345                         unsigned char *q=p; \
346                         f(a,&p); \
347                         *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
348                         }
349
350 #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
351                         V_ASN1_UNIVERSAL,IS_SET)
352 #define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
353                         V_ASN1_CONTEXT_SPECIFIC,IS_SET)
354 #define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
355                         V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
356
357 #define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
358                                              V_ASN1_UNIVERSAL,IS_SEQUENCE)
359
360 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
361                 if ((a != NULL) && (sk_num(a) != 0)) \
362                         M_ASN1_I2D_put_SEQUENCE(a,f);
363
364 #define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
365                 if ((a != NULL) && (sk_num(a) != 0)) \
366                         { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
367                                        IS_SET); }
368
369 #define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
370                 if ((a != NULL) && (sk_num(a) != 0)) \
371                         { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
372                                        IS_SEQUENCE); }
373
374 #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
375                 if (a != NULL) \
376                         { \
377                         ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
378                         f(a,&p); \
379                         }
380
381 #define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
382                 if ((a != NULL) && (sk_num(a) != 0)) \
383                         { \
384                         ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
385                         i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
386                         }
387
388 #define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
389                 if ((a != NULL) && (sk_num(a) != 0)) \
390                         { \
391                         ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
392                         i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \
393                         }
394
395 #define M_ASN1_I2D_seq_total() \
396                 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
397                 if (pp == NULL) return(r); \
398                 p= *pp; \
399                 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
400
401 #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
402                 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
403                 *(p++)=0x80
404
405 #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
406
407 #define M_ASN1_I2D_finish()     *pp=p; \
408                                 return(r);
409
410 #ifndef NOPROTO
411 int asn1_GetSequence(ASN1_CTX *c, long *length);
412 void asn1_add_error(unsigned char *address,int offset);
413 #else 
414 int asn1_GetSequence();
415 void asn1_add_error();
416 #endif
417
418 #ifdef  __cplusplus
419 }
420 #endif
421
422 #endif