Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/tasn_enc.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <string.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/asn1.h>
14
#include <openssl/asn1t.h>
15
#include <openssl/objects.h>
16
#include "crypto/asn1.h"
17
#include "asn1_local.h"
18
19
static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out,
20
    const ASN1_ITEM *it, int tag, int aclass);
21
static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk,
22
    unsigned char **out,
23
    int skcontlen, const ASN1_ITEM *item,
24
    int do_sort, int iclass);
25
static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
26
    const ASN1_TEMPLATE *tt, int tag, int aclass);
27
static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
28
    const ASN1_ITEM *it, int flags);
29
static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype,
30
    const ASN1_ITEM *it);
31
32
/*
33
 * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
34
 * indefinite length constructed encoding, where appropriate
35
 */
36
37
int ASN1_item_ndef_i2d(const ASN1_VALUE *val, unsigned char **out,
38
    const ASN1_ITEM *it)
39
0
{
40
0
    return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
41
0
}
42
43
int ASN1_item_i2d(const ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
44
1.16M
{
45
1.16M
    return asn1_item_flags_i2d(val, out, it, 0);
46
1.16M
}
47
48
/*
49
 * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
50
 * points to a buffer to output the data to. The new i2d has one additional
51
 * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
52
 * allocated and populated with the encoding.
53
 */
54
55
static int asn1_item_flags_i2d(const ASN1_VALUE *val, unsigned char **out,
56
    const ASN1_ITEM *it, int flags)
57
391k
{
58
391k
    if (out != NULL && *out == NULL) {
59
318k
        unsigned char *p, *buf;
60
318k
        int len, outlen;
61
62
318k
        len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
63
318k
        if (len <= 0)
64
186
            return len;
65
318k
        if ((buf = OPENSSL_malloc(len)) == NULL)
66
0
            return -1;
67
318k
        p = buf;
68
318k
        outlen = ASN1_item_ex_i2d(&val, &p, it, -1, flags);
69
318k
        if (outlen != len || p != buf + len) {
70
0
            OPENSSL_free(buf);
71
0
            return -1;
72
0
        }
73
318k
        *out = buf;
74
318k
        return len;
75
318k
    }
76
77
72.9k
    return ASN1_item_ex_i2d(&val, out, it, -1, flags);
78
391k
}
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
178M
{
88
178M
    const ASN1_TEMPLATE *tt = NULL;
89
178M
    int i, seqcontlen, seqlen, ndef = 1;
90
178M
    const ASN1_EXTERN_FUNCS *ef;
91
178M
    const ASN1_AUX *aux = it->funcs;
92
93
178M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL)
94
2.26M
        return 0;
95
96
176M
    switch (it->itype) {
97
98
115M
    case ASN1_ITYPE_PRIMITIVE:
99
115M
        if (it->templates)
100
7.90M
            return asn1_template_ex_i2d(pval, out, it->templates,
101
7.90M
                tag, aclass);
102
107M
        return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
103
104
27.6M
    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
27.6M
        if (tag != -1) {
110
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
111
0
            return -1;
112
0
        }
113
27.6M
        return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
114
115
7.06M
    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
7.06M
        if (tag != -1) {
121
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
122
0
            return -1;
123
0
        }
124
7.06M
        if (!ossl_asn1_call_aux_cb(aux, ASN1_OP_I2D_PRE, pval, it, NULL))
125
0
            return 0;
126
7.06M
        i = ossl_asn1_get_choice_selector_const(pval, it);
127
7.06M
        if ((i >= 0) && (i < it->tcount)) {
128
7.06M
            const ASN1_VALUE **pchval;
129
7.06M
            const ASN1_TEMPLATE *chtt;
130
7.06M
            chtt = it->templates + i;
131
7.06M
            pchval = ossl_asn1_get_const_field_ptr(pval, chtt);
132
7.06M
            return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
133
7.06M
        }
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
682k
    case ASN1_ITYPE_EXTERN:
140
        /* If new style i2d it does all the work */
141
682k
        ef = it->funcs;
142
682k
        return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
143
144
30.3k
    case ASN1_ITYPE_NDEF_SEQUENCE:
145
        /* Use indefinite length constructed if requested */
146
30.3k
        if (aclass & ASN1_TFLG_NDEF)
147
0
            ndef = 2;
148
        /* fall through */
149
150
25.7M
    case ASN1_ITYPE_SEQUENCE:
151
25.7M
        i = ossl_asn1_enc_restore(&seqcontlen, out, pval, it);
152
        /* An error occurred */
153
25.7M
        if (i < 0)
154
0
            return 0;
155
        /* We have a valid cached encoding... */
156
25.7M
        if (i > 0)
157
381k
            return seqcontlen;
158
        /* Otherwise carry on */
159
25.3M
        seqcontlen = 0;
160
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
161
25.3M
        if (tag == -1) {
162
25.0M
            tag = V_ASN1_SEQUENCE;
163
            /* Retain any other flags in aclass */
164
25.0M
            aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
165
25.0M
                | V_ASN1_UNIVERSAL;
166
25.0M
        }
167
25.3M
        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
83.6M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
171
58.2M
            const ASN1_TEMPLATE *seqtt;
172
58.2M
            const ASN1_VALUE **pseqval;
173
58.2M
            int tmplen;
174
58.2M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
175
58.2M
            if (!seqtt)
176
0
                return 0;
177
58.2M
            pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
178
58.2M
            tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
179
58.2M
            if (tmplen < 0 || (tmplen > INT_MAX - seqcontlen))
180
184
                return -1;
181
58.2M
            seqcontlen += tmplen;
182
58.2M
        }
183
184
25.3M
        seqlen = ASN1_object_size(ndef, seqcontlen, tag);
185
25.3M
        if (!out || seqlen == -1)
186
17.6M
            return seqlen;
187
        /* Output SEQUENCE header */
188
7.70M
        ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
189
24.5M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
190
16.8M
            const ASN1_TEMPLATE *seqtt;
191
16.8M
            const ASN1_VALUE **pseqval;
192
16.8M
            int tmplen;
193
194
16.8M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
195
16.8M
            if (!seqtt)
196
0
                return 0;
197
16.8M
            pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt);
