Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/x_int64.c
Line
Count
Source
1
/*
2
 * Copyright 2017-2026 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 <inttypes.h>
12
#include "internal/cryptlib.h"
13
#include "internal/numbers.h"
14
#include <openssl/asn1t.h>
15
#include <openssl/bn.h>
16
#include "asn1_local.h"
17
18
/*
19
 * Custom primitive types for handling int32_t, int64_t, uint32_t, uint64_t.
20
 * This converts between an ASN1_INTEGER and those types directly.
21
 * This is preferred to using the LONG / ZLONG primitives.
22
 */
23
24
/*
25
 * We abuse the ASN1_ITEM fields |size| as a flags field
26
 */
27
2.62M
#define INTxx_FLAG_ZERO_DEFAULT (1 << 0)
28
5.98M
#define INTxx_FLAG_SIGNED (1 << 1)
29
30
static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
31
18.7k
{
32
18.7k
    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL)
33
0
        return 0;
34
18.7k
    return 1;
35
18.7k
}
36
37
static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
38
210k
{
39
210k
    OPENSSL_free(*pval);
40
210k
    *pval = NULL;
41
210k
}
42
43
static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
44
349k
{
45
349k
    **(uint64_t **)pval = 0;
46
349k
}
47
48
static int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
49
    const ASN1_ITEM *it)
50
43.5k
{
51
43.5k
    uint64_t utmp;
52
43.5k
    int neg = 0;
53
    /* this exists to bypass broken gcc optimization */
54
43.5k
    char *cp = (char *)*pval;
55
56
    /* use memcpy, because we may not be uint64_t aligned */
57
43.5k
    memcpy(&utmp, cp, sizeof(utmp));
58
59
43.5k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
60
35.3k
        && utmp == 0)
61
8.31k
        return -1;
62
35.1k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
63
25.8k
        && (int64_t)utmp < 0) {
64
        /* ossl_i2c_uint64_int() assumes positive values */
65
3.75k
        utmp = 0 - utmp;
66
3.75k
        neg = 1;
67
3.75k
    }
68
69
35.1k
    return ossl_i2c_uint64_int(cont, utmp, neg);
70
43.5k
}
71
72
static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
73
    int utype, char *free_cont, const ASN1_ITEM *it)
74
12.3k
{
75
12.3k
    uint64_t utmp = 0;
76
12.3k
    char *cp;
77
12.3k
    int neg = 0;
78
79
12.3k
    if (*pval == NULL && !uint64_new(pval, it))
80
0
        return 0;
81
82
12.3k
    cp = (char *)*pval;
83
84
    /*
85
     * Strictly speaking, zero length is malformed.  However, long_c2i
86
     * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
87
     * so for the sake of backward compatibility, we still decode zero
88
     * length INTEGERs as the number zero.
89
     */
90
12.3k
    if (len == 0)
91
40
        goto long_compat;
92
93
12.2k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
94
1.75k
        return 0;
95
10.5k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
96
1.59k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
97
1.59k
        return 0;
98
1.59k
    }
99
8.95k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
100
5.97k
        && !neg && utmp > INT64_MAX) {
101
647
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
102
647
        return 0;
103
647
    }
104
8.31k
    if (neg)
105
        /* ossl_c2i_uint64_int() returns positive values */
106
2.36k
        utmp = 0 - utmp;
107
108
8.35k
long_compat:
109
8.35k
    memcpy(cp, &utmp, sizeof(utmp));
110
8.35k
    return 1;
111
8.31k
}
112
113
static int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
114
    int indent, const ASN1_PCTX *pctx)
115
5.41k
{
116
5.41k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
117
3.17k
        return BIO_printf(out, "%" PRId64 "\n", **(int64_t **)pval);
118
2.23k
    return BIO_printf(out, "%" PRIu64 "\n", **(uint64_t **)pval);
119
5.41k
}
120
121
/* 32-bit variants */
122
123
static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
124
0
{
125
0
    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL)
126
0
        return 0;
127
0
    return 1;
128
0
}
129
130
static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
131
210k
{
132
210k
    OPENSSL_free(*pval);
133
210k
    *pval = NULL;
134
210k
}
135
136
static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
137
1.80M
{
138
1.80M
    **(uint32_t **)pval = 0;
139
1.80M
}
140
141
static int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
142
    const ASN1_ITEM *it)
143
393k
{
144
393k
    uint32_t utmp;
145
393k
    int neg = 0;
146
    /* this exists to bypass broken gcc optimization */
147
393k
    char *cp = (char *)*pval;
148
149
    /* use memcpy, because we may not be uint32_t aligned */
150
393k
    memcpy(&utmp, cp, sizeof(utmp));
151
152
393k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
153
25.6k
        && utmp == 0)
154
20.9k
        return -1;
