Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/crypto/asn1/tasn_enc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <string.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/asn1.h>
14
#include <openssl/asn1t.h>
15
#include <openssl/objects.h>
16
#include "crypto/asn1.h"
17
#include "asn1_local.h"
18
19
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
20
                                 const ASN1_ITEM *it, int tag, int aclass);
21
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
22
                            int skcontlen, const ASN1_ITEM *item,
23
                            int do_sort, int iclass);
24
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
25
                                const ASN1_TEMPLATE *tt, int tag, int aclass);
26
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
27
                               const ASN1_ITEM *it, int flags);
28
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
29
                       const ASN1_ITEM *it);
30
31
/*
32
 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
33
 * indefinite length constructed encoding, where appropriate
34
 */
35
36
int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
37
                       const ASN1_ITEM *it)
38
0
{
39
0
    return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
40
0
}
41
42
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
43
575k
{
44
575k
    return asn1_item_flags_i2d(val, out, it, 0);
45
575k
}
46
47
/*
48
 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
49
 * points to a buffer to output the data to. The new i2d has one additional
50
 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
51
 * allocated and populated with the encoding.
52
 */
53
54
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
55
                               const ASN1_ITEM *it, int flags)
56
201k
{
57
201k
    if (out && !*out) {
58
169k
        unsigned char *p, *buf;
59
169k
        int len;
60
61
169k
        len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
62
169k
        if (len <= 0)
63
16
            return len;
64
169k
        if ((buf = OPENSSL_malloc(len)) == NULL) {
65
0
            ASN1err(ASN1_F_ASN1_ITEM_FLAGS_I2D, ERR_R_MALLOC_FAILURE);
66
0
            return -1;
67
0
        }
68
169k
        p = buf;
69
169k
        ASN1_item_ex_i2d(&val, &p, it, -1, flags);
70
169k
        *out = buf;
71
169k
        return len;
72
169k
    }
73
74
32.2k
    return ASN1_item_ex_i2d(&val, out, it, -1, flags);
75
201k
}
76
77
/*
78
 * Encode an item, taking care of IMPLICIT tagging (if any). This function
79
 * performs the normal item handling: it can be used in external types.
80
 */
81
82
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
83
                     const ASN1_ITEM *it, int tag, int aclass)
