Coverage Report

Created: 2025-12-04 06:33

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