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.
8296465Sdelphij *
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).
15296465Sdelphij *
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.
22296465Sdelphij *
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 :-).
37296465Sdelphij * 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)"
40296465Sdelphij *
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.
52296465Sdelphij *
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
65296465Sdelphijtypedef struct {
6655714Skris    unsigned char *pbData;
6755714Skris    int cbData;
68296465Sdelphij} MYBLOB;
6955714Skris
70296465Sdelphij/*
71296465Sdelphij * SetBlobCmp This function compares two elements of SET_OF block
7255714Skris */
73296465Sdelphijstatic int SetBlobCmp(const void *elem1, const void *elem2)
74296465Sdelphij{
7555714Skris    const MYBLOB *b1 = (const MYBLOB *)elem1;
7655714Skris    const MYBLOB *b2 = (const MYBLOB *)elem2;
7755714Skris    int r;
7855714Skris
7955714Skris    r = memcmp(b1->pbData, b2->pbData,
80296465Sdelphij               b1->cbData < b2->cbData ? b1->cbData : b2->cbData);
81296465Sdelphij    if (r != 0)
82296465Sdelphij        return r;
83296465Sdelphij    return b1->cbData - b2->cbData;
84296465Sdelphij}
8555714Skris
86296465Sdelphij/*
87296465Sdelphij * int is_set: if TRUE, then sort the contents (i.e. it isn't a SEQUENCE)
88296465Sdelphij */
89296465Sdelphijint i2d_ASN1_SET(STACK * a, unsigned char **pp, i2d_of_void *i2d, int ex_tag,
90296465Sdelphij                 int ex_class, int is_set)
91296465Sdelphij{
92296465Sdelphij    int ret = 0, r;
93296465Sdelphij    int i;
94296465Sdelphij    unsigned char *p;
95296465Sdelphij    unsigned char *pStart, *pTempMem;
96296465Sdelphij    MYBLOB *rgSetBlob;
97296465Sdelphij    int totSize;
9855714Skris
99296465Sdelphij    if (a == NULL)
100296465Sdelphij        return (0);
101296465Sdelphij    for (i = sk_num(a) - 1; i >= 0; i--)
102296465Sdelphij        ret += i2d(sk_value(a, i), NULL);
103296465Sdelphij    r = ASN1_object_size(1, ret, ex_tag);
104296465Sdelphij    if (pp == NULL)
105296465Sdelphij        return (r);
10655714Skris
107296465Sdelphij    p = *pp;
108296465Sdelphij    ASN1_put_object(&p, 1, ret, ex_tag, ex_class);
10955714Skris
11055714Skris/* Modified by gp@nsj.co.jp */
111296465Sdelphij    /* And then again by Ben */
112296465Sdelphij    /* And again by Steve */
11355714Skris
114296465Sdelphij    if (!is_set || (sk_num(a) < 2)) {
115296465Sdelphij        for (i = 0; i < sk_num(a); i++)
116296465Sdelphij            i2d(sk_value(a, i), &p);
11755714Skris
118296465Sdelphij        *pp = p;
119296465Sdelphij        return (r);
120296465Sdelphij    }
12155714Skris
122296465Sdelphij    pStart = p;                 /* Catch the beg of Setblobs */
123296465Sdelphij    /* In this array we will store the SET blobs */
124296465Sdelphij    rgSetBlob = (MYBLOB *) OPENSSL_malloc(sk_num(a) * sizeof(MYBLOB));
125296465Sdelphij    if (rgSetBlob == NULL) {
126296465Sdelphij        ASN1err(ASN1_F_I2D_ASN1_SET, ERR_R_MALLOC_FAILURE);
127296465Sdelphij        return (0);
128296465Sdelphij    }
12955714Skris
130296465Sdelphij    for (i = 0; i < sk_num(a); i++) {
131296465Sdelphij        rgSetBlob[i].pbData = p; /* catch each set encode blob */
132296465Sdelphij        i2d(sk_value(a, i), &p);
133296465Sdelphij        rgSetBlob[i].cbData = p - rgSetBlob[i].pbData; /* Length of this
134296465Sdelphij                                                        * SetBlob */
135296465Sdelphij    }
136296465Sdelphij    *pp = p;
137296465Sdelphij    totSize = p - pStart;       /* This is the total size of all set blobs */
13855714Skris
139296465Sdelphij    /*
140296465Sdelphij     * Now we have to sort the blobs. I am using a simple algo. *Sort ptrs
141296465Sdelphij     * *Copy to temp-mem *Copy from temp-mem to user-mem
142296465Sdelphij     */
143296465Sdelphij    qsort(rgSetBlob, sk_num(a), sizeof(MYBLOB), SetBlobCmp);
144296465Sdelphij    if (!(pTempMem = OPENSSL_malloc(totSize))) {
145296465Sdelphij        ASN1err(ASN1_F_I2D_ASN1_SET, ERR_R_MALLOC_FAILURE);
146296465Sdelphij        return (0);
147296465Sdelphij    }
14855714Skris
14955714Skris/* Copy to temp mem */
150296465Sdelphij    p = pTempMem;
151296465Sdelphij    for (i = 0; i < sk_num(a); ++i) {
152296465Sdelphij        memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData);
153296465Sdelphij        p += rgSetBlob[i].cbData;
154296465Sdelphij    }
15555714Skris
15655714Skris/* Copy back to user mem*/
157296465Sdelphij    memcpy(pStart, pTempMem, totSize);
158296465Sdelphij    OPENSSL_free(pTempMem);
159296465Sdelphij    OPENSSL_free(rgSetBlob);
16055714Skris
161296465Sdelphij    return (r);
162296465Sdelphij}
16355714Skris
164296465SdelphijSTACK *d2i_ASN1_SET(STACK ** a, const unsigned char **pp, long length,
165296465Sdelphij                    d2i_of_void *d2i, void (*free_func) (void *), int ex_tag,
166296465Sdelphij                    int ex_class)
167296465Sdelphij{
168296465Sdelphij    ASN1_const_CTX c;
169296465Sdelphij    STACK *ret = NULL;
17055714Skris
171296465Sdelphij    if ((a == NULL) || ((*a) == NULL)) {
172296465Sdelphij        if ((ret = sk_new_null()) == NULL) {
173296465Sdelphij            ASN1err(ASN1_F_D2I_ASN1_SET, ERR_R_MALLOC_FAILURE);
174296465Sdelphij            goto err;
175296465Sdelphij        }
176296465Sdelphij    } else
177296465Sdelphij        ret = (*a);
17855714Skris
179296465Sdelphij    c.p = *pp;
180296465Sdelphij    c.max = (length == 0) ? 0 : (c.p + length);
18155714Skris
182296465Sdelphij    c.inf = ASN1_get_object(&c.p, &c.slen, &c.tag, &c.xclass, c.max - c.p);
183296465Sdelphij    if (c.inf & 0x80)
184296465Sdelphij        goto err;
185296465Sdelphij    if (ex_class != c.xclass) {
186296465Sdelphij        ASN1err(ASN1_F_D2I_ASN1_SET, ASN1_R_BAD_CLASS);
187296465Sdelphij        goto err;
188296465Sdelphij    }
189296465Sdelphij    if (ex_tag != c.tag) {
190296465Sdelphij        ASN1err(ASN1_F_D2I_ASN1_SET, ASN1_R_BAD_TAG);
191296465Sdelphij        goto err;
192296465Sdelphij    }
193296465Sdelphij    if ((c.slen + c.p) > c.max) {
194296465Sdelphij        ASN1err(ASN1_F_D2I_ASN1_SET, ASN1_R_LENGTH_ERROR);
195296465Sdelphij        goto err;
196296465Sdelphij    }
197296465Sdelphij    /*
198296465Sdelphij     * check for infinite constructed - it can be as long as the amount of
199296465Sdelphij     * data passed to us
200296465Sdelphij     */
201296465Sdelphij    if (c.inf == (V_ASN1_CONSTRUCTED + 1))
202296465Sdelphij        c.slen = length + *pp - c.p;
203296465Sdelphij    c.max = c.p + c.slen;
20455714Skris
205296465Sdelphij    while (c.p < c.max) {
206296465Sdelphij        char *s;
20755714Skris
208296465Sdelphij        if (M_ASN1_D2I_end_sequence())
209296465Sdelphij            break;
210296465Sdelphij        /*
211296465Sdelphij         * XXX: This was called with 4 arguments, incorrectly, it seems if
212296465Sdelphij         * ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL)
213296465Sdelphij         */
214296465Sdelphij        if ((s = d2i(NULL, &c.p, c.slen)) == NULL) {
215296465Sdelphij            ASN1err(ASN1_F_D2I_ASN1_SET, ASN1_R_ERROR_PARSING_SET_ELEMENT);
216296465Sdelphij            asn1_add_error(*pp, (int)(c.q - *pp));
217296465Sdelphij            goto err;
218296465Sdelphij        }
219296465Sdelphij        if (!sk_push(ret, s))
220296465Sdelphij            goto err;
221296465Sdelphij    }
222296465Sdelphij    if (a != NULL)
223296465Sdelphij        (*a) = ret;
224296465Sdelphij    *pp = c.p;
225296465Sdelphij    return (ret);
226296465Sdelphij err:
227296465Sdelphij    if ((ret != NULL) && ((a == NULL) || (*a != ret))) {
228296465Sdelphij        if (free_func != NULL)
229296465Sdelphij            sk_pop_free(ret, free_func);
230296465Sdelphij        else
231296465Sdelphij            sk_free(ret);
232296465Sdelphij    }
233296465Sdelphij    return (NULL);
234296465Sdelphij}
23555714Skris
236109998Smarkm#endif
237