84
93.8M
{
85
93.8M
    const ASN1_TEMPLATE *tt = NULL;
86
93.8M
    int i, seqcontlen, seqlen, ndef = 1;
87
93.8M
    const ASN1_EXTERN_FUNCS *ef;
88
93.8M
    const ASN1_AUX *aux = it->funcs;
89
93.8M
    ASN1_aux_cb *asn1_cb = 0;
90
91
93.8M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
92
26.7k
        return 0;
93
94
93.8M
    if (aux && aux->asn1_cb)
95
810k
        asn1_cb = aux->asn1_cb;
96
97
93.8M
    switch (it->itype) {
98
99
45.7M
    case ASN1_ITYPE_PRIMITIVE:
100
45.7M
        if (it->templates)
101
1.89M
            return asn1_template_ex_i2d(pval, out, it->templates,
102
1.89M
                                        tag, aclass);
103
43.8M
        return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
104
105
23.8M
    case ASN1_ITYPE_MSTRING:
106
        /*
107
         * It never makes sense for multi-strings to have implicit tagging, so
108
         * if tag != -1, then this looks like an error in the template.
109
         */
110
23.8M
        if (tag != -1) {
111
0
            ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
112
0
            return -1;
113
0
        }
114
23.8M
        return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
115
116
4.85M
    case ASN1_ITYPE_CHOICE:
117
        /*
118
         * It never makes sense for CHOICE types to have implicit tagging, so
119
         * if tag != -1, then this looks like an error in the template.
120
         */
121
4.85M
        if (tag != -1) {
122
0
            ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE);
123
0
            return -1;
124
0
        }
125
4.85M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
126
0
            return 0;
127
4.85M
        i = asn1_get_choice_selector(pval, it);
128
4.85M
        if ((i >= 0) && (i < it->tcount)) {
129
4.85M
            ASN1_VALUE **pchval;
130
4.85M
            const ASN1_TEMPLATE *chtt;
131
4.85M
            chtt = it->templates + i;
132
4.85M
            pchval = asn1_get_field_ptr(pval, chtt);
133
4.85M
            return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
134
4.85M
        }
135
        /* Fixme: error condition if selector out of range */
136
0
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
137
0
            return 0;
138
0
        break;
139
140
38.7k
    case ASN1_ITYPE_EXTERN:
141
        /* If new style i2d it does all the work */
142
38.7k
        ef = it->funcs;
143
38.7k
        return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
144
145
12.8k
    case ASN1_ITYPE_NDEF_SEQUENCE:
146
        /* Use indefinite length constructed if requested */
147
12.8k
        if (aclass & ASN1_TFLG_NDEF)
148
0
            ndef = 2;
149
        /* fall through */
150
151
19.2M
    case ASN1_ITYPE_SEQUENCE:
152
19.2M
        i = asn1_enc_restore(&seqcontlen, out, pval, it);
153
        /* An error occurred */
154
19.2M
        if (i < 0)
155
0
            return 0;
156
        /* We have a valid cached encoding... */
157
19.2M
        if (i > 0)
158
259k
            return seqcontlen;
159
        /* Otherwise carry on */
160
19.0M
        seqcontlen = 0;
161
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
162
19.0M
        if (tag == -1) {
163
18.9M
            tag = V_ASN1_SEQUENCE;
164
            /* Retain any other flags in aclass */
165
18.9M
            aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
166
18.9M
                | V_ASN1_UNIVERSAL;
167
18.9M
        }
168
19.0M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
169
0
            return 0;
170
        /* First work out sequence content length */
171
57.5M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
172
38.5M
            const ASN1_TEMPLATE *seqtt;
173
38.5M
            ASN1_VALUE **pseqval;
174
38.5M
            int tmplen;
175
38.5M
            seqtt = asn1_do_adb(pval, tt, 1);
176
38.5M
            if (!seqtt)
177
0
                return 0;
178
38.5M
            pseqval = asn1_get_field_ptr(pval, seqtt);
179
38.5M
            tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
180
38.5M
            if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
181
0
                return -1;
182
38.5M
            seqcontlen += tmplen;
183
38.5M
        }
184
185
19.0M
        seqlen = ASN1_object_size(ndef, seqcontlen, tag);
186
19.0M
        if (!out || seqlen == -1)
187
13.1M
            return seqlen;
188
        /* Output SEQUENCE header */
189
5.90M
        ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
190
17.9M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
191
12.0M
            const ASN1_TEMPLATE *seqtt;
192
12.0M
            ASN1_VALUE **pseqval;
193
12.0M
            seqtt = asn1_do_adb(pval, tt, 1);
194
12.0M
            if (!seqtt)
195
0
                return 0;
196
12.0M
            pseqval = asn1_get_field_ptr(pval, seqtt);
197
            /* FIXME: check for errors in enhanced version */
198
12.0M
            asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
199
12.0M
        }
200
5.90M
        if (ndef == 2)
201
0
            ASN1_put_eoc(out);
202
5.90M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
203
0
            return 0;
204
5.90M
        return seqlen;
205
206
0
    default:
207
0
        return 0;
208
209
93.8M
    }
210
0
    return 0;
211
93.8M
}
212
213
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
214
                                const ASN1_TEMPLATE *tt, int tag, int iclass)
