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
10.3k
{
79
10.3k
    unsigned int carry = pad & 1;
80
81
    /* Begin at the end of the encoding */
82
10.3k
    dst += len;
83
10.3k
    src += len;
84
    /* two's complement value: ~value + 1 */
85
574k
    while (len-- != 0) {
86
563k
        *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
87
563k
        carry >>= 8;
88
563k
    }
89
10.3k
}
90
91
static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
92
                       unsigned char **pp)
93
225
{
94
225
    unsigned int pad = 0;
95
225
    size_t ret, i;
96
225
    unsigned char *p, pb = 0;
97
98
225
    if (b != NULL && blen) {
99
225
        ret = blen;
100
225
        i = b[0];
101
225
        if (!neg && (i > 127)) {
102
15
            pad = 1;
103
15
            pb = 0;
104
210
        } else if (neg) {
105
165
            pb = 0xFF;
106
165
            if (i > 128) {
107
5
                pad = 1;
108
160
            } 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
3.99k
                for (pad = 0, i = 1; i < blen; i++)
114
3.93k
                    pad |= b[i];
115
65
                pb = pad != 0 ? 0xffU : 0;
116
65
                pad = pb & 1;
117
65
            }
118
165
        }
119
225
        ret += pad;
120
225
    } else {
121
0
        ret = 1;
122
0
        blen = 0;   /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
123
0
    }
124
125
225
    if (pp == NULL || (p = *pp) == NULL)
126
180
        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
45
    *p = pb;
134
45
    p += pad;       /* yes, p[0] can be written twice, but it's little
135
                     * price to pay for eliminated branches */
136
45
    twos_complement(p, b, blen, pb);
137
138
45
    *pp += ret;
139
45
    return ret;
140
225
}
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
82.4k
{
151
82.4k
    int neg, pad;
152
    /* Zero content length is illegal */
153
82.4k
    if (plen == 0) {
154
204
        ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
155
204
        return 0;
156
204
    }
157
82.2k
    neg = p[0] & 0x80;
158
82.2k
    if (pneg)
159
41.0k
        *pneg = neg;
160
    /* Handle common case where length is 1 octet separately */
161
82.2k
    if (plen == 1) {
162
61.3k
        if (b != NULL) {
163
30.6k
            if (neg)
164
22.8k
                b[0] = (p[0] ^ 0xFF) + 1;
165
7.80k
            else
166
7.80k
                b[0] = p[0];
167
30.6k
        }
168
61.3k
        return 1;
169
61.3k
    }
170
171
20.9k
    pad = 0;
172
20.9k
    if (p[0] == 0) {
173
2.37k
        pad = 1;
174
18.5k
    } else if (p[0] == 0xFF) {
175
1.70k
        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
415k
        for (pad = 0, i = 1; i < plen; i++)
182
413k
            pad |= p[i];
183
1.70k
        pad = pad != 0 ? 1 : 0;
184
1.70k
    }
185
    /* reject illegal padding: first two octets MSB can't match */
186
20.9k
    if (pad && (neg == (p[1] & 0x80))) {
187
242
        ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
188
242
        return 0;
189
242
    }
190
191
    /* skip over pad */
192
20.6k
    p += pad;
193
20.6k
    plen -= pad;
194
195
20.6k
    if (b != NULL)
196
10.3k
        twos_complement(b, p, plen, neg ? 0xffU : 0);
197
198
20.6k
    return plen;
199
20.9k
}
200
201
int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
202
225
{
203
225
    return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
204
225
}
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
9.33k
{
209
9.33k
    size_t i;
210
9.33k
    uint64_t r;
211
212
9.33k
    if (blen > sizeof(*pr)) {
213
634
        ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
214
634
        return 0;
215
634
    }
216
8.70k
    if (b == NULL)
217
0
        return 0;
218
31.0k
    for (r = 0, i = 0; i < blen; i++) {
219
22.2k
        r <<= 8;
220
22.2k
        r |= b[i];
221
22.2k
    }
222
8.70k
    *pr = r;
223
8.70k
    return 1;
224
8.70k
}
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
0
{
233
0
    size_t off = sizeof(uint64_t);
234
235
0
    do {
236
0
        b[--off] = (unsigned char)r;
237
0
    } while (r >>= 8);
238
239
0
    return off;
240
0
}
241
242
/*
243
 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as gcc produces
244
 * overflow warnings.
245
 */
246
192
#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
8.59k
{
252
8.59k
    uint64_t r;
253
8.59k
    if (asn1_get_uint64(&r, b, blen) == 0)
254
634
        return 0;
255
7.95k
    if (neg) {
256
2.61k
        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
2.42k
            *pr = -(int64_t)r;
260
2.42k
        } else if (r == ABS_INT64_MIN) {
261
            /* This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
262
             * on ones'-complement system. */
263
1
            *pr = (int64_t)(0 - r);
264
191
        } else {
265
191
            ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
266
191
            return 0;
267
191
        }
268
5.34k
    } else {
269
5.34k
        if (r <= INT64_MAX) {
270
5.02k
            *pr = (int64_t)r;
271
5.02k
        } else {
272
317
            ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
273
317
            return 0;
274
317
        }
275
5.34k
    }
276
7.44k
    return 1;