198
16.8M
            tmplen = asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
199
16.8M
            if (tmplen < 0)
200
0
                return -1;
201
16.8M
        }
202
7.70M
        if (ndef == 2)
203
0
            ASN1_put_eoc(out);
204
7.70M
        if (!ossl_asn1_call_aux_cb(aux, ASN1_OP_I2D_POST, pval, it, NULL))
205
0
            return 0;
206
7.70M
        return seqlen;
207
208
0
    default:
209
0
        return 0;
210
176M
    }
211
0
    return 0;
212
176M
}
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
90.0M
{
217
90.0M
    const int flags = tt->flags;
218
90.0M
    int i, ret, ttag, tclass, ndef, len;
219
90.0M
    const ASN1_VALUE *tval;
220
90.0M
    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
90.0M
    if (flags & ASN1_TFLG_EMBED) {
227
2.40M
        tval = (ASN1_VALUE *)pval;
228
2.40M
        pval = &tval;
229
2.40M
    }
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
90.0M
    if (flags & ASN1_TFLG_TAG_MASK) {
238
        /* Error if argument and template tagging */
239
15.5M
        if (tag != -1)
240
            /* FIXME: error code here */
241
0
            return -1;
242
        /* Get tagging from template */
243
15.5M
        ttag = tt->tag;
244
15.5M
        tclass = flags & ASN1_TFLG_TAG_CLASS;
245
74.5M
    } else if (tag != -1) {
246
        /* No template tagging, get from arguments */
247
0
        ttag = tag;
248
0
        tclass = iclass & ASN1_TFLG_TAG_CLASS;
249
74.5M
    } else {
250
74.5M
        ttag = -1;
251
74.5M
        tclass = 0;
252
74.5M
    }
253
    /*
254
     * Remove any class mask from iflag.
255
     */
256
90.0M
    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
90.0M
    if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
265
0
        ndef = 2;
266
90.0M
    else
267
90.0M
        ndef = 1;
268
269
90.0M
    if (flags & ASN1_TFLG_SK_MASK) {
270
        /* SET OF, SEQUENCE OF */
271
10.8M
        STACK_OF(const_ASN1_VALUE) *sk = (STACK_OF(const_ASN1_VALUE) *)*pval;
272
10.8M
        int isset, sktag, skaclass;
273
10.8M
        int skcontlen, sklen;
274
10.8M
        const ASN1_VALUE *skitem;
275
276
10.8M
        if (*pval == NULL)
277
2.00M
            return 0;
278
279
8.84M
        if (flags & ASN1_TFLG_SET_OF) {
280
7.88M
            isset = 1;
281
            /* 2 means we reorder */
282
7.88M
            if (flags & ASN1_TFLG_SEQUENCE_OF)
283
486
                isset = 2;
284
7.88M
        } else
285
964k
            isset = 0;
286
287
        /*
288
         * Work out inner tag value: if EXPLICIT or no tagging use underlying
289
         * type.
290
         */
291
8.84M
        if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
292
444k
            sktag = ttag;
293
444k
            skaclass = tclass;
294
8.40M
        } else {
295
8.40M
            skaclass = V_ASN1_UNIVERSAL;
296
8.40M
            if (isset)
297
7.86M
                sktag = V_ASN1_SET;
298
539k
            else
299
539k
                sktag = V_ASN1_SEQUENCE;
300
8.40M
        }
301
302
        /* Determine total length of items */
303
8.84M
        skcontlen = 0;
304
82.5M
        for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
305
73.7M
            skitem = sk_const_ASN1_VALUE_value(sk, i);
306
73.7M
            len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
307
73.7M
                -1, iclass);
