Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / include / openssl / asn1_mac.h
1 /* $OpenBSD: asn1_mac.h,v 1.13 2014/06/12 15:49:27 deraadt Exp $ */
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 #include <openssl/asn1.h>
63
64 #ifdef  __cplusplus
65 extern "C" {
66 #endif
67
68 #ifndef ASN1_MAC_ERR_LIB
69 #define ASN1_MAC_ERR_LIB        ERR_LIB_ASN1
70 #endif
71
72 #define ASN1_MAC_H_err(f,r,line) \
73         ERR_PUT_error(ASN1_MAC_ERR_LIB,(f),(r),__FILE__,(line))
74
75 #define M_ASN1_D2I_vars(a,type,func) \
76         ASN1_const_CTX c; \
77         type ret=NULL; \
78         \
79         c.pp=(const unsigned char **)pp; \
80         c.q= *(const unsigned char **)pp; \
81         c.error=ERR_R_NESTED_ASN1_ERROR; \
82         if ((a == NULL) || ((*a) == NULL)) \
83                 { if ((ret=(type)func()) == NULL) \
84                         { c.line=__LINE__; goto err; } } \
85         else    ret=(*a);
86
87 #define M_ASN1_D2I_Init() \
88         c.p= *(const unsigned char **)pp; \
89         c.max=(length == 0)?0:(c.p+length);
90
91 #define M_ASN1_D2I_Finish_2(a) \
92         if (!asn1_const_Finish(&c)) \
93                 { c.line=__LINE__; goto err; } \
94         *(const unsigned char **)pp=c.p; \
95         if (a != NULL) (*a)=ret; \
96         return(ret);
97
98 #define M_ASN1_D2I_Finish(a,func,e) \
99         M_ASN1_D2I_Finish_2(a); \
100 err:\
101         ASN1_MAC_H_err((e),c.error,c.line); \
102         asn1_add_error(*(const unsigned char **)pp,(int)(c.q- *pp)); \
103         if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
104         return(NULL)
105
106 #define M_ASN1_D2I_start_sequence() \
107         if (!asn1_GetSequence(&c,&length)) \
108                 { c.line=__LINE__; goto err; }
109 /* Begin reading ASN1 without a surrounding sequence */
110 #define M_ASN1_D2I_begin() \
111         c.slen = length;
112
113 /* End reading ASN1 with no check on length */
114 #define M_ASN1_D2I_Finish_nolen(a, func, e) \
115         *pp=c.p; \
116         if (a != NULL) (*a)=ret; \
117         return(ret); \
118 err:\
119         ASN1_MAC_H_err((e),c.error,c.line); \
120         asn1_add_error(*pp,(int)(c.q- *pp)); \
121         if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \
122         return(NULL)
123
124 #define M_ASN1_D2I_end_sequence() \
125         (((c.inf&1) == 0)?(c.slen <= 0): \
126                 (c.eos=ASN1_const_check_infinite_end(&c.p,c.slen)))
127
128 /* Don't use this with d2i_ASN1_BOOLEAN() */
129 #define M_ASN1_D2I_get(b, func) \
130         c.q=c.p; \
131         if (func(&(b),&c.p,c.slen) == NULL) \
132                 {c.line=__LINE__; goto err; } \
133         c.slen-=(c.p-c.q);
134
135 /* Don't use this with d2i_ASN1_BOOLEAN() */
136 #define M_ASN1_D2I_get_x(type,b,func) \
137         c.q=c.p; \
138         if (((D2I_OF(type))func)(&(b),&c.p,c.slen) == NULL) \
139                 {c.line=__LINE__; goto err; } \
140         c.slen-=(c.p-c.q);
141
142 /* use this instead () */
143 #define M_ASN1_D2I_get_int(b,func) \
144         c.q=c.p; \
145         if (func(&(b),&c.p,c.slen) < 0) \
146                 {c.line=__LINE__; goto err; } \
147         c.slen-=(c.p-c.q);
148
149 #define M_ASN1_D2I_get_opt(b,func,type) \
150         if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
151                 == (V_ASN1_UNIVERSAL|(type)))) \
152                 { \
153                 M_ASN1_D2I_get(b,func); \
154                 }
155
156 #define M_ASN1_D2I_get_int_opt(b,func,type) \
157         if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \
158                 == (V_ASN1_UNIVERSAL|(type)))) \
159                 { \
160                 M_ASN1_D2I_get_int(b,func); \
161                 }
162
163 #define M_ASN1_D2I_get_imp(b,func, type) \
164         M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \
165         c.q=c.p; \
166         if (func(&(b),&c.p,c.slen) == NULL) \
167                 {c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \
168         c.slen-=(c.p-c.q);\
169         M_ASN1_next_prev=_tmp;
170
171 #define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \
172         if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \
173                 (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \
174                 { \
175                 unsigned char _tmp = M_ASN1_next; \
176                 M_ASN1_D2I_get_imp(b,func, type);\
177                 }
178
179 #define M_ASN1_D2I_get_set_type(type,r,func,free_func) \
180                 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \
181                         V_ASN1_SET,V_ASN1_UNIVERSAL);
182
183 #define M_ASN1_D2I_get_set_opt_type(type,r,func,free_func) \
184         if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
185                 V_ASN1_CONSTRUCTED|V_ASN1_SET)))\
186                 { M_ASN1_D2I_get_set_type(type,r,func,free_func); }
187
188 #define M_ASN1_I2D_len_SET_opt(a,f) \
189         if ((a != NULL) && (sk_num(a) != 0)) \
190                 M_ASN1_I2D_len_SET(a,f);
191
192 #define M_ASN1_I2D_put_SET_opt(a,f) \
193         if ((a != NULL) && (sk_num(a) != 0)) \
194                 M_ASN1_I2D_put_SET(a,f);
195
196 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
197         if ((a != NULL) && (sk_num(a) != 0)) \
198                 M_ASN1_I2D_put_SEQUENCE(a,f);
199
200 #define M_ASN1_I2D_put_SEQUENCE_opt_type(type,a,f) \
201         if ((a != NULL) && (sk_##type##_num(a) != 0)) \
202                 M_ASN1_I2D_put_SEQUENCE_type(type,a,f);
203
204 #define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \
205         if ((c.slen != 0) && \
206                 (M_ASN1_next == \
207                 (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\
208                 { \
209                 M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\
210                         tag,V_ASN1_CONTEXT_SPECIFIC); \
211                 }
212
213 #define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \
214                 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
215                                             V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
216
217 #define M_ASN1_D2I_get_seq_opt_type(type,r,func,free_func) \
218         if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
219                 V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\
220                 { M_ASN1_D2I_get_seq_type(type,r,func,free_func); }
221
222 #define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \
223                 M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\
224                         x,V_ASN1_CONTEXT_SPECIFIC);
225
226 #define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \
227         c.q=c.p; \
228         if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\
229                                    free_func,a,b) == NULL) \
230                 { c.line=__LINE__; goto err; } \
231         c.slen-=(c.p-c.q);
232
233 #define M_ASN1_D2I_get_set_strings(r,func,a,b) \
234         c.q=c.p; \
235         if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \
236                 { c.line=__LINE__; goto err; } \
237         c.slen-=(c.p-c.q);
238
239 #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \
240         if ((c.slen != 0L) && (M_ASN1_next == \
241                 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
242                 { \
243                 int Tinf,Ttag,Tclass; \
244                 long Tlen; \
245                 \
246                 c.q=c.p; \
247                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
248                 if (Tinf & 0x80) \
249                         { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
250                         c.line=__LINE__; goto err; } \
251                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
252                                         Tlen = c.slen - (c.p - c.q) - 2; \
253                 if (func(&(r),&c.p,Tlen) == NULL) \
254                         { c.line=__LINE__; goto err; } \
255                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
256                         Tlen = c.slen - (c.p - c.q); \
257                         if(!ASN1_const_check_infinite_end(&c.p, Tlen)) \
258                                 { c.error=ERR_R_MISSING_ASN1_EOS; \
259                                 c.line=__LINE__; goto err; } \
260                 }\
261                 c.slen-=(c.p-c.q); \
262                 }
263
264 #define M_ASN1_D2I_get_EXP_set_opt_type(type,r,func,free_func,tag,b) \
265         if ((c.slen != 0) && (M_ASN1_next == \
266                 (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \
267                 { \
268                 int Tinf,Ttag,Tclass; \
269                 long Tlen; \
270                 \
271                 c.q=c.p; \
272                 Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \
273                 if (Tinf & 0x80) \
274                         { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \
275                         c.line=__LINE__; goto err; } \
276                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) \
277                                         Tlen = c.slen - (c.p - c.q) - 2; \
278                 if (d2i_ASN1_SET_OF_##type(&(r),&c.p,Tlen,func, \
279                         free_func,b,V_ASN1_UNIVERSAL) == NULL) \
280                         { c.line=__LINE__; goto err; } \
281                 if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \
282                         Tlen = c.slen - (c.p - c.q); \
283                         if(!ASN1_check_infinite_end(&c.p, Tlen)) \
284                                 { c.error=ERR_R_MISSING_ASN1_EOS; \
285                                 c.line=__LINE__; goto err; } \
286                 }\
287                 c.slen-=(c.p-c.q); \
288                 }
289
290 /* BIG UGLY WARNING!  This is so damn ugly I wanna puke.  Unfortunately,
291    some macros that use ASN1_const_CTX still insist on writing in the input
292    stream.  ARGH!  ARGH!  ARGH!  Let's get rid of this macro package.
293    Please?                                              -- Richard Levitte */
294 #define M_ASN1_next             (*((unsigned char *)(c.p)))
295 #define M_ASN1_next_prev        (*((unsigned char *)(c.q)))
296
297 /*************************************************/
298
299 #define M_ASN1_I2D_vars(a)      int r=0,ret=0; \
300                                 unsigned char *p; \
301                                 if (a == NULL) return(0)
302
303 /* Length Macros */
304 #define M_ASN1_I2D_len(a,f)     ret+=f(a,NULL)
305 #define M_ASN1_I2D_len_IMP_opt(a,f)     if (a != NULL) M_ASN1_I2D_len(a,f)
306
307 #define M_ASN1_I2D_len_SET_type(type,a,f) \
308                 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \
309                                             V_ASN1_UNIVERSAL,IS_SET);
310
311 #define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \
312                 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \
313                                             V_ASN1_UNIVERSAL,IS_SEQUENCE)
314
315 #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \
316                 if ((a != NULL) && (sk_num(a) != 0)) \
317                         M_ASN1_I2D_len_SEQUENCE(a,f);
318
319 #define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \
320                 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
321                         M_ASN1_I2D_len_SEQUENCE_type(type,a,f);
322
323 #define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \
324                 ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
325                                             V_ASN1_CONTEXT_SPECIFIC,IS_SET);
326
327 #define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \
328                 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
329                         ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
330                                                V_ASN1_CONTEXT_SPECIFIC,IS_SET);
331
332 #define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \
333                 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
334                         ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \
335                                                     V_ASN1_CONTEXT_SPECIFIC, \
336                                                     IS_SEQUENCE);
337
338 #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \
339                 if (a != NULL)\
340                         { \
341                         v=f(a,NULL); \
342                         ret+=ASN1_object_size(1,v,mtag); \
343                         }
344
345 #define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
346                 if ((a != NULL) && (sk_##type##_num(a) != 0))\
347                         { \
348                         v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \
349                                                  V_ASN1_UNIVERSAL, \
350                                                  IS_SEQUENCE); \
351                         ret+=ASN1_object_size(1,v,mtag); \
352                         }
353
354 /* Put Macros */
355 #define M_ASN1_I2D_put(a,f)     f(a,&p)
356
357 #define M_ASN1_I2D_put_IMP_opt(a,f,t)   \
358                 if (a != NULL) \
359                         { \
360                         unsigned char *q=p; \
361                         f(a,&p); \
362                         *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\
363                         }
364
365 #define M_ASN1_I2D_put_SET_type(type,a,f) \
366      i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET)
367 #define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \
368      i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET)
369
370 #define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \
371      i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \
372                             IS_SEQUENCE)
373
374 #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \
375                 if ((a != NULL) && (sk_num(a) != 0)) \
376                         M_ASN1_I2D_put_SEQUENCE(a,f);
377
378 #define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \
379                 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
380                         { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
381                                                  V_ASN1_CONTEXT_SPECIFIC, \
382                                                  IS_SET); }
383
384 #define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \
385                 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
386                         { i2d_ASN1_SET_OF_##type(a,&p,f,x, \
387                                                  V_ASN1_CONTEXT_SPECIFIC, \
388                                                  IS_SEQUENCE); }
389
390 #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \
391                 if (a != NULL) \
392                         { \
393                         ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \
394                         f(a,&p); \
395                         }
396
397 #define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \
398                 if ((a != NULL) && (sk_##type##_num(a) != 0)) \
399                         { \
400                         ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \
401                         i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \
402                                                IS_SEQUENCE); \
403                         }
404
405 #define M_ASN1_I2D_seq_total() \
406                 r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \
407                 if (pp == NULL) return(r); \
408                 p= *pp; \
409                 ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL)
410
411 #define M_ASN1_I2D_INF_seq_start(tag,ctx) \
412                 *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \
413                 *(p++)=0x80
414
415 #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00
416
417 #define M_ASN1_I2D_finish()     *pp=p; \
418                                 return(r);
419
420 int asn1_GetSequence(ASN1_const_CTX *c, long *length);
421 void asn1_add_error(const unsigned char *address, int offset);
422 #ifdef  __cplusplus
423 }
424 #endif
425
426 #endif