Coverage Report

Created: 2025-08-28 07:07

/src/openssl32/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
278
{
20
278
    return ASN1_STRING_dup(x);
21
278
}
22
23
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
24
2.18k
{
25
2.18k
    int neg, ret;
26
    /* Compare signs */
27
2.18k
    neg = x->type & V_ASN1_NEG;
28
2.18k
    if (neg != (y->type & V_ASN1_NEG)) {
29
652
        if (neg)
30
312
            return -1;
31
340
        else
32
340
            return 1;
33
652
    }
34
35
1.53k
    ret = ASN1_STRING_cmp(x, y);
36
37
1.53k
    if (neg)
38
257
        return -ret;
39
1.27k
    else
40
1.27k
        return ret;
41
1.53k
}
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
11.9M
{
79
11.9M
    unsigned int carry = pad & 1;
80
81
    /* Begin at the end of the encoding */
82
11.9M
    if (len != 0) {
83
        /*
84
         * if len == 0 then src/dst could be NULL, and this would be undefined
85
         * behaviour.
86
         */
87
11.9M
        dst += len;
88
11.9M
        src += len;
89
11.9M
    }
90
    /* two's complement value: ~value + 1 */
91
675M
    while (len-- != 0) {
92
663M
        *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
93
663M
        carry >>= 8;
94
663M
    }
95
11.9M
}
96
97
static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
98
                       unsigned char **pp)
99
31.1M
{
100
31.1M
    unsigned int pad = 0;
101
31.1M
    size_t ret, i;
102
31.1M
    unsigned char *p, pb = 0;
103
104
31.1M
    if (b != NULL && blen) {
105
31.1M
        ret = blen;
106
31.1M
        i = b[0];
107
31.1M
        if (!neg && (i > 127)) {
108
144k
            pad = 1;
109
144k
            pb = 0;
110
31.0M
        } else if (neg) {
111
5.25M
            pb = 0xFF;
112
5.25M
            if (i > 128) {
113
124k
                pad = 1;
114
5.13M
            } 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.09M
                for (pad = 0, i = 1; i < blen; i++)
120
2.60M
                    pad |= b[i];
121
485k
                pb = pad != 0 ? 0xffU : 0;
122
485k
                pad = pb & 1;
123
485k
            }
124
5.25M
        }
125
31.1M
        ret += pad;
126
31.1M
    } else {
127
236
        ret = 1;
128
236
        blen = 0;   /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
129
236
    }
130
131
31.1M
    if (pp == NULL || (p = *pp) == NULL)
132
26.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
4.63M
    *p = pb;
140
4.63M
    p += pad;       /* yes, p[0] can be written twice, but it's little
141
                     * price to pay for eliminated branches */
142
4.63M
    twos_complement(p, b, blen, pb);
143
144
4.63M
    *pp += ret;
145
4.63M
    return ret;
146
31.1M
}
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
21.1M
{
157
21.1M
    int neg, pad;
158
    /* Zero content length is illegal */
159
21.1M
    if (plen == 0) {
160
459k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
161
459k
        return 0;
162
459k
    }
163
20.7M
    neg = p[0] & 0x80;
164
20.7M
    if (pneg)
165
10.2M
        *pneg = neg;
166
    /* Handle common case where length is 1 octet separately */
167
20.7M
    if (plen == 1) {
168
5.87M
        if (b != NULL) {
169
2.93M
            if (neg)
170
547k
                b[0] = (p[0] ^ 0xFF) + 1;
171
2.39M
            else
172
2.39M
                b[0] = p[0];
173
2.93M
        }
174
5.87M
        return 1;
175
5.87M
    }
176
177
14.8M
    pad = 0;
178
14.8M
    if (p[0] == 0) {
179
759k
        pad = 1;
180
14.0M
    } else if (p[0] == 0xFF) {
181
279k
        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
89.3M
        for (pad = 0, i = 1; i < plen; i++)
188
89.0M
            pad |= p[i];
189
279k
        pad = pad != 0 ? 1 : 0;
190
279k
    }
191
    /* reject illegal padding: first two octets MSB can't match */
192
14.8M
    if (pad && (neg == (p[1] & 0x80))) {
193
110k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
194
110k
        return 0;
195
110k
    }
196
197
    /* skip over pad */
198
14.7M
    p += pad;
199
14.7M
    plen -= pad;
200
201
14.7M
    if (b != NULL)
202
7.34M
        twos_complement(b, p, plen, neg ? 0xffU : 0);
203
204
14.7M
    return plen;
205
14.8M
}
206
207
int ossl_i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
208
29.9M
{
209
29.9M
    return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
210
29.9M
}
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.25M
{
215
1.25M
    size_t i;
216
1.25M
    uint64_t r;
217
218
1.25M
    if (blen > sizeof(*pr)) {
219
14.0k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
220
14.0k
        return 0;
221
14.0k
    }
222
1.23M
    if (b == NULL)
223
0
        return 0;
224
3.46M
    for (r = 0, i = 0; i < blen; i++) {
225
2.22M
        r <<= 8;
226
2.22M
        r |= b[i];
227
2.22M
    }
228
1.23M
    *pr = r;
229
1.23M
    return 1;
230
1.23M
}
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
580k
{
239
580k
    size_t off = sizeof(uint64_t);
240
241
929k
    do {
242
929k
        b[--off] = (unsigned char)r;
243
929k
    } while (r >>= 8);
244
245
580k
    return off;
246
580k
}
247
248
/*
249
 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as gcc produces
250
 * overflow warnings.
251
 */
