1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to.  The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 *    notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 *    notice, this list of conditions and the following disclaimer in the
29 *    documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 *    must display the following acknowledgement:
32 *    "This product includes cryptographic software written by
33 *     Eric Young (eay@cryptsoft.com)"
34 *    The word 'cryptographic' can be left out if the rouines from the library
35 *    being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 *    the apps directory (application code) you must include an acknowledgement:
38 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/asn1.h>
58
59#include <openssl/asn1t.h>
60#include <openssl/mem.h>
61
62
63static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
64					const ASN1_ITEM *it,
65					int tag, int aclass);
66static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
67					int skcontlen, const ASN1_ITEM *item,
68					int do_sort, int iclass);
69static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
70					const ASN1_TEMPLATE *tt,
71					int tag, int aclass);
72static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
73					const ASN1_ITEM *it, int flags);
74
75/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
76 * to use indefinite length constructed encoding, where appropriate
77 */
78
79int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
80						const ASN1_ITEM *it)
81	{
82	return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
83	}
84
85int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
86	{
87	return asn1_item_flags_i2d(val, out, it, 0);
88	}
89
90/* Encode an ASN1 item, this is use by the
91 * standard 'i2d' function. 'out' points to
92 * a buffer to output the data to.
93 *
94 * The new i2d has one additional feature. If the output
95 * buffer is NULL (i.e. *out == NULL) then a buffer is
96 * allocated and populated with the encoding.
97 */
98
99static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
100					const ASN1_ITEM *it, int flags)
101	{
102	if (out && !*out)
103		{
104		unsigned char *p, *buf;
105		int len;
106		len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
107		if (len <= 0)
108			return len;
109		buf = OPENSSL_malloc(len);
110		if (!buf)
111			return -1;
112		p = buf;
113		ASN1_item_ex_i2d(&val, &p, it, -1, flags);
114		*out = buf;
115		return len;
116		}
117
118	return ASN1_item_ex_i2d(&val, out, it, -1, flags);
119	}
120
121/* Encode an item, taking care of IMPLICIT tagging (if any).
122 * This function performs the normal item handling: it can be
123 * used in external types.
124 */
125
126int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
127			const ASN1_ITEM *it, int tag, int aclass)
128	{
129	const ASN1_TEMPLATE *tt = NULL;
130	unsigned char *p = NULL;
131	int i, seqcontlen, seqlen, ndef = 1;
132	const ASN1_COMPAT_FUNCS *cf;
133	const ASN1_EXTERN_FUNCS *ef;
134	const ASN1_AUX *aux = it->funcs;
135	ASN1_aux_cb *asn1_cb = 0;
136
137	if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
138		return 0;
139
140	if (aux && aux->asn1_cb)
141		 asn1_cb = aux->asn1_cb;
142
143	switch(it->itype)
144		{
145
146		case ASN1_ITYPE_PRIMITIVE:
147		if (it->templates)
148			return asn1_template_ex_i2d(pval, out, it->templates,
149								tag, aclass);
150		return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
151		break;
152
153		case ASN1_ITYPE_MSTRING:
154		return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
155
156		case ASN1_ITYPE_CHOICE:
157		if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
158				return 0;
159		i = asn1_get_choice_selector(pval, it);
160		if ((i >= 0) && (i < it->tcount))
161			{
162			ASN1_VALUE **pchval;
163			const ASN1_TEMPLATE *chtt;
164			chtt = it->templates + i;
165			pchval = asn1_get_field_ptr(pval, chtt);
166			return asn1_template_ex_i2d(pchval, out, chtt,
167								-1, aclass);
168			}
169		/* Fixme: error condition if selector out of range */
170		if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
171				return 0;
172		break;
173
174		case ASN1_ITYPE_EXTERN:
175		/* If new style i2d it does all the work */
176		ef = it->funcs;
177		return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
178
179		case ASN1_ITYPE_COMPAT:
180		/* old style hackery... */
181		cf = it->funcs;
182		if (out)
183			p = *out;
184		i = cf->asn1_i2d(*pval, out);
185		/* Fixup for IMPLICIT tag: note this messes up for tags > 30,
186		 * but so did the old code. Tags > 30 are very rare anyway.
187		 */
188		if (out && (tag != -1))
189			*p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
190		return i;
191
192		case ASN1_ITYPE_NDEF_SEQUENCE:
193		/* Use indefinite length constructed if requested */
194		if (aclass & ASN1_TFLG_NDEF) ndef = 2;
195		/* fall through */
196
197		case ASN1_ITYPE_SEQUENCE:
198		i = asn1_enc_restore(&seqcontlen, out, pval, it);
199		/* An error occurred */
200		if (i < 0)
201			return 0;
202		/* We have a valid cached encoding... */
203		if (i > 0)
204			return seqcontlen;
205		/* Otherwise carry on */
206		seqcontlen = 0;
207		/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
208		if (tag == -1)
209			{
210			tag = V_ASN1_SEQUENCE;
211			/* Retain any other flags in aclass */
212			aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
213					| V_ASN1_UNIVERSAL;
214			}
215		if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
216				return 0;
217		/* First work out sequence content length */
218		for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
219			{
220			const ASN1_TEMPLATE *seqtt;
221			ASN1_VALUE **pseqval;
222			seqtt = asn1_do_adb(pval, tt, 1);
223			if (!seqtt)
224				return 0;
225			pseqval = asn1_get_field_ptr(pval, seqtt);
226			/* FIXME: check for errors in enhanced version */
227			seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
228								-1, aclass);
229			}
230
231		seqlen = ASN1_object_size(ndef, seqcontlen, tag);
232		if (!out)
233			return seqlen;
234		/* Output SEQUENCE header */
235		ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
236		for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
237			{
238			const ASN1_TEMPLATE *seqtt;
239			ASN1_VALUE **pseqval;
240			seqtt = asn1_do_adb(pval, tt, 1);
241			if (!seqtt)
242				return 0;
243			pseqval = asn1_get_field_ptr(pval, seqtt);
244			/* FIXME: check for errors in enhanced version */
245			asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
246			}
247		if (ndef == 2)
248			ASN1_put_eoc(out);
249		if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
250				return 0;
251		return seqlen;
252
253		default:
254		return 0;
255
256		}
257	return 0;
258	}
259
260int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
261							const ASN1_TEMPLATE *tt)
262	{
263	return asn1_template_ex_i2d(pval, out, tt, -1, 0);
264	}
265
266static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
267				const ASN1_TEMPLATE *tt, int tag, int iclass)
268	{
269	int i, ret, flags, ttag, tclass, ndef;
270	size_t j;
271	flags = tt->flags;
272	/* Work out tag and class to use: tagging may come
273	 * either from the template or the arguments, not both
274	 * because this would create ambiguity. Additionally
275	 * the iclass argument may contain some additional flags
276	 * which should be noted and passed down to other levels.
277	 */
278	if (flags & ASN1_TFLG_TAG_MASK)
279		{
280		/* Error if argument and template tagging */
281		if (tag != -1)
282			/* FIXME: error code here */
283			return -1;
284		/* Get tagging from template */
285		ttag = tt->tag;
286		tclass = flags & ASN1_TFLG_TAG_CLASS;
287		}
288	else if (tag != -1)
289		{
290		/* No template tagging, get from arguments */
291		ttag = tag;
292		tclass = iclass & ASN1_TFLG_TAG_CLASS;
293		}
294	else
295		{
296		ttag = -1;
297		tclass = 0;
298		}
299	/*
300	 * Remove any class mask from iflag.
301	 */
302	iclass &= ~ASN1_TFLG_TAG_CLASS;
303
304	/* At this point 'ttag' contains the outer tag to use,
305	 * 'tclass' is the class and iclass is any flags passed
306	 * to this function.
307	 */
308
309	/* if template and arguments require ndef, use it */
310	if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
311		ndef = 2;
312	else ndef = 1;
313
314	if (flags & ASN1_TFLG_SK_MASK)
315		{
316		/* SET OF, SEQUENCE OF */
317		STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
318		int isset, sktag, skaclass;
319		int skcontlen, sklen;
320		ASN1_VALUE *skitem;
321
322		if (!*pval)
323			return 0;
324
325		if (flags & ASN1_TFLG_SET_OF)
326			{
327			isset = 1;
328			/* 2 means we reorder */
329			if (flags & ASN1_TFLG_SEQUENCE_OF)
330				isset = 2;
331			}
332		else isset = 0;
333
334		/* Work out inner tag value: if EXPLICIT
335		 * or no tagging use underlying type.
336		 */
337		if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG))
338			{
339			sktag = ttag;
340			skaclass = tclass;
341			}
342		else
343			{
344			skaclass = V_ASN1_UNIVERSAL;
345			if (isset)
346				sktag = V_ASN1_SET;
347			else sktag = V_ASN1_SEQUENCE;
348			}
349
350		/* Determine total length of items */
351		skcontlen = 0;
352		for (j = 0; j < sk_ASN1_VALUE_num(sk); j++)
353			{
354			skitem = sk_ASN1_VALUE_value(sk, j);
355			skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
356						ASN1_ITEM_ptr(tt->item),
357							-1, iclass);
358			}
359		sklen = ASN1_object_size(ndef, skcontlen, sktag);
360		/* If EXPLICIT need length of surrounding tag */
361		if (flags & ASN1_TFLG_EXPTAG)
362			ret = ASN1_object_size(ndef, sklen, ttag);
363		else ret = sklen;
364
365		if (!out)
366			return ret;
367
368		/* Now encode this lot... */
369		/* EXPLICIT tag */
370		if (flags & ASN1_TFLG_EXPTAG)
371			ASN1_put_object(out, ndef, sklen, ttag, tclass);
372		/* SET or SEQUENCE and IMPLICIT tag */
373		ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
374		/* And the stuff itself */
375		asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
376								isset, iclass);
377		if (ndef == 2)
378			{
379			ASN1_put_eoc(out);
380			if (flags & ASN1_TFLG_EXPTAG)
381				ASN1_put_eoc(out);
382			}
383
384		return ret;
385		}
386
387	if (flags & ASN1_TFLG_EXPTAG)
388		{
389		/* EXPLICIT tagging */
390		/* Find length of tagged item */
391		i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item),
392								-1, iclass);
393		if (!i)
394			return 0;
395		/* Find length of EXPLICIT tag */
396		ret = ASN1_object_size(ndef, i, ttag);
397		if (out)
398			{
399			/* Output tag and item */
400			ASN1_put_object(out, ndef, i, ttag, tclass);
401			ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
402								-1, iclass);
403			if (ndef == 2)
404				ASN1_put_eoc(out);
405			}
406		return ret;
407		}
408
409	/* Either normal or IMPLICIT tagging: combine class and flags */
410	return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
411						ttag, tclass | iclass);
412
413}
414
415/* Temporary structure used to hold DER encoding of items for SET OF */
416
417typedef	struct {
418	unsigned char *data;
419	int length;
420	ASN1_VALUE *field;
421} DER_ENC;
422
423static int der_cmp(const void *a, const void *b)
424	{
425	const DER_ENC *d1 = a, *d2 = b;
426	int cmplen, i;
427	cmplen = (d1->length < d2->length) ? d1->length : d2->length;
428	i = memcmp(d1->data, d2->data, cmplen);
429	if (i)
430		return i;
431	return d1->length - d2->length;
432	}
433
434/* Output the content octets of SET OF or SEQUENCE OF */
435
436static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
437					int skcontlen, const ASN1_ITEM *item,
438					int do_sort, int iclass)
439	{
440	size_t i;
441	ASN1_VALUE *skitem;
442	unsigned char *tmpdat = NULL, *p = NULL;
443	DER_ENC *derlst = NULL, *tder;
444	if (do_sort)
445		 {
446		/* Don't need to sort less than 2 items */
447		if (sk_ASN1_VALUE_num(sk) < 2)
448			do_sort = 0;
449		else
450			{
451			derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
452						* sizeof(*derlst));
453			if (!derlst)
454				return 0;
455			tmpdat = OPENSSL_malloc(skcontlen);
456			if (!tmpdat)
457				{
458				OPENSSL_free(derlst);
459				return 0;
460				}
461			}
462		}
463	/* If not sorting just output each item */
464	if (!do_sort)
465		{
466		for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
467			{
468			skitem = sk_ASN1_VALUE_value(sk, i);
469			ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
470			}
471		return 1;
472		}
473	p = tmpdat;
474
475	/* Doing sort: build up a list of each member's DER encoding */
476	for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
477		{
478		skitem = sk_ASN1_VALUE_value(sk, i);
479		tder->data = p;
480		tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
481		tder->field = skitem;
482		}
483
484	/* Now sort them */
485	qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
486	/* Output sorted DER encoding */
487	p = *out;
488	for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
489		{
490		memcpy(p, tder->data, tder->length);
491		p += tder->length;
492		}
493	*out = p;
494	/* If do_sort is 2 then reorder the STACK */
495	if (do_sort == 2)
496		{
497		for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk);
498							i++, tder++)
499			(void)sk_ASN1_VALUE_set(sk, i, tder->field);
500		}
501	OPENSSL_free(derlst);
502	OPENSSL_free(tmpdat);
503	return 1;
504	}
505
506static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
507				const ASN1_ITEM *it, int tag, int aclass)
508	{
509	int len;
510	int utype;
511	int usetag;
512	int ndef = 0;
513
514	utype = it->utype;
515
516	/* Get length of content octets and maybe find
517	 * out the underlying type.
518	 */
519
520	len = asn1_ex_i2c(pval, NULL, &utype, it);
521
522	/* If SEQUENCE, SET or OTHER then header is
523	 * included in pseudo content octets so don't
524	 * include tag+length. We need to check here
525	 * because the call to asn1_ex_i2c() could change
526	 * utype.
527	 */
528	if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
529	   (utype == V_ASN1_OTHER))
530		usetag = 0;
531	else usetag = 1;
532
533	/* -1 means omit type */
534
535	if (len == -1)
536		return 0;
537
538	/* -2 return is special meaning use ndef */
539	if (len == -2)
540		{
541		ndef = 2;
542		len = 0;
543		}
544
545	/* If not implicitly tagged get tag from underlying type */
546	if (tag == -1) tag = utype;
547
548	/* Output tag+length followed by content octets */
549	if (out)
550		{
551		if (usetag)
552			ASN1_put_object(out, ndef, len, tag, aclass);
553		asn1_ex_i2c(pval, *out, &utype, it);
554		if (ndef)
555			ASN1_put_eoc(out);
556		else
557			*out += len;
558		}
559
560	if (usetag)
561		return ASN1_object_size(ndef, len, tag);
562	return len;
563	}
564
565/* Produce content octets from a structure */
566
567int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
568				const ASN1_ITEM *it)
569	{
570	ASN1_BOOLEAN *tbool = NULL;
571	ASN1_STRING *strtmp;
572	ASN1_OBJECT *otmp;
573	int utype;
574	const unsigned char *cont;
575	unsigned char c;
576	int len;
577	const ASN1_PRIMITIVE_FUNCS *pf;
578	pf = it->funcs;
579	if (pf && pf->prim_i2c)
580		return pf->prim_i2c(pval, cout, putype, it);
581
582	/* Should type be omitted? */
583	if ((it->itype != ASN1_ITYPE_PRIMITIVE)
584		|| (it->utype != V_ASN1_BOOLEAN))
585		{
586		if (!*pval) return -1;
587		}
588
589	if (it->itype == ASN1_ITYPE_MSTRING)
590		{
591		/* If MSTRING type set the underlying type */
592		strtmp = (ASN1_STRING *)*pval;
593		utype = strtmp->type;
594		*putype = utype;
595		}
596	else if (it->utype == V_ASN1_ANY)
597		{
598		/* If ANY set type and pointer to value */
599		ASN1_TYPE *typ;
600		typ = (ASN1_TYPE *)*pval;
601		utype = typ->type;
602		*putype = utype;
603		pval = &typ->value.asn1_value;
604		}
605	else utype = *putype;
606
607	switch(utype)
608		{
609		case V_ASN1_OBJECT:
610		otmp = (ASN1_OBJECT *)*pval;
611		cont = otmp->data;
612		len = otmp->length;
613		break;
614
615		case V_ASN1_NULL:
616		cont = NULL;
617		len = 0;
618		break;
619
620		case V_ASN1_BOOLEAN:
621		tbool = (ASN1_BOOLEAN *)pval;
622		if (*tbool == -1)
623			return -1;
624		if (it->utype != V_ASN1_ANY)
625			{
626			/* Default handling if value == size field then omit */
627			if (*tbool && (it->size > 0))
628				return -1;
629			if (!*tbool && !it->size)
630				return -1;
631			}
632		c = (unsigned char)*tbool;
633		cont = &c;
634		len = 1;
635		break;
636
637		case V_ASN1_BIT_STRING:
638		return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
639							cout ? &cout : NULL);
640		break;
641
642		case V_ASN1_INTEGER:
643		case V_ASN1_NEG_INTEGER:
644		case V_ASN1_ENUMERATED:
645		case V_ASN1_NEG_ENUMERATED:
646		/* These are all have the same content format
647		 * as ASN1_INTEGER
648		 */
649		return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval,
650							cout ? &cout : NULL);
651		break;
652
653		case V_ASN1_OCTET_STRING:
654		case V_ASN1_NUMERICSTRING:
655		case V_ASN1_PRINTABLESTRING:
656		case V_ASN1_T61STRING:
657		case V_ASN1_VIDEOTEXSTRING:
658		case V_ASN1_IA5STRING:
659		case V_ASN1_UTCTIME:
660		case V_ASN1_GENERALIZEDTIME:
661		case V_ASN1_GRAPHICSTRING:
662		case V_ASN1_VISIBLESTRING:
663		case V_ASN1_GENERALSTRING:
664		case V_ASN1_UNIVERSALSTRING:
665		case V_ASN1_BMPSTRING:
666		case V_ASN1_UTF8STRING:
667		case V_ASN1_SEQUENCE:
668		case V_ASN1_SET:
669		default:
670		/* All based on ASN1_STRING and handled the same */
671		strtmp = (ASN1_STRING *)*pval;
672		/* Special handling for NDEF */
673		if ((it->size == ASN1_TFLG_NDEF)
674			&& (strtmp->flags & ASN1_STRING_FLAG_NDEF))
675			{
676			if (cout)
677				{
678				strtmp->data = cout;
679				strtmp->length = 0;
680				}
681			/* Special return code */
682			return -2;
683			}
684		cont = strtmp->data;
685		len = strtmp->length;
686
687		break;
688
689		}
690	if (cout && len)
691		memcpy(cout, cont, len);
692	return len;
693	}
694