2000-12-08 19:09:35 +00:00
|
|
|
/* tasn_enc.c */
|
2008-11-05 18:39:08 +00:00
|
|
|
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
2000-12-08 19:09:35 +00:00
|
|
|
* project 2000.
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
2004-04-24 17:02:48 +00:00
|
|
|
* Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
|
2000-12-08 19:09:35 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* licensing@OpenSSL.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2001-02-20 12:51:56 +00:00
|
|
|
#include <string.h>
|
2005-09-20 20:19:07 +00:00
|
|
|
#include "cryptlib.h"
|
2000-12-08 19:09:35 +00:00
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
|
|
|
const ASN1_ITEM *it,
|
|
|
|
int tag, int aclass);
|
|
|
|
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
|
|
|
int skcontlen, const ASN1_ITEM *item,
|
|
|
|
int do_sort, int iclass);
|
|
|
|
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
|
|
|
const ASN1_TEMPLATE *tt,
|
|
|
|
int tag, int aclass);
|
|
|
|
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
|
|
|
|
const ASN1_ITEM *it, int flags);
|
|
|
|
|
|
|
|
/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
|
|
|
|
* to use indefinite length constructed encoding, where appropriate
|
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
|
|
|
|
const ASN1_ITEM *it)
|
|
|
|
{
|
2002-10-03 12:38:52 +00:00
|
|
|
return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2002-10-03 12:38:52 +00:00
|
|
|
return asn1_item_flags_i2d(val, out, it, 0);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2002-10-03 12:38:52 +00:00
|
|
|
|
|
|
|
/* Encode an ASN1 item, this is use by the
|
2000-12-08 19:09:35 +00:00
|
|
|
* standard 'i2d' function. 'out' points to
|
2002-10-03 12:38:52 +00:00
|
|
|
* a buffer to output the data to.
|
2000-12-08 19:09:35 +00:00
|
|
|
*
|
|
|
|
* The new i2d has one additional feature. If the output
|
|
|
|
* buffer is NULL (i.e. *out == NULL) then a buffer is
|
|
|
|
* allocated and populated with the encoding.
|
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
|
|
|
|
const ASN1_ITEM *it, int flags)
|
|
|
|
{
|
|
|
|
if (out && !*out)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
unsigned char *p, *buf;
|
|
|
|
int len;
|
2002-10-03 12:38:52 +00:00
|
|
|
len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (len <= 0)
|
|
|
|
return len;
|
2000-12-08 19:09:35 +00:00
|
|
|
buf = OPENSSL_malloc(len);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!buf)
|
|
|
|
return -1;
|
2000-12-08 19:09:35 +00:00
|
|
|
p = buf;
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_item_ex_i2d(&val, &p, it, -1, flags);
|
2000-12-08 19:09:35 +00:00
|
|
|
*out = buf;
|
|
|
|
return len;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2002-10-03 12:38:52 +00:00
|
|
|
|
|
|
|
return ASN1_item_ex_i2d(&val, out, it, -1, flags);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Encode an item, taking care of IMPLICIT tagging (if any).
|
|
|
|
* This function performs the normal item handling: it can be
|
|
|
|
* used in external types.
|
|
|
|
*/
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
|
|
|
const ASN1_ITEM *it, int tag, int aclass)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_TEMPLATE *tt = NULL;
|
|
|
|
unsigned char *p = NULL;
|
2002-10-03 12:38:52 +00:00
|
|
|
int i, seqcontlen, seqlen, ndef = 1;
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_COMPAT_FUNCS *cf;
|
|
|
|
const ASN1_EXTERN_FUNCS *ef;
|
|
|
|
const ASN1_AUX *aux = it->funcs;
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_aux_cb *asn1_cb = 0;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
|
2002-10-03 12:38:52 +00:00
|
|
|
return 0;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (aux && aux->asn1_cb)
|
2002-10-03 12:38:52 +00:00
|
|
|
asn1_cb = aux->asn1_cb;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
switch(it->itype)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
case ASN1_ITYPE_PRIMITIVE:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (it->templates)
|
2002-10-03 12:38:52 +00:00
|
|
|
return asn1_template_ex_i2d(pval, out, it->templates,
|
|
|
|
tag, aclass);
|
2000-12-08 19:09:35 +00:00
|
|
|
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASN1_ITYPE_MSTRING:
|
2002-10-03 12:38:52 +00:00
|
|
|
return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
case ASN1_ITYPE_CHOICE:
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
|
|
|
i = asn1_get_choice_selector(pval, it);
|
2004-04-24 17:02:48 +00:00
|
|
|
if ((i >= 0) && (i < it->tcount))
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_VALUE **pchval;
|
|
|
|
const ASN1_TEMPLATE *chtt;
|
|
|
|
chtt = it->templates + i;
|
|
|
|
pchval = asn1_get_field_ptr(pval, chtt);
|
2002-10-03 12:38:52 +00:00
|
|
|
return asn1_template_ex_i2d(pchval, out, chtt,
|
|
|
|
-1, aclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Fixme: error condition if selector out of range */
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASN1_ITYPE_EXTERN:
|
|
|
|
/* If new style i2d it does all the work */
|
|
|
|
ef = it->funcs;
|
|
|
|
return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
|
|
|
|
|
|
|
|
case ASN1_ITYPE_COMPAT:
|
|
|
|
/* old style hackery... */
|
|
|
|
cf = it->funcs;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (out)
|
|
|
|
p = *out;
|
2000-12-08 19:09:35 +00:00
|
|
|
i = cf->asn1_i2d(*pval, out);
|
|
|
|
/* Fixup for IMPLICIT tag: note this messes up for tags > 30,
|
|
|
|
* but so did the old code. Tags > 30 are very rare anyway.
|
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if (out && (tag != -1))
|
2000-12-08 19:09:35 +00:00
|
|
|
*p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
|
|
|
|
return i;
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
case ASN1_ITYPE_NDEF_SEQUENCE:
|
|
|
|
/* Use indefinite length constructed if requested */
|
|
|
|
if (aclass & ASN1_TFLG_NDEF) ndef = 2;
|
|
|
|
/* fall through */
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
case ASN1_ITYPE_SEQUENCE:
|
|
|
|
i = asn1_enc_restore(&seqcontlen, out, pval, it);
|
|
|
|
/* An error occurred */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i < 0)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* We have a valid cached encoding... */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i > 0)
|
|
|
|
return seqcontlen;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Otherwise carry on */
|
|
|
|
seqcontlen = 0;
|
|
|
|
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag == -1)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
tag = V_ASN1_SEQUENCE;
|
2002-10-03 12:38:52 +00:00
|
|
|
/* Retain any other flags in aclass */
|
|
|
|
aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
|
|
|
|
| V_ASN1_UNIVERSAL;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
|
|
|
/* First work out sequence content length */
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_TEMPLATE *seqtt;
|
|
|
|
ASN1_VALUE **pseqval;
|
|
|
|
seqtt = asn1_do_adb(pval, tt, 1);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!seqtt)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
pseqval = asn1_get_field_ptr(pval, seqtt);
|
|
|
|
/* FIXME: check for errors in enhanced version */
|
2002-10-03 12:38:52 +00:00
|
|
|
seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
|
|
|
|
-1, aclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
seqlen = ASN1_object_size(ndef, seqcontlen, tag);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!out)
|
|
|
|
return seqlen;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Output SEQUENCE header */
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_TEMPLATE *seqtt;
|
|
|
|
ASN1_VALUE **pseqval;
|
|
|
|
seqtt = asn1_do_adb(pval, tt, 1);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!seqtt)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
pseqval = asn1_get_field_ptr(pval, seqtt);
|
|
|
|
/* FIXME: check for errors in enhanced version */
|
2002-10-03 12:38:52 +00:00
|
|
|
asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (ndef == 2)
|
|
|
|
ASN1_put_eoc(out);
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
|
|
|
return seqlen;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
|
|
|
|
const ASN1_TEMPLATE *tt)
|
2002-10-06 12:10:35 +00:00
|
|
|
{
|
|
|
|
return asn1_template_ex_i2d(pval, out, tt, -1, 0);
|
|
|
|
}
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
|
|
|
const ASN1_TEMPLATE *tt, int tag, int iclass)
|
|
|
|
{
|
2002-10-03 12:38:52 +00:00
|
|
|
int i, ret, flags, ttag, tclass, ndef;
|
2000-12-08 19:09:35 +00:00
|
|
|
flags = tt->flags;
|
2002-10-03 12:38:52 +00:00
|
|
|
/* Work out tag and class to use: tagging may come
|
|
|
|
* either from the template or the arguments, not both
|
|
|
|
* because this would create ambiguity. Additionally
|
|
|
|
* the iclass argument may contain some additional flags
|
|
|
|
* which should be noted and passed down to other levels.
|
|
|
|
*/
|
|
|
|
if (flags & ASN1_TFLG_TAG_MASK)
|
|
|
|
{
|
|
|
|
/* Error if argument and template tagging */
|
|
|
|
if (tag != -1)
|
|
|
|
/* FIXME: error code here */
|
|
|
|
return -1;
|
|
|
|
/* Get tagging from template */
|
|
|
|
ttag = tt->tag;
|
|
|
|
tclass = flags & ASN1_TFLG_TAG_CLASS;
|
|
|
|
}
|
|
|
|
else if (tag != -1)
|
|
|
|
{
|
|
|
|
/* No template tagging, get from arguments */
|
|
|
|
ttag = tag;
|
|
|
|
tclass = iclass & ASN1_TFLG_TAG_CLASS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ttag = -1;
|
|
|
|
tclass = 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Remove any class mask from iflag.
|
|
|
|
*/
|
|
|
|
iclass &= ~ASN1_TFLG_TAG_CLASS;
|
|
|
|
|
|
|
|
/* At this point 'ttag' contains the outer tag to use,
|
|
|
|
* 'tclass' is the class and iclass is any flags passed
|
|
|
|
* to this function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* if template and arguments require ndef, use it */
|
|
|
|
if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
|
|
|
|
ndef = 2;
|
|
|
|
else ndef = 1;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_SK_MASK)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* SET OF, SEQUENCE OF */
|
|
|
|
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
|
|
|
|
int isset, sktag, skaclass;
|
|
|
|
int skcontlen, sklen;
|
|
|
|
ASN1_VALUE *skitem;
|
2002-10-03 12:38:52 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!*pval)
|
|
|
|
return 0;
|
2002-10-03 12:38:52 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_SET_OF)
|
|
|
|
{
|
2000-12-13 23:54:30 +00:00
|
|
|
isset = 1;
|
|
|
|
/* 2 means we reorder */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_SEQUENCE_OF)
|
|
|
|
isset = 2;
|
|
|
|
}
|
|
|
|
else isset = 0;
|
2002-10-03 12:38:52 +00:00
|
|
|
|
|
|
|
/* Work out inner tag value: if EXPLICIT
|
|
|
|
* or no tagging use underlying type.
|
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG))
|
|
|
|
{
|
2002-10-03 12:38:52 +00:00
|
|
|
sktag = ttag;
|
|
|
|
skaclass = tclass;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
skaclass = V_ASN1_UNIVERSAL;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (isset)
|
|
|
|
sktag = V_ASN1_SET;
|
2000-12-08 19:09:35 +00:00
|
|
|
else sktag = V_ASN1_SEQUENCE;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2002-10-03 12:38:52 +00:00
|
|
|
|
|
|
|
/* Determine total length of items */
|
2000-12-08 19:09:35 +00:00
|
|
|
skcontlen = 0;
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
skitem = sk_ASN1_VALUE_value(sk, i);
|
2002-10-03 12:38:52 +00:00
|
|
|
skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_ITEM_ptr(tt->item),
|
2002-10-03 12:38:52 +00:00
|
|
|
-1, iclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2002-10-03 12:38:52 +00:00
|
|
|
sklen = ASN1_object_size(ndef, skcontlen, sktag);
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If EXPLICIT need length of surrounding tag */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_EXPTAG)
|
2002-10-03 12:38:52 +00:00
|
|
|
ret = ASN1_object_size(ndef, sklen, ttag);
|
2000-12-08 19:09:35 +00:00
|
|
|
else ret = sklen;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!out)
|
|
|
|
return ret;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Now encode this lot... */
|
|
|
|
/* EXPLICIT tag */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_EXPTAG)
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_put_object(out, ndef, sklen, ttag, tclass);
|
2000-12-08 19:09:35 +00:00
|
|
|
/* SET or SEQUENCE and IMPLICIT tag */
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
|
|
|
|
/* And the stuff itself */
|
|
|
|
asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
|
|
|
|
isset, iclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ndef == 2)
|
|
|
|
{
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_put_eoc(out);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_EXPTAG)
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_put_eoc(out);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
return ret;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2002-10-03 12:38:52 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_EXPTAG)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* EXPLICIT tagging */
|
|
|
|
/* Find length of tagged item */
|
2002-10-03 12:38:52 +00:00
|
|
|
i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item),
|
|
|
|
-1, iclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!i)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Find length of EXPLICIT tag */
|
2002-10-03 12:38:52 +00:00
|
|
|
ret = ASN1_object_size(ndef, i, ttag);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (out)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Output tag and item */
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_put_object(out, ndef, i, ttag, tclass);
|
|
|
|
ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
|
|
|
|
-1, iclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ndef == 2)
|
|
|
|
ASN1_put_eoc(out);
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
return ret;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2002-10-03 12:38:52 +00:00
|
|
|
|
|
|
|
/* Either normal or IMPLICIT tagging: combine class and flags */
|
|
|
|
return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
|
|
|
|
ttag, tclass | iclass);
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Temporary structure used to hold DER encoding of items for SET OF */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char *data;
|
|
|
|
int length;
|
2000-12-13 23:54:30 +00:00
|
|
|
ASN1_VALUE *field;
|
2000-12-08 19:09:35 +00:00
|
|
|
} DER_ENC;
|
|
|
|
|
|
|
|
static int der_cmp(const void *a, const void *b)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const DER_ENC *d1 = a, *d2 = b;
|
|
|
|
int cmplen, i;
|
|
|
|
cmplen = (d1->length < d2->length) ? d1->length : d2->length;
|
|
|
|
i = memcmp(d1->data, d2->data, cmplen);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i)
|
|
|
|
return i;
|
2000-12-08 19:09:35 +00:00
|
|
|
return d1->length - d2->length;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Output the content octets of SET OF or SEQUENCE OF */
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
|
|
|
int skcontlen, const ASN1_ITEM *item,
|
|
|
|
int do_sort, int iclass)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int i;
|
|
|
|
ASN1_VALUE *skitem;
|
2001-03-08 14:02:28 +00:00
|
|
|
unsigned char *tmpdat = NULL, *p = NULL;
|
2001-08-26 21:04:21 +00:00
|
|
|
DER_ENC *derlst = NULL, *tder;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (do_sort)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Don't need to sort less than 2 items */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (sk_ASN1_VALUE_num(sk) < 2)
|
|
|
|
do_sort = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
|
|
|
|
* sizeof(*derlst));
|
2014-06-27 02:21:10 +00:00
|
|
|
if (!derlst)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
tmpdat = OPENSSL_malloc(skcontlen);
|
2014-06-27 02:21:10 +00:00
|
|
|
if (!tmpdat)
|
|
|
|
{
|
|
|
|
OPENSSL_free(derlst);
|
2004-04-24 17:02:48 +00:00
|
|
|
return 0;
|
2014-06-27 02:21:10 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
/* If not sorting just output each item */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!do_sort)
|
|
|
|
{
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
skitem = sk_ASN1_VALUE_value(sk, i);
|
2002-10-03 12:38:52 +00:00
|
|
|
ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
return 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
p = tmpdat;
|
2004-04-24 17:02:48 +00:00
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Doing sort: build up a list of each member's DER encoding */
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
skitem = sk_ASN1_VALUE_value(sk, i);
|
|
|
|
tder->data = p;
|
2002-10-03 12:38:52 +00:00
|
|
|
tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
|
2000-12-13 23:54:30 +00:00
|
|
|
tder->field = skitem;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Now sort them */
|
|
|
|
qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
|
|
|
|
/* Output sorted DER encoding */
|
|
|
|
p = *out;
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
memcpy(p, tder->data, tder->length);
|
|
|
|
p += tder->length;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
*out = p;
|
2000-12-13 23:54:30 +00:00
|
|
|
/* If do_sort is 2 then reorder the STACK */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (do_sort == 2)
|
|
|
|
{
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk);
|
2004-04-24 17:02:48 +00:00
|
|
|
i++, tder++)
|
2008-02-28 14:05:01 +00:00
|
|
|
(void)sk_ASN1_VALUE_set(sk, i, tder->field);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
OPENSSL_free(derlst);
|
|
|
|
OPENSSL_free(tmpdat);
|
|
|
|
return 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
|
|
|
const ASN1_ITEM *it, int tag, int aclass)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int len;
|
|
|
|
int utype;
|
|
|
|
int usetag;
|
2002-10-03 12:38:52 +00:00
|
|
|
int ndef = 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
utype = it->utype;
|
|
|
|
|
|
|
|
/* Get length of content octets and maybe find
|
|
|
|
* out the underlying type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
len = asn1_ex_i2c(pval, NULL, &utype, it);
|
|
|
|
|
|
|
|
/* If SEQUENCE, SET or OTHER then header is
|
|
|
|
* included in pseudo content octets so don't
|
|
|
|
* include tag+length. We need to check here
|
|
|
|
* because the call to asn1_ex_i2c() could change
|
|
|
|
* utype.
|
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
|
2000-12-08 19:09:35 +00:00
|
|
|
(utype == V_ASN1_OTHER))
|
|
|
|
usetag = 0;
|
|
|
|
else usetag = 1;
|
|
|
|
|
|
|
|
/* -1 means omit type */
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (len == -1)
|
2002-10-03 12:38:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* -2 return is special meaning use ndef */
|
|
|
|
if (len == -2)
|
|
|
|
{
|
|
|
|
ndef = 2;
|
|
|
|
len = 0;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* If not implicitly tagged get tag from underlying type */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag == -1) tag = utype;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Output tag+length followed by content octets */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (out)
|
|
|
|
{
|
|
|
|
if (usetag)
|
|
|
|
ASN1_put_object(out, ndef, len, tag, aclass);
|
2000-12-08 19:09:35 +00:00
|
|
|
asn1_ex_i2c(pval, *out, &utype, it);
|
2003-02-25 19:03:31 +00:00
|
|
|
if (ndef)
|
|
|
|
ASN1_put_eoc(out);
|
|
|
|
else
|
|
|
|
*out += len;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (usetag)
|
|
|
|
return ASN1_object_size(ndef, len, tag);
|
2000-12-08 19:09:35 +00:00
|
|
|
return len;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Produce content octets from a structure */
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
|
|
|
const ASN1_ITEM *it)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_BOOLEAN *tbool = NULL;
|
|
|
|
ASN1_STRING *strtmp;
|
|
|
|
ASN1_OBJECT *otmp;
|
|
|
|
int utype;
|
2007-09-18 22:15:31 +00:00
|
|
|
const unsigned char *cont;
|
|
|
|
unsigned char c;
|
2000-12-08 19:09:35 +00:00
|
|
|
int len;
|
|
|
|
const ASN1_PRIMITIVE_FUNCS *pf;
|
|
|
|
pf = it->funcs;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (pf && pf->prim_i2c)
|
|
|
|
return pf->prim_i2c(pval, cout, putype, it);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Should type be omitted? */
|
2004-04-24 17:02:48 +00:00
|
|
|
if ((it->itype != ASN1_ITYPE_PRIMITIVE)
|
|
|
|
|| (it->utype != V_ASN1_BOOLEAN))
|
|
|
|
{
|
|
|
|
if (!*pval) return -1;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (it->itype == ASN1_ITYPE_MSTRING)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If MSTRING type set the underlying type */
|
|
|
|
strtmp = (ASN1_STRING *)*pval;
|
|
|
|
utype = strtmp->type;
|
|
|
|
*putype = utype;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (it->utype == V_ASN1_ANY)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If ANY set type and pointer to value */
|
|
|
|
ASN1_TYPE *typ;
|
|
|
|
typ = (ASN1_TYPE *)*pval;
|
|
|
|
utype = typ->type;
|
|
|
|
*putype = utype;
|
2006-11-16 00:19:39 +00:00
|
|
|
pval = &typ->value.asn1_value;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else utype = *putype;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
switch(utype)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
case V_ASN1_OBJECT:
|
|
|
|
otmp = (ASN1_OBJECT *)*pval;
|
|
|
|
cont = otmp->data;
|
|
|
|
len = otmp->length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_NULL:
|
|
|
|
cont = NULL;
|
|
|
|
len = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_BOOLEAN:
|
|
|
|
tbool = (ASN1_BOOLEAN *)pval;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (*tbool == -1)
|
|
|
|
return -1;
|
2006-01-19 17:16:56 +00:00
|
|
|
if (it->utype != V_ASN1_ANY)
|
|
|
|
{
|
|
|
|
/* Default handling if value == size field then omit */
|
|
|
|
if (*tbool && (it->size > 0))
|
|
|
|
return -1;
|
|
|
|
if (!*tbool && !it->size)
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
c = (unsigned char)*tbool;
|
|
|
|
cont = &c;
|
|
|
|
len = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_BIT_STRING:
|
2004-04-24 17:02:48 +00:00
|
|
|
return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
|
|
|
|
cout ? &cout : NULL);
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_INTEGER:
|
|
|
|
case V_ASN1_NEG_INTEGER:
|
|
|
|
case V_ASN1_ENUMERATED:
|
|
|
|
case V_ASN1_NEG_ENUMERATED:
|
|
|
|
/* These are all have the same content format
|
|
|
|
* as ASN1_INTEGER
|
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval,
|
|
|
|
cout ? &cout : NULL);
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_OCTET_STRING:
|
|
|
|
case V_ASN1_NUMERICSTRING:
|
|
|
|
case V_ASN1_PRINTABLESTRING:
|
|
|
|
case V_ASN1_T61STRING:
|
|
|
|
case V_ASN1_VIDEOTEXSTRING:
|
|
|
|
case V_ASN1_IA5STRING:
|
|
|
|
case V_ASN1_UTCTIME:
|
|
|
|
case V_ASN1_GENERALIZEDTIME:
|
|
|
|
case V_ASN1_GRAPHICSTRING:
|
|
|
|
case V_ASN1_VISIBLESTRING:
|
|
|
|
case V_ASN1_GENERALSTRING:
|
|
|
|
case V_ASN1_UNIVERSALSTRING:
|
|
|
|
case V_ASN1_BMPSTRING:
|
|
|
|
case V_ASN1_UTF8STRING:
|
|
|
|
case V_ASN1_SEQUENCE:
|
|
|
|
case V_ASN1_SET:
|
|
|
|
default:
|
|
|
|
/* All based on ASN1_STRING and handled the same */
|
|
|
|
strtmp = (ASN1_STRING *)*pval;
|
2002-10-03 12:38:52 +00:00
|
|
|
/* Special handling for NDEF */
|
|
|
|
if ((it->size == ASN1_TFLG_NDEF)
|
|
|
|
&& (strtmp->flags & ASN1_STRING_FLAG_NDEF))
|
|
|
|
{
|
|
|
|
if (cout)
|
|
|
|
{
|
|
|
|
strtmp->data = cout;
|
|
|
|
strtmp->length = 0;
|
|
|
|
}
|
|
|
|
/* Special return code */
|
|
|
|
return -2;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
cont = strtmp->data;
|
|
|
|
len = strtmp->length;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (cout && len)
|
|
|
|
memcpy(cout, cont, len);
|
2000-12-08 19:09:35 +00:00
|
|
|
return len;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|