Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/asn1/tasn_enc.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (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(const ASN1_VALUE **pval, unsigned char **out,
20
    const ASN1_ITEM *it, int tag, int aclass);
21
static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
22
    unsigned char **out,
23
    int skcontlen, const ASN1_ITEM *item,
24
    int do_sort, int iclass);
25
static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
26
    const ASN1_TEMPLATE *tt, int tag, int aclass);
27
static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
28
    const ASN1_ITEM *it, int flags);
29
static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
30
    const ASN1_ITEM *it);
31
32
/*
33
 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
34
 * indefinite length constructed encoding, where appropriate
35
 */
36
37
int ASN1_item_ndef_i2d(const ASN1_VALUE *val, unsigned char **out,
38
    const ASN1_ITEM *it)
39
0
{
40
0
    return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
41
0
}
42
43
int ASN1_item_i2d(const ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
44
1.16M
{
45
1.16M
    return asn1_item_flags_i2d(val, out, it, 0);
46
1.16M
}
47
48
/*
49
 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
50
 * points to a buffer to output the data to. The new i2d has one additional
51
 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
52
 * allocated and populated with the encoding.
53
 */
54
55
static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
56
    const ASN1_ITEM *it, int flags)
57
774k
{
58
774k
    if (out != NULL && *out == NULL) {
59
615k
        unsigned char *p, *buf;
60
615k
        int len;
61
62
615k
        len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
63
615k
        if (len <= 0)
64
69
            return len;
65
614k
        if ((buf = OPENSSL_malloc(len)) == NULL)
66
0
            return -1;
67
614k
        p = buf;
68
614k
        ASN1_item_ex_i2d(&val, &p, it, -1, flags);
69
614k
        *out = buf;
70
614k
        return len;
71
614k
    }
72
73
159k
    return ASN1_item_ex_i2d(&val, out, it, -1, flags);
74
774k
}
75
76
/*
77
 * Encode an item, taking care of IMPLICIT tagging (if any). This function
78
 * performs the normal item handling: it can be used in external types.
79
 */
80
81
int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
82
    const ASN1_ITEM *it, int tag, int aclass)
83
330M
{
84
330M
    const ASN1_TEMPLATE *tt = NULL;
85
330M
    int i, seqcontlen, seqlen, ndef = 1;
86
330M
    const ASN1_EXTERN_FUNCS *ef;
87
330M
    const ASN1_AUX *aux = it->funcs;
88
330M
    ASN1_aux_const_cb *asn1_cb = NULL;
89
90
330M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
91
7.57M
        return 0;
92
93
322M
    if (aux != NULL) {
94
5.05M
        asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
95
5.05M
                                                           : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
96
5.05M
    }
97
98
322M
    switch (it->itype) {
99
100
209M
    case ASN1_ITYPE_PRIMITIVE:
101
209M
        if (it->templates)
102
17.8M
            return asn1_template_ex_i2d(pval, out, it->templates,
103
17.8M
                tag, aclass);
104
191M
        return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
105
106
55.4M
    case ASN1_ITYPE_MSTRING:
107
        /*
108
         * It never makes sense for multi-strings to have implicit tagging, so
109
         * if tag != -1, then this looks like an error in the template.
110
         */
111
55.4M
        if (tag != -1) {
112
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
113
0
            return -1;
114
0
        }
115
55.4M
        return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
116
117
4.05M
    case ASN1_ITYPE_CHOICE:
118
        /*
119
         * It never makes sense for CHOICE types to have implicit tagging, so
120
         * if tag != -1, then this looks like an error in the template.
121
         */
122
4.05M
        if (tag != -1) {
123
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
124
0
            return -1;
125
0
        }
126
4.05M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
127
0
            return 0;
128
4.05M
        i = ossl_asn1_get_choice_selector_const(pval, it);
129
4.05M
        if ((i >= 0) && (i < it->tcount)) {
130
4.05M
            const ASN1_VALUE **pchval;
131
4.05M
            const ASN1_TEMPLATE *chtt;
132
4.05M
            chtt = it->templates + i;
133
4.05M
            pchval = ossl_asn1_get_const_field_ptr(pval, chtt);
134
4.05M
            return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
135
4.05M
        }
136
        /* Fixme: error condition if selector out of range */
137
0
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
138
0
            return 0;
139
0
        break;
140
141
1.15M
    case ASN1_ITYPE_EXTERN:
142
        /* If new style i2d it does all the work */
143
1.15M
        ef = it->funcs;
144
1.15M
        return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
145
146
70.8k
    case ASN1_ITYPE_NDEF_SEQUENCE:
147
        /* Use indefinite length constructed if requested */
148
70.8k
        if (aclass & ASN1_TFLG_NDEF)
149
0
            ndef = 2;
150
        /* fall through */
151
152
52.2M
    case ASN1_ITYPE_SEQUENCE:
153
52.2M
        i = ossl_asn1_enc_restore(&seqcontlen, out, pval, it);
154
        /* An error occurred */
155
52.2M
        if (i < 0)
156
0
            return 0;
157
        /* We have a valid cached encoding... */
158
52.2M
        if (i > 0)
159
1.02M
            return seqcontlen;
160
        /* Otherwise carry on */
161
51.2M
        seqcontlen = 0;
162
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
163
51.2M
        if (tag == -1) {
164
50.4M
            tag = V_ASN1_SEQUENCE;
165
            /* Retain any other flags in aclass */
166
50.4M
            aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
167
50.4M
                | V_ASN1_UNIVERSAL;
168
50.4M
        }
169
51.2M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
170
0
            return 0;
171
        /* First work out sequence content length */
172
172M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
173
120M
            const ASN1_TEMPLATE *seqtt;
174
120M
            const ASN1_VALUE **pseqval;
175
120M
            int tmplen;
176
120M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
177
120M
            if (!seqtt)
178
0
                return 0;
179
120M
            pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
180
120M
            tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
181
120M
            if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
182
0
                return -1;
183
120M
            seqcontlen += tmplen;
184
120M
        }
185
186
51.2M
        seqlen = ASN1_object_size(ndef, seqcontlen, tag);
187
51.2M
        if (!out || seqlen == -1)
188
35.8M
            return seqlen;
189
        /* Output SEQUENCE header */
190
15.3M
        ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
191
49.3M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
192
34.0M
            const ASN1_TEMPLATE *seqtt;
193
34.0M
            const ASN1_VALUE **pseqval;
194
34.0M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
195
34.0M
            if (!seqtt)
196
0
                return 0;
197
34.0M
            pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
198
            /* FIXME: check for errors in enhanced version */
199
34.0M
            asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
200
34.0M
        }
