Coverage Report

Created: 2026-02-14 07:20

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