Coverage Report

Created: 2026-04-01 06:39

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.28M
{
45
1.28M
    return asn1_item_flags_i2d(val, out, it, 0);
46
1.28M
}
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.28M
{
58
1.28M
    if (out != NULL && *out == NULL) {
59
997k
        unsigned char *p, *buf;
60
997k
        int len;
61
62
997k
        len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
63
997k
        if (len <= 0)
64
104
            return len;
65
997k
        if ((buf = OPENSSL_malloc(len)) == NULL) {
66
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
67
0
            return -1;
68
0
        }
69
997k
        p = buf;
70
997k
        ASN1_item_ex_i2d(&val, &p, it, -1, flags);
71
997k
        *out = buf;
72
997k
        return len;
73
997k
    }
74
75
283k
    return ASN1_item_ex_i2d(&val, out, it, -1, flags);
76
1.28M
}
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
537M
{
86
537M
    const ASN1_TEMPLATE *tt = NULL;
87
537M
    int i, seqcontlen, seqlen, ndef = 1;
88
537M
    const ASN1_EXTERN_FUNCS *ef;
89
537M
    const ASN1_AUX *aux = it->funcs;
90
537M
    ASN1_aux_const_cb *asn1_cb = NULL;
91
92
537M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
93
11.2M
        return 0;
94
95
526M
    if (aux != NULL) {
96
8.18M
        asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
97
8.18M
                                                           : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
98
8.18M
    }
99
100
526M
    switch (it->itype) {
101
102
312M
    case ASN1_ITYPE_PRIMITIVE:
103
312M
        if (it->templates)
104
26.2M
            return asn1_template_ex_i2d(pval, out, it->templates,
105
26.2M
                tag, aclass);
106
285M
        return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
107
108
107M
    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
107M
        if (tag != -1) {
114
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
115
0
            return -1;
116
0
        }
117
107M
        return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
118
119
8.22M
    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
8.22M
        if (tag != -1) {
125
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
126
0
            return -1;
127
0
        }
128
8.22M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
129
0
            return 0;
130
8.22M
        i = ossl_asn1_get_choice_selector_const(pval, it);
131
8.22M
        if ((i >= 0) && (i < it->tcount)) {
132
8.22M
            const ASN1_VALUE **pchval;
133
8.22M
            const ASN1_TEMPLATE *chtt;
134
8.22M
            chtt = it->templates + i;
135
8.22M
            pchval = ossl_asn1_get_const_field_ptr(pval, chtt);
136
8.22M
            return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
137
8.22M
        }
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.58M
    case ASN1_ITYPE_EXTERN:
144
        /* If new style i2d it does all the work */
145
1.58M
        ef = it->funcs;
146
1.58M
        return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
147
148
111k
    case ASN1_ITYPE_NDEF_SEQUENCE:
149
        /* Use indefinite length constructed if requested */
150
111k
        if (aclass & ASN1_TFLG_NDEF)
151
0
            ndef = 2;
152
        /* fall through */
153
154
97.2M
    case ASN1_ITYPE_SEQUENCE:
155
97.2M
        i = ossl_asn1_enc_restore(&seqcontlen, out, pval, it);
156
        /* An error occurred */
157
97.2M
        if (i < 0)
158
0
            return 0;
159
        /* We have a valid cached encoding... */
160
97.2M
        if (i > 0)
161
1.72M
            return seqcontlen;
162
        /* Otherwise carry on */
163
95.4M
        seqcontlen = 0;
164
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
165
95.4M
        if (tag == -1) {
166
94.2M
            tag = V_ASN1_SEQUENCE;
167
            /* Retain any other flags in aclass */
168
94.2M
            aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
169
94.2M
                | V_ASN1_UNIVERSAL;
170
94.2M
        }
171
95.4M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
172
0
            return 0;
173
        /* First work out sequence content length */
174
312M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
175
216M
            const ASN1_TEMPLATE *seqtt;
176
216M
            const ASN1_VALUE **pseqval;
177
216M
            int tmplen;
178
216M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
179
216M
            if (!seqtt)
180
0
                return 0;
181
216M
            pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
182
216M
            tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
183
216M
            if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
184
0
                return -1;
185
216M
            seqcontlen += tmplen;
186
216M
        }
187
188
95.4M
        seqlen = ASN1_object_size(ndef, seqcontlen, tag);
189
95.4M
        if (!out || seqlen == -1)
190
66.4M
            return seqlen;
191
        /* Output SEQUENCE header */
192
29.0M
        ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
193
92.1M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
194
63.0M
            const ASN1_TEMPLATE *seqtt;
195
63.0M
            const ASN1_VALUE **pseqval;
196
63.0M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
197
63.0M
            if (!seqtt)
198
0
                return 0;
199
63.0M
            pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
200
            /* FIXME: check for errors in enhanced version */
201
63.0M
            asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
202
63.0M
        }