201
15.3M
        if (ndef == 2)
202
0
            ASN1_put_eoc(out);
203
15.3M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
204
0
            return 0;
205
15.3M
        return seqlen;
206
207
0
    default:
208
0
        return 0;
209
322M
    }
210
0
    return 0;
211
322M
}
212
213
static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
214
    const ASN1_TEMPLATE *tt, int tag, int iclass)
215
176M
{
216
176M
    const int flags = tt->flags;
217
176M
    int i, ret, ttag, tclass, ndef, len;
218
176M
    const ASN1_VALUE *tval;
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
176M
    if (flags & ASN1_TFLG_EMBED) {
225
4.50M
        tval = (ASN1_VALUE *)pval;
226
4.50M
        pval = &tval;
227
4.50M
    }
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
176M
    if (flags & ASN1_TFLG_TAG_MASK) {
236
        /* Error if argument and template tagging */
237
25.1M
        if (tag != -1)
238
            /* FIXME: error code here */
239
0
            return -1;
240
        /* Get tagging from template */
241
25.1M
        ttag = tt->tag;
242
25.1M
        tclass = flags & ASN1_TFLG_TAG_CLASS;
243
151M
    } else if (tag != -1) {
244
        /* No template tagging, get from arguments */
245
0
        ttag = tag;
246
0
        tclass = iclass & ASN1_TFLG_TAG_CLASS;
247
151M
    } else {
248
151M
        ttag = -1;
249
151M
        tclass = 0;
250
151M
    }
251
    /*
252
     * Remove any class mask from iflag.
253
     */
254
176M
    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
176M
    if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
263
0
        ndef = 2;
264
176M
    else
265
176M
        ndef = 1;
266
267
176M
    if (flags & ASN1_TFLG_SK_MASK) {
268
        /* SET OF, SEQUENCE OF */
269
22.4M
        STACK_OF(const_ASN1_VALUE) *sk = (STACK_OF(const_ASN1_VALUE) *)*pval;
270
22.4M
        int isset, sktag, skaclass;
271
22.4M
        int skcontlen, sklen;
272
22.4M
        const ASN1_VALUE *skitem;
273
274
22.4M
        if (*pval == NULL)
275
4.54M
            return 0;
276
277
17.8M
        if (flags & ASN1_TFLG_SET_OF) {
278
16.8M
            isset = 1;
279
            /* 2 means we reorder */
280
16.8M
            if (flags & ASN1_TFLG_SEQUENCE_OF)
281
646
                isset = 2;
282
16.8M
        } else
283
1.02M
            isset = 0;
284
285
        /*
286
         * Work out inner tag value: if EXPLICIT or no tagging use underlying
287
         * type.
288
         */
289
17.8M
        if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
290
93.7k
            sktag = ttag;
291
93.7k
            skaclass = tclass;
292
17.7M
        } else {
293
17.7M
            skaclass = V_ASN1_UNIVERSAL;
294
17.7M
            if (isset)
295
16.8M
                sktag = V_ASN1_SET;
296
958k
            else
297
958k
                sktag = V_ASN1_SEQUENCE;
298
17.7M
        }
299
300
        /* Determine total length of items */
301
17.8M
        skcontlen = 0;
302
147M
        for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
303
129M
            skitem = sk_const_ASN1_VALUE_value(sk, i);
304
129M
            len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
305
129M
                -1, iclass);
