Coverage Report

Created: 2026-09-17 06:47

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