203
29.0M
        if (ndef == 2)
204
0
            ASN1_put_eoc(out);
205
29.0M
        if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
206
0
            return 0;
207
29.0M
        return seqlen;
208
209
0
    default:
210
0
        return 0;
211
526M
    }
212
0
    return 0;
213
526M
}
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
314M
{
218
314M
    const int flags = tt->flags;
219
314M
    int i, ret, ttag, tclass, ndef, len;
220
314M
    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
314M
    if (flags & ASN1_TFLG_EMBED) {
227
7.60M
        tval = (ASN1_VALUE *)pval;
228
7.60M
        pval = &tval;
229
7.60M
    }
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
314M
    if (flags & ASN1_TFLG_TAG_MASK) {
238
        /* Error if argument and template tagging */
239
37.6M
        if (tag != -1)
240
            /* FIXME: error code here */
241
0
            return -1;
242
        /* Get tagging from template */
243
37.6M
        ttag = tt->tag;
244
37.6M
        tclass = flags & ASN1_TFLG_TAG_CLASS;
245
276M
    } else if (tag != -1) {
246
        /* No template tagging, get from arguments */
247
0
        ttag = tag;
248
0
        tclass = iclass & ASN1_TFLG_TAG_CLASS;
249
276M
    } else {
250
276M
        ttag = -1;
251
276M
        tclass = 0;
252
276M
    }
253
    /*
254
     * Remove any class mask from iflag.
255
     */
256
314M
    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
314M
    if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
265
0
        ndef = 2;
266
314M
    else
267
314M
        ndef = 1;
268
269
314M
    if (flags & ASN1_TFLG_SK_MASK) {
270
        /* SET OF, SEQUENCE OF */
271
32.3M
        STACK_OF(const_ASN1_VALUE) *sk = (STACK_OF(const_ASN1_VALUE) *)*pval;
272
32.3M
        int isset, sktag, skaclass;
273
32.3M
        int skcontlen, sklen;
274
32.3M
        const ASN1_VALUE *skitem;
275
276
32.3M
        if (*pval == NULL)
277
6.14M
            return 0;
278
279
26.1M
        if (flags & ASN1_TFLG_SET_OF) {
280
24.8M
            isset = 1;
281
            /* 2 means we reorder */
282
24.8M
            if (flags & ASN1_TFLG_SEQUENCE_OF)
283
1.19k
                isset = 2;
284
24.8M
        } 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
26.1M
        if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
292
135k
            sktag = ttag;
293
135k
            skaclass = tclass;
294
26.0M
        } else {
295
26.0M
            skaclass = V_ASN1_UNIVERSAL;
296
26.0M
            if (isset)
297
24.7M
                sktag = V_ASN1_SET;
298
1.23M
            else
299
1.23M
                sktag = V_ASN1_SEQUENCE;
300
26.0M
        }
301
302
        /* Determine total length of items */
303
26.1M
        skcontlen = 0;
304
209M
        for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
305
183M
            skitem = sk_const_ASN1_VALUE_value(sk, i);
306
183M
            len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
307
183M
                -1, iclass);
308
183M
            if (len == -1 || (skcontlen > INT_MAX - len))
309
0
                return -1;
310
183M
            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
183M
            skcontlen += len;
315
183M
        }
316
26.1M
        sklen = ASN1_object_size(ndef, skcontlen, sktag);
317
26.1M
        if (sklen == -1)
318
0
            return -1;
319
        /* If EXPLICIT need length of surrounding tag */
320
26.1M
        if (flags & ASN1_TFLG_EXPTAG)
321
89.5k
            ret = ASN1_object_size(ndef, sklen, ttag);
322
26.0M
        else
323
26.0M
            ret = sklen;
324
325
26.1M
        if (!out || ret == -1)
326
14.7M
            return ret;
327
328
        /* Now encode this lot... */
329
        /* EXPLICIT tag */
330
11.3M
        if (flags & ASN1_TFLG_EXPTAG)
331
9.29k
            ASN1_put_object(out, ndef, sklen, ttag, tclass);
332
        /* SET or SEQUENCE and IMPLICIT tag */
333
11.3M
        ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
334
        /* And the stuff itself */
335
11.3M
        asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
336
11.3M
            isset, iclass);
337
11.3M
        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
11.3M
        return ret;
344
26.1M
    }