306
129M
            if (len == -1 || (skcontlen > INT_MAX - len))
307
0
                return -1;
308
129M
            if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
309
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
310
0
                return -1;
311
0
            }
312
129M
            skcontlen += len;
313
129M
        }
314
17.8M
        sklen = ASN1_object_size(ndef, skcontlen, sktag);
315
17.8M
        if (sklen == -1)
316
0
            return -1;
317
        /* If EXPLICIT need length of surrounding tag */
318
17.8M
        if (flags & ASN1_TFLG_EXPTAG)
319
73.8k
            ret = ASN1_object_size(ndef, sklen, ttag);
320
17.8M
        else
321
17.8M
            ret = sklen;
322
323
17.8M
        if (!out || ret == -1)
324
10.2M
            return ret;
325
326
        /* Now encode this lot... */
327
        /* EXPLICIT tag */
328
7.64M
        if (flags & ASN1_TFLG_EXPTAG)
329
7.23k
            ASN1_put_object(out, ndef, sklen, ttag, tclass);
330
        /* SET or SEQUENCE and IMPLICIT tag */
331
7.64M
        ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
332
        /* And the stuff itself */
333
7.64M
        asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
334
7.64M
            isset, iclass);
335
7.64M
        if (ndef == 2) {
336
0
            ASN1_put_eoc(out);
337
0
            if (flags & ASN1_TFLG_EXPTAG)
338
0
                ASN1_put_eoc(out);
339
0
        }
340
341
7.64M
        return ret;
342
17.8M
    }
343
344
154M
    if (flags & ASN1_TFLG_EXPTAG) {
345
        /* EXPLICIT tagging */
346
        /* Find length of tagged item */
347
8.15M
        i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
348
8.15M
        if (i == 0) {
349
6.88M
            if ((tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
350
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
351
0
                return -1;
352
0
            }
353
6.88M
            return 0;
354
6.88M
        }
355
        /* Find length of EXPLICIT tag */
356
1.26M
        ret = ASN1_object_size(ndef, i, ttag);
357
1.26M
        if (out && ret != -1) {
358
            /* Output tag and item */
359
207k
            ASN1_put_object(out, ndef, i, ttag, tclass);
360
207k
            ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
361
207k
            if (ndef == 2)
362
0
                ASN1_put_eoc(out);
363
207k
        }
364
1.26M
        return ret;
365
8.15M
    }
366
367
    /* Either normal or IMPLICIT tagging: combine class and flags */
368
146M
    len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
369
146M
        ttag, tclass | iclass);
370
146M
    if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
371
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
372
0
        return -1;
373
0
    }
374
146M
    return len;
375
146M
}
376
377
/* Temporary structure used to hold DER encoding of items for SET OF */
378
379
typedef struct {
380
    unsigned char *data;
381
    int length;
382
    const ASN1_VALUE *field;
383
} DER_ENC;
384
385
static int der_cmp(const void *a, const void *b)
386
363M
{
387
363M
    const DER_ENC *d1 = a, *d2 = b;
388
363M
    int cmplen, i;
389
363M
    cmplen = (d1->length < d2->length) ? d1->length : d2->length;
390
363M
    i = memcmp(d1->data, d2->data, cmplen);
391
363M
    if (i)
392
169M
        return i;
393
194M
    return d1->length - d2->length;
394
363M
}
395
396
/* Output the content octets of SET OF or SEQUENCE OF */
397
398
static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
399
    unsigned char **out,
400
    int skcontlen, const ASN1_ITEM *item,
401
    int do_sort, int iclass)
