Coverage Report

Created: 2025-08-11 07:04

/src/openssl35/crypto/asn1/a_int.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-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 <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
263
{
20
263
    return ASN1_STRING_dup(x);
21
263
}
22
23
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
24
2.15k
{
25
2.15k
    int neg, ret;
26
    /* Compare signs */
27
2.15k
    neg = x->type & V_ASN1_NEG;
28
2.15k
    if (neg != (y->type & V_ASN1_NEG)) {
29
659
        if (neg)
30
302
            return -1;
31
357
        else
32
357
            return 1;
33
659
    }
34
35
1.49k
    ret = ASN1_STRING_cmp(x, y);
36
37
1.49k
    if (neg)
38
259
        return -ret;
39
1.23k
    else
40
1.23k
        return ret;
41
1.49k
}
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
14.5M
{
79
14.5M
    unsigned int carry = pad & 1;
80
81
    /* Begin at the end of the encoding */
82
14.5M
    if (len != 0) {
83
        /*
84
         * if len == 0 then src/dst could be NULL, and this would be undefined
85
         * behaviour.
86
         */
87
14.5M
        dst += len;
88
14.5M
        src += len;
89
14.5M
    }
90
    /* two's complement value: ~value + 1 */
91
665M
    while (len-- != 0) {
92
651M
        *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
93
651M
        carry >>= 8;
94
651M
    }
95
14.5M
}
96
97
static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
98
                       unsigned char **pp)
99
38.0M
{
100
38.0M
    unsigned int pad = 0;
101
38.0M
    size_t ret, i;
102
38.0M
    unsigned char *p, pb = 0;
103
104
38.0M
    if (b != NULL && blen) {
105
38.0M
        ret = blen;
106
38.0M
        i = b[0];
107
38.0M
        if (!neg && (i > 127)) {
108
146k
            pad = 1;
109
146k
            pb = 0;
110
37.8M
        } else if (neg) {
111
4.12M
            pb = 0xFF;
112
4.12M
            if (i > 128) {
113
113k
                pad = 1;
114
4.00M
            } else if (i == 128) {
115
                /*
116
                 * Special case [of minimal negative for given length]:
117
                 * if any other bytes non zero we pad, otherwise we don't.
118
                 */
119
3.17M
                for (pad = 0, i = 1; i < blen; i++)
120
2.63M
                    pad |= b[i];
121
536k
                pb = pad != 0 ? 0xffU : 0;
122
536k
                pad = pb & 1;
123
536k
            }
124
4.12M
        }
125
38.0M
        ret += pad;
126
38.0M
    } else {
127
224
        ret = 1;
128
224
        blen = 0;   /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
129
224
    }
130
131
38.0M
    if (pp == NULL || (p = *pp) == NULL)
132
32.5M
        return ret;
133
134
    /*
135
     * This magically handles all corner cases, such as '(b == NULL ||
136
     * blen == 0)', non-negative value, "negative" zero, 0x80 followed
137
     * by any number of zeros...
138
     */
139
5.52M
    *p = pb;
140
5.52M
    p += pad;       /* yes, p[0] can be written twice, but it's little
141
                     * price to pay for eliminated branches */
142
5.52M
    twos_complement(p, b, blen, pb);
143
144
5.52M
    *pp += ret;
145
5.52M
    return ret;
146
38.0M
}
147
148
/*
149
 * convert content octets into a big endian buffer. Returns the length
150
 * of buffer or 0 on error: for malformed INTEGER. If output buffer is
151
 * NULL just return length.
152
 */
153
154
static size_t c2i_ibuf(unsigned char *b, int *pneg,
155
                       const unsigned char *p, size_t plen)