345
346
282M
    if (flags & ASN1_TFLG_EXPTAG) {
347
        /* EXPLICIT tagging */
348
        /* Find length of tagged item */
349
10.1M
        i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
350
10.1M
        if (i == 0) {
351
8.63M
            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.63M
            return 0;
356
8.63M
        }
357
        /* Find length of EXPLICIT tag */
358
1.48M
        ret = ASN1_object_size(ndef, i, ttag);
359
1.48M
        if (out && ret != -1) {
360
            /* Output tag and item */
361
277k
            ASN1_put_object(out, ndef, i, ttag, tclass);
362
277k
            ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
363
277k
            if (ndef == 2)
364
0
                ASN1_put_eoc(out);
365
277k
        }
366
1.48M
        return ret;
367
10.1M
    }
368
369
    /* Either normal or IMPLICIT tagging: combine class and flags */
370
271M
    len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
371
271M
        ttag, tclass | iclass);
372
271M
    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
271M
    return len;
377
271M
}
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
369M
{
389
369M
    const DER_ENC *d1 = a, *d2 = b;
390
369M
    int cmplen, i;
391
369M
    cmplen = (d1->length < d2->length) ? d1->length : d2->length;
392
369M
    i = memcmp(d1->data, d2->data, cmplen);
393
369M
    if (i)
394
167M
        return i;
395
201M
    return d1->length - d2->length;
396
369M
}
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
11.3M
{
405
11.3M
    int i, ret = 0;
406
11.3M
    const ASN1_VALUE *skitem;
407
11.3M
    unsigned char *tmpdat = NULL, *p = NULL;
408
11.3M
    DER_ENC *derlst = NULL, *tder;
409
410
11.3M
    if (do_sort) {
411
        /* Don't need to sort less than 2 items */
412
11.0M
        if (sk_const_ASN1_VALUE_num(sk) < 2)
413
10.4M
            do_sort = 0;
414
584k
        else {
415
584k
            derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk)
416
584k
                * sizeof(*derlst));
417
584k
            if (derlst == NULL) {
418
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
419
0
                return 0;
420
0
            }
421
584k
            tmpdat = OPENSSL_malloc(skcontlen);
422
584k
            if (tmpdat == NULL) {
423
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
424
0
                goto err;
425
0
            }
426
584k
        }
427
11.0M
    }
428
    /* If not sorting just output each item */
429
11.3M
    if (!do_sort) {
430
30.1M
        for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
431
19.3M
            skitem = sk_const_ASN1_VALUE_value(sk, i);
432
19.3M
            ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
433
19.3M
        }
434
10.7M
        return 1;
435
10.7M
    }
436
584k
    p = tmpdat;
437
438
    /* Doing sort: build up a list of each member's DER encoding */
439
32.0M
    for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
440
31.5M
        skitem = sk_const_ASN1_VALUE_value(sk, i);
441
31.5M
        tder->data = p;
442
31.5M
        tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
443
31.5M
        tder->field = skitem;
444
31.5M
    }
445
446
    /* Now sort them */
447
584k
    qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
448
    /* Output sorted DER encoding */
449
584k
    p = *out;
450
32.0M
    for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
451
31.5M
        memcpy(p, tder->data, tder->length);
452
31.5M
        p += tder->length;
453
31.5M
    }
454
584k
    *out = p;
455
    /* If do_sort is 2 then reorder the STACK */
456
584k
    if (do_sort == 2) {
457
2.64k
        for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++)
458
2.51k
            (void)sk_const_ASN1_VALUE_set(sk, i, tder->field);
459
126
    }
460
584k
    ret = 1;
461
584k
err:
462
584k
    OPENSSL_free(derlst);
463
584k
    OPENSSL_free(tmpdat);
464
584k
    return ret;
465
584k
}
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
393M
{
470
393M
    int len;
471
393M
    int utype;
472
393M
    int usetag;
473
393M
    int ndef = 0;
474
475
393M
    utype = it->utype;
476
477
    /*
478
     * Get length of content octets and maybe find out the underlying type.
479
     */
480
481
393M
    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
393M
    if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
489
101M
        usetag = 0;
490
291M
    else
491
291M
        usetag = 1;
492
493
    /* -1 means omit type */
494
495
393M
    if (len == -1)
496
16.4M
        return 0;
497
498
    /* -2 return is special meaning use ndef */
499
376M
    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
376M
    if (tag == -1)
506
369M
        tag = utype;
507
508
    /* Output tag+length followed by content octets */
509
376M
    if (out) {
510
77.8M
        if (usetag)
511
61.0M
            ASN1_put_object(out, ndef, len, tag, aclass);
512
77.8M
        asn1_ex_i2c(pval, *out, &utype, it);
513
77.8M
        if (ndef)
514
0
            ASN1_put_eoc(out);
515
77.8M
        else
516
77.8M
            *out += len;
517
77.8M
    }
518
519
376M
    if (usetag)
520
275M
        return ASN1_object_size(ndef, len, tag);
521
101M
    return len;
522
376M
}
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
470M
{
529
470M
    ASN1_BOOLEAN *tbool = NULL;
530
470M
    ASN1_STRING *strtmp;
531
470M
    ASN1_OBJECT *otmp;
532
470M
    int utype;
533
470M
    const unsigned char *cont;
534
470M
    unsigned char c;
535
470M
    int len;
536
470M
    const ASN1_PRIMITIVE_FUNCS *pf;
537
470M
    pf = it->funcs;
538
470M
    if (pf && pf->prim_i2c)
539
3.34M
        return pf->prim_i2c(pval, cout, putype, it);
540
541
    /* Should type be omitted? */
542
467M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE)
543
465M
        || (it->utype != V_ASN1_BOOLEAN)) {
544
465M
        if (*pval == NULL)
545
14.8M
            return -1;
546
465M
    }