402
7.64M
{
403
7.64M
    int i, ret = 0;
404
7.64M
    const ASN1_VALUE *skitem;
405
7.64M
    unsigned char *tmpdat = NULL, *p = NULL;
406
7.64M
    DER_ENC *derlst = NULL, *tder;
407
408
7.64M
    if (do_sort) {
409
        /* Don't need to sort less than 2 items */
410
7.42M
        if (sk_const_ASN1_VALUE_num(sk) < 2)
411
7.10M
            do_sort = 0;
412
326k
        else {
413
326k
            derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk)
414
326k
                * sizeof(*derlst));
415
326k
            if (derlst == NULL)
416
0
                return 0;
417
326k
            tmpdat = OPENSSL_malloc(skcontlen);
418
326k
            if (tmpdat == NULL)
419
0
                goto err;
420
326k
        }
421
7.42M
    }
422
    /* If not sorting just output each item */
423
7.64M
    if (!do_sort) {
424
20.4M
        for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
425
13.1M
            skitem = sk_const_ASN1_VALUE_value(sk, i);
426
13.1M
            ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
427
13.1M
        }
428
7.31M
        return 1;
429
7.31M
    }
430
326k
    p = tmpdat;
431
432
    /* Doing sort: build up a list of each member's DER encoding */
433
19.3M
    for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
434
18.9M
        skitem = sk_const_ASN1_VALUE_value(sk, i);
435
18.9M
        tder->data = p;
436
18.9M
        tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
437
18.9M
        tder->field = skitem;
438
18.9M
    }
439
440
    /* Now sort them */
441
326k
    qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
442
    /* Output sorted DER encoding */
443
326k
    p = *out;
444
19.3M
    for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
445
18.9M
        memcpy(p, tder->data, tder->length);
446
18.9M
        p += tder->length;
447
18.9M
    }
448
326k
    *out = p;
449
    /* If do_sort is 2 then reorder the STACK */
450
326k
    if (do_sort == 2) {
451
1.44k
        for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++)
452
1.37k
            (void)sk_const_ASN1_VALUE_set(sk, i, tder->field);
453
68
    }
454
326k
    ret = 1;
455
326k
err:
456
326k
    OPENSSL_free(derlst);
457
326k
    OPENSSL_free(tmpdat);
458
326k
    return ret;
459
326k
}
460
461
static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
462
    const ASN1_ITEM *it, int tag, int aclass)
463
382M
{
464
382M
    int len;
465
382M
    int utype;
466
382M
    int usetag;
467
382M
    int ndef = 0;
468
469
382M
    utype = it->utype;
470
471
    /*
472
     * Get length of content octets and maybe find out the underlying type.
473
     */
474
475
382M
    len = asn1_ex_i2c(pval, NULL, &utype, it);
476
477
    /*
478
     * If SEQUENCE, SET or OTHER then header is included in pseudo content
479
     * octets so don't include tag+length. We need to check here because the
480
     * call to asn1_ex_i2c() could change utype.
481
     */
482
382M
    if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
483
101M
        usetag = 0;
484
280M
    else
485
280M
        usetag = 1;
486
487
    /* -1 means omit type */
488
489
382M
    if (len == -1)
490
16.8M
        return 0;
491
492
    /* -2 return is special meaning use ndef */
493
365M
    if (len == -2) {
494
16
        ndef = 2;
495
16
        len = 0;
496
16
    }
497
498
    /* If not implicitly tagged get tag from underlying type */
499
365M
    if (tag == -1)
500
356M
        tag = utype;
501
502
    /* Output tag+length followed by content octets */
503
365M
    if (out) {
504
71.5M
        if (usetag)
505
55.1M
            ASN1_put_object(out, ndef, len, tag, aclass);
506
71.5M
        asn1_ex_i2c(pval, *out, &utype, it);
507
71.5M
        if (ndef)
508
2
            ASN1_put_eoc(out);
509
71.5M
        else
510
71.5M
            *out += len;
511
71.5M
    }
512
513
365M
    if (usetag)
514
263M
        return ASN1_object_size(ndef, len, tag);
515
101M
    return len;
516
365M
}
517
518
/* Produce content octets from a structure */
519
520
static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
521
    const ASN1_ITEM *it)
522
453M
{
523
453M
    ASN1_BOOLEAN *tbool = NULL;
524
453M
    ASN1_STRING *strtmp;
525
453M
    ASN1_OBJECT *otmp;
526
453M
    int utype;
527
453M
    const unsigned char *cont;
528
453M
    unsigned char c;
529
453M
    int len;
530
453M
    const ASN1_PRIMITIVE_FUNCS *pf;
531
453M
    pf = it->funcs;
532
453M
    if (pf && pf->prim_i2c)
533
1.99M
        return pf->prim_i2c(pval, cout, putype, it);
534
535
    /* Should type be omitted? */
536
451M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE)
537
450M
        || (it->utype != V_ASN1_BOOLEAN)) {
538
450M
        if (*pval == NULL)
539
15.1M
            return -1;
540
450M
    }