215
57.3M
{
216
57.3M
    int i, ret, flags, ttag, tclass, ndef;
217
57.3M
    ASN1_VALUE *tval;
218
57.3M
    flags = tt->flags;
219
220
    /*
221
     * If field is embedded then val needs fixing so it is a pointer to
222
     * a pointer to a field.
223
     */
224
57.3M
    if (flags & ASN1_TFLG_EMBED) {
225
958k
        tval = (ASN1_VALUE *)pval;
226
958k
        pval = &tval;
227
958k
    }
228
    /*
229
     * Work out tag and class to use: tagging may come either from the
230
     * template or the arguments, not both because this would create
231
     * ambiguity. Additionally the iclass argument may contain some
232
     * additional flags which should be noted and passed down to other
233
     * levels.
234
     */
235
57.3M
    if (flags & ASN1_TFLG_TAG_MASK) {
236
        /* Error if argument and template tagging */
237
4.98M
        if (tag != -1)
238
            /* FIXME: error code here */
239
0
            return -1;
240
        /* Get tagging from template */
241
4.98M
        ttag = tt->tag;
242
4.98M
        tclass = flags & ASN1_TFLG_TAG_CLASS;
243
52.3M
    } else if (tag != -1) {
244
        /* No template tagging, get from arguments */
245
0
        ttag = tag;
246
0
        tclass = iclass & ASN1_TFLG_TAG_CLASS;
247
52.3M
    } else {
248
52.3M
        ttag = -1;
249
52.3M
        tclass = 0;
250
52.3M
    }
251
    /*
252
     * Remove any class mask from iflag.
253
     */
254
57.3M
    iclass &= ~ASN1_TFLG_TAG_CLASS;
255
256
    /*
257
     * At this point 'ttag' contains the outer tag to use, 'tclass' is the
258
     * class and iclass is any flags passed to this function.
259
     */
260
261
    /* if template and arguments require ndef, use it */
262
57.3M
    if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
263
0
        ndef = 2;
264
57.3M
    else
265
57.3M
        ndef = 1;
266
267
57.3M
    if (flags & ASN1_TFLG_SK_MASK) {
268
        /* SET OF, SEQUENCE OF */
269
1.98M
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
270
1.98M
        int isset, sktag, skaclass;
271
1.98M
        int skcontlen, sklen;
272
1.98M
        ASN1_VALUE *skitem;
273
274
1.98M
        if (!*pval)
275
39.7k
            return 0;
276
277
1.94M
        if (flags & ASN1_TFLG_SET_OF) {
278
1.89M
            isset = 1;
279
            /* 2 means we reorder */
280
1.89M
            if (flags & ASN1_TFLG_SEQUENCE_OF)
281
224
                isset = 2;
282
1.89M
        } else
283
54.5k
            isset = 0;
284
285
        /*
286
         * Work out inner tag value: if EXPLICIT or no tagging use underlying
287
         * type.
288
         */
289
1.94M
        if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
290
11.2k
            sktag = ttag;
291
11.2k
            skaclass = tclass;
292
1.93M
        } else {
293
1.93M
            skaclass = V_ASN1_UNIVERSAL;
294
1.93M
            if (isset)
295
1.88M
                sktag = V_ASN1_SET;
296
49.3k
            else
297
49.3k
                sktag = V_ASN1_SEQUENCE;
298
1.93M
        }
299
300
        /* Determine total length of items */
301
1.94M
        skcontlen = 0;
302
29.2M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
303
27.3M
            int tmplen;
304
27.3M
            skitem = sk_ASN1_VALUE_value(sk, i);
305
27.3M
            tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
306
27.3M
                                      -1, iclass);
307
27.3M
            if (tmplen == -1 || (skcontlen > INT_MAX - tmplen))
308
0
                return -1;
309
27.3M
            skcontlen += tmplen;
310
27.3M
        }
311
1.94M
        sklen = ASN1_object_size(ndef, skcontlen, sktag);
312
1.94M
        if (sklen == -1)
313
0
            return -1;
314
        /* If EXPLICIT need length of surrounding tag */
315
1.94M
        if (flags & ASN1_TFLG_EXPTAG)
316
33
            ret = ASN1_object_size(ndef, sklen, ttag);
317
1.94M
        else
318
1.94M
            ret = sklen;
319
320
1.94M
        if (!out || ret == -1)
321
1.07M
            return ret;
