Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/crypto/asn1/x_int64.c
Line
Count
Source (jump to first uncovered line)
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
2.32M
#define INTxx_FLAG_ZERO_DEFAULT (1<<0)
27
5.06M
#define INTxx_FLAG_SIGNED       (1<<1)
28
29
static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
30
16.7k
{
31
16.7k
    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL)
32
0
        return 0;
33
16.7k
    return 1;
34
16.7k
}
35
36
static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
37
195k
{
38
195k
    OPENSSL_free(*pval);
39
195k
    *pval = NULL;
40
195k
}
41
42
static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
43
327k
{
44
327k
    **(uint64_t **)pval = 0;
45
327k
}
46
47
static int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
48
                      const ASN1_ITEM *it)
49
40.3k
{
50
40.3k
    uint64_t utmp;
51
40.3k
    int neg = 0;
52
    /* this exists to bypass broken gcc optimization */
53
40.3k
    char *cp = (char *)*pval;
54
55
    /* use memcpy, because we may not be uint64_t aligned */
56
40.3k
    memcpy(&utmp, cp, sizeof(utmp));
57
58
40.3k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
59
40.3k
        && utmp == 0)
60
8.99k
        return -1;
61
31.3k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
62
31.3k
        && (int64_t)utmp < 0) {
63
        /* ossl_i2c_uint64_int() assumes positive values */
64
4.25k
        utmp = 0 - utmp;
65
4.25k
        neg = 1;
66
4.25k
    }
67
68
31.3k
    return ossl_i2c_uint64_int(cont, utmp, neg);
69
40.3k
}
70
71
static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
72
                      int utype, char *free_cont, const ASN1_ITEM *it)
73
10.9k
{
74
10.9k
    uint64_t utmp = 0;
75
10.9k
    char *cp;
76
10.9k
    int neg = 0;
77
78
10.9k
    if (*pval == NULL && !uint64_new(pval, it))
79
0
        return 0;
80
81
10.9k
    cp = (char *)*pval;
82
83
    /*
84
     * Strictly speaking, zero length is malformed.  However, long_c2i
85
     * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
86
     * so for the sake of backward compatibility, we still decode zero
87
     * length INTEGERs as the number zero.
88
     */
89
10.9k
    if (len == 0)
90
32
        goto long_compat;
91
92
10.9k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
93
1.88k
        return 0;
94
9.04k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
95
1.33k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
96
1.33k
        return 0;
97
1.33k
    }
98
7.71k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
99
7.71k
            && !neg && utmp > INT64_MAX) {
100
500
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
101
500
        return 0;
102
500
    }
103
7.21k
    if (neg)
104
        /* ossl_c2i_uint64_int() returns positive values */
105
2.09k
        utmp = 0 - utmp;
106
107
7.24k
 long_compat:
108
7.24k
    memcpy(cp, &utmp, sizeof(utmp));
109
7.24k
    return 1;
110
7.21k
}
111
112
static int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
113
                        int indent, const ASN1_PCTX *pctx)
114
4.66k
{
115
4.66k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
116
2.74k
        return BIO_printf(out, "%jd\n", **(int64_t **)pval);
117
1.91k
    return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
118
4.66k
}
119
120
/* 32-bit variants */
121
122
static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
123
0
{
124
0
    if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL)
125
0
        return 0;
126
0
    return 1;
127
0
}
128
129
static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
130
195k
{
131
195k
    OPENSSL_free(*pval);
132
195k
    *pval = NULL;
133
195k
}
134
135
static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
136
1.61M
{
137
1.61M
    **(uint32_t **)pval = 0;
138
1.61M
}
139
140
static int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
141
                      const ASN1_ITEM *it)
