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