155714Skris/* crypto/asn1/a_set.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/asn1_mac.h>
6255714Skris
63109998Smarkm#ifndef NO_ASN1_OLD
64109998Smarkm
6555714Skristypedef struct
6655714Skris    {
6755714Skris    unsigned char *pbData;
6855714Skris    int cbData;
6955714Skris    } MYBLOB;
7055714Skris
7155714Skris/* SetBlobCmp
7255714Skris * This function compares two elements of SET_OF block
7355714Skris */
7455714Skrisstatic int SetBlobCmp(const void *elem1, const void *elem2 )
7555714Skris    {
7655714Skris    const MYBLOB *b1 = (const MYBLOB *)elem1;
7755714Skris    const MYBLOB *b2 = (const MYBLOB *)elem2;
7855714Skris    int r;
7955714Skris
8055714Skris    r = memcmp(b1->pbData, b2->pbData,
8155714Skris	       b1->cbData < b2->cbData ? b1->cbData : b2->cbData);
8255714Skris    if(r != 0)
8355714Skris	return r;
8455714Skris    return b1->cbData-b2->cbData;
8555714Skris    }
8655714Skris
8755714Skris/* int is_set:  if TRUE, then sort the contents (i.e. it isn't a SEQUENCE)    */
88238405Sjkimint i2d_ASN1_SET(STACK_OF(OPENSSL_BLOCK) *a, unsigned char **pp,
89238405Sjkim		 i2d_of_void *i2d, int ex_tag, int ex_class,
90238405Sjkim		 int is_set)
9155714Skris	{
9255714Skris	int ret=0,r;
9355714Skris	int i;
9455714Skris	unsigned char *p;
9555714Skris        unsigned char *pStart, *pTempMem;
9655714Skris        MYBLOB *rgSetBlob;
9755714Skris        int totSize;
9855714Skris
9955714Skris	if (a == NULL) return(0);
100238405Sjkim	for (i=sk_OPENSSL_BLOCK_num(a)-1; i>=0; i--)
101238405Sjkim		ret+=i2d(sk_OPENSSL_BLOCK_value(a,i),NULL);
10255714Skris	r=ASN1_object_size(1,ret,ex_tag);
10355714Skris	if (pp == NULL) return(r);
10455714Skris
10555714Skris	p= *pp;
10655714Skris	ASN1_put_object(&p,1,ret,ex_tag,ex_class);
10755714Skris
10855714Skris/* Modified by gp@nsj.co.jp */
10955714Skris	/* And then again by Ben */
11055714Skris	/* And again by Steve */
11155714Skris
112238405Sjkim	if(!is_set || (sk_OPENSSL_BLOCK_num(a) < 2))
11355714Skris		{
114238405Sjkim		for (i=0; i<sk_OPENSSL_BLOCK_num(a); i++)
115238405Sjkim                	i2d(sk_OPENSSL_BLOCK_value(a,i),&p);
11655714Skris
11755714Skris		*pp=p;
11855714Skris		return(r);
11955714Skris		}
12055714Skris
12155714Skris        pStart  = p; /* Catch the beg of Setblobs*/
122160814Ssimon		/* In this array we will store the SET blobs */
123238405Sjkim		rgSetBlob = OPENSSL_malloc(sk_OPENSSL_BLOCK_num(a) * sizeof(MYBLOB));
124160814Ssimon		if (rgSetBlob == NULL)
125160814Ssimon			{
126160814Ssimon			ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
127160814Ssimon			return(0);
128160814Ssimon			}
12955714Skris
130238405Sjkim        for (i=0; i<sk_OPENSSL_BLOCK_num(a); i++)
13155714Skris	        {
13255714Skris                rgSetBlob[i].pbData = p;  /* catch each set encode blob */
133238405Sjkim                i2d(sk_OPENSSL_BLOCK_value(a,i),&p);
13455714Skris                rgSetBlob[i].cbData = p - rgSetBlob[i].pbData; /* Length of this
13555714SkrisSetBlob
13655714Skris*/
13755714Skris		}
13855714Skris        *pp=p;
13955714Skris        totSize = p - pStart; /* This is the total size of all set blobs */
14055714Skris
14155714Skris /* Now we have to sort the blobs. I am using a simple algo.
14255714Skris    *Sort ptrs *Copy to temp-mem *Copy from temp-mem to user-mem*/
143238405Sjkim        qsort( rgSetBlob, sk_OPENSSL_BLOCK_num(a), sizeof(MYBLOB), SetBlobCmp);
144160814Ssimon		if (!(pTempMem = OPENSSL_malloc(totSize)))
145160814Ssimon			{
146160814Ssimon			ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
147160814Ssimon			return(0);
148160814Ssimon			}
14955714Skris
15055714Skris/* Copy to temp mem */
15155714Skris        p = pTempMem;
152238405Sjkim        for(i=0; i<sk_OPENSSL_BLOCK_num(a); ++i)
15355714Skris		{
15455714Skris                memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData);
15555714Skris                p += rgSetBlob[i].cbData;
15655714Skris		}
15755714Skris
15855714Skris/* Copy back to user mem*/
15955714Skris        memcpy(pStart, pTempMem, totSize);
16068651Skris        OPENSSL_free(pTempMem);
16168651Skris        OPENSSL_free(rgSetBlob);
16255714Skris
16355714Skris        return(r);
16455714Skris        }
16555714Skris
166238405SjkimSTACK_OF(OPENSSL_BLOCK) *d2i_ASN1_SET(STACK_OF(OPENSSL_BLOCK) **a,
167238405Sjkim			      const unsigned char **pp,
168238405Sjkim			      long length, d2i_of_void *d2i,
169238405Sjkim			      void (*free_func)(OPENSSL_BLOCK), int ex_tag,
170238405Sjkim			      int ex_class)
17155714Skris	{
172160814Ssimon	ASN1_const_CTX c;
173238405Sjkim	STACK_OF(OPENSSL_BLOCK) *ret=NULL;
17455714Skris
17555714Skris	if ((a == NULL) || ((*a) == NULL))
176160814Ssimon		{
177238405Sjkim		if ((ret=sk_OPENSSL_BLOCK_new_null()) == NULL)
178160814Ssimon			{
179160814Ssimon			ASN1err(ASN1_F_D2I_ASN1_SET,ERR_R_MALLOC_FAILURE);
180160814Ssimon			goto err;
181160814Ssimon			}
182160814Ssimon		}
18355714Skris	else
18455714Skris		ret=(*a);
18555714Skris
18655714Skris	c.p= *pp;
18755714Skris	c.max=(length == 0)?0:(c.p+length);
18855714Skris
18955714Skris	c.inf=ASN1_get_object(&c.p,&c.slen,&c.tag,&c.xclass,c.max-c.p);
19055714Skris	if (c.inf & 0x80) goto err;
19155714Skris	if (ex_class != c.xclass)
19255714Skris		{
19355714Skris		ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_CLASS);
19455714Skris		goto err;
19555714Skris		}
19655714Skris	if (ex_tag != c.tag)
19755714Skris		{
19855714Skris		ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_BAD_TAG);
19955714Skris		goto err;
20055714Skris		}
20155714Skris	if ((c.slen+c.p) > c.max)
20255714Skris		{
20355714Skris		ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_LENGTH_ERROR);
20455714Skris		goto err;
20555714Skris		}
20655714Skris	/* check for infinite constructed - it can be as long
20755714Skris	 * as the amount of data passed to us */
20855714Skris	if (c.inf == (V_ASN1_CONSTRUCTED+1))
20955714Skris		c.slen=length+ *pp-c.p;
21055714Skris	c.max=c.p+c.slen;
21155714Skris
21255714Skris	while (c.p < c.max)
21355714Skris		{
21455714Skris		char *s;
21555714Skris
21655714Skris		if (M_ASN1_D2I_end_sequence()) break;
217160814Ssimon		/* XXX: This was called with 4 arguments, incorrectly, it seems
218160814Ssimon		   if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) */
219160814Ssimon		if ((s=d2i(NULL,&c.p,c.slen)) == NULL)
22055714Skris			{
22155714Skris			ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_ERROR_PARSING_SET_ELEMENT);
222238405Sjkim			asn1_add_error(*pp,(int)(c.p- *pp));
22355714Skris			goto err;
22455714Skris			}
225238405Sjkim		if (!sk_OPENSSL_BLOCK_push(ret,s)) goto err;
22655714Skris		}
22755714Skris	if (a != NULL) (*a)=ret;
22855714Skris	*pp=c.p;
22955714Skris	return(ret);
23055714Skriserr:
23155714Skris	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
23255714Skris		{
23355714Skris		if (free_func != NULL)
234238405Sjkim			sk_OPENSSL_BLOCK_pop_free(ret,free_func);
23555714Skris		else
236238405Sjkim			sk_OPENSSL_BLOCK_free(ret);
23755714Skris		}
23855714Skris	return(NULL);
23955714Skris	}
24055714Skris
241109998Smarkm#endif
242