547
548
452M
    if (it->itype == ASN1_ITYPE_MSTRING) {
549
        /* If MSTRING type set the underlying type */
550
133M
        strtmp = (ASN1_STRING *)*pval;
551
133M
        utype = strtmp->type;
552
133M
        *putype = utype;
553
319M
    } else if (it->utype == V_ASN1_ANY) {
554
        /* If ANY set type and pointer to value */
555
143M
        ASN1_TYPE *typ;
556
143M
        typ = (ASN1_TYPE *)*pval;
557
143M
        utype = typ->type;
558
143M
        *putype = utype;
559
143M
        pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */
560
143M
    } else
561
176M
        utype = *putype;
562
563
452M
    switch (utype) {
564
144M
    case V_ASN1_OBJECT:
565
144M
        otmp = (ASN1_OBJECT *)*pval;
566
144M
        cont = otmp->data;
567
144M
        len = otmp->length;
568
144M
        if (cont == NULL || len == 0)
569
0
            return -1;
570
144M
        break;
571
572
144M
    case V_ASN1_UNDEF:
573
0
        return -2;
574
575
3.06M
    case V_ASN1_NULL:
576
3.06M
        cont = NULL;
577
3.06M
        len = 0;
578
3.06M
        break;
579
580
1.73M
    case V_ASN1_BOOLEAN:
581
1.73M
        tbool = (ASN1_BOOLEAN *)pval;
582
1.73M
        if (*tbool == -1)
583
325k
            return -1;
584
1.41M
        if (it->utype != V_ASN1_ANY) {
585
            /*
586
             * Default handling if value == size field then omit
587
             */
588
1.26M
            if (*tbool && (it->size > 0))
589
48
                return -1;
590
1.26M
            if (!*tbool && !it->size)
591
1.18M
                return -1;
592
1.26M
        }
593
223k
        c = (unsigned char)*tbool;
594
223k
        cont = &c;
595
223k
        len = 1;
596
223k
        break;
597
598
25.5M
    case V_ASN1_BIT_STRING:
599
25.5M
        return ossl_i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
600
25.5M
            cout ? &cout : NULL);
601
602
21.0M
    case V_ASN1_INTEGER:
603
21.1M
    case V_ASN1_ENUMERATED:
604
        /*
605
         * These are all have the same content format as ASN1_INTEGER
606
         */
607
21.1M
        return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
608
609
3.65M
    case V_ASN1_OCTET_STRING:
610
24.1M
    case V_ASN1_NUMERICSTRING:
611
25.1M
    case V_ASN1_PRINTABLESTRING:
612
27.2M
    case V_ASN1_T61STRING:
613
27.2M
    case V_ASN1_VIDEOTEXSTRING:
614
29.4M
    case V_ASN1_IA5STRING:
615
29.5M
    case V_ASN1_UTCTIME:
616
29.8M
    case V_ASN1_GENERALIZEDTIME:
617
34.4M
    case V_ASN1_GRAPHICSTRING:
618
34.5M
    case V_ASN1_VISIBLESTRING:
619
34.7M
    case V_ASN1_GENERALSTRING:
620
36.0M
    case V_ASN1_UNIVERSALSTRING:
621
37.2M
    case V_ASN1_BMPSTRING:
622
88.3M
    case V_ASN1_UTF8STRING:
623
107M
    case V_ASN1_SEQUENCE:
624
185M
    case V_ASN1_SET:
625
256M
    default:
626
        /* All based on ASN1_STRING and handled the same */
627
256M
        strtmp = (ASN1_STRING *)*pval;
628
        /* Special handling for NDEF */
629
256M
        if ((it->size == ASN1_TFLG_NDEF)
630
997
            && (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
256M
        cont = strtmp->data;
639
256M
        len = strtmp->length;
640
641
256M
        break;
642
452M
    }
643
404M
    if (cout && len)
644
47.1M
        memcpy(cout, cont, len);
645
404M
    return len;
646
452M
}