Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/asn1/a_int.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "internal/numbers.h"
13
#include <limits.h>
14
#include <openssl/asn1.h>
15
#include <openssl/bn.h>
16
#include "asn1_local.h"
17
18
ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
19
0
{
20
0
    return ASN1_STRING_dup(x);
21
0
}
22
23
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
24
0
{
25
0
    int neg, ret;
26
    /* Compare signs */
27
0
    neg = x->type & V_ASN1_NEG;
28
0
    if (neg != (y->type & V_ASN1_NEG)) {
29
0
        if (neg)
30
0
            return -1;
31
0
        else
32
0
            return 1;
33
0
    }
34
35
0
    ret = ASN1_STRING_cmp(x, y);
36
37
0
    if (neg)
38
0
        return -ret;
39
0
    else
40
0
        return ret;
41
0
}
42
43
/*-
44
 * This converts a big endian buffer and sign into its content encoding.
45
 * This is used for INTEGER and ENUMERATED types.
46
 * The internal representation is an ASN1_STRING whose data is a big endian
47
 * representation of the value, ignoring the sign. The sign is determined by
48
 * the type: if type & V_ASN1_NEG is true it is negative, otherwise positive.
49
 *
50
 * Positive integers are no problem: they are almost the same as the DER
51
 * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
52
 *
53
 * Negative integers are a bit trickier...
54
 * The DER representation of negative integers is in 2s complement form.
55
 * The internal form is converted by complementing each octet and finally
56
 * adding one to the result. This can be done less messily with a little trick.
57
 * If the internal form has trailing zeroes then they will become FF by the
58
 * complement and 0 by the add one (due to carry) so just copy as many trailing
59
 * zeros to the destination as there are in the source. The carry will add one
60
 * to the last none zero octet: so complement this octet and add one and finally
61
 * complement any left over until you get to the start of the string.
62
 *
63
 * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
64
 * with 0xff. However if the first byte is 0x80 and one of the following bytes
65
 * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
66
 * followed by optional zeros isn't padded.
67
 */
68
69
/*
70
 * If |pad| is zero, the operation is effectively reduced to memcpy,
71
 * and if |pad| is 0xff, then it performs two's complement, ~dst + 1.
72
 * Note that in latter case sequence of zeros yields itself, and so
73
 * does 0x80 followed by any number of zeros. These properties are
74
 * used elsewhere below...
75
 */
76
static void twos_complement(unsigned char *dst, const unsigned char *src,
77
                            size_t len, unsigned char pad)
78
372k
{
79
372k
    unsigned int carry = pad & 1;
80
81
    /* Begin at the end of the encoding */
82
372k
    dst += len;
83
372k
    src += len;
84
    /* two's complement value: ~value + 1 */
85
40.5M
    while (len-- != 0) {
86
40.1M
        *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
87
40.1M
        carry >>= 8;
88
40.1M
    }
89
372k
}
90
91
static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
92
                       unsigned char **pp)
93
162k
{
94
162k
    unsigned int pad = 0;
95
162k
    size_t ret, i;
96
162k
    unsigned char *p, pb = 0;
97
98
162k
    if (b != NULL && blen) {
99
162k
        ret = blen;
100
162k
        i = b[0];
101
162k
        if (!neg && (i > 127)) {
102
8.78k
            pad = 1;
103
8.78k
            pb = 0;
104
154k
        } else if (neg) {
105
44.1k
            pb = 0xFF;
106
44.1k
            if (i > 128) {
107
8.39k
                pad = 1;
108
35.7k
            } else if (i == 128) {
109
                /*
110
                 * Special case [of minimal negative for given length]:
111
                 * if any other bytes non zero we pad, otherwise we don't.
112
                 */
113
26.8k
                for (pad = 0, i = 1; i < blen; i++)
114
16.1k
                    pad |= b[i];
115
10.7k
                pb = pad != 0 ? 0xffU : 0;
116
10.7k
                pad = pb & 1;
117
10.7k
            }
118
44.1k
        }
119
162k
        ret += pad;
120
162k
    } else {
121
0
        ret = 1;
122
0
        blen = 0;   /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
123
0
    }
124
125
162k
    if (pp == NULL || (p = *pp) == NULL)
126
128k
        return ret;
127
128
    /*
129
     * This magically handles all corner cases, such as '(b == NULL ||
130
     * blen == 0)', non-negative value, "negative" zero, 0x80 followed
131
     * by any number of zeros...
132
     */
133
34.5k
    *p = pb;
134
34.5k
    p += pad;       /* yes, p[0] can be written twice, but it's little
135
                     * price to pay for eliminated branches */
136
34.5k
    twos_complement(p, b, blen, pb);
137
138
34.5k
    *pp += ret;
139
34.5k
    return ret;
140
162k
}
141
142
/*
143
 * convert content octets into a big endian buffer. Returns the length
144
 * of buffer or 0 on error: for malformed INTEGER. If output buffer is
145
 * NULL just return length.
146
 */
