1 /* crypto/asn1/asn1_mac.h */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 #ifndef HEADER_ASN1_MAC_H
60 #define HEADER_ASN1_MAC_H
66 #include <openssl/asn1.h>
67 #include <openssl/x509.h>
68 #include <openssl/pkcs7.h>
70 #ifndef ASN1_MAC_ERR_LIB
71 #define ASN1_MAC_ERR_LIB ERR_LIB_ASN1
74 #define ASN1_MAC_H_err(f,r,line) \
75 ERR_PUT_error(ASN1_MAC_ERR_LIB,(f),(r),ERR_file_name,(line))
77 #define M_ASN1_D2I_vars(a,type,func) \
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; } } \
89 #define M_ASN1_D2I_Init() \
91 c.max=(length == 0)?0:(c.p+length);
93 #define M_ASN1_D2I_Finish_2(a) \
94 if (!asn1_Finish(&c)) \
95 { c.line=__LINE__; goto err; } \
97 if (a != NULL) (*a)=ret; \
100 #define M_ASN1_D2I_Finish(a,func,e) \
101 M_ASN1_D2I_Finish_2(a); \
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); \
108 #define M_ASN1_D2I_start_sequence() \
109 if (!asn1_GetSequence(&c,&length)) \
110 { c.line=__LINE__; goto err; }
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)))
116 /* Don't use this with d2i_ASN1_BOOLEAN() */
117 #define M_ASN1_D2I_get(b,func) \
119 if (func(&(b),&c.p,c.slen) == NULL) \
120 {c.line=__LINE__; goto err; } \
123 /* use this instead () */
124 #define M_ASN1_D2I_get_int(b,func) \
126 if (func(&(b),&c.p,c.slen) < 0) \
127 {c.line=__LINE__; goto err; } \
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)))) \
134 M_ASN1_D2I_get(b,func); \
137 #define M_ASN1_D2I_get_imp(b,func, type) \
138 M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \
140 if (func(&(b),&c.p,c.slen) == NULL) \
141 {c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \
143 M_ASN1_next_prev=_tmp;
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)))) \
149 unsigned char _tmp = M_ASN1_next; \
150 M_ASN1_D2I_get_imp(b,func, type);\
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);
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);
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); }
166 #define M_ASN1_I2D_len_SET_opt(a,f) \
167 if ((a != NULL) && (sk_num(a) != 0)) \
168 M_ASN1_I2D_len_SET(a,f);
170 #define M_ASN1_I2D_put_SET_opt(a,f) \
171 if ((a != NULL) && (sk_num(a) != 0)) \
172 M_ASN1_I2D_put_SET(a,f);
174 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
175 if ((a != NULL) && (sk_num(a) != 0)) \
176 M_ASN1_I2D_put_SEQUENCE(a,f);
178 #define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \
179 if ((c.slen != 0) && \
181 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
183 M_ASN1_D2I_get_imp_set(b,func,free_func,\
184 tag,V_ASN1_CONTEXT_SPECIFIC); \
187 #define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \
188 if ((c.slen != 0) && \
190 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
192 M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\
193 tag,V_ASN1_CONTEXT_SPECIFIC); \
196 #define M_ASN1_D2I_get_seq(r,func,free_func) \
197 M_ASN1_D2I_get_imp_set(r,func,free_func,\
198 V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
200 #define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \
201 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
202 V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
204 #define M_ASN1_D2I_get_seq_opt(r,func,free_func) \
205 if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
206 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
207 { M_ASN1_D2I_get_seq(r,func,free_func); }
209 #define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \
210 M_ASN1_D2I_get_imp_set(r,func,free_func,\
211 x,V_ASN1_CONTEXT_SPECIFIC);
213 #define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \
214 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
215 x,V_ASN1_CONTEXT_SPECIFIC);
217 #define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \
219 if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,\
220 (void (*)())free_func,a,b) == NULL) \
221 { c.line=__LINE__; goto err; } \
224 #define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \
226 if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\
227 free_func,a,b) == NULL) \
228 { c.line=__LINE__; goto err; } \
231 #define M_ASN1_D2I_get_set_strings(r,func,a,b) \
233 if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
234 { c.line=__LINE__; goto err; } \
237 #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
238 if ((c.slen != 0L) && (M_ASN1_next == \
239 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
241 int Tinf,Ttag,Tclass; \
245 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
247 { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
248 c.line=__LINE__; goto err; } \
249 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
250 Tlen = c.slen - (c.p - c.q) - 2; \
251 if (func(&(r),&c.p,Tlen) == NULL) \
252 { c.line=__LINE__; goto err; } \
253 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
254 Tlen = c.slen - (c.p - c.q); \
255 if(!ASN1_check_infinite_end(&c.p, Tlen)) \
256 { c.error=ERR_R_MISSING_ASN1_EOS; \
257 c.line=__LINE__; goto err; } \
262 #define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \
263 if ((c.slen != 0) && (M_ASN1_next == \
264 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
266 int Tinf,Ttag,Tclass; \
270 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
272 { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
273 c.line=__LINE__; goto err; } \
274 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
275 Tlen = c.slen - (c.p - c.q) - 2; \
276 if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \
277 (void (*)())free_func, \
278 b,V_ASN1_UNIVERSAL) == NULL) \
279 { c.line=__LINE__; goto err; } \
280 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
281 Tlen = c.slen - (c.p - c.q); \
282 if(!ASN1_check_infinite_end(&c.p, Tlen)) \
283 { c.error=ERR_R_MISSING_ASN1_EOS; \
284 c.line=__LINE__; goto err; } \
290 #define M_ASN1_New_Malloc(ret,type) \
291 if ((ret=(type *)Malloc(sizeof(type))) == NULL) \
292 { c.line=__LINE__; goto err2; }
294 #define M_ASN1_New(arg,func) \
295 if (((arg)=func()) == NULL) return(NULL)
297 #define M_ASN1_New_Error(a) \
298 /* err: ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
300 err2: ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
304 #define M_ASN1_next (*c.p)
305 #define M_ASN1_next_prev (*c.q)
307 /*************************************************/
309 #define M_ASN1_I2D_vars(a) int r=0,ret=0; \
311 if (a == NULL) return(0)
314 #define M_ASN1_I2D_len(a,f) ret+=f(a,NULL)
315 #define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f)
317 #define M_ASN1_I2D_len_SET(a,f) \
318 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET);
320 #define M_ASN1_I2D_len_SEQUENCE(a,f) \
321 ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
324 #define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
325 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
326 V_ASN1_UNIVERSAL,IS_SEQUENCE)
328 #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
329 if ((a != NULL) && (sk_num(a) != 0)) \
330 M_ASN1_I2D_len_SEQUENCE(a,f);
332 #define M_ASN1_I2D_len_IMP_SET(a,f,x) \
333 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET);
335 #define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
336 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
337 V_ASN1_CONTEXT_SPECIFIC,IS_SET);
339 #define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \
340 if ((a != NULL) && (sk_num(a) != 0)) \
341 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
344 #define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
345 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
346 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
347 V_ASN1_CONTEXT_SPECIFIC,IS_SET);
349 #define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \
350 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
353 #define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \
354 if ((a != NULL) && (sk_num(a) != 0)) \
355 ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \
358 #define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
359 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
360 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
361 V_ASN1_CONTEXT_SPECIFIC, \
364 #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
368 ret+=ASN1_object_size(1,v,mtag); \
371 #define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \
372 if ((a != NULL) && (sk_num(a) != 0))\
374 v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
375 ret+=ASN1_object_size(1,v,mtag); \
378 #define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
379 if ((a != NULL) && (sk_num(a) != 0))\
381 v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \
383 ret+=ASN1_object_size(1,v,mtag); \
387 #define M_ASN1_I2D_put(a,f) f(a,&p)
389 #define M_ASN1_I2D_put_IMP_opt(a,f,t) \
392 unsigned char *q=p; \
394 *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
397 #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\
398 V_ASN1_UNIVERSAL,IS_SET)
399 #define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
400 V_ASN1_CONTEXT_SPECIFIC,IS_SET)
401 #define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
402 i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
403 #define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\
404 V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE)
406 #define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\
407 V_ASN1_UNIVERSAL,IS_SEQUENCE)
409 #define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
410 i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
413 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
414 if ((a != NULL) && (sk_num(a) != 0)) \
415 M_ASN1_I2D_put_SEQUENCE(a,f);
417 #define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \
418 if ((a != NULL) && (sk_num(a) != 0)) \
419 { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
422 #define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
423 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
424 { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
425 V_ASN1_CONTEXT_SPECIFIC, \
428 #define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \
429 if ((a != NULL) && (sk_num(a) != 0)) \
430 { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \
433 #define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
434 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
435 { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
436 V_ASN1_CONTEXT_SPECIFIC, \
439 #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
442 ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
446 #define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \
447 if ((a != NULL) && (sk_num(a) != 0)) \
449 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
450 i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \
453 #define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \
454 if ((a != NULL) && (sk_num(a) != 0)) \
456 ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
457 i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \
460 #define M_ASN1_I2D_seq_total() \
461 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
462 if (pp == NULL) return(r); \
464 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
466 #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
467 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
470 #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
472 #define M_ASN1_I2D_finish() *pp=p; \
475 int asn1_GetSequence(ASN1_CTX *c, long *length);
476 void asn1_add_error(unsigned char *address,int offset);