322
323
        /* Now encode this lot... */
324
        /* EXPLICIT tag */
325
870k
        if (flags & ASN1_TFLG_EXPTAG)
326
11
            ASN1_put_object(out, ndef, sklen, ttag, tclass);
327
        /* SET or SEQUENCE and IMPLICIT tag */
328
870k
        ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
329
        /* And the stuff itself */
330
870k
        asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
331
870k
                         isset, iclass);
332
870k
        if (ndef == 2) {
333
0
            ASN1_put_eoc(out);
334
0
            if (flags & ASN1_TFLG_EXPTAG)
335
0
                ASN1_put_eoc(out);
336
0
        }
337
338
870k
        return ret;
339
1.94M
    }
340
341
55.3M
    if (flags & ASN1_TFLG_EXPTAG) {
342
        /* EXPLICIT tagging */
343
        /* Find length of tagged item */
344
81.2k
        i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
345
81.2k
        if (!i)
346
45.7k
            return 0;
347
        /* Find length of EXPLICIT tag */
348
35.4k
        ret = ASN1_object_size(ndef, i, ttag);
349
35.4k
        if (out && ret != -1) {
350
            /* Output tag and item */
351
8.40k
            ASN1_put_object(out, ndef, i, ttag, tclass);
352
8.40k
            ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
353
8.40k
            if (ndef == 2)
354
0
                ASN1_put_eoc(out);
355
8.40k
        }
356
35.4k
        return ret;
357
81.2k
    }
358
359
    /* Either normal or IMPLICIT tagging: combine class and flags */
360
55.2M
    return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
361
55.2M
                            ttag, tclass | iclass);
362
363
55.3M
}
364
365
/* Temporary structure used to hold DER encoding of items for SET OF */
366
367
typedef struct {
368
    unsigned char *data;
369
    int length;
370
    ASN1_VALUE *field;
371
} DER_ENC;
372
373
static int der_cmp(const void *a, const void *b)
374
188M
{
375
188M
    const DER_ENC *d1 = a, *d2 = b;
376
188M
    int cmplen, i;
377
188M
    cmplen = (d1->length < d2->length) ? d1->length : d2->length;
378
188M
    i = memcmp(d1->data, d2->data, cmplen);
379
188M
    if (i)
380
91.8M
        return i;
381
97.0M
    return d1->length - d2->length;
382
188M
}
383
384
/* Output the content octets of SET OF or SEQUENCE OF */
385
386
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
387
                            int skcontlen, const ASN1_ITEM *item,
388
                            int do_sort, int iclass)
389
870k
{
390
870k
    int i;
391
870k
    ASN1_VALUE *skitem;
392
870k
    unsigned char *tmpdat = NULL, *p = NULL;
393
870k
    DER_ENC *derlst = NULL, *tder;
394
870k
    if (do_sort) {
395
        /* Don't need to sort less than 2 items */
396
851k
        if (sk_ASN1_VALUE_num(sk) < 2)
397
833k
            do_sort = 0;
398
17.9k
        else {
399
17.9k
            derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
400
17.9k
                                    * sizeof(*derlst));
401
17.9k
            if (derlst == NULL)
402
0
                return 0;
403
17.9k
            tmpdat = OPENSSL_malloc(skcontlen);
404
17.9k
            if (tmpdat == NULL) {
405
0
                OPENSSL_free(derlst);
406
0
                return 0;
407
0
            }
408
17.9k
        }
409
851k
    }
410
    /* If not sorting just output each item */
411
870k
    if (!do_sort) {
412
3.25M
        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
413
2.40M
            skitem = sk_ASN1_VALUE_value(sk, i);
414
2.40M
            ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
415
2.40M
        }
416
852k
        return 1;
417
852k
    }
418
17.9k
    p = tmpdat;
419
420
    /* Doing sort: build up a list of each member's DER encoding */
421
7.05M
    for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
422
7.03M
        skitem = sk_ASN1_VALUE_value(sk, i);
423
7.03M
        tder->data = p;
