Coverage Report

Created: 2023-09-25 06:45

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