Coverage Report

Created: 2025-12-04 06:33

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