252
16.3k
#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
493k
{
258
493k
    uint64_t r;
259
493k
    if (asn1_get_uint64(&r, b, blen) == 0)
260
14.0k
        return 0;
261
479k
    if (neg) {
262
81.6k
        if (r <= INT64_MAX) {
263
            /* Most significant bit is guaranteed to be clear, negation
264
             * is guaranteed to be meaningful in platform-neutral sense. */
265
65.2k
            *pr = -(int64_t)r;
266
65.2k
        } else if (r == ABS_INT64_MIN) {
267
            /* This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
268
             * on ones'-complement system. */
269
2.51k
            *pr = (int64_t)(0 - r);
270
13.8k
        } else {
271
13.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
272
13.8k
            return 0;
273
13.8k
        }
274
397k
    } else {
275
397k
        if (r <= INT64_MAX) {
276
384k
            *pr = (int64_t)r;
277
384k
        } else {
278
12.9k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
279
12.9k
            return 0;
280
12.9k
        }
281
397k
    }
282
452k
    return 1;
283
479k
}
284
285
/* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
286
ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
287
                                    long len)
288
8.97M
{
289
8.97M
    ASN1_INTEGER *ret = NULL;
290
8.97M
    size_t r;
291
8.97M
    int neg;
292
293
8.97M
    r = c2i_ibuf(NULL, NULL, *pp, len);
294
295
8.97M
    if (r == 0)
296
417k
        return NULL;
297
298
8.55M
    if ((a == NULL) || ((*a) == NULL)) {
299
6.68M
        ret = ASN1_INTEGER_new();
300
6.68M
        if (ret == NULL)
301
0
            return NULL;
302
6.68M
        ret->type = V_ASN1_INTEGER;
303
6.68M
    } else
304
1.86M
        ret = *a;
305
306
8.55M
    if (ASN1_STRING_set(ret, NULL, r) == 0) {
307
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
308
0
        goto err;
309
0
    }
310
311
8.55M
    c2i_ibuf(ret->data, &neg, *pp, len);
312
313
8.55M
    if (neg != 0)
314
1.36M
        ret->type |= V_ASN1_NEG;
315
7.18M
    else
316
7.18M
        ret->type &= ~V_ASN1_NEG;
317
318
8.55M
    *pp += len;
319
8.55M
    if (a != NULL)
320
8.55M
        (*a) = ret;
321
8.55M
    return ret;
322
0
 err:
323
0
    if (a == NULL || *a != ret)
324
0
        ASN1_INTEGER_free(ret);
325
0
    return NULL;
326
8.55M
}
327
328
static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
329
494k
{
330
494k
    if (a == NULL) {
331
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
332
0
        return 0;
333
0
    }
334
494k
    if ((a->type & ~V_ASN1_NEG) != itype) {
335
1.64k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
336
1.64k
        return 0;
337
1.64k
    }
338
493k
    return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
339
494k
}
340
341
static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
342
119k
{
343
119k
    unsigned char tbuf[sizeof(r)];
344
119k
    size_t off;
345
346
119k
    a->type = itype;
347
119k
    if (r < 0) {
348
        /* Most obvious '-r' triggers undefined behaviour for most
349
         * common INT64_MIN. Even though below '0 - (uint64_t)r' can
350
         * appear two's-complement centric, it does produce correct/
351
         * expected result even on one's-complement. This is because
352
         * cast to unsigned has to change bit pattern... */
