Coverage Report

Created: 2023-09-25 06:43

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