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