156
24.4M
{
157
24.4M
    int neg, pad;
158
    /* Zero content length is illegal */
159
24.4M
    if (plen == 0) {
160
467k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
161
467k
        return 0;
162
467k
    }
163
23.9M
    neg = p[0] & 0x80;
164
23.9M
    if (pneg)
165
11.9M
        *pneg = neg;
166
    /* Handle common case where length is 1 octet separately */
167
23.9M
    if (plen == 1) {
168
5.74M
        if (b != NULL) {
169
2.87M
            if (neg)
170
543k
                b[0] = (p[0] ^ 0xFF) + 1;
171
2.32M
            else
172
2.32M
                b[0] = p[0];
173
2.87M
        }
174
5.74M
        return 1;
175
5.74M
    }
176
177
18.2M
    pad = 0;
178
18.2M
    if (p[0] == 0) {
179
685k
        pad = 1;
180
17.5M
    } else if (p[0] == 0xFF) {
181
268k
        size_t i;
182
183
        /*
184
         * Special case [of "one less minimal negative" for given length]:
185
         * if any other bytes non zero it was padded, otherwise not.
186
         */
187
114M
        for (pad = 0, i = 1; i < plen; i++)
188
114M
            pad |= p[i];
189
268k
        pad = pad != 0 ? 1 : 0;
190
268k
    }
191
    /* reject illegal padding: first two octets MSB can't match */
192
18.2M
    if (pad && (neg == (p[1] & 0x80))) {
193
108k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
194
108k
        return 0;
195
108k
    }
196
197
    /* skip over pad */
198
18.1M
    p += pad;
199
18.1M
    plen -= pad;
200
201
18.1M
    if (b != NULL)
202
9.06M
        twos_complement(b, p, plen, neg ? 0xffU : 0);
203
204
18.1M
    return plen;
205
18.2M
}
206
207
int ossl_i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
208
36.6M
{
209
36.6M
    return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
210
36.6M
}
211
212
/* Convert big endian buffer into uint64_t, return 0 on error */
213
static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
214
1.19M
{
215
1.19M
    size_t i;
216
1.19M
    uint64_t r;
217
218
1.19M
    if (blen > sizeof(*pr)) {
219
12.0k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
220
12.0k
        return 0;
221
12.0k
    }
222
1.18M
    if (b == NULL)
223
0
        return 0;
224
3.29M
    for (r = 0, i = 0; i < blen; i++) {
225
2.11M
        r <<= 8;
226
2.11M
        r |= b[i];
227
2.11M
    }
228
1.18M
    *pr = r;
229
1.18M
    return 1;
230
1.18M
}
231
232
/*
233
 * Write uint64_t to big endian buffer and return offset to first
234
 * written octet. In other words it returns offset in range from 0
235
 * to 7, with 0 denoting 8 written octets and 7 - one.
236
 */
237
static size_t asn1_put_uint64(unsigned char b[sizeof(uint64_t)], uint64_t r)
238
561k
{
239
561k
    size_t off = sizeof(uint64_t);
240
241
907k
    do {
242
907k
        b[--off] = (unsigned char)r;
243
907k
    } while (r >>= 8);
244
245
561k
    return off;
246
561k
}
247
248
/*
249
 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as gcc produces
250
 * overflow warnings.
251
 */
252
15.7k
#define ABS_INT64_MIN ((uint64_t)INT64_MAX + (-(INT64_MIN + INT64_MAX)))
253
254
/* signed version of asn1_get_uint64 */
255
static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
256
                          int neg)
