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