308
73.7M
            if (len < 0 || (skcontlen > INT_MAX - len))
309
86
                return -1;
310
73.7M
            if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
311
6
                ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
312
6
                return -1;
313
6
            }
314
73.7M
            skcontlen += len;
315
73.7M
        }
316
8.84M
        sklen = ASN1_object_size(ndef, skcontlen, sktag);
317
8.84M
        if (sklen == -1)
318
0
            return -1;
319
        /* If EXPLICIT need length of surrounding tag */
320
8.84M
        if (flags & ASN1_TFLG_EXPTAG)
321
32.8k
            ret = ASN1_object_size(ndef, sklen, ttag);
322
8.81M
        else
323
8.81M
            ret = sklen;
324
325
8.84M
        if (!out || ret == -1)
326
5.19M
            return ret;
327
328
        /* Now encode this lot... */
329
        /* EXPLICIT tag */
330
3.65M
        if (flags & ASN1_TFLG_EXPTAG)
331
3.29k
            ASN1_put_object(out, ndef, sklen, ttag, tclass);
332
        /* SET or SEQUENCE and IMPLICIT tag */
333
3.65M
        ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
334
        /* And the stuff itself */
335
3.65M
        if (!asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
336
3.65M
                isset, iclass))
337
0
            return -1;
338
3.65M
        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
3.65M
        return ret;
345
3.65M
    }
346
347
79.1M
    if (flags & ASN1_TFLG_EXPTAG) {
348
        /* EXPLICIT tagging */
349
        /* Find length of tagged item */
350
3.80M
        i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
351
3.80M
        if (i < 0)
352
21
            return -1;
353
3.80M
        if (i == 0) {
354
2.75M
            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
2.75M
            return 0;
359
2.75M
        }
360
        /* Find length of EXPLICIT tag */
361
1.04M
        ret = ASN1_object_size(ndef, i, ttag);
362
1.04M
        if (out && ret != -1) {
363
            /* Output tag and item */
364
209k
            ASN1_put_object(out, ndef, i, ttag, tclass);
365
209k
            p = *out;
366
209k
            len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1,
367
209k
                iclass);
368
209k
            if (len != i || *out != p + i)
369
0
                return -1;
370
209k
            if (ndef == 2)
371
0
                ASN1_put_eoc(out);
372
209k
        }
373
1.04M
        return ret;
374
1.04M
    }
375
376
    /* Either normal or IMPLICIT tagging: combine class and flags */
377
75.3M
    len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
378
75.3M
        ttag, tclass | iclass);
379
75.3M
    if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) {
380
142
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
381
142
        return -1;
382
142
    }
383
75.3M
    return len;