424
7.03M
        tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
425
7.03M
        tder->field = skitem;
426
7.03M
    }
427
428
    /* Now sort them */
429
17.9k
    qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
430
    /* Output sorted DER encoding */
431
17.9k
    p = *out;
432
7.05M
    for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
433
7.03M
        memcpy(p, tder->data, tder->length);
434
7.03M
        p += tder->length;
435
7.03M
    }
436
17.9k
    *out = p;
437
    /* If do_sort is 2 then reorder the STACK */
438
17.9k
    if (do_sort == 2) {
439
652
        for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
440
622
            (void)sk_ASN1_VALUE_set(sk, i, tder->field);
441
30
    }
442
17.9k
    OPENSSL_free(derlst);
443
17.9k
    OPENSSL_free(tmpdat);
444
17.9k
    return 1;
445
870k
}
446
447
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
448
                                 const ASN1_ITEM *it, int tag, int aclass)
449
160M
{
450
160M
    int len;
451
160M
    int utype;
452
160M
    int usetag;
453
160M
    int ndef = 0;
454
455
160M
    utype = it->utype;
456
457
    /*
458
     * Get length of content octets and maybe find out the underlying type.
459
     */
460
461
160M
    len = asn1_ex_i2c(pval, NULL, &utype, it);
462
463
    /*
464
     * If SEQUENCE, SET or OTHER then header is included in pseudo content
465
     * octets so don't include tag+length. We need to check here because the
466
     * call to asn1_ex_i2c() could change utype.
467
     */
468
160M
    if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
469
160M
        (utype == V_ASN1_OTHER))
470
19.3M
        usetag = 0;
471
141M
    else
472
141M
        usetag = 1;
473
474
    /* -1 means omit type */
475
476
160M
    if (len == -1)
477
4.75M
        return 0;
478
479
    /* -2 return is special meaning use ndef */
480
156M
    if (len == -2) {
481
0
        ndef = 2;
482
0
        len = 0;
483
0
    }
484
485
    /* If not implicitly tagged get tag from underlying type */
486
156M
    if (tag == -1)
487
149M
        tag = utype;
488
489
    /* Output tag+length followed by content octets */
490
156M
    if (out) {
491
35.1M
        if (usetag)
492
30.1M
            ASN1_put_object(out, ndef, len, tag, aclass);
493
35.1M
        asn1_ex_i2c(pval, *out, &utype, it);
494
35.1M
        if (ndef)
495
0
            ASN1_put_eoc(out);
496
35.1M
        else
497
35.1M
            *out += len;
498
35.1M
    }
499
500
156M
    if (usetag)
501
136M
        return ASN1_object_size(ndef, len, tag);
502
19.3M
    return len;
503
156M
}
504
505
/* Produce content octets from a structure */
506
507
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
508
                       const ASN1_ITEM *it)
509
83.0M
{
510
83.0M
    ASN1_BOOLEAN *tbool = NULL;
511
83.0M
    ASN1_STRING *strtmp;
512
83.0M
    ASN1_OBJECT *otmp;
513
83.0M
    int utype;
514
83.0M
    const unsigned char *cont;
515
83.0M
    unsigned char c;
516
83.0M
    int len;
517
83.0M
    const ASN1_PRIMITIVE_FUNCS *pf;
518
83.0M
    pf = it->funcs;
519
83.0M
    if (pf && pf->prim_i2c)
520
476k
        return pf->prim_i2c(pval, cout, putype, it);
521
522
    /* Should type be omitted? */
523
82.5M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE)
524
82.5M
        || (it->utype != V_ASN1_BOOLEAN)) {
525
82.4M
        if (!*pval)
526
314k
            return -1;
527
82.4M
    }