277
7.95k
}
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
40.7k
{
283
40.7k
    ASN1_INTEGER *ret = NULL;
284
40.7k
    size_t r;
285
40.7k
    int neg;
286
287
40.7k
    r = c2i_ibuf(NULL, NULL, *pp, len);
288
289
40.7k
    if (r == 0)
290
438
        return NULL;
291
292
40.2k
    if ((a == NULL) || ((*a) == NULL)) {
293
6.29k
        ret = ASN1_INTEGER_new();
294
6.29k
        if (ret == NULL)
295
0
            return NULL;
296
6.29k
        ret->type = V_ASN1_INTEGER;
297
6.29k
    } else
298
33.9k
        ret = *a;
299
300
40.2k
    if (ASN1_STRING_set(ret, NULL, r) == 0)
301
0
        goto err;
302
303
40.2k
    c2i_ibuf(ret->data, &neg, *pp, len);
304
305
40.2k
    if (neg)
306
26.2k
        ret->type |= V_ASN1_NEG;
307
308
40.2k
    *pp += len;
309
40.2k
    if (a != NULL)
310
40.2k
        (*a) = ret;
311
40.2k
    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
40.2k
}
318
319
static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
320
8.59k
{
321
8.59k
    if (a == NULL) {
322
0
        ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
323
0
        return 0;
324
0
    }
325
8.59k
    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
8.59k
    return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
330
8.59k
}
331
332
static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
333
0
{
334
0
    unsigned char tbuf[sizeof(r)];
335
0
    size_t off;
336
337
0
    a->type = itype;
338
0
    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
0
    } else {
347
0
        off = asn1_put_uint64(tbuf, r);
348
0
        a->type &= ~V_ASN1_NEG;
349
0
    }
350
0
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
351
0
}
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
0
{
452
0
    ASN1_INTEGER *ret;
453
0
    int len;
454
455
0
    if (ai == NULL) {
456
0
        ret = ASN1_STRING_type_new(atype);
457
0
    } else {
458
0
        ret = ai;
459
0
        ret->type = atype;
460
0
    }
461
462
0
    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
0
    if (BN_is_negative(bn) && !BN_is_zero(bn))
468
0
        ret->type |= V_ASN1_NEG_INTEGER;
469
470
0
    len = BN_num_bytes(bn);
471
472
0
    if (len == 0)
473
0
        len = 1;
474
475
0
    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
0
    if (BN_is_zero(bn))
482
0
        ret->data[0] = 0;
483
0
    else
484
0
        len = BN_bn2bin(bn, ret->data);
485
0
    ret->length = len;
486
0
    return ret;
487
0
 err:
488
0
    if (ret != ai)
489
0
        ASN1_INTEGER_free(ret);
490
0
    return NULL;
491
0
}
492
493
static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
494
                                 int itype)
495
4.81k
{
496
4.81k
    BIGNUM *ret;
497
498
4.81k
    if ((ai->type & ~V_ASN1_NEG) != itype) {
499
0
        ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
500
0
        return NULL;
501
0
    }
502
503
4.81k
    ret = BN_bin2bn(ai->data, ai->length, bn);
504
4.81k
    if (ret == NULL) {
505
0
        ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
506
0
        return NULL;
507
0
    }
508
4.81k
    if (ai->type & V_ASN1_NEG)
509
1.70k
        BN_set_negative(ret, 1);
510
4.81k
    return ret;
511
4.81k
}
512
513
int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
514
7.49k
{
515
7.49k
    return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
516
7.49k
}
517
518
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
519
0
{
520
0
    return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
521
0
}
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
0
{
535
0
    return ASN1_INTEGER_set_int64(a, v);
536
0
}
537
538
long ASN1_INTEGER_get(const ASN1_INTEGER *a)
539
11.6k
{
540
11.6k
    int i;
541
11.6k
    int64_t r;
542
11.6k
    if (a == NULL)
543
4.88k
        return 0;
544
6.79k
    i = ASN1_INTEGER_get_int64(&r, a);
545
6.79k
    if (i == 0)
546
810
        return -1;
547
5.98k
    if (r > LONG_MAX || r < LONG_MIN)
548
0
        return -1;
549
5.98k
    return (long)r;
550
5.98k
}
551
552
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
553
0
{
554
0
    return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
555
0
}
556
557
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
558
2.69k
{
559
2.69k
    return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
560
2.69k
}
561
562
int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
563
1.09k
{
564
1.09k
    return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
565
1.09k
}
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.26k
{
579
2.26k
    int i;
580
2.26k
    int64_t r;
581
2.26k
    if (a == NULL)
582
0
        return 0;
583
2.26k
    if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
584
0
        return -1;
585
2.26k
    if (a->length > (int)sizeof(long))
586
1.16k
        return 0xffffffffL;
587
1.09k
    i = ASN1_ENUMERATED_get_int64(&r, a);
588
1.09k
    if (i == 0)
589
332
        return -1;
590
766
    if (r > LONG_MAX || r < LONG_MIN)
591
0
        return -1;
592
766
    return (long)r;
593
766
}
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
2.12k
{
602
2.12k
    return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
603
2.12k
}
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
766
{
608
766
    unsigned char buf[sizeof(uint64_t)];
609
766
    size_t buflen;
610
611
766
    buflen = c2i_ibuf(NULL, NULL, *pp, len);
612
766
    if (buflen == 0)
613
8
        return 0;
614
758
    if (buflen > sizeof(uint64_t)) {
615
12
        ASN1err(ASN1_F_C2I_UINT64_INT, ASN1_R_TOO_LARGE);
616
12
        return 0;
617
12
    }
618
746
    (void)c2i_ibuf(buf, neg, *pp, len);
619
746
    return asn1_get_uint64(ret, buf, buflen);
620
758
}
621
622
int i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
623
0
{
624
0
    unsigned char buf[sizeof(uint64_t)];
625
0
    size_t off;
626
627
0
    off = asn1_put_uint64(buf, r);
628
0
    return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
629
0
}
630