384
75.3M
}
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
363M
{
396
363M
    const DER_ENC *d1 = a, *d2 = b;
397
363M
    int cmplen, i;
398
363M
    cmplen = (d1->length < d2->length) ? d1->length : d2->length;
399
363M
    i = memcmp(d1->data, d2->data, cmplen);
400
363M
    if (i)
401
169M
        return i;
402
194M
    return d1->length - d2->length;
403
363M
}
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
3.65M
{
412
3.65M
    int i, ret = 0;
413
3.65M
    const ASN1_VALUE *skitem;
414
3.65M
    unsigned char *tmpdat = NULL, *p = NULL;
415
3.65M
    DER_ENC *derlst = NULL, *tder;
416
417
3.65M
    if (do_sort) {
418
        /* Don't need to sort less than 2 items */
419
3.43M
        if (sk_const_ASN1_VALUE_num(sk) < 2)
420
3.31M
            do_sort = 0;
421
122k
        else {
422
122k
            derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk)
423
122k
                * sizeof(*derlst));
424
122k
            if (derlst == NULL)
425
0
                return 0;
426
122k
            tmpdat = OPENSSL_malloc(skcontlen);
427
122k
            if (tmpdat == NULL)
428
0
                goto err;
429
122k
        }
430
3.43M
    }
431
    /* If not sorting just output each item */
432
3.65M
    if (!do_sort) {
433
11.8M
        for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) {
434
8.27M
            skitem = sk_const_ASN1_VALUE_value(sk, i);
435
8.27M
            if (ASN1_item_ex_i2d(&skitem, out, item, -1, iclass) <= 0)
436
0
                return 0;
437
8.27M
        }
438
3.53M
        return 1;
439
3.53M
    }
440
122k
    p = tmpdat;
441
442
    /* Doing sort: build up a list of each member's DER encoding */
443
10.7M
    for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
444
10.5M
        skitem = sk_const_ASN1_VALUE_value(sk, i);
445
10.5M
        tder->data = p;
446
10.5M
        tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
447
10.5M
        if (tder->length <= 0)
448
0
            goto err;
449
10.5M
        tder->field = skitem;
450
10.5M
    }
451
452
    /* Now sort them */
453
122k
    qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
454
    /* Output sorted DER encoding */
455
122k
    p = *out;
456
10.7M
    for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) {
457
10.5M
        memcpy(p, tder->data, tder->length);
458
10.5M
        p += tder->length;
459
10.5M
    }
460
122k
    *out = p;
461
    /* If do_sort is 2 then reorder the STACK */
462
122k
    if (do_sort == 2) {
463
735
        for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++)
464
663
            (void)sk_const_ASN1_VALUE_set(sk, i, tder->field);
465
72
    }
466
122k
    ret = 1;
467
122k
err:
468
122k
    OPENSSL_free(derlst);
469
122k
    OPENSSL_free(tmpdat);
470
122k
    return ret;