147
148
static size_t c2i_ibuf(unsigned char *b, int *pneg,
149
                       const unsigned char *p, size_t plen)
150
1.07M
{
151
1.07M
    int neg, pad;
152
    /* Zero content length is illegal */
153
1.07M
    if (plen == 0) {
154
27.7k
        ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
155
27.7k
        return 0;
156
27.7k
    }
157
1.04M
    neg = p[0] & 0x80;
158
1.04M
    if (pneg)
159
518k
        *pneg = neg;
160
    /* Handle common case where length is 1 octet separately */
161
1.04M
    if (plen == 1) {
162
361k
        if (b != NULL) {
163
180k
            if (neg)
164
36.3k
                b[0] = (p[0] ^ 0xFF) + 1;
165
144k
            else
166
144k
                b[0] = p[0];
167
180k
        }
168
361k
        return 1;
169
361k
    }
170
171
684k
    pad = 0;
172
684k
    if (p[0] == 0) {
173
43.5k
        pad = 1;
174
641k
    } else if (p[0] == 0xFF) {
175
19.8k
        size_t i;
176
177
        /*
178
         * Special case [of "one less minimal negative" for given length]:
179
         * if any other bytes non zero it was padded, otherwise not.
180
         */
181
11.4M
        for (pad = 0, i = 1; i < plen; i++)
182
11.3M
            pad |= p[i];
183
19.8k
        pad = pad != 0 ? 1 : 0;
184
19.8k
    }
185
    /* reject illegal padding: first two octets MSB can't match */
186
684k
    if (pad && (neg == (p[1] & 0x80))) {
187
7.45k
        ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
188
7.45k
        return 0;
189
7.45k
    }
190
191
    /* skip over pad */
192
677k
    p += pad;
193
677k
    plen -= pad;
194
195
677k
    if (b != NULL)
196
338k
        twos_complement(b, p, plen, neg ? 0xffU : 0);
197
198
677k
    return plen;
199
684k
}
200
201
int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
202
120k
{
203
120k
    return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
204
120k
}
205
206
/* Convert big endian buffer into uint64_t, return 0 on error */
207
static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
208
57.0k
{
209
57.0k
    size_t i;
210
57.0k
    uint64_t r;
211
212
57.0k
    if (blen > sizeof(*pr)) {
213
26
        ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
214
26
        return 0;
215
26
    }
216
57.0k
    if (b == NULL)
217
0
        return 0;
218
180k
    for (r = 0, i = 0; i < blen; i++) {
219
123k
        r <<= 8;
220
123k
        r |= b[i];
221
123k
    }
222
57.0k
    *pr = r;
223
57.0k
    return 1;
224
57.0k
}
225
226
/*
227
 * Write uint64_t to big endian buffer and return offset to first
228
 * written octet. In other words it returns offset in range from 0
229
 * to 7, with 0 denoting 8 written octets and 7 - one.
230
 */
231
static size_t asn1_put_uint64(unsigned char b[sizeof(uint64_t)], uint64_t r)
232
42.0k
{
233
42.0k
    size_t off = sizeof(uint64_t);
234
235
80.4k
    do {
236
80.4k
        b[--off] = (unsigned char)r;
237
80.4k
    } while (r >>= 8);
238
239
42.0k
    return off;
240
42.0k
}
241
242
/*
243
 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as gcc produces
244
 * overflow warnings.
245
 */
246
636
#define ABS_INT64_MIN ((uint64_t)INT64_MAX + (-(INT64_MIN + INT64_MAX)))
247
248
/* signed version of asn1_get_uint64 */
249
static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
250
                          int neg)
251
4.40k
{
252
4.40k
    uint64_t r;
253
4.40k
    if (asn1_get_uint64(&r, b, blen) == 0)
254
26
        return 0;
255
4.37k
    if (neg) {
256
1.39k
        if (r <= INT64_MAX) {
257
            /* Most significant bit is guaranteed to be clear, negation
258
             * is guaranteed to be meaningful in platform-neutral sense. */
259
757
            *pr = -(int64_t)r;
260
757
        } else if (r == ABS_INT64_MIN) {
261
            /* This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
262
             * on ones'-complement system. */
263
356
            *pr = (int64_t)(0 - r);
264
356
        } else {
265
280
            ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
266
280
            return 0;
267
280
        }
268
2.98k
    } else {
269
2.98k
        if (r <= INT64_MAX) {
270
2.71k
            *pr = (int64_t)r;
271
2.71k
        } else {
272
271
            ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
273
271
            return 0;
274
271
        }
275
2.98k
    }
276
3.82k
    return 1;
277
4.37k
}
278
279
/* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
280
ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
281
                               long len)
282
500k
{
283
500k
    ASN1_INTEGER *ret = NULL;
284
500k
    size_t r;
285
500k
    int neg;
286
287
500k
    r = c2i_ibuf(NULL, NULL, *pp, len);
288
289
500k
    if (r == 0)
290
33.8k
        return NULL;
291
292
466k
    if ((a == NULL) || ((*a) == NULL)) {
293
324k
        ret = ASN1_INTEGER_new();
294
324k
        if (ret == NULL)
295
0
            return NULL;
296
324k
        ret->type = V_ASN1_INTEGER;
297
324k
    } else
298
141k
        ret = *a;
299
300
466k
    if (ASN1_STRING_set(ret, NULL, r) == 0)
301
0
        goto err;
302
303
466k
    c2i_ibuf(ret->data, &neg, *pp, len);
304
305
466k
    if (neg)
306
89.7k
        ret->type |= V_ASN1_NEG;
307
308
466k
    *pp += len;
309
466k
    if (a != NULL)
310
466k
        (*a) = ret;
311
466k
    return ret;
312
0
 err:
313
0
    ASN1err(ASN1_F_C2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
314
0
    if ((a == NULL) || (*a != ret))
315
0
        ASN1_INTEGER_free(ret);
316
0
    return NULL;
317
466k
}
318
319
static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
320
4.40k
{
321
4.40k
    if (a == NULL) {
322
0
        ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
323
0
        return 0;
324
0
    }
325
4.40k
    if ((a->type & ~V_ASN1_NEG) != itype) {
326
0
        ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ASN1_R_WRONG_INTEGER_TYPE);
327
0
        return 0;
328
0
    }
329
4.40k
    return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
330
4.40k
}
331
332
static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
333
31
{
334
31
    unsigned char tbuf[sizeof(r)];
335
31
    size_t off;
336
337
31
    a->type = itype;
338
31
    if (r < 0) {
339
        /* Most obvious '-r' triggers undefined behaviour for most
340
         * common INT64_MIN. Even though below '0 - (uint64_t)r' can
341
         * appear two's-complement centric, it does produce correct/
342
         * expected result even on one's-complement. This is because
343
         * cast to unsigned has to change bit pattern... */
344
0
        off = asn1_put_uint64(tbuf, 0 - (uint64_t)r);
345
0
        a->type |= V_ASN1_NEG;
346
31
    } else {
347
31
        off = asn1_put_uint64(tbuf, r);
348
31
        a->type &= ~V_ASN1_NEG;
349
31
    }
350
31
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
351
31
}
352
353
static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
354
                                  int itype)
355
0
{
356
0
    if (a == NULL) {
357
0
        ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ERR_R_PASSED_NULL_PARAMETER);
358
0
        return 0;
359
0
    }
360
0
    if ((a->type & ~V_ASN1_NEG) != itype) {
361
0
        ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_WRONG_INTEGER_TYPE);
362
0
        return 0;
363
0
    }
364
0
    if (a->type & V_ASN1_NEG) {
365
0
        ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
366
0
        return 0;
367
0
    }
368
0
    return asn1_get_uint64(pr, a->data, a->length);
369
0
}
370
371
static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
372
0
{
373
0
    unsigned char tbuf[sizeof(r)];
374
0
    size_t off;
375
376
0
    a->type = itype;
377
0
    off = asn1_put_uint64(tbuf, r);
378
0
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
379
0
}
380
381
/*
382
 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
383
 * integers: some broken software can encode a positive INTEGER with its MSB
384
 * set as negative (it doesn't add a padding zero).
385
 */
386
387
ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
388
                                long length)
389
0
{
390
0
    ASN1_INTEGER *ret = NULL;
391
0
    const unsigned char *p;
392
0
    unsigned char *s;
393
0
    long len;
394
0
    int inf, tag, xclass;
395
0
    int i;
396
397
0
    if ((a == NULL) || ((*a) == NULL)) {
398
0
        if ((ret = ASN1_INTEGER_new()) == NULL)
399
0
            return NULL;
400
0
        ret->type = V_ASN1_INTEGER;
401
0
    } else
402
0
        ret = (*a);
403
404
0
    p = *pp;
405
0
    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
406
0
    if (inf & 0x80) {
407
0
        i = ASN1_R_BAD_OBJECT_HEADER;
408
0
        goto err;
409
0
    }
410
411
0
    if (tag != V_ASN1_INTEGER) {
412
0
        i = ASN1_R_EXPECTING_AN_INTEGER;
413
0
        goto err;
414
0
    }
415
416
    /*
417
     * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
418
     * a missing NULL parameter.
419
     */
420
0
    s = OPENSSL_malloc((int)len + 1);
421
0
    if (s == NULL) {
422
0
        i = ERR_R_MALLOC_FAILURE;
423
0
        goto err;
424
0
    }
425
0
    ret->type = V_ASN1_INTEGER;
426
0
    if (len) {
427
0
        if ((*p == 0) && (len != 1)) {
428
0
            p++;
429
0
            len--;
430
0
        }
431
0
        memcpy(s, p, (int)len);
432
0
        p += len;
433
0
    }
434
435
0
    OPENSSL_free(ret->data);
436
0
    ret->data = s;
437
0
    ret->length = (int)len;
438
0
    if (a != NULL)
439
0
        (*a) = ret;
440
0
    *pp = p;
441
0
    return ret;
442
0
 err:
443
0
    ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
444
0
    if ((a == NULL) || (*a != ret))
445
0
        ASN1_INTEGER_free(ret);
446
0
    return NULL;
447
0
}
448
449
static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
450
                                      int atype)
451
309
{
452
309
    ASN1_INTEGER *ret;
453
309
    int len;
454
455
309
    if (ai == NULL) {
456
206
        ret = ASN1_STRING_type_new(atype);
457
206
    } else {
458
103
        ret = ai;
459
103
        ret->type = atype;
460
103
    }
461
462
309
    if (ret == NULL) {
463
0
        ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_NESTED_ASN1_ERROR);
464
0
        goto err;
465
0
    }
466
467
309
    if (BN_is_negative(bn) && !BN_is_zero(bn))
468
0
        ret->type |= V_ASN1_NEG_INTEGER;
469
470
309
    len = BN_num_bytes(bn);
471
472
309
    if (len == 0)
473
63
        len = 1;
474
475
309
    if (ASN1_STRING_set(ret, NULL, len) == 0) {
476
0
        ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_MALLOC_FAILURE);
477
0
        goto err;
478
0
    }
479
480
    /* Correct zero case */
481
309
    if (BN_is_zero(bn))
482
63
        ret->data[0] = 0;
483
246
    else
484
246
        len = BN_bn2bin(bn, ret->data);
485
309
    ret->length = len;
486
309
    return ret;
487
0
 err:
488
0
    if (ret != ai)
489
0
        ASN1_INTEGER_free(ret);
490
0
    return NULL;
491
309
}
492
493
static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
494
                                 int itype)
495
32.2k
{
496
32.2k
    BIGNUM *ret;
497
498
32.2k
    if ((ai->type & ~V_ASN1_NEG) != itype) {
499
101
        ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
500
101
        return NULL;
501
101
    }
502
503
32.1k
    ret = BN_bin2bn(ai->data, ai->length, bn);
504
32.1k
    if (ret == NULL) {
505
0
        ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
506
0
        return NULL;
507
0
    }
508
32.1k
    if (ai->type & V_ASN1_NEG)
509
5.04k
        BN_set_negative(ret, 1);
510
32.1k
    return ret;
511
32.1k
}
512
513
int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
514
1.90k
{
515
1.90k
    return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
516
1.90k
}
517
518
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
519
31
{
520
31
    return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
521
31
}
522
523
int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
524
0
{
525
0
    return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
526
0
}
527
528
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
529
0
{
530
0
    return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
531
0
}
532
533
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
534
31
{
535
31
    return ASN1_INTEGER_set_int64(a, v);
536
31
}
537
538
long ASN1_INTEGER_get(const ASN1_INTEGER *a)
539
1.90k
{
540
1.90k
    int i;
541
1.90k
    int64_t r;
542
1.90k
    if (a == NULL)
543
0
        return 0;
544
1.90k
    i = ASN1_INTEGER_get_int64(&r, a);
545
1.90k
    if (i == 0)
546
173
        return -1;
547
1.73k
    if (r > LONG_MAX || r < LONG_MIN)
548
0
        return -1;
549
1.73k
    return (long)r;
550
1.73k
}
551
552
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
553
309
{
554
309
    return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
555
309
}
556
557
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
558
32.0k
{
559
32.0k
    return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
560
32.0k
}
561
562
int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
563
2.49k
{
564
2.49k
    return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
565
2.49k
}
566
567
int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
568
0
{
569
0
    return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
570
0
}
571
572
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
573
0
{
574
0
    return ASN1_ENUMERATED_set_int64(a, v);
575
0
}
576
577
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
578
2.62k
{
579
2.62k
    int i;
580
2.62k
    int64_t r;
581
2.62k
    if (a == NULL)
582
0
        return 0;
583
2.62k
    if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
584
0
        return -1;
585
2.62k
    if (a->length > (int)sizeof(long))
586
128
        return 0xffffffffL;
587
2.49k
    i = ASN1_ENUMERATED_get_int64(&r, a);
588
2.49k
    if (i == 0)
589
404
        return -1;
590
2.09k
    if (r > LONG_MAX || r < LONG_MIN)
591
0
        return -1;
592
2.09k
    return (long)r;
593
2.09k
}
594
595
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
596
0
{
597
0
    return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
598
0
}
599
600
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
601
207
{
602
207
    return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
603
207
}
604
605
/* Internal functions used by x_int64.c */
606
int c2i_uint64_int(uint64_t *ret, int *neg, const unsigned char **pp, long len)
607
55.3k
{
608
55.3k
    unsigned char buf[sizeof(uint64_t)];
609
55.3k
    size_t buflen;
610
611
55.3k
    buflen = c2i_ibuf(NULL, NULL, *pp, len);
612
55.3k
    if (buflen == 0)
613
1.36k
        return 0;
614
53.9k
    if (buflen > sizeof(uint64_t)) {
615
1.33k
        ASN1err(ASN1_F_C2I_UINT64_INT, ASN1_R_TOO_LARGE);
616
1.33k
        return 0;
617
1.33k
    }
618
52.6k
    (void)c2i_ibuf(buf, neg, *pp, len);
619
52.6k
    return asn1_get_uint64(ret, buf, buflen);
620
53.9k
}
621
622
int i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
623
42.0k
{
624
42.0k
    unsigned char buf[sizeof(uint64_t)];
625
42.0k
    size_t off;
626
627
42.0k
    off = asn1_put_uint64(buf, r);
628
42.0k
    return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
629
42.0k
}
630