⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tasn_enc.c

📁 OpenSSL 0.9.8k 最新版OpenSSL
💻 C
📖 第 1 页 / 共 2 页
字号:
			skaclass = V_ASN1_UNIVERSAL;			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++)			{			skitem = sk_ASN1_VALUE_value(sk, i);			skcontlen += ASN1_item_ex_i2d(&skitem, NULL,						ASN1_ITEM_ptr(tt->item),							-1, iclass);			}		sklen = ASN1_object_size(ndef, skcontlen, sktag);		/* If EXPLICIT need length of surrounding tag */		if (flags & ASN1_TFLG_EXPTAG)			ret = ASN1_object_size(ndef, sklen, ttag);		else ret = sklen;		if (!out)			return ret;		/* Now encode this lot... */		/* EXPLICIT tag */		if (flags & ASN1_TFLG_EXPTAG)			ASN1_put_object(out, ndef, sklen, ttag, tclass);		/* SET or SEQUENCE and IMPLICIT tag */		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);		if (ndef == 2)			{			ASN1_put_eoc(out);			if (flags & ASN1_TFLG_EXPTAG)				ASN1_put_eoc(out);			}		return ret;		}	if (flags & ASN1_TFLG_EXPTAG)		{		/* EXPLICIT tagging */		/* Find length of tagged item */		i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item),								-1, iclass);		if (!i)			return 0;		/* Find length of EXPLICIT tag */		ret = ASN1_object_size(ndef, i, ttag);		if (out)			{			/* Output tag and item */			ASN1_put_object(out, ndef, i, ttag, tclass);			ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),								-1, iclass);			if (ndef == 2)				ASN1_put_eoc(out);			}		return ret;		}	/* Either normal or IMPLICIT tagging: combine class and flags */	return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),						ttag, tclass | iclass);}/* Temporary structure used to hold DER encoding of items for SET OF */typedef	struct {	unsigned char *data;	int length;	ASN1_VALUE *field;} DER_ENC;static int der_cmp(const void *a, const void *b)	{	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);	if (i)		return i;	return d1->length - d2->length;	}/* Output the content octets of SET OF or SEQUENCE OF */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)	{	int i;	ASN1_VALUE *skitem;	unsigned char *tmpdat = NULL, *p = NULL;	DER_ENC *derlst = NULL, *tder;	if (do_sort)		 {		/* Don't need to sort less than 2 items */		if (sk_ASN1_VALUE_num(sk) < 2)			do_sort = 0;		else			{			derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)						* sizeof(*derlst));			tmpdat = OPENSSL_malloc(skcontlen);			if (!derlst || !tmpdat)				return 0;			}		}	/* If not sorting just output each item */	if (!do_sort)		{		for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)			{			skitem = sk_ASN1_VALUE_value(sk, i);			ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);			}		return 1;		}	p = tmpdat;	/* Doing sort: build up a list of each member's DER encoding */	for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)		{		skitem = sk_ASN1_VALUE_value(sk, i);		tder->data = p;		tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);		tder->field = skitem;		}	/* Now sort them */	qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);	/* Output sorted DER encoding */		p = *out;	for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)		{		memcpy(p, tder->data, tder->length);		p += tder->length;		}	*out = p;	/* If do_sort is 2 then reorder the STACK */	if (do_sort == 2)		{		for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk);							i++, tder++)			(void)sk_ASN1_VALUE_set(sk, i, tder->field);		}	OPENSSL_free(derlst);	OPENSSL_free(tmpdat);	return 1;	}static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,				const ASN1_ITEM *it, int tag, int aclass)	{	int len;	int utype;	int usetag;	int ndef = 0;	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.	 */	if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||	   (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 */	if (tag == -1) tag = utype;	/* Output tag+length followed by content octets */	if (out)		{		if (usetag)			ASN1_put_object(out, ndef, len, tag, aclass);		asn1_ex_i2c(pval, *out, &utype, it);		if (ndef)			ASN1_put_eoc(out);		else			*out += len;		}	if (usetag)		return ASN1_object_size(ndef, len, tag);	return len;	}/* Produce content octets from a structure */int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,				const ASN1_ITEM *it)	{	ASN1_BOOLEAN *tbool = NULL;	ASN1_STRING *strtmp;	ASN1_OBJECT *otmp;	int utype;	unsigned char *cont, c;	int len;	const ASN1_PRIMITIVE_FUNCS *pf;	pf = it->funcs;	if (pf && pf->prim_i2c)		return pf->prim_i2c(pval, cout, putype, it);	/* Should type be omitted? */	if ((it->itype != ASN1_ITYPE_PRIMITIVE)		|| (it->utype != V_ASN1_BOOLEAN))		{		if (!*pval) return -1;		}	if (it->itype == ASN1_ITYPE_MSTRING)		{		/* If MSTRING type set the underlying type */		strtmp = (ASN1_STRING *)*pval;		utype = strtmp->type;		*putype = utype;		}	else if (it->utype == V_ASN1_ANY)		{		/* If ANY set type and pointer to value */		ASN1_TYPE *typ;		typ = (ASN1_TYPE *)*pval;		utype = typ->type;		*putype = utype;		pval = &typ->value.asn1_value;		}	else utype = *putype;	switch(utype)		{		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;		if (*tbool == -1)			return -1;		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;			}		c = (unsigned char)*tbool;		cont = &c;		len = 1;		break;		case V_ASN1_BIT_STRING:		return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,							cout ? &cout : NULL);		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		 */		return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval,							cout ? &cout : NULL);		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;		/* 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;			}		cont = strtmp->data;		len = strtmp->length;		break;		}	if (cout && len)		memcpy(cout, cont, len);	return len;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -