tasn_enc.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/* tasn_enc.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
* project 2000.
*/
/* ====================================================================
* Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
*
* 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
* 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>
#include <string.h>
#include "cryptlib.h"
const ASN1_TEMPLATE *tt,
/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
* to use indefinite length constructed encoding, where appropriate
*/
{
}
{
}
/* Encode an ASN1 item, this is use by the
* standard 'i2d' function. 'out' points to
* a buffer to output the data to.
*
* 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.
*/
{
{
unsigned char *p, *buf;
int len;
if (len <= 0)
return len;
if (!buf)
return -1;
p = buf;
return len;
}
}
/* Encode an item, taking care of IMPLICIT tagging (if any).
* This function performs the normal item handling: it can be
* used in external types.
*/
{
unsigned char *p = NULL;
const ASN1_COMPAT_FUNCS *cf;
const ASN1_EXTERN_FUNCS *ef;
ASN1_aux_cb *asn1_cb = 0;
return 0;
{
case ASN1_ITYPE_PRIMITIVE:
break;
case ASN1_ITYPE_MSTRING:
case ASN1_ITYPE_CHOICE:
return 0;
{
ASN1_VALUE **pchval;
const ASN1_TEMPLATE *chtt;
-1, aclass);
}
/* Fixme: error condition if selector out of range */
return 0;
break;
case ASN1_ITYPE_EXTERN:
/* If new style i2d it does all the work */
case ASN1_ITYPE_COMPAT:
/* old style hackery... */
if (out)
p = *out;
/* Fixup for IMPLICIT tag: note this messes up for tags > 30,
* but so did the old code. Tags > 30 are very rare anyway.
*/
return i;
case ASN1_ITYPE_NDEF_SEQUENCE:
/* Use indefinite length constructed if requested */
/* fall through */
case ASN1_ITYPE_SEQUENCE:
/* An error occurred */
if (i < 0)
return 0;
/* We have a valid cached encoding... */
if (i > 0)
return seqcontlen;
/* Otherwise carry on */
seqcontlen = 0;
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
if (tag == -1)
{
/* Retain any other flags in aclass */
}
return 0;
/* First work out sequence content length */
{
const ASN1_TEMPLATE *seqtt;
if (!seqtt)
return 0;
/* FIXME: check for errors in enhanced version */
-1, aclass);
}
if (!out)
return seqlen;
/* Output SEQUENCE header */
{
const ASN1_TEMPLATE *seqtt;
if (!seqtt)
return 0;
/* FIXME: check for errors in enhanced version */
}
if (ndef == 2)
return 0;
return seqlen;
default:
return 0;
}
return 0;
}
const ASN1_TEMPLATE *tt)
{
}
{
/* 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 */
}
else if (tag != -1)
{
/* No template tagging, get from arguments */
}
else
{
ttag = -1;
tclass = 0;
}
/*
* Remove any class mask from iflag.
*/
/* 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 */
ndef = 2;
else ndef = 1;
if (flags & ASN1_TFLG_SK_MASK)
{
/* SET OF, SEQUENCE OF */
if (!*pval)
return 0;
if (flags & ASN1_TFLG_SET_OF)
{
isset = 1;
/* 2 means we reorder */
if (flags & ASN1_TFLG_SEQUENCE_OF)
isset = 2;
}
else isset = 0;
/* Work out inner tag value: if EXPLICIT
* or no tagging use underlying type.
*/
{
}
else
{
if (isset)
sktag = V_ASN1_SET;
else sktag = V_ASN1_SEQUENCE;
}
/* Determine total length of items */
skcontlen = 0;
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
{
-1, iclass);
}
/* If EXPLICIT need length of surrounding tag */
if (flags & ASN1_TFLG_EXPTAG)
if (!out)
return ret;
/* Now encode this lot... */
/* EXPLICIT tag */
if (flags & ASN1_TFLG_EXPTAG)
/* SET or SEQUENCE and IMPLICIT tag */
/* And the stuff itself */
if (ndef == 2)
{
if (flags & ASN1_TFLG_EXPTAG)
}
return ret;
}
if (flags & ASN1_TFLG_EXPTAG)
{
/* EXPLICIT tagging */
/* Find length of tagged item */
-1, iclass);
if (!i)
return 0;
/* Find length of EXPLICIT tag */
if (out)
{
/* Output tag and item */
-1, iclass);
if (ndef == 2)
}
return ret;
}
/* Either normal or IMPLICIT tagging: combine class and flags */
}
/* Temporary structure used to hold DER encoding of items for SET OF */
typedef struct {
unsigned char *data;
int length;
} DER_ENC;
static int der_cmp(const void *a, const void *b)
{
int cmplen, i;
if (i)
return i;
}
/* Output the content octets of SET OF or SEQUENCE OF */
{
int i;
if (do_sort)
{
/* Don't need to sort less than 2 items */
do_sort = 0;
else
{
* sizeof(*derlst));
return 0;
}
}
/* If not sorting just output each item */
if (!do_sort)
{
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
{
}
return 1;
}
p = tmpdat;
/* Doing sort: build up a list of each member's DER encoding */
{
}
/* Now sort them */
/* Output sorted DER encoding */
p = *out;
{
}
*out = p;
/* If do_sort is 2 then reorder the STACK */
if (do_sort == 2)
{
i++, tder++)
}
return 1;
}
{
int len;
int utype;
int usetag;
int ndef = 0;
/* Get length of content octets and maybe find
* out the underlying type.
*/
/* 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.
*/
(utype == V_ASN1_OTHER))
usetag = 0;
else usetag = 1;
/* -1 means omit type */
if (len == -1)
return 0;
/* -2 return is special meaning use ndef */
if (len == -2)
{
ndef = 2;
len = 0;
}
/* If not implicitly tagged get tag from underlying type */
/* Output tag+length followed by content octets */
if (out)
{
if (usetag)
if (ndef)
else
}
if (usetag)
return len;
}
/* Produce content octets from a structure */
{
int utype;
unsigned char *cont, c;
int len;
const ASN1_PRIMITIVE_FUNCS *pf;
/* Should type be omitted? */
{
if (!*pval) return -1;
}
{
/* If MSTRING type set the underlying type */
}
{
/* If ANY set type and pointer to value */
}
switch(utype)
{
case V_ASN1_OBJECT:
break;
case V_ASN1_NULL:
len = 0;
break;
case V_ASN1_BOOLEAN:
if (*tbool == -1)
return -1;
/* Default handling if value == size field then omit */
return -1;
return -1;
c = (unsigned char)*tbool;
cont = &c;
len = 1;
break;
case V_ASN1_BIT_STRING:
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
*/
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 */
/* Special handling for NDEF */
{
if (cout)
{
}
/* Special return code */
return -2;
}
break;
}
return len;
}