541
542
436M
    if (it->itype == ASN1_ITYPE_MSTRING) {
543
        /* If MSTRING type set the underlying type */
544
103M
        strtmp = (ASN1_STRING *)*pval;
545
103M
        utype = strtmp->type;
546
103M
        *putype = utype;
547
333M
    } else if (it->utype == V_ASN1_ANY) {
548
        /* If ANY set type and pointer to value */
549
182M
        ASN1_TYPE *typ;
550
182M
        typ = (ASN1_TYPE *)*pval;
551
182M
        utype = typ->type;
552
182M
        *putype = utype;
553
182M
        pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */
554
182M
    } else
555
150M
        utype = *putype;
556
557
436M
    switch (utype) {
558
117M
    case V_ASN1_OBJECT:
559
117M
        otmp = (ASN1_OBJECT *)*pval;
560
117M
        cont = otmp->data;
561
117M
        len = otmp->length;
562
117M
        if (cont == NULL || len == 0)
563
154
            return -1;
564
117M
        break;
565
566
117M
    case V_ASN1_UNDEF:
567
18
        return -2;
568
569
2.28M
    case V_ASN1_NULL:
570
2.28M
        cont = NULL;
571
2.28M
        len = 0;
572
2.28M
        break;
573
574
2.00M
    case V_ASN1_BOOLEAN:
575
2.00M
        tbool = (ASN1_BOOLEAN *)pval;
576
2.00M
        if (*tbool == -1)
577
0
            return -1;
578
2.00M
        if (it->utype != V_ASN1_ANY) {
579
            /*
580
             * Default handling if value == size field then omit
581
             */
582
1.67M
            if (*tbool && (it->size > 0))
583
8.67k
                return -1;
584
1.66M
            if (!*tbool && !it->size)
585
1.60M
                return -1;
586
1.66M
        }
587
387k
        c = (unsigned char)*tbool;
588
387k
        cont = &c;
589
387k
        len = 1;
590
387k
        break;
591
592
11.1M
    case V_ASN1_BIT_STRING:
593
11.1M
        return ossl_i2c_ASN1_BIT_STRING((const ASN1_BIT_STRING *)*pval,
594
11.1M
            cout ? &cout : NULL);
595
596
39.9M
    case V_ASN1_INTEGER:
597
40.1M
    case V_ASN1_ENUMERATED:
598
        /*
599
         * These are all have the same content format as ASN1_INTEGER
600
         */
601
40.1M
        return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
602
603
4.29M
    case V_ASN1_OCTET_STRING:
604
41.3M
    case V_ASN1_NUMERICSTRING:
605
41.8M
    case V_ASN1_PRINTABLESTRING:
606
47.4M
    case V_ASN1_T61STRING:
607
47.4M
    case V_ASN1_VIDEOTEXSTRING:
608
50.9M
    case V_ASN1_IA5STRING:
609
51.0M
    case V_ASN1_UTCTIME:
610
51.8M
    case V_ASN1_GENERALIZEDTIME:
611
58.4M
    case V_ASN1_GRAPHICSTRING:
612
58.4M
    case V_ASN1_VISIBLESTRING:
613
58.6M
    case V_ASN1_GENERALSTRING:
614
61.0M
    case V_ASN1_UNIVERSALSTRING:
615
62.5M
    case V_ASN1_BMPSTRING:
616
108M
    case V_ASN1_UTF8STRING:
617
129M
    case V_ASN1_SEQUENCE:
618
205M
    case V_ASN1_SET:
619
263M
    default:
620
        /* All based on ASN1_STRING and handled the same */
621
263M
        strtmp = (ASN1_STRING *)*pval;
622
        /* Special handling for NDEF */
623
263M
        if ((it->size == ASN1_TFLG_NDEF)
624
936
            && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
625
0
            if (cout) {
626
0
                strtmp->data = cout;
627
0
                strtmp->length = 0;
628
0
            }
629
            /* Special return code */
630
0
            return -2;
631
0
        }
632
263M
        cont = strtmp->data;
633
263M
        len = strtmp->length;
634
635
263M
        break;
636
436M
    }
637
383M
    if (cout && len)
638
42.8M
        memcpy(cout, cont, len);
639
383M
    return len;
640
436M
}