353
0
        off = asn1_put_uint64(tbuf, 0 - (uint64_t)r);
354
0
        a->type |= V_ASN1_NEG;
355
119k
    } else {
356
119k
        off = asn1_put_uint64(tbuf, r);
357
119k
        a->type &= ~V_ASN1_NEG;
358
119k
    }
359
119k
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
360
119k
}
361
362
static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
363
                                  int itype)
364
0
{
365
0
    if (a == NULL) {
366
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
367
0
        return 0;
368
0
    }
369
0
    if ((a->type & ~V_ASN1_NEG) != itype) {
370
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
371
0
        return 0;
372
0
    }
373
0
    if (a->type & V_ASN1_NEG) {
374
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
375
0
        return 0;
376
0
    }
377
0
    return asn1_get_uint64(pr, a->data, a->length);
378
0
}
379
380
static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
381
0
{
382
0
    unsigned char tbuf[sizeof(r)];
383
0
    size_t off;
384
385
0
    a->type = itype;
386
0
    off = asn1_put_uint64(tbuf, r);
387
0
    return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
388
0
}
389
390
/*
391
 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
392
 * integers: some broken software can encode a positive INTEGER with its MSB
393
 * set as negative (it doesn't add a padding zero).
394
 */
395
396
ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
397
                                long length)
398
0
{
399
0
    ASN1_INTEGER *ret = NULL;
400
0
    const unsigned char *p;
401
0
    unsigned char *s;
402
0
    long len = 0;
403
0
    int inf, tag, xclass;
404
0
    int i = 0;
405
406
0
    if ((a == NULL) || ((*a) == NULL)) {
407
0
        if ((ret = ASN1_INTEGER_new()) == NULL)
408
0
            return NULL;
409
0
        ret->type = V_ASN1_INTEGER;
410
0
    } else
411
0
        ret = (*a);
412
413
0
    p = *pp;
414
0
    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
415
0
    if (inf & 0x80) {
416
0
        i = ASN1_R_BAD_OBJECT_HEADER;
417
0
        goto err;
418
0
    }
419
420
0
    if (tag != V_ASN1_INTEGER) {
421
0
        i = ASN1_R_EXPECTING_AN_INTEGER;
422
0
        goto err;
423
0
    }
424
425
0
    if (len < 0) {
426
0
        i = ASN1_R_ILLEGAL_NEGATIVE_VALUE;
427
0
        goto err;
428
0
    }
429
    /*
430
     * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
431
     * a missing NULL parameter.
432
     */
433
0
    s = OPENSSL_malloc((int)len + 1);
434
0
    if (s == NULL)
435
0
        goto err;
436
0
    ret->type = V_ASN1_INTEGER;
437
0
    if (len) {
438
0
        if ((*p == 0) && (len != 1)) {
439
0
            p++;
440
0
            len--;
441
0
        }
442
0
        memcpy(s, p, (int)len);
443
0
        p += len;
444
0
    }
445
446
0
    ASN1_STRING_set0(ret, s, (int)len);
447
0
    if (a != NULL)
448
0
        (*a) = ret;
449
0
    *pp = p;
450
0
    return ret;
451
0
 err:
452
0
    if (i != 0)
453
0
        ERR_raise(ERR_LIB_ASN1, i);
454
0
    if ((a == NULL) || (*a != ret))
455
0
        ASN1_INTEGER_free(ret);
456
0
    return NULL;
457
0
}
458
459
static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
460
                                      int atype)
461
4.84k
{
462
4.84k
    ASN1_INTEGER *ret;
463
4.84k
    int len;
464
465
4.84k
    if (ai == NULL) {
466
3.25k
        ret = ASN1_STRING_type_new(atype);
467
3.25k
    } else {
468
1.58k
        ret = ai;
469
1.58k
        ret->type = atype;
470
1.58k
    }
471
472
4.84k
    if (ret == NULL) {
473
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
474
0
        goto err;
475
0
    }
476
477
4.84k
    if (BN_is_negative(bn) && !BN_is_zero(bn))
478
50
        ret->type |= V_ASN1_NEG_INTEGER;
479
480
4.84k
    len = BN_num_bytes(bn);
481
482
4.84k
    if (len == 0)
483
836
        len = 1;
484
485
4.84k
    if (ASN1_STRING_set(ret, NULL, len) == 0) {
486
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
487
0
        goto err;
488
0
    }
489
490
    /* Correct zero case */
491
4.84k
    if (BN_is_zero(bn))
492
836
        ret->data[0] = 0;
493
4.00k
    else
494
4.00k
        len = BN_bn2bin(bn, ret->data);
495
4.84k
    ret->length = len;
496
4.84k
    return ret;
497
0
 err:
498
0
    if (ret != ai)
499
0
        ASN1_INTEGER_free(ret);
500
0
    return NULL;
501
4.84k
}
502
503
static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
504
                                 int itype)