142
317k
{
143
317k
    uint32_t utmp;
144
317k
    int neg = 0;
145
    /* this exists to bypass broken gcc optimization */
146
317k
    char *cp = (char *)*pval;
147
148
    /* use memcpy, because we may not be uint32_t aligned */
149
317k
    memcpy(&utmp, cp, sizeof(utmp));
150
151
317k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
152
317k
        && utmp == 0)
153
20.3k
        return -1;
154
297k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
155
297k
        && (int32_t)utmp < 0) {
156
        /* ossl_i2c_uint64_int() assumes positive values */
157
70.6k
        utmp = 0 - utmp;
158
70.6k
        neg = 1;
159
70.6k
    }
160
161
297k
    return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
162
317k
}
163
164
/*
165
 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
166
 * overflow warnings.
167
 */
168
169
101k
#define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
170
171
static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
172
                      int utype, char *free_cont, const ASN1_ITEM *it)
173
565k
{
174
565k
    uint64_t utmp = 0;
175
565k
    uint32_t utmp2 = 0;
176
565k
    char *cp;
177
565k
    int neg = 0;
178
179
565k
    if (*pval == NULL && !uint64_new(pval, it))
180
0
        return 0;
181
182
565k
    cp = (char *)*pval;
183
184
    /*
185
     * Strictly speaking, zero length is malformed.  However, long_c2i
186
     * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
187
     * so for the sake of backward compatibility, we still decode zero
188
     * length INTEGERs as the number zero.
189
     */
190
565k
    if (len == 0)
191
65.3k
        goto long_compat;
192
193
500k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
194
21.2k
        return 0;
195
478k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
196
4.77k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
197
4.77k
        return 0;
198
4.77k
    }
199
473k
    if (neg) {
200
101k
        if (utmp > ABS_INT32_MIN) {
201
11.7k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
202
11.7k
            return 0;
203
11.7k
        }
204
89.4k
        utmp = 0 - utmp;
205
372k
    } else {
206
372k
        if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
207
372k
            || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
208
15.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
209
15.3k
            return 0;
210
15.3k
        }
211
372k
    }
212
213
512k
 long_compat:
214
512k
    utmp2 = (uint32_t)utmp;
215
512k
    memcpy(cp, &utmp2, sizeof(utmp2));
216
512k
    return 1;
217
473k
}
218
219
static int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
220
                        int indent, const ASN1_PCTX *pctx)
221
11.1k
{
222
11.1k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
223
10.3k
        return BIO_printf(out, "%d\n", (int)**(int32_t **)pval);
224
814
    return BIO_printf(out, "%u\n", (unsigned int)**(uint32_t **)pval);
225
11.1k
}
226
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.24M
ASN1_ITEM_start(INT32)
251
2.24M
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
252
2.24M
    INTxx_FLAG_SIGNED, "INT32"
253
2.24M
ASN1_ITEM_end(INT32)
254
255
302k
ASN1_ITEM_start(UINT32)
256
302k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
257
302k
ASN1_ITEM_end(UINT32)
258
259
48.9k
ASN1_ITEM_start(INT64)
260
48.9k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
261
48.9k
    INTxx_FLAG_SIGNED, "INT64"
262
48.9k
ASN1_ITEM_end(INT64)
263
264
48.9k
ASN1_ITEM_start(UINT64)
265
48.9k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
266
48.9k
ASN1_ITEM_end(UINT64)
267
268
204k
ASN1_ITEM_start(ZINT32)
269
204k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
270
204k
    INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"
271
204k
ASN1_ITEM_end(ZINT32)
272
273
431k
ASN1_ITEM_start(ZUINT32)
274
431k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
275
431k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"
276
431k
ASN1_ITEM_end(ZUINT32)
277
278
306k
ASN1_ITEM_start(ZINT64)
279
306k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
280
306k
    INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"
281
306k
ASN1_ITEM_end(ZINT64)
282
283
304k
ASN1_ITEM_start(ZUINT64)
284
304k
    ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
285
304k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"
286
304k
ASN1_ITEM_end(ZUINT64)
287