155
372k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
156
354k
        && (int32_t)utmp < 0) {
157
        /* ossl_i2c_uint64_int() assumes positive values */
158
86.7k
        utmp = 0 - utmp;
159
86.7k
        neg = 1;
160
86.7k
    }
161
162
372k
    return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
163
393k
}
164
165
/*
166
 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
167
 * overflow warnings.
168
 */
169
170
96.0k
#define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
171
172
static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
173
    int utype, char *free_cont, const ASN1_ITEM *it)
174
699k
{
175
699k
    uint64_t utmp = 0;
176
699k
    uint32_t utmp2 = 0;
177
699k
    char *cp;
178
699k
    int neg = 0;
179
180
699k
    if (*pval == NULL && !uint64_new(pval, it))
181
0
        return 0;
182
183
699k
    cp = (char *)*pval;
184
185
    /*
186
     * Strictly speaking, zero length is malformed.  However, long_c2i
187
     * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
188
     * so for the sake of backward compatibility, we still decode zero
189
     * length INTEGERs as the number zero.
190
     */
191
699k
    if (len == 0)
192
88.5k
        goto long_compat;
193
194
610k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
195
26.1k
        return 0;
196
584k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
197
4.67k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
198
4.67k
        return 0;
199
4.67k
    }
200
579k
    if (neg) {
201
96.0k
        if (utmp > ABS_INT32_MIN) {
202
17.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
203
17.0k
            return 0;
204
17.0k
        }
205
79.0k
        utmp = 0 - utmp;
206
483k
    } else {
207
483k
        if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
208
466k
            || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
209
18.9k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
210
18.9k
            return 0;
211
18.9k
        }
212
483k
    }
213
214
632k
long_compat:
215
632k
    utmp2 = (uint32_t)utmp;
216
632k
    memcpy(cp, &utmp2, sizeof(utmp2));
217
632k
    return 1;
218
579k
}
219
220
static int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
221
    int indent, const ASN1_PCTX *pctx)
222
12.8k
{
223
12.8k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
224
11.9k
        return BIO_printf(out, "%d\n", (int)**(int32_t **)pval);
225
942
    return BIO_printf(out, "%u\n", (unsigned int)**(uint32_t **)pval);
226
12.8k
}
227
228
/* Define the primitives themselves */
229
230
static ASN1_PRIMITIVE_FUNCS uint32_pf = {
231
    NULL, 0,
232
    uint32_new,
233
    uint32_free,
234
    uint32_clear,
235
    uint32_c2i,
236
    uint32_i2c,
237
    uint32_print
238
};
239
240
static ASN1_PRIMITIVE_FUNCS uint64_pf = {
241
    NULL, 0,
242
    uint64_new,
243
    uint64_free,
244
    uint64_clear,
245
    uint64_c2i,
246
    uint64_i2c,
247
    uint64_print
248
};
249
250
2.56M
ASN1_ITEM_start(INT32)
251
2.56M
    ASN1_ITYPE_PRIMITIVE,
252
2.56M
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
253
2.56M
    INTxx_FLAG_SIGNED, "INT32" ASN1_ITEM_end(INT32)
254
255
322k
                           ASN1_ITEM_start(UINT32) ASN1_ITYPE_PRIMITIVE,
256
322k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32" ASN1_ITEM_end(UINT32)
257
258
52.5k
                                                ASN1_ITEM_start(INT64) ASN1_ITYPE_PRIMITIVE,
259
52.5k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
260
52.5k
    INTxx_FLAG_SIGNED, "INT64" ASN1_ITEM_end(INT64)
261
262
52.5k
                           ASN1_ITEM_start(UINT64) ASN1_ITYPE_PRIMITIVE,
263
52.5k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64" ASN1_ITEM_end(UINT64)
264
265
205k
                                                ASN1_ITEM_start(ZINT32) ASN1_ITYPE_PRIMITIVE,
266
205k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
267
205k
    INTxx_FLAG_ZERO_DEFAULT | INTxx_FLAG_SIGNED, "ZINT32" ASN1_ITEM_end(ZINT32)
268
269
459k
                                                     ASN1_ITEM_start(ZUINT32) ASN1_ITYPE_PRIMITIVE,
270
459k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
271
459k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT32" ASN1_ITEM_end(ZUINT32)
272
273
327k
                                 ASN1_ITEM_start(ZINT64) ASN1_ITYPE_PRIMITIVE,
274
327k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
275
327k
    INTxx_FLAG_ZERO_DEFAULT | INTxx_FLAG_SIGNED, "ZINT64" ASN1_ITEM_end(ZINT64)
276
277
324k
                                                     ASN1_ITEM_start(ZUINT64) ASN1_ITYPE_PRIMITIVE,
278
324k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
279
324k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT64" ASN1_ITEM_end(ZUINT64)