257
451k
{
258
451k
    uint64_t r;
259
451k
    if (asn1_get_uint64(&r, b, blen) == 0)
260
12.0k
        return 0;
261
439k
    if (neg) {
262
75.1k
        if (r <= INT64_MAX) {
263
            /*
264
             * Most significant bit is guaranteed to be clear, negation
265
             * is guaranteed to be meaningful in platform-neutral sense.
266
             */
267
59.4k
            *pr = -(int64_t)r;
268
59.4k
        } else if (r == ABS_INT64_MIN) {
269
            /*
270
             * This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
271
             * on ones'-complement system.
272
             */
273
2.44k
            *pr = (int64_t)(0 - r);
274
13.2k
        } else {
275
13.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
276
13.2k
            return 0;
277
13.2k
        }
278
364k
    } else {
279
364k
        if (r <= INT64_MAX) {
280
352k
            *pr = (int64_t)r;
281
352k
        } else {
282
11.5k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
283
11.5k
            return 0;
284
11.5k
        }
285
364k
    }
286
414k
    return 1;
287
439k
}
288
289
/* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
290
ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
291
                                    long len)
292
10.6M
{
293
10.6M
    ASN1_INTEGER *ret = NULL;
294
10.6M
    size_t r;
295
10.6M
    int neg;
296
297
10.6M
    r = c2i_ibuf(NULL, NULL, *pp, len);
298
299
10.6M
    if (r == 0)
300
423k
        return NULL;
301
302
10.2M
    if ((a == NULL) || ((*a) == NULL)) {
303
8.53M
        ret = ASN1_INTEGER_new();
304
8.53M
        if (ret == NULL)
305
0
            return NULL;
306
8.53M
        ret->type = V_ASN1_INTEGER;
307
8.53M
    } else
308
1.70M
        ret = *a;
309
310
10.2M
    if (ASN1_STRING_set(ret, NULL, r) == 0) {
311
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
312
0
        goto err;
313
0
    }
314
315
10.2M
    c2i_ibuf(ret->data, &neg, *pp, len);
316
317
10.2M
    if (neg != 0)
318
1.15M
        ret->type |= V_ASN1_NEG;
319
9.07M
    else
320
9.07M
        ret->type &= ~V_ASN1_NEG;
321
322
10.2M
    *pp += len;
323
10.2M
    if (a != NULL)
324
10.2M
        (*a) = ret;
325
10.2M
    return ret;
326
0
 err:
327
0
    if (a == NULL || *a != ret)
328
0
        ASN1_INTEGER_free(ret);
329
0
    return NULL;
330
10.2M
}
331
332
static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
333
453k
{
334
453k
    if (a == NULL) {
335
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
336
0
        return 0;
337
0
    }
338
453k
    if ((a->type & ~V_ASN1_NEG) != itype) {
339
1.43k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
340
1.43k
        return 0;
341
1.43k
    }
342
451k
    return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
343
453k
}
344
345
static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
346
114k
{
347
114k
    unsigned char tbuf[sizeof(r)];
348
114k
    size_t off;
349
350
114k
    a->type = itype;
351
114k
    if (r < 0) {
352
        /*
353
         * Most obvious '-r' triggers undefined behaviour for most
354
         * common INT64_MIN. Even though below '0 - (uint64_t)r' can
355
         * appear two's-complement centric, it does produce correct/
356
         * expected result even on ones' complement. This is because
357
         * cast to unsigned has to change bit pattern...
358
         */
359
0
        off = asn1_put_uint64(tbuf, 0 - (uint64_t)r);
360
0
        a->type |= V_ASN1_NEG;
361
114k
    } else {
362
114k
        off = asn1_put_uint64(tbuf, r);
363
114k
        a->type &= ~V_ASN1_NEG;
364
114k
    }
365
114k
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
366
114k
}
367
368
static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
369
                                  int itype)
370
0
{
371
0
    if (a == NULL) {
372
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
373
0
        return 0;
374
0
    }
375
0
    if ((a->type & ~V_ASN1_NEG) != itype) {
376
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
377
0
        return 0;
378
0
    }
379
0
    if (a->type & V_ASN1_NEG) {
380
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
381
0
        return 0;
382
0
    }
383
0
    return asn1_get_uint64(pr, a->data, a->length);
384
0
}
385
386
static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
387
0
{
388
0
    unsigned char tbuf[sizeof(r)];
389
0
    size_t off;
390
391
0
    a->type = itype;
392
0
    off = asn1_put_uint64(tbuf, r);
393
0
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
394
0
}
395
396
/*
397
 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
398
 * integers: some broken software can encode a positive INTEGER with its MSB
399
 * set as negative (it doesn't add a padding zero).
400
 */