528
529
82.2M
    if (it->itype == ASN1_ITYPE_MSTRING) {
530
        /* If MSTRING type set the underlying type */
531
29.5M
        strtmp = (ASN1_STRING *)*pval;
532
29.5M
        utype = strtmp->type;
533
29.5M
        *putype = utype;
534
52.7M
    } else if (it->utype == V_ASN1_ANY) {
535
        /* If ANY set type and pointer to value */
536
16.0M
        ASN1_TYPE *typ;
537
16.0M
        typ = (ASN1_TYPE *)*pval;
538
16.0M
        utype = typ->type;
539
16.0M
        *putype = utype;
540
16.0M
        pval = &typ->value.asn1_value;
541
16.0M
    } else
542
36.6M
        utype = *putype;
543
544
82.2M
    switch (utype) {
545
30.2M
    case V_ASN1_OBJECT:
546
30.2M
        otmp = (ASN1_OBJECT *)*pval;
547
30.2M
        cont = otmp->data;
548
30.2M
        len = otmp->length;
549
30.2M
        if (cont == NULL || len == 0)
550
0
            return -1;
551
30.2M
        break;
552
553
30.2M
    case V_ASN1_NULL:
554
3.62M
        cont = NULL;
555
3.62M
        len = 0;
556
3.62M
        break;
557
558
151k
    case V_ASN1_BOOLEAN:
559
151k
        tbool = (ASN1_BOOLEAN *)pval;
560
151k
        if (*tbool == -1)
561
106k
            return -1;
562
45.9k
        if (it->utype != V_ASN1_ANY) {
563
            /*
564
             * Default handling if value == size field then omit
565
             */
566
11.9k
            if (*tbool && (it->size > 0))
567
8
                return -1;
568
11.9k
            if (!*tbool && !it->size)
569
4.12k
                return -1;
570
11.9k
        }
571
41.8k
        c = (unsigned char)*tbool;
572
41.8k
        cont = &c;
573
41.8k
        len = 1;
574
41.8k
        break;
575
576
1.35M
    case V_ASN1_BIT_STRING:
577
1.35M
        return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
578
1.35M
                                   cout ? &cout : NULL);
579
580
437k
    case V_ASN1_INTEGER:
581
452k
    case V_ASN1_ENUMERATED:
582
        /*
583
         * These are all have the same content format as ASN1_INTEGER
584
         */
585
452k
        return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
586
587
240k
    case V_ASN1_OCTET_STRING:
588
5.02M
    case V_ASN1_NUMERICSTRING:
589
5.43M
    case V_ASN1_PRINTABLESTRING:
590
5.81M
    case V_ASN1_T61STRING:
591
5.81M
    case V_ASN1_VIDEOTEXSTRING:
592
7.91M
    case V_ASN1_IA5STRING:
593
7.91M
    case V_ASN1_UTCTIME:
594
7.95M
    case V_ASN1_GENERALIZEDTIME:
595
9.63M
    case V_ASN1_GRAPHICSTRING:
596
9.63M
    case V_ASN1_VISIBLESTRING:
597
9.65M
    case V_ASN1_GENERALSTRING:
598
9.81M
    case V_ASN1_UNIVERSALSTRING:
599
11.9M
    case V_ASN1_BMPSTRING:
600
26.0M
    case V_ASN1_UTF8STRING:
601
33.0M
    case V_ASN1_SEQUENCE:
602
33.0M
    case V_ASN1_SET:
603
46.3M
    default:
604
        /* All based on ASN1_STRING and handled the same */
605
46.3M
        strtmp = (ASN1_STRING *)*pval;
606
        /* Special handling for NDEF */
607
46.3M
        if ((it->size == ASN1_TFLG_NDEF)
608
46.3M
            && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
609
0
            if (cout) {
610
0
                strtmp->data = cout;
611
0
                strtmp->length = 0;
612
0
            }
613
            /* Special return code */
614
0
            return -2;
615
0
        }
616
46.3M
        cont = strtmp->data;
617
46.3M
        len = strtmp->length;
618
619
46.3M
        break;
620
621
82.2M
    }
622
80.3M
    if (cout && len)
623
8.47M
        memcpy(cout, cont, len);
624
80.3M
    return len;
625
82.2M
}