Coverage Report

Created: 2026-10-28 06:28

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
3.34M
#define INTxx_FLAG_ZERO_DEFAULT (1 << 0)
28
7.64M
#define INTxx_FLAG_SIGNED (1 << 1)
29
30
static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
31
23.2k
{
32
23.2k
    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL)
33
0
        return 0;
34
23.2k
    return 1;
35
23.2k
}
36
37
static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
38
269k
{
39
269k
    OPENSSL_free(*pval);
40
269k
    *pval = NULL;
41
269k
}
42
43
static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
44
449k
{
45
449k
    **(uint64_t **)pval = 0;
46
449k
}
47
48
static int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
49
    const ASN1_ITEM *it)
50
60.9k
{
51
60.9k
    uint64_t utmp;
52
60.9k
    int neg = 0;
53
    /* this exists to bypass broken gcc optimization */
54
60.9k
    char *cp = (char *)*pval;
55
56
    /* use memcpy, because we may not be uint64_t aligned */
57
60.9k
    memcpy(&utmp, cp, sizeof(utmp));
58
59
60.9k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
60
50.6k
        && utmp == 0)
61
11.4k
        return -1;
62
49.4k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
63
36.0k
        && (int64_t)utmp < 0) {
64
        /* ossl_i2c_uint64_int() assumes positive values */
65
4.68k
        utmp = 0 - utmp;
66
4.68k
        neg = 1;
67
4.68k
    }
68
69
49.4k
    return ossl_i2c_uint64_int(cont, utmp, neg);
70
60.9k
}
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
15.6k
{
75
15.6k
    uint64_t utmp = 0;
76
15.6k
    char *cp;
77
15.6k
    int neg = 0;
78
79
15.6k
    if (*pval == NULL && !uint64_new(pval, it))
80
0
        return 0;
81
82
15.6k
    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
15.6k
    if (len == 0)
91
42
        goto long_compat;
92
93
15.6k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
94
2.03k
        return 0;
95
13.5k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
96
2.00k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
97
2.00k
        return 0;
98
2.00k
    }
99
11.5k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
100
7.70k
        && !neg && utmp > INT64_MAX) {
101
766
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
102
766
        return 0;
103
766
    }
104
10.8k
    if (neg)
105
        /* ossl_c2i_uint64_int() returns positive values */
106
2.85k
        utmp = 0 - utmp;
107
108
10.8k
long_compat:
109
10.8k
    memcpy(cp, &utmp, sizeof(utmp));
110
10.8k
    return 1;
111
10.8k
}
112
113
static int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
114
    int indent, const ASN1_PCTX *pctx)
115
6.85k
{
116
6.85k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
117
4.04k
        return BIO_printf(out, "%" PRId64 "\n", **(int64_t **)pval);
118
2.81k
    return BIO_printf(out, "%" PRIu64 "\n", **(uint64_t **)pval);
119
6.85k
}
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
269k
{
132
269k
    OPENSSL_free(*pval);
133
269k
    *pval = NULL;
134
269k
}
135
136
static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
137
2.33M
{
138
2.33M
    **(uint32_t **)pval = 0;
139
2.33M
}
140
141
static int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
142
    const ASN1_ITEM *it)
143
487k
{
144
487k
    uint32_t utmp;
145
487k
    int neg = 0;
146
    /* this exists to bypass broken gcc optimization */
147
487k
    char *cp = (char *)*pval;
148
149
    /* use memcpy, because we may not be uint32_t aligned */
150
487k
    memcpy(&utmp, cp, sizeof(utmp));
151
152
487k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
153
36.5k
        && utmp == 0)
154
30.0k
        return -1;
155
457k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
156
431k
        && (int32_t)utmp < 0) {
157
        /* ossl_i2c_uint64_int() assumes positive values */
158
104k
        utmp = 0 - utmp;
159
104k
        neg = 1;
160
104k
    }
161
162
457k
    return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
163
487k
}
164
165
/*
166
 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
167
 * overflow warnings.
168
 */
169
170
120k
#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
889k
{
175
889k
    uint64_t utmp = 0;
176
889k
    uint32_t utmp2 = 0;
177
889k
    char *cp;
178
889k
    int neg = 0;
179
180
889k
    if (*pval == NULL && !uint64_new(pval, it))
181
0
        return 0;
182
183
889k
    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
889k
    if (len == 0)
192
111k
        goto long_compat;
193
194
778k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
195
31.6k
        return 0;
196
746k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
197
5.68k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
198
5.68k
        return 0;
199
5.68k
    }
200
741k
    if (neg) {
201
120k
        if (utmp > ABS_INT32_MIN) {
202
23.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
203
23.2k
            return 0;
204
23.2k
        }
205
97.3k
        utmp = 0 - utmp;
206
620k
    } else {
207
620k
        if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
208
596k
            || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
209
25.7k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
210
25.7k
            return 0;
211
25.7k
        }
212
620k
    }
213
214
803k
long_compat:
215
803k
    utmp2 = (uint32_t)utmp;
216
803k
    memcpy(cp, &utmp2, sizeof(utmp2));
217
803k
    return 1;
218
741k
}
219
220
static int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
221
    int indent, const ASN1_PCTX *pctx)
222
17.0k
{
223
17.0k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
224
15.7k
        return BIO_printf(out, "%d\n", (int)**(int32_t **)pval);
225
1.25k
    return BIO_printf(out, "%u\n", (unsigned int)**(uint32_t **)pval);
226
17.0k
}
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
3.30M
ASN1_ITEM_start(INT32)
251
3.30M
    ASN1_ITYPE_PRIMITIVE,
252
3.30M
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
253
3.30M
    INTxx_FLAG_SIGNED, "INT32" ASN1_ITEM_end(INT32)
254
255
417k
                           ASN1_ITEM_start(UINT32) ASN1_ITYPE_PRIMITIVE,
256
417k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32" ASN1_ITEM_end(UINT32)
257
258
67.3k
                                                ASN1_ITEM_start(INT64) ASN1_ITYPE_PRIMITIVE,
259
67.3k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
260
67.3k
    INTxx_FLAG_SIGNED, "INT64" ASN1_ITEM_end(INT64)
261
262
67.3k
                           ASN1_ITEM_start(UINT64) ASN1_ITYPE_PRIMITIVE,
263
67.3k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64" ASN1_ITEM_end(UINT64)
264
265
264k
                                                ASN1_ITEM_start(ZINT32) ASN1_ITYPE_PRIMITIVE,
266
264k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
267
264k
    INTxx_FLAG_ZERO_DEFAULT | INTxx_FLAG_SIGNED, "ZINT32" ASN1_ITEM_end(ZINT32)
268
269
594k
                                                     ASN1_ITEM_start(ZUINT32) ASN1_ITYPE_PRIMITIVE,
270
594k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
271
594k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT32" ASN1_ITEM_end(ZUINT32)
272
273
423k
                                 ASN1_ITEM_start(ZINT64) ASN1_ITYPE_PRIMITIVE,
274
423k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
275
423k
    INTxx_FLAG_ZERO_DEFAULT | INTxx_FLAG_SIGNED, "ZINT64" ASN1_ITEM_end(ZINT64)
276
277
419k
                                                     ASN1_ITEM_start(ZUINT64) ASN1_ITYPE_PRIMITIVE,
278
419k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
279
419k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT64" ASN1_ITEM_end(ZUINT64)