401
402
ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
403
                                long length)
404
0
{
405
0
    ASN1_INTEGER *ret = NULL;
406
0
    const unsigned char *p;
407
0
    unsigned char *s;
408
0
    long len = 0;
409
0
    int inf, tag, xclass;
410
0
    int i = 0;
411
412
0
    if ((a == NULL) || ((*a) == NULL)) {
413
0
        if ((ret = ASN1_INTEGER_new()) == NULL)
414
0
            return NULL;
415
0
        ret->type = V_ASN1_INTEGER;
416
0
    } else
417
0
        ret = (*a);
418
419
0
    p = *pp;
420
0
    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
421
0
    if (inf & 0x80) {
422
0
        i = ASN1_R_BAD_OBJECT_HEADER;
423
0
        goto err;
424
0
    }
425
426
0
    if (tag != V_ASN1_INTEGER) {
427
0
        i = ASN1_R_EXPECTING_AN_INTEGER;
428
0
        goto err;
429
0
    }
430
431
0
    if (len < 0) {
432
0
        i = ASN1_R_ILLEGAL_NEGATIVE_VALUE;
433
0
        goto err;
434
0
    }
435
    /*
436
     * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
437
     * a missing NULL parameter.
438
     */
439
0
    s = OPENSSL_malloc((int)len + 1);
440
0
    if (s == NULL)
441
0
        goto err;
442
0
    ret->type = V_ASN1_INTEGER;
443
0
    if (len) {
444
0
        if ((*p == 0) && (len != 1)) {
445
0
            p++;
446
0
            len--;
447
0
        }
448
0
        memcpy(s, p, (int)len);
449
0
        p += len;
450
0
    }
451
452
0
    ASN1_STRING_set0(ret, s, (int)len);
453
0
    if (a != NULL)
454
0
        (*a) = ret;
455
0
    *pp = p;
456
0
    return ret;
457
0
 err:
458
0
    if (i != 0)
459
0
        ERR_raise(ERR_LIB_ASN1, i);
460
0
    if ((a == NULL) || (*a != ret))
461
0
        ASN1_INTEGER_free(ret);
462
0
    return NULL;
463
0
}
464
465
static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
466
                                      int atype)
467
4.56k
{
468
4.56k
    ASN1_INTEGER *ret;
469
4.56k
    int len;
470
471
4.56k
    if (ai == NULL) {
472
3.07k
        ret = ASN1_STRING_type_new(atype);
473
3.07k
    } else {
474
1.49k
        ret = ai;
475
1.49k
        ret->type = atype;
476
1.49k
    }
477
478
4.56k
    if (ret == NULL) {
479
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
480
0
        goto err;
481
0
    }
482
483
4.56k
    if (BN_is_negative(bn) && !BN_is_zero(bn))
484
49
        ret->type |= V_ASN1_NEG_INTEGER;
485
486
4.56k
    len = BN_num_bytes(bn);
487
488
4.56k
    if (len == 0)
489
824
        len = 1;
490
491
4.56k
    if (ASN1_STRING_set(ret, NULL, len) == 0) {
492
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
493
0
        goto err;
494
0
    }
495
496
    /* Correct zero case */
497
4.56k
    if (BN_is_zero(bn))
498
824
        ret->data[0] = 0;
499
3.74k
    else
500
3.74k
        len = BN_bn2bin(bn, ret->data);
501
4.56k
    ret->length = len;
502
4.56k
    return ret;
503
0
 err:
504
0
    if (ret != ai)
505
0
        ASN1_INTEGER_free(ret);
506
0
    return NULL;
507
4.56k
}
508
509
static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
510
                                 int itype)