505
2.55M
{
506
2.55M
    BIGNUM *ret;
507
508
2.55M
    if ((ai->type & ~V_ASN1_NEG) != itype) {
509
406
        ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
510
406
        return NULL;
511
406
    }
512
513
2.55M
    ret = BN_bin2bn(ai->data, ai->length, bn);
514
2.55M
    if (ret == NULL) {
515
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BN_LIB);
516
0
        return NULL;
517
0
    }
518
2.55M
    if (ai->type & V_ASN1_NEG)
519
811k
        BN_set_negative(ret, 1);
520
2.55M
    return ret;
521
2.55M
}
522
523
int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
524
412k
{
525
412k
    return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
526
412k
}
527
528
int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
529
119k
{
530
119k
    return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
531
119k
}
532
533
int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
534
0
{
535
0
    return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
536
0
}
537
538
int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
539
0
{
540
0
    return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
541
0
}
542
543
int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
544
81.2k
{
545
81.2k
    return ASN1_INTEGER_set_int64(a, v);
546
81.2k
}
547
548
long ASN1_INTEGER_get(const ASN1_INTEGER *a)
549
387k
{
550
387k
    int i;
551
387k
    int64_t r;
552
387k
    if (a == NULL)
553
66.0k
        return 0;
554
321k
    i = ASN1_INTEGER_get_int64(&r, a);
555
321k
    if (i == 0)
556
17.1k
        return -1;
557
304k
    if (r > LONG_MAX || r < LONG_MIN)
558
0
        return -1;
559
304k
    return (long)r;
560
304k
}
561
562
ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
563
4.84k
{
564
4.84k
    return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
565
4.84k
}
566
567
BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
568
2.53M
{
569
2.53M
    return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
570
2.53M
}
571
572
int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
573
82.1k
{
574
82.1k
    return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
575
82.1k
}
576
577
int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
578
0
{
579
0
    return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
580
0
}
581
582
int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
583
0
{
584
0
    return ASN1_ENUMERATED_set_int64(a, v);
585
0
}
586
587
long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
588
83.5k
{
589
83.5k
    int i;
590
83.5k
    int64_t r;
591
83.5k
    if (a == NULL)
592
0
        return 0;
593
83.5k
    if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
594
0
        return -1;
595
83.5k
    if (a->length > (int)sizeof(long))
596
7.56k
        return 0xffffffffL;
597
75.9k
    i = ASN1_ENUMERATED_get_int64(&r, a);
598
75.9k
    if (i == 0)
599
11.9k
        return -1;
600
64.0k
    if (r > LONG_MAX || r < LONG_MIN)
601
0
        return -1;
602
64.0k
    return (long)r;
603
64.0k
}
604
605
ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
606
0
{
607
0
    return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
608
0
}
609
610
BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
611
16.3k
{
612
16.3k
    return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
613
16.3k
}
614
615
/* Internal functions used by x_int64.c */
616
int ossl_c2i_uint64_int(uint64_t *ret, int *neg,
617
                        const unsigned char **pp, long len)
618
800k
{
619
800k
    unsigned char buf[sizeof(uint64_t)];
620
800k
    size_t buflen;
621
622
800k
    buflen = c2i_ibuf(NULL, NULL, *pp, len);
623
800k
    if (buflen == 0)
624
19.3k
        return 0;
625
780k
    if (buflen > sizeof(uint64_t)) {
626
22.5k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
627
22.5k
        return 0;
628
22.5k
    }
629
758k
    (void)c2i_ibuf(buf, neg, *pp, len);
630
758k
    return asn1_get_uint64(ret, buf, buflen);
631
780k
}
632
633
int ossl_i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
634
461k
{
635
461k
    unsigned char buf[sizeof(uint64_t)];
636
461k
    size_t off;
637
638
461k
    off = asn1_put_uint64(buf, r);
639
461k
    return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
640
461k
}
641