471
122k
}
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
382M
{
476
382M
    int len;
477
382M
    int utype;
478
382M
    int usetag;
479
382M
    int ndef = 0;
480
481
382M
    utype = it->utype;
482
483
    /*
484
     * Get length of content octets and maybe find out the underlying type.
485
     */
486
487
382M
    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
382M
    if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
495
101M
        usetag = 0;
496
280M
    else
497
280M
        usetag = 1;
498
499
    /* -1 means omit type */
500
501
382M
    if (len == -1)
502
16.8M
        return 0;
503
504
    /* -2 return is special meaning use ndef */
505
365M
    if (len == -2) {
506
16
        ndef = 2;
507
16
        len = 0;
508
16
    }
509
510
    /* If not implicitly tagged get tag from underlying type */
511
365M
    if (tag == -1)
512
356M
        tag = utype;
513
514
    /* Output tag+length followed by content octets */
515
365M
    if (out) {
516
71.5M
        if (usetag)
517
55.1M
            ASN1_put_object(out, ndef, len, tag, aclass);
518
71.5M
        asn1_ex_i2c(pval, *out, &utype, it);
519
71.5M
        if (ndef)
520
2
            ASN1_put_eoc(out);
521
71.5M
        else
522
71.5M
            *out += len;
523
71.5M
    }
524
525
365M
    if (usetag)
526
263M
        return ASN1_object_size(ndef, len, tag);
527
101M
    return len;
528
365M
}
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
453M
{
535
453M
    ASN1_BOOLEAN *tbool = NULL;
536
453M
    ASN1_STRING *strtmp;
537
453M
    ASN1_OBJECT *otmp;
538
453M
    int utype;
539
453M
    const unsigned char *cont;
540
453M
    unsigned char c;
541
453M
    int len;
542
453M
    const ASN1_PRIMITIVE_FUNCS *pf;
543
453M
    pf = it->funcs;
544
453M
    if (pf && pf->prim_i2c)
545
1.99M
        return pf->prim_i2c(pval, cout, putype, it);
546
547
    /* Should type be omitted? */
548
451M
    if ((it->itype != ASN1_ITYPE_PRIMITIVE)
549
450M
        || (it->utype != V_ASN1_BOOLEAN)) {
550
450M
        if (*pval == NULL)
551
15.1M
            return -1;
552
450M
    }
553
554
436M
    if (it->itype == ASN1_ITYPE_MSTRING) {
555
        /* If MSTRING type set the underlying type */
556
103M
        strtmp = (ASN1_STRING *)*pval;
557
103M
        utype = strtmp->type;
558
103M
        *putype = utype;
559
333M
    } else if (it->utype == V_ASN1_ANY) {
560
        /* If ANY set type and pointer to value */
561
182M
        ASN1_TYPE *typ;
562
182M
        typ = (ASN1_TYPE *)*pval;
563
182M
        utype = typ->type;
564
182M
        *putype = utype;
565
182M
        pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */
566
182M
    } else
567
150M
        utype = *putype;
568
569
436M
    switch (utype) {
570
117M
    case V_ASN1_OBJECT:
571
117M
        otmp = (ASN1_OBJECT *)*pval;
572
117M
        cont = otmp->data;
573
117M
        len = otmp->length;
574
117M
        if (cont == NULL || len == 0)
575
154
            return -1;
576
117M
        break;
577
578
117M
    case V_ASN1_UNDEF:
579
18
        return -2;
580
581
2.28M
    case V_ASN1_NULL:
582
2.28M
        cont = NULL;
583
2.28M
        len = 0;
584
2.28M
        break;
585
586
2.00M
    case V_ASN1_BOOLEAN:
587
2.00M
        tbool = (ASN1_BOOLEAN *)pval;
588
2.00M
        if (*tbool == -1)
589
0
            return -1;
590
2.00M
        if (it->utype != V_ASN1_ANY) {
591
            /*
592
             * Default handling if value == size field then omit
593
             */
594
1.67M
            if (*tbool && (it->size > 0))
595
8.67k
                return -1;
596
1.66M
            if (!*tbool && !it->size)
597
1.60M
                return -1;
598
1.66M
        }
599
387k
        c = (unsigned char)*tbool;
600
387k
        cont = &c;
601
387k
        len = 1;
602
387k
        break;
603
604
11.1M
    case V_ASN1_BIT_STRING:
605
11.1M
        return ossl_i2c_ASN1_BIT_STRING((const ASN1_BIT_STRING *)*pval,
606
11.1M
            cout ? &cout : NULL);
607
608
39.9M
    case V_ASN1_INTEGER:
609
40.1M
    case V_ASN1_ENUMERATED:
610
        /*
611
         * These are all have the same content format as ASN1_INTEGER
612
         */
613
40.1M
        return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
614
615
4.29M
    case V_ASN1_OCTET_STRING:
616
41.3M
    case V_ASN1_NUMERICSTRING:
617
41.8M
    case V_ASN1_PRINTABLESTRING:
618
47.4M
    case V_ASN1_T61STRING:
619
47.4M
    case V_ASN1_VIDEOTEXSTRING:
620
50.9M
    case V_ASN1_IA5STRING:
621
51.0M
    case V_ASN1_UTCTIME:
622
51.8M
    case V_ASN1_GENERALIZEDTIME:
623
58.4M
    case V_ASN1_GRAPHICSTRING:
624
58.4M
    case V_ASN1_VISIBLESTRING:
625
58.6M
    case V_ASN1_GENERALSTRING:
626
61.0M
    case V_ASN1_UNIVERSALSTRING:
627
62.5M
    case V_ASN1_BMPSTRING:
628
108M
    case V_ASN1_UTF8STRING:
629
129M
    case V_ASN1_SEQUENCE:
630
205M
    case V_ASN1_SET:
631
263M
    default:
632
        /* All based on ASN1_STRING and handled the same */
633
263M
        strtmp = (ASN1_STRING *)*pval;
634
        /* Special handling for NDEF */
635
263M
        if ((it->size == ASN1_TFLG_NDEF)
636
936
            && (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
263M
        cont = strtmp->data;
645
263M
        len = strtmp->length;
646
647
263M
        break;
648
436M
    }
649
383M
    if (cout && len)
650
42.8M
        memcpy(cout, cont, len);
651
383M
    return len;
652
436M
}