Coverage Report

Created: 2025-12-31 06:58

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