10Sstevel@tonic-gate /* crypto/asn1/a_set.c */
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 #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/asn1_mac.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate #ifndef NO_ASN1_OLD
640Sstevel@tonic-gate
650Sstevel@tonic-gate typedef struct
660Sstevel@tonic-gate {
670Sstevel@tonic-gate unsigned char *pbData;
680Sstevel@tonic-gate int cbData;
690Sstevel@tonic-gate } MYBLOB;
700Sstevel@tonic-gate
710Sstevel@tonic-gate /* SetBlobCmp
720Sstevel@tonic-gate * This function compares two elements of SET_OF block
730Sstevel@tonic-gate */
SetBlobCmp(const void * elem1,const void * elem2)740Sstevel@tonic-gate static int SetBlobCmp(const void *elem1, const void *elem2 )
750Sstevel@tonic-gate {
760Sstevel@tonic-gate const MYBLOB *b1 = (const MYBLOB *)elem1;
770Sstevel@tonic-gate const MYBLOB *b2 = (const MYBLOB *)elem2;
780Sstevel@tonic-gate int r;
790Sstevel@tonic-gate
800Sstevel@tonic-gate r = memcmp(b1->pbData, b2->pbData,
810Sstevel@tonic-gate b1->cbData < b2->cbData ? b1->cbData : b2->cbData);
820Sstevel@tonic-gate if(r != 0)
830Sstevel@tonic-gate return r;
840Sstevel@tonic-gate return b1->cbData-b2->cbData;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* int is_set: if TRUE, then sort the contents (i.e. it isn't a SEQUENCE) */
i2d_ASN1_SET(STACK * a,unsigned char ** pp,i2d_of_void * i2d,int ex_tag,int ex_class,int is_set)88*2139Sjp161948 int i2d_ASN1_SET(STACK *a, unsigned char **pp, i2d_of_void *i2d, int ex_tag,
89*2139Sjp161948 int ex_class, int is_set)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate int ret=0,r;
920Sstevel@tonic-gate int i;
930Sstevel@tonic-gate unsigned char *p;
940Sstevel@tonic-gate unsigned char *pStart, *pTempMem;
950Sstevel@tonic-gate MYBLOB *rgSetBlob;
960Sstevel@tonic-gate int totSize;
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (a == NULL) return(0);
990Sstevel@tonic-gate for (i=sk_num(a)-1; i>=0; i--)
100*2139Sjp161948 ret+=i2d(sk_value(a,i),NULL);
1010Sstevel@tonic-gate r=ASN1_object_size(1,ret,ex_tag);
1020Sstevel@tonic-gate if (pp == NULL) return(r);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate p= *pp;
1050Sstevel@tonic-gate ASN1_put_object(&p,1,ret,ex_tag,ex_class);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /* Modified by gp@nsj.co.jp */
1080Sstevel@tonic-gate /* And then again by Ben */
1090Sstevel@tonic-gate /* And again by Steve */
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if(!is_set || (sk_num(a) < 2))
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate for (i=0; i<sk_num(a); i++)
114*2139Sjp161948 i2d(sk_value(a,i),&p);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate *pp=p;
1170Sstevel@tonic-gate return(r);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate pStart = p; /* Catch the beg of Setblobs*/
121*2139Sjp161948 /* In this array we will store the SET blobs */
122*2139Sjp161948 rgSetBlob = (MYBLOB *)OPENSSL_malloc(sk_num(a) * sizeof(MYBLOB));
123*2139Sjp161948 if (rgSetBlob == NULL)
124*2139Sjp161948 {
125*2139Sjp161948 ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
126*2139Sjp161948 return(0);
127*2139Sjp161948 }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate for (i=0; i<sk_num(a); i++)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate rgSetBlob[i].pbData = p; /* catch each set encode blob */
132*2139Sjp161948 i2d(sk_value(a,i),&p);
1330Sstevel@tonic-gate rgSetBlob[i].cbData = p - rgSetBlob[i].pbData; /* Length of this
1340Sstevel@tonic-gate SetBlob
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate *pp=p;
1380Sstevel@tonic-gate totSize = p - pStart; /* This is the total size of all set blobs */
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /* Now we have to sort the blobs. I am using a simple algo.
1410Sstevel@tonic-gate *Sort ptrs *Copy to temp-mem *Copy from temp-mem to user-mem*/
1420Sstevel@tonic-gate qsort( rgSetBlob, sk_num(a), sizeof(MYBLOB), SetBlobCmp);
143*2139Sjp161948 if (!(pTempMem = OPENSSL_malloc(totSize)))
144*2139Sjp161948 {
145*2139Sjp161948 ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
146*2139Sjp161948 return(0);
147*2139Sjp161948 }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /* Copy to temp mem */
1500Sstevel@tonic-gate p = pTempMem;
1510Sstevel@tonic-gate for(i=0; i<sk_num(a); ++i)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData);
1540Sstevel@tonic-gate p += rgSetBlob[i].cbData;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /* Copy back to user mem*/
1580Sstevel@tonic-gate memcpy(pStart, pTempMem, totSize);
1590Sstevel@tonic-gate OPENSSL_free(pTempMem);
1600Sstevel@tonic-gate OPENSSL_free(rgSetBlob);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate return(r);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
d2i_ASN1_SET(STACK ** a,const unsigned char ** pp,long length,d2i_of_void * d2i,void (* free_func)(void *),int ex_tag,int ex_class)165*2139Sjp161948 STACK *d2i_ASN1_SET(STACK **a, const unsigned char **pp, long length,
166*2139Sjp161948 d2i_of_void *d2i, void (*free_func)(void *), int ex_tag,
167*2139Sjp161948 int ex_class)
1680Sstevel@tonic-gate {
169*2139Sjp161948 ASN1_const_CTX c;
1700Sstevel@tonic-gate STACK *ret=NULL;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate if ((a == NULL) || ((*a) == NULL))
173*2139Sjp161948 {
174*2139Sjp161948 if ((ret=sk_new_null()) == NULL)
175*2139Sjp161948 {
176*2139Sjp161948 ASN1err(ASN1_F_D2I_ASN1_SET,ERR_R_MALLOC_FAILURE);
177*2139Sjp161948 goto err;
178*2139Sjp161948 }
179*2139Sjp161948 }
1800Sstevel@tonic-gate else
1810Sstevel@tonic-gate ret=(*a);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate c.p= *pp;
1840Sstevel@tonic-gate c.max=(length == 0)?0:(c.p+length);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate c.inf=ASN1_get_object(&c.p,&c.slen,&c.tag,&c.xclass,c.max-c.p);
1870Sstevel@tonic-gate if (c.inf & 0x80) goto err;
1880Sstevel@tonic-gate if (ex_class != c.xclass)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_CLASS);
1910Sstevel@tonic-gate goto err;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate if (ex_tag != c.tag)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_TAG);
1960Sstevel@tonic-gate goto err;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate if ((c.slen+c.p) > c.max)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_LENGTH_ERROR);
2010Sstevel@tonic-gate goto err;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate /* check for infinite constructed - it can be as long
2040Sstevel@tonic-gate * as the amount of data passed to us */
2050Sstevel@tonic-gate if (c.inf == (V_ASN1_CONSTRUCTED+1))
2060Sstevel@tonic-gate c.slen=length+ *pp-c.p;
2070Sstevel@tonic-gate c.max=c.p+c.slen;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate while (c.p < c.max)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate char *s;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (M_ASN1_D2I_end_sequence()) break;
214*2139Sjp161948 /* XXX: This was called with 4 arguments, incorrectly, it seems
215*2139Sjp161948 if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) */
216*2139Sjp161948 if ((s=d2i(NULL,&c.p,c.slen)) == NULL)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_ERROR_PARSING_SET_ELEMENT);
2190Sstevel@tonic-gate asn1_add_error(*pp,(int)(c.q- *pp));
2200Sstevel@tonic-gate goto err;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate if (!sk_push(ret,s)) goto err;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate if (a != NULL) (*a)=ret;
2250Sstevel@tonic-gate *pp=c.p;
2260Sstevel@tonic-gate return(ret);
2270Sstevel@tonic-gate err:
2280Sstevel@tonic-gate if ((ret != NULL) && ((a == NULL) || (*a != ret)))
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate if (free_func != NULL)
2310Sstevel@tonic-gate sk_pop_free(ret,free_func);
2320Sstevel@tonic-gate else
2330Sstevel@tonic-gate sk_free(ret);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate return(NULL);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate #endif
239