10Sstevel@tonic-gate /* crypto/asn1/asn1_mac.h */ 20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 30Sstevel@tonic-gate * All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * This package is an SSL implementation written 60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 150Sstevel@tonic-gate * 160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 170Sstevel@tonic-gate * the code are not to be removed. 180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 190Sstevel@tonic-gate * as the author of the parts of the library used. 200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 210Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 240Sstevel@tonic-gate * modification, are permitted provided that the following conditions 250Sstevel@tonic-gate * are met: 260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 320Sstevel@tonic-gate * must display the following acknowledgement: 330Sstevel@tonic-gate * "This product includes cryptographic software written by 340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 360Sstevel@tonic-gate * being used are not cryptographic related :-). 370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 510Sstevel@tonic-gate * SUCH DAMAGE. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 550Sstevel@tonic-gate * copied and put under another distribution licence 560Sstevel@tonic-gate * [including the GNU Public Licence.] 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #ifndef HEADER_ASN1_MAC_H 600Sstevel@tonic-gate #define HEADER_ASN1_MAC_H 610Sstevel@tonic-gate 620Sstevel@tonic-gate #include <openssl/asn1.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate #ifdef __cplusplus 650Sstevel@tonic-gate extern "C" { 660Sstevel@tonic-gate #endif 670Sstevel@tonic-gate 680Sstevel@tonic-gate #ifndef ASN1_MAC_ERR_LIB 690Sstevel@tonic-gate #define ASN1_MAC_ERR_LIB ERR_LIB_ASN1 700Sstevel@tonic-gate #endif 710Sstevel@tonic-gate 720Sstevel@tonic-gate #define ASN1_MAC_H_err(f,r,line) \ 730Sstevel@tonic-gate ERR_PUT_error(ASN1_MAC_ERR_LIB,(f),(r),__FILE__,(line)) 740Sstevel@tonic-gate 750Sstevel@tonic-gate #define M_ASN1_D2I_vars(a,type,func) \ 76*2139Sjp161948 ASN1_const_CTX c; \ 770Sstevel@tonic-gate type ret=NULL; \ 780Sstevel@tonic-gate \ 79*2139Sjp161948 c.pp=(const unsigned char **)pp; \ 80*2139Sjp161948 c.q= *(const unsigned char **)pp; \ 810Sstevel@tonic-gate c.error=ERR_R_NESTED_ASN1_ERROR; \ 820Sstevel@tonic-gate if ((a == NULL) || ((*a) == NULL)) \ 830Sstevel@tonic-gate { if ((ret=(type)func()) == NULL) \ 840Sstevel@tonic-gate { c.line=__LINE__; goto err; } } \ 850Sstevel@tonic-gate else ret=(*a); 860Sstevel@tonic-gate 870Sstevel@tonic-gate #define M_ASN1_D2I_Init() \ 88*2139Sjp161948 c.p= *(const unsigned char **)pp; \ 890Sstevel@tonic-gate c.max=(length == 0)?0:(c.p+length); 900Sstevel@tonic-gate 910Sstevel@tonic-gate #define M_ASN1_D2I_Finish_2(a) \ 92*2139Sjp161948 if (!asn1_const_Finish(&c)) \ 930Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 94*2139Sjp161948 *(const unsigned char **)pp=c.p; \ 950Sstevel@tonic-gate if (a != NULL) (*a)=ret; \ 960Sstevel@tonic-gate return(ret); 970Sstevel@tonic-gate 980Sstevel@tonic-gate #define M_ASN1_D2I_Finish(a,func,e) \ 990Sstevel@tonic-gate M_ASN1_D2I_Finish_2(a); \ 1000Sstevel@tonic-gate err:\ 1010Sstevel@tonic-gate ASN1_MAC_H_err((e),c.error,c.line); \ 102*2139Sjp161948 asn1_add_error(*(const unsigned char **)pp,(int)(c.q- *pp)); \ 1030Sstevel@tonic-gate if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \ 1040Sstevel@tonic-gate return(NULL) 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate #define M_ASN1_D2I_start_sequence() \ 1070Sstevel@tonic-gate if (!asn1_GetSequence(&c,&length)) \ 1080Sstevel@tonic-gate { c.line=__LINE__; goto err; } 1090Sstevel@tonic-gate /* Begin reading ASN1 without a surrounding sequence */ 1100Sstevel@tonic-gate #define M_ASN1_D2I_begin() \ 1110Sstevel@tonic-gate c.slen = length; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* End reading ASN1 with no check on length */ 1140Sstevel@tonic-gate #define M_ASN1_D2I_Finish_nolen(a, func, e) \ 1150Sstevel@tonic-gate *pp=c.p; \ 1160Sstevel@tonic-gate if (a != NULL) (*a)=ret; \ 1170Sstevel@tonic-gate return(ret); \ 1180Sstevel@tonic-gate err:\ 1190Sstevel@tonic-gate ASN1_MAC_H_err((e),c.error,c.line); \ 1200Sstevel@tonic-gate asn1_add_error(*pp,(int)(c.q- *pp)); \ 1210Sstevel@tonic-gate if ((ret != NULL) && ((a == NULL) || (*a != ret))) func(ret); \ 1220Sstevel@tonic-gate return(NULL) 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate #define M_ASN1_D2I_end_sequence() \ 1250Sstevel@tonic-gate (((c.inf&1) == 0)?(c.slen <= 0): \ 126*2139Sjp161948 (c.eos=ASN1_const_check_infinite_end(&c.p,c.slen))) 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* Don't use this with d2i_ASN1_BOOLEAN() */ 129*2139Sjp161948 #define M_ASN1_D2I_get(b, func) \ 1300Sstevel@tonic-gate c.q=c.p; \ 1310Sstevel@tonic-gate if (func(&(b),&c.p,c.slen) == NULL) \ 1320Sstevel@tonic-gate {c.line=__LINE__; goto err; } \ 1330Sstevel@tonic-gate c.slen-=(c.p-c.q); 1340Sstevel@tonic-gate 135*2139Sjp161948 /* Don't use this with d2i_ASN1_BOOLEAN() */ 136*2139Sjp161948 #define M_ASN1_D2I_get_x(type,b,func) \ 137*2139Sjp161948 c.q=c.p; \ 138*2139Sjp161948 if (((D2I_OF(type))func)(&(b),&c.p,c.slen) == NULL) \ 139*2139Sjp161948 {c.line=__LINE__; goto err; } \ 140*2139Sjp161948 c.slen-=(c.p-c.q); 141*2139Sjp161948 1420Sstevel@tonic-gate /* use this instead () */ 1430Sstevel@tonic-gate #define M_ASN1_D2I_get_int(b,func) \ 1440Sstevel@tonic-gate c.q=c.p; \ 1450Sstevel@tonic-gate if (func(&(b),&c.p,c.slen) < 0) \ 1460Sstevel@tonic-gate {c.line=__LINE__; goto err; } \ 1470Sstevel@tonic-gate c.slen-=(c.p-c.q); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate #define M_ASN1_D2I_get_opt(b,func,type) \ 1500Sstevel@tonic-gate if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) \ 1510Sstevel@tonic-gate == (V_ASN1_UNIVERSAL|(type)))) \ 1520Sstevel@tonic-gate { \ 1530Sstevel@tonic-gate M_ASN1_D2I_get(b,func); \ 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate #define M_ASN1_D2I_get_imp(b,func, type) \ 1570Sstevel@tonic-gate M_ASN1_next=(_tmp& V_ASN1_CONSTRUCTED)|type; \ 1580Sstevel@tonic-gate c.q=c.p; \ 1590Sstevel@tonic-gate if (func(&(b),&c.p,c.slen) == NULL) \ 1600Sstevel@tonic-gate {c.line=__LINE__; M_ASN1_next_prev = _tmp; goto err; } \ 1610Sstevel@tonic-gate c.slen-=(c.p-c.q);\ 1620Sstevel@tonic-gate M_ASN1_next_prev=_tmp; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate #define M_ASN1_D2I_get_IMP_opt(b,func,tag,type) \ 1650Sstevel@tonic-gate if ((c.slen != 0) && ((M_ASN1_next & (~V_ASN1_CONSTRUCTED)) == \ 1660Sstevel@tonic-gate (V_ASN1_CONTEXT_SPECIFIC|(tag)))) \ 1670Sstevel@tonic-gate { \ 1680Sstevel@tonic-gate unsigned char _tmp = M_ASN1_next; \ 1690Sstevel@tonic-gate M_ASN1_D2I_get_imp(b,func, type);\ 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate #define M_ASN1_D2I_get_set(r,func,free_func) \ 1730Sstevel@tonic-gate M_ASN1_D2I_get_imp_set(r,func,free_func, \ 1740Sstevel@tonic-gate V_ASN1_SET,V_ASN1_UNIVERSAL); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate #define M_ASN1_D2I_get_set_type(type,r,func,free_func) \ 1770Sstevel@tonic-gate M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \ 1780Sstevel@tonic-gate V_ASN1_SET,V_ASN1_UNIVERSAL); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate #define M_ASN1_D2I_get_set_opt(r,func,free_func) \ 1810Sstevel@tonic-gate if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \ 1820Sstevel@tonic-gate V_ASN1_CONSTRUCTED|V_ASN1_SET)))\ 1830Sstevel@tonic-gate { M_ASN1_D2I_get_set(r,func,free_func); } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate #define M_ASN1_D2I_get_set_opt_type(type,r,func,free_func) \ 1860Sstevel@tonic-gate if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \ 1870Sstevel@tonic-gate V_ASN1_CONSTRUCTED|V_ASN1_SET)))\ 1880Sstevel@tonic-gate { M_ASN1_D2I_get_set_type(type,r,func,free_func); } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate #define M_ASN1_I2D_len_SET_opt(a,f) \ 1910Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 1920Sstevel@tonic-gate M_ASN1_I2D_len_SET(a,f); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate #define M_ASN1_I2D_put_SET_opt(a,f) \ 1950Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 1960Sstevel@tonic-gate M_ASN1_I2D_put_SET(a,f); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \ 1990Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 2000Sstevel@tonic-gate M_ASN1_I2D_put_SEQUENCE(a,f); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate #define M_ASN1_I2D_put_SEQUENCE_opt_type(type,a,f) \ 2030Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 2040Sstevel@tonic-gate M_ASN1_I2D_put_SEQUENCE_type(type,a,f); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate #define M_ASN1_D2I_get_IMP_set_opt(b,func,free_func,tag) \ 2070Sstevel@tonic-gate if ((c.slen != 0) && \ 2080Sstevel@tonic-gate (M_ASN1_next == \ 2090Sstevel@tonic-gate (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\ 2100Sstevel@tonic-gate { \ 2110Sstevel@tonic-gate M_ASN1_D2I_get_imp_set(b,func,free_func,\ 2120Sstevel@tonic-gate tag,V_ASN1_CONTEXT_SPECIFIC); \ 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate #define M_ASN1_D2I_get_IMP_set_opt_type(type,b,func,free_func,tag) \ 2160Sstevel@tonic-gate if ((c.slen != 0) && \ 2170Sstevel@tonic-gate (M_ASN1_next == \ 2180Sstevel@tonic-gate (V_ASN1_CONTEXT_SPECIFIC|V_ASN1_CONSTRUCTED|(tag))))\ 2190Sstevel@tonic-gate { \ 2200Sstevel@tonic-gate M_ASN1_D2I_get_imp_set_type(type,b,func,free_func,\ 2210Sstevel@tonic-gate tag,V_ASN1_CONTEXT_SPECIFIC); \ 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate #define M_ASN1_D2I_get_seq(r,func,free_func) \ 2250Sstevel@tonic-gate M_ASN1_D2I_get_imp_set(r,func,free_func,\ 2260Sstevel@tonic-gate V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate #define M_ASN1_D2I_get_seq_type(type,r,func,free_func) \ 2290Sstevel@tonic-gate M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\ 2300Sstevel@tonic-gate V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL) 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate #define M_ASN1_D2I_get_seq_opt(r,func,free_func) \ 2330Sstevel@tonic-gate if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \ 2340Sstevel@tonic-gate V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\ 2350Sstevel@tonic-gate { M_ASN1_D2I_get_seq(r,func,free_func); } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate #define M_ASN1_D2I_get_seq_opt_type(type,r,func,free_func) \ 2380Sstevel@tonic-gate if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \ 2390Sstevel@tonic-gate V_ASN1_CONSTRUCTED|V_ASN1_SEQUENCE)))\ 2400Sstevel@tonic-gate { M_ASN1_D2I_get_seq_type(type,r,func,free_func); } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate #define M_ASN1_D2I_get_IMP_set(r,func,free_func,x) \ 2430Sstevel@tonic-gate M_ASN1_D2I_get_imp_set(r,func,free_func,\ 2440Sstevel@tonic-gate x,V_ASN1_CONTEXT_SPECIFIC); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate #define M_ASN1_D2I_get_IMP_set_type(type,r,func,free_func,x) \ 2470Sstevel@tonic-gate M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,\ 2480Sstevel@tonic-gate x,V_ASN1_CONTEXT_SPECIFIC); 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate #define M_ASN1_D2I_get_imp_set(r,func,free_func,a,b) \ 2510Sstevel@tonic-gate c.q=c.p; \ 2520Sstevel@tonic-gate if (d2i_ASN1_SET(&(r),&c.p,c.slen,(char *(*)())func,\ 2530Sstevel@tonic-gate (void (*)())free_func,a,b) == NULL) \ 2540Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 2550Sstevel@tonic-gate c.slen-=(c.p-c.q); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate #define M_ASN1_D2I_get_imp_set_type(type,r,func,free_func,a,b) \ 2580Sstevel@tonic-gate c.q=c.p; \ 2590Sstevel@tonic-gate if (d2i_ASN1_SET_OF_##type(&(r),&c.p,c.slen,func,\ 2600Sstevel@tonic-gate free_func,a,b) == NULL) \ 2610Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 2620Sstevel@tonic-gate c.slen-=(c.p-c.q); 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate #define M_ASN1_D2I_get_set_strings(r,func,a,b) \ 2650Sstevel@tonic-gate c.q=c.p; \ 2660Sstevel@tonic-gate if (d2i_ASN1_STRING_SET(&(r),&c.p,c.slen,a,b) == NULL) \ 2670Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 2680Sstevel@tonic-gate c.slen-=(c.p-c.q); 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate #define M_ASN1_D2I_get_EXP_opt(r,func,tag) \ 2710Sstevel@tonic-gate if ((c.slen != 0L) && (M_ASN1_next == \ 2720Sstevel@tonic-gate (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \ 2730Sstevel@tonic-gate { \ 2740Sstevel@tonic-gate int Tinf,Ttag,Tclass; \ 2750Sstevel@tonic-gate long Tlen; \ 2760Sstevel@tonic-gate \ 2770Sstevel@tonic-gate c.q=c.p; \ 2780Sstevel@tonic-gate Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \ 2790Sstevel@tonic-gate if (Tinf & 0x80) \ 2800Sstevel@tonic-gate { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \ 2810Sstevel@tonic-gate c.line=__LINE__; goto err; } \ 2820Sstevel@tonic-gate if (Tinf == (V_ASN1_CONSTRUCTED+1)) \ 2830Sstevel@tonic-gate Tlen = c.slen - (c.p - c.q) - 2; \ 2840Sstevel@tonic-gate if (func(&(r),&c.p,Tlen) == NULL) \ 2850Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 2860Sstevel@tonic-gate if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \ 2870Sstevel@tonic-gate Tlen = c.slen - (c.p - c.q); \ 288*2139Sjp161948 if(!ASN1_const_check_infinite_end(&c.p, Tlen)) \ 2890Sstevel@tonic-gate { c.error=ERR_R_MISSING_ASN1_EOS; \ 2900Sstevel@tonic-gate c.line=__LINE__; goto err; } \ 2910Sstevel@tonic-gate }\ 2920Sstevel@tonic-gate c.slen-=(c.p-c.q); \ 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate #define M_ASN1_D2I_get_EXP_set_opt(r,func,free_func,tag,b) \ 2960Sstevel@tonic-gate if ((c.slen != 0) && (M_ASN1_next == \ 2970Sstevel@tonic-gate (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \ 2980Sstevel@tonic-gate { \ 2990Sstevel@tonic-gate int Tinf,Ttag,Tclass; \ 3000Sstevel@tonic-gate long Tlen; \ 3010Sstevel@tonic-gate \ 3020Sstevel@tonic-gate c.q=c.p; \ 3030Sstevel@tonic-gate Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \ 3040Sstevel@tonic-gate if (Tinf & 0x80) \ 3050Sstevel@tonic-gate { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \ 3060Sstevel@tonic-gate c.line=__LINE__; goto err; } \ 3070Sstevel@tonic-gate if (Tinf == (V_ASN1_CONSTRUCTED+1)) \ 3080Sstevel@tonic-gate Tlen = c.slen - (c.p - c.q) - 2; \ 3090Sstevel@tonic-gate if (d2i_ASN1_SET(&(r),&c.p,Tlen,(char *(*)())func, \ 3100Sstevel@tonic-gate (void (*)())free_func, \ 3110Sstevel@tonic-gate b,V_ASN1_UNIVERSAL) == NULL) \ 3120Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 3130Sstevel@tonic-gate if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \ 3140Sstevel@tonic-gate Tlen = c.slen - (c.p - c.q); \ 3150Sstevel@tonic-gate if(!ASN1_check_infinite_end(&c.p, Tlen)) \ 3160Sstevel@tonic-gate { c.error=ERR_R_MISSING_ASN1_EOS; \ 3170Sstevel@tonic-gate c.line=__LINE__; goto err; } \ 3180Sstevel@tonic-gate }\ 3190Sstevel@tonic-gate c.slen-=(c.p-c.q); \ 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate #define M_ASN1_D2I_get_EXP_set_opt_type(type,r,func,free_func,tag,b) \ 3230Sstevel@tonic-gate if ((c.slen != 0) && (M_ASN1_next == \ 3240Sstevel@tonic-gate (V_ASN1_CONSTRUCTED|V_ASN1_CONTEXT_SPECIFIC|tag))) \ 3250Sstevel@tonic-gate { \ 3260Sstevel@tonic-gate int Tinf,Ttag,Tclass; \ 3270Sstevel@tonic-gate long Tlen; \ 3280Sstevel@tonic-gate \ 3290Sstevel@tonic-gate c.q=c.p; \ 3300Sstevel@tonic-gate Tinf=ASN1_get_object(&c.p,&Tlen,&Ttag,&Tclass,c.slen); \ 3310Sstevel@tonic-gate if (Tinf & 0x80) \ 3320Sstevel@tonic-gate { c.error=ERR_R_BAD_ASN1_OBJECT_HEADER; \ 3330Sstevel@tonic-gate c.line=__LINE__; goto err; } \ 3340Sstevel@tonic-gate if (Tinf == (V_ASN1_CONSTRUCTED+1)) \ 3350Sstevel@tonic-gate Tlen = c.slen - (c.p - c.q) - 2; \ 3360Sstevel@tonic-gate if (d2i_ASN1_SET_OF_##type(&(r),&c.p,Tlen,func, \ 3370Sstevel@tonic-gate free_func,b,V_ASN1_UNIVERSAL) == NULL) \ 3380Sstevel@tonic-gate { c.line=__LINE__; goto err; } \ 3390Sstevel@tonic-gate if (Tinf == (V_ASN1_CONSTRUCTED+1)) { \ 3400Sstevel@tonic-gate Tlen = c.slen - (c.p - c.q); \ 3410Sstevel@tonic-gate if(!ASN1_check_infinite_end(&c.p, Tlen)) \ 3420Sstevel@tonic-gate { c.error=ERR_R_MISSING_ASN1_EOS; \ 3430Sstevel@tonic-gate c.line=__LINE__; goto err; } \ 3440Sstevel@tonic-gate }\ 3450Sstevel@tonic-gate c.slen-=(c.p-c.q); \ 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* New macros */ 3490Sstevel@tonic-gate #define M_ASN1_New_Malloc(ret,type) \ 3500Sstevel@tonic-gate if ((ret=(type *)OPENSSL_malloc(sizeof(type))) == NULL) \ 3510Sstevel@tonic-gate { c.line=__LINE__; goto err2; } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate #define M_ASN1_New(arg,func) \ 3540Sstevel@tonic-gate if (((arg)=func()) == NULL) return(NULL) 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate #define M_ASN1_New_Error(a) \ 3570Sstevel@tonic-gate /* err: ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \ 3580Sstevel@tonic-gate return(NULL);*/ \ 3590Sstevel@tonic-gate err2: ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \ 3600Sstevel@tonic-gate return(NULL) 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate 363*2139Sjp161948 /* BIG UGLY WARNING! This is so damn ugly I wanna puke. Unfortunately, 364*2139Sjp161948 some macros that use ASN1_const_CTX still insist on writing in the input 365*2139Sjp161948 stream. ARGH! ARGH! ARGH! Let's get rid of this macro package. 366*2139Sjp161948 Please? -- Richard Levitte */ 367*2139Sjp161948 #define M_ASN1_next (*((unsigned char *)(c.p))) 368*2139Sjp161948 #define M_ASN1_next_prev (*((unsigned char *)(c.q))) 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /*************************************************/ 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate #define M_ASN1_I2D_vars(a) int r=0,ret=0; \ 3730Sstevel@tonic-gate unsigned char *p; \ 3740Sstevel@tonic-gate if (a == NULL) return(0) 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* Length Macros */ 3770Sstevel@tonic-gate #define M_ASN1_I2D_len(a,f) ret+=f(a,NULL) 3780Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_opt(a,f) if (a != NULL) M_ASN1_I2D_len(a,f) 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate #define M_ASN1_I2D_len_SET(a,f) \ 3810Sstevel@tonic-gate ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate #define M_ASN1_I2D_len_SET_type(type,a,f) \ 3840Sstevel@tonic-gate ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SET, \ 3850Sstevel@tonic-gate V_ASN1_UNIVERSAL,IS_SET); 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate #define M_ASN1_I2D_len_SEQUENCE(a,f) \ 3880Sstevel@tonic-gate ret+=i2d_ASN1_SET(a,NULL,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \ 3890Sstevel@tonic-gate IS_SEQUENCE); 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate #define M_ASN1_I2D_len_SEQUENCE_type(type,a,f) \ 3920Sstevel@tonic-gate ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,V_ASN1_SEQUENCE, \ 3930Sstevel@tonic-gate V_ASN1_UNIVERSAL,IS_SEQUENCE) 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate #define M_ASN1_I2D_len_SEQUENCE_opt(a,f) \ 3960Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 3970Sstevel@tonic-gate M_ASN1_I2D_len_SEQUENCE(a,f); 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate #define M_ASN1_I2D_len_SEQUENCE_opt_type(type,a,f) \ 4000Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 4010Sstevel@tonic-gate M_ASN1_I2D_len_SEQUENCE_type(type,a,f); 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SET(a,f,x) \ 4040Sstevel@tonic-gate ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET); 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SET_type(type,a,f,x) \ 4070Sstevel@tonic-gate ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \ 4080Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC,IS_SET); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SET_opt(a,f,x) \ 4110Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 4120Sstevel@tonic-gate ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \ 4130Sstevel@tonic-gate IS_SET); 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SET_opt_type(type,a,f,x) \ 4160Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 4170Sstevel@tonic-gate ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \ 4180Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC,IS_SET); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SEQUENCE(a,f,x) \ 4210Sstevel@tonic-gate ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \ 4220Sstevel@tonic-gate IS_SEQUENCE); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SEQUENCE_opt(a,f,x) \ 4250Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 4260Sstevel@tonic-gate ret+=i2d_ASN1_SET(a,NULL,f,x,V_ASN1_CONTEXT_SPECIFIC, \ 4270Sstevel@tonic-gate IS_SEQUENCE); 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate #define M_ASN1_I2D_len_IMP_SEQUENCE_opt_type(type,a,f,x) \ 4300Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 4310Sstevel@tonic-gate ret+=i2d_ASN1_SET_OF_##type(a,NULL,f,x, \ 4320Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC, \ 4330Sstevel@tonic-gate IS_SEQUENCE); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate #define M_ASN1_I2D_len_EXP_opt(a,f,mtag,v) \ 4360Sstevel@tonic-gate if (a != NULL)\ 4370Sstevel@tonic-gate { \ 4380Sstevel@tonic-gate v=f(a,NULL); \ 4390Sstevel@tonic-gate ret+=ASN1_object_size(1,v,mtag); \ 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate #define M_ASN1_I2D_len_EXP_SET_opt(a,f,mtag,tag,v) \ 4430Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0))\ 4440Sstevel@tonic-gate { \ 4450Sstevel@tonic-gate v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL,IS_SET); \ 4460Sstevel@tonic-gate ret+=ASN1_object_size(1,v,mtag); \ 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate #define M_ASN1_I2D_len_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \ 4500Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0))\ 4510Sstevel@tonic-gate { \ 4520Sstevel@tonic-gate v=i2d_ASN1_SET(a,NULL,f,tag,V_ASN1_UNIVERSAL, \ 4530Sstevel@tonic-gate IS_SEQUENCE); \ 4540Sstevel@tonic-gate ret+=ASN1_object_size(1,v,mtag); \ 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate #define M_ASN1_I2D_len_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \ 4580Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0))\ 4590Sstevel@tonic-gate { \ 4600Sstevel@tonic-gate v=i2d_ASN1_SET_OF_##type(a,NULL,f,tag, \ 4610Sstevel@tonic-gate V_ASN1_UNIVERSAL, \ 4620Sstevel@tonic-gate IS_SEQUENCE); \ 4630Sstevel@tonic-gate ret+=ASN1_object_size(1,v,mtag); \ 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* Put Macros */ 4670Sstevel@tonic-gate #define M_ASN1_I2D_put(a,f) f(a,&p) 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_opt(a,f,t) \ 4700Sstevel@tonic-gate if (a != NULL) \ 4710Sstevel@tonic-gate { \ 4720Sstevel@tonic-gate unsigned char *q=p; \ 4730Sstevel@tonic-gate f(a,&p); \ 4740Sstevel@tonic-gate *q=(V_ASN1_CONTEXT_SPECIFIC|t|(*q&V_ASN1_CONSTRUCTED));\ 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate #define M_ASN1_I2D_put_SET(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SET,\ 4780Sstevel@tonic-gate V_ASN1_UNIVERSAL,IS_SET) 4790Sstevel@tonic-gate #define M_ASN1_I2D_put_SET_type(type,a,f) \ 4800Sstevel@tonic-gate i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SET,V_ASN1_UNIVERSAL,IS_SET) 4810Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SET(a,f,x) i2d_ASN1_SET(a,&p,f,x,\ 4820Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC,IS_SET) 4830Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SET_type(type,a,f,x) \ 4840Sstevel@tonic-gate i2d_ASN1_SET_OF_##type(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC,IS_SET) 4850Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SEQUENCE(a,f,x) i2d_ASN1_SET(a,&p,f,x,\ 4860Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC,IS_SEQUENCE) 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate #define M_ASN1_I2D_put_SEQUENCE(a,f) i2d_ASN1_SET(a,&p,f,V_ASN1_SEQUENCE,\ 4890Sstevel@tonic-gate V_ASN1_UNIVERSAL,IS_SEQUENCE) 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate #define M_ASN1_I2D_put_SEQUENCE_type(type,a,f) \ 4920Sstevel@tonic-gate i2d_ASN1_SET_OF_##type(a,&p,f,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL, \ 4930Sstevel@tonic-gate IS_SEQUENCE) 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate #define M_ASN1_I2D_put_SEQUENCE_opt(a,f) \ 4960Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 4970Sstevel@tonic-gate M_ASN1_I2D_put_SEQUENCE(a,f); 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SET_opt(a,f,x) \ 5000Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 5010Sstevel@tonic-gate { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \ 5020Sstevel@tonic-gate IS_SET); } 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SET_opt_type(type,a,f,x) \ 5050Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 5060Sstevel@tonic-gate { i2d_ASN1_SET_OF_##type(a,&p,f,x, \ 5070Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC, \ 5080Sstevel@tonic-gate IS_SET); } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SEQUENCE_opt(a,f,x) \ 5110Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 5120Sstevel@tonic-gate { i2d_ASN1_SET(a,&p,f,x,V_ASN1_CONTEXT_SPECIFIC, \ 5130Sstevel@tonic-gate IS_SEQUENCE); } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate #define M_ASN1_I2D_put_IMP_SEQUENCE_opt_type(type,a,f,x) \ 5160Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 5170Sstevel@tonic-gate { i2d_ASN1_SET_OF_##type(a,&p,f,x, \ 5180Sstevel@tonic-gate V_ASN1_CONTEXT_SPECIFIC, \ 5190Sstevel@tonic-gate IS_SEQUENCE); } 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate #define M_ASN1_I2D_put_EXP_opt(a,f,tag,v) \ 5220Sstevel@tonic-gate if (a != NULL) \ 5230Sstevel@tonic-gate { \ 5240Sstevel@tonic-gate ASN1_put_object(&p,1,v,tag,V_ASN1_CONTEXT_SPECIFIC); \ 5250Sstevel@tonic-gate f(a,&p); \ 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate #define M_ASN1_I2D_put_EXP_SET_opt(a,f,mtag,tag,v) \ 5290Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 5300Sstevel@tonic-gate { \ 5310Sstevel@tonic-gate ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \ 5320Sstevel@tonic-gate i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SET); \ 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate #define M_ASN1_I2D_put_EXP_SEQUENCE_opt(a,f,mtag,tag,v) \ 5360Sstevel@tonic-gate if ((a != NULL) && (sk_num(a) != 0)) \ 5370Sstevel@tonic-gate { \ 5380Sstevel@tonic-gate ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \ 5390Sstevel@tonic-gate i2d_ASN1_SET(a,&p,f,tag,V_ASN1_UNIVERSAL,IS_SEQUENCE); \ 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate #define M_ASN1_I2D_put_EXP_SEQUENCE_opt_type(type,a,f,mtag,tag,v) \ 5430Sstevel@tonic-gate if ((a != NULL) && (sk_##type##_num(a) != 0)) \ 5440Sstevel@tonic-gate { \ 5450Sstevel@tonic-gate ASN1_put_object(&p,1,v,mtag,V_ASN1_CONTEXT_SPECIFIC); \ 5460Sstevel@tonic-gate i2d_ASN1_SET_OF_##type(a,&p,f,tag,V_ASN1_UNIVERSAL, \ 5470Sstevel@tonic-gate IS_SEQUENCE); \ 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate #define M_ASN1_I2D_seq_total() \ 5510Sstevel@tonic-gate r=ASN1_object_size(1,ret,V_ASN1_SEQUENCE); \ 5520Sstevel@tonic-gate if (pp == NULL) return(r); \ 5530Sstevel@tonic-gate p= *pp; \ 5540Sstevel@tonic-gate ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL) 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate #define M_ASN1_I2D_INF_seq_start(tag,ctx) \ 5570Sstevel@tonic-gate *(p++)=(V_ASN1_CONSTRUCTED|(tag)|(ctx)); \ 5580Sstevel@tonic-gate *(p++)=0x80 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate #define M_ASN1_I2D_INF_seq_end() *(p++)=0x00; *(p++)=0x00 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate #define M_ASN1_I2D_finish() *pp=p; \ 5630Sstevel@tonic-gate return(r); 5640Sstevel@tonic-gate 565*2139Sjp161948 int asn1_GetSequence(ASN1_const_CTX *c, long *length); 566*2139Sjp161948 void asn1_add_error(const unsigned char *address,int offset); 5670Sstevel@tonic-gate #ifdef __cplusplus 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate #endif 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate #endif 572