511
2.69M
{
512
2.69M
    BIGNUM *ret;
513
514
2.69M
    if ((ai->type & ~V_ASN1_NEG) != itype) {
515
407
        ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
516
407
        return NULL;
517
407
    }
518
519
2.69M
    ret = BN_bin2bn(ai->data, ai->length, bn);
520
2.69M
    if (ret == NULL) {
521
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BN_LIB);
522
0
        return NULL;
523
0
    }
524
2.69M
    if (ai->type & V_ASN1_NEG)
525
598k
        BN_set_negative(ret, 1);
526
2.69M
    return ret;
527
2.69M
}
528
529
int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
530
387k
{
531
387k
    return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
532
387k
}
533
534
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
535
114k
{
536
114k
    return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
537
114k
}
538
539
int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
540
0
{
541
0
    return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
542
0
}
543
544
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
545
0
{
546
0
    return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
547
0
}
548
549
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
550
77.9k
{
551
77.9k
    return ASN1_INTEGER_set_int64(a, v);
552
77.9k
}
553
554
long ASN1_INTEGER_get(const ASN1_INTEGER *a)
555
363k
{
556
363k
    int i;
557
363k
    int64_t r;
558
363k
    if (a == NULL)
559
62.5k
        return 0;
560
300k
    i = ASN1_INTEGER_get_int64(&r, a);
561
300k
    if (i == 0)
562
15.2k
        return -1;
563
285k
    if (r > LONG_MAX || r < LONG_MIN)
564
0
        return -1;
565
285k
    return (long)r;
566
285k
}
567
568
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
569
4.56k
{
570
4.56k
    return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
571
4.56k
}
572
573
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
574
2.67M
{
575
2.67M
    return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
576
2.67M
}
577
578
int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
579
65.8k
{
580
65.8k
    return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
581
65.8k
}
582
583
int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
584
0
{
585
0
    return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
586
0
}
587
588
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
589
0
{
590
0
    return ASN1_ENUMERATED_set_int64(a, v);
591
0
}
592
593
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
594
67.3k
{
595
67.3k
    int i;
596
67.3k
    int64_t r;
597
67.3k
    if (a == NULL)
598
0
        return 0;
599
67.3k
    if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
600
0
        return -1;
601
67.3k
    if (a->length > (int)sizeof(long))
602
7.63k
        return 0xffffffffL;
603
59.7k
    i = ASN1_ENUMERATED_get_int64(&r, a);
604
59.7k
    if (i == 0)
605
11.2k
        return -1;
606
48.5k
    if (r > LONG_MAX || r < LONG_MIN)
607
0
        return -1;
608
48.5k
    return (long)r;
609
48.5k
}
610
611
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
612
0
{
613
0
    return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
614
0
}
615
616
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
617
16.5k
{
618
16.5k
    return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
619
16.5k
}
620
621
/* Internal functions used by x_int64.c */
622
int ossl_c2i_uint64_int(uint64_t *ret, int *neg,
623
                        const unsigned char **pp, long len)
624
780k
{
625
780k
    unsigned char buf[sizeof(uint64_t)];
626
780k
    size_t buflen;
627
628
780k
    buflen = c2i_ibuf(NULL, NULL, *pp, len);
629
780k
    if (buflen == 0)
630
18.7k
        return 0;
631
761k
    if (buflen > sizeof(uint64_t)) {
632
21.5k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
633
21.5k
        return 0;
634
21.5k
    }
635
740k
    (void)c2i_ibuf(buf, neg, *pp, len);
636
740k
    return asn1_get_uint64(ret, buf, buflen);
637
761k
}
638
639
int ossl_i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
640
447k
{
641
447k
    unsigned char buf[sizeof(uint64_t)];
642
447k
    size_t off;
643
644
447k
    off = asn1_put_uint64(buf, r);
645
447k
    return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
646
447k
}
647