Coverage Report

Created: 2023-06-08 06:40

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