Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
        return 0;
33
25.0k
    return 1;
34
25.0k
}
35
36
static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
37
285k
{
38
285k
    OPENSSL_free(*pval);
39
285k
    *pval = NULL;
40
285k
}
41
42
static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
43
477k
{
44
477k
    **(uint64_t **)pval = 0;
45
477k
}
46
47
static int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
48
    const ASN1_ITEM *it)
49
58.5k
{
50
58.5k
    uint64_t utmp;
51
58.5k
    int neg = 0;
52
    /* this exists to bypass broken gcc optimization */
53
58.5k
    char *cp = (char *)*pval;
54
55
    /* use memcpy, because we may not be uint64_t aligned */
56
58.5k
    memcpy(&utmp, cp, sizeof(utmp));
57
58
58.5k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
59
47.9k
        && utmp == 0)
60
11.6k
        return -1;
61
46.9k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
62
35.1k
        && (int64_t)utmp < 0) {
63
        /* ossl_i2c_uint64_int() assumes positive values */
64
6.43k
        utmp = 0 - utmp;
65
6.43k
        neg = 1;
66
6.43k
    }
67
68
46.9k
    return ossl_i2c_uint64_int(cont, utmp, neg);
69
58.5k
}
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
16.3k
{
74
16.3k
    uint64_t utmp = 0;
75
16.3k
    char *cp;
76
16.3k
    int neg = 0;
77
78
16.3k
    if (*pval == NULL && !uint64_new(pval, it))
79
0
        return 0;
80
81
16.3k
    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
16.3k
    if (len == 0)
90
45
        goto long_compat;
91
92
16.2k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
93
2.57k
        return 0;
94
13.6k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
95
2.17k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
96
2.17k
        return 0;
97
2.17k
    }
98
11.5k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
99
7.73k
        && !neg && utmp > INT64_MAX) {
100
764
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
101
764
        return 0;
102
764
    }
103
10.7k
    if (neg)
104
        /* ossl_c2i_uint64_int() returns positive values */
105
3.36k
        utmp = 0 - utmp;
106
107
10.7k
long_compat:
108
10.7k
    memcpy(cp, &utmp, sizeof(utmp));
109
10.7k
    return 1;
110
10.7k
}
111
112
static int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
113
    int indent, const ASN1_PCTX *pctx)
114
7.04k
{
115
7.04k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
116
4.22k
        return BIO_printf(out, "%jd\n", **(int64_t **)pval);
117
2.82k
    return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
118
7.04k
}
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
285k
{
131
285k
    OPENSSL_free(*pval);
132
285k
    *pval = NULL;
133
285k
}
134
135
static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
136
2.45M
{
137
2.45M
    **(uint32_t **)pval = 0;
138
2.45M
}
139
140
static int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
141
    const ASN1_ITEM *it)
142
488k
{
143
488k
    uint32_t utmp;
144
488k
    int neg = 0;
145
    /* this exists to bypass broken gcc optimization */
146
488k
    char *cp = (char *)*pval;
147
148
    /* use memcpy, because we may not be uint32_t aligned */
149
488k
    memcpy(&utmp, cp, sizeof(utmp));
150
151
488k
    if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
152
35.0k
        && utmp == 0)
153
28.1k
        return -1;
154
460k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
155
436k
        && (int32_t)utmp < 0) {
156
        /* ossl_i2c_uint64_int() assumes positive values */
157
97.4k
        utmp = 0 - utmp;
158
97.4k
        neg = 1;
159
97.4k
    }
160
161
460k
    return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
162
488k
}
163
164
/*
165
 * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
166
 * overflow warnings.
167
 */
168
169
132k
#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
944k
{
174
944k
    uint64_t utmp = 0;
175
944k
    uint32_t utmp2 = 0;
176
944k
    char *cp;
177
944k
    int neg = 0;
178
179
944k
    if (*pval == NULL && !uint64_new(pval, it))
180
0
        return 0;
181
182
944k
    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
944k
    if (len == 0)
191
106k
        goto long_compat;
192
193
837k
    if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
194
35.2k
        return 0;
195
802k
    if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
196
6.68k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
197
6.68k
        return 0;
198
6.68k
    }
199
795k
    if (neg) {
200
132k
        if (utmp > ABS_INT32_MIN) {
201
23.1k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
202
23.1k
            return 0;
203
23.1k
        }
204
109k
        utmp = 0 - utmp;
205
663k
    } else {
206
663k
        if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
207
639k
            || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
208
26.1k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
209
26.1k
            return 0;
210
26.1k
        }
211
663k
    }
212
213
853k
long_compat:
214
853k
    utmp2 = (uint32_t)utmp;
215
853k
    memcpy(cp, &utmp2, sizeof(utmp2));
216
853k
    return 1;
217
795k
}
218
219
static int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
220
    int indent, const ASN1_PCTX *pctx)
221
15.3k
{
222
15.3k
    if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
223
14.1k
        return BIO_printf(out, "%d\n", (int)**(int32_t **)pval);
224
1.20k
    return BIO_printf(out, "%u\n", (unsigned int)**(uint32_t **)pval);
225
15.3k
}
226
227
/* Define the primitives themselves */
228
229
static ASN1_PRIMITIVE_FUNCS uint32_pf = {
230
    NULL, 0,
231
    uint32_new,
232
    uint32_free,
233
    uint32_clear,
234
    uint32_c2i,
235
    uint32_i2c,
236
    uint32_print
237
};
238
239
static ASN1_PRIMITIVE_FUNCS uint64_pf = {
240
    NULL, 0,
241
    uint64_new,
242
    uint64_free,
243
    uint64_clear,
244
    uint64_c2i,
245
    uint64_i2c,
246
    uint64_print
247
};
248
249
3.47M
ASN1_ITEM_start(INT32)
250
3.47M
    ASN1_ITYPE_PRIMITIVE,
251
3.47M
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
252
3.47M
    INTxx_FLAG_SIGNED, "INT32" ASN1_ITEM_end(INT32)
253
254
441k
                           ASN1_ITEM_start(UINT32) ASN1_ITYPE_PRIMITIVE,
255
441k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32" ASN1_ITEM_end(UINT32)
256
257
71.4k
                                                ASN1_ITEM_start(INT64) ASN1_ITYPE_PRIMITIVE,
258
71.4k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
259
71.4k
    INTxx_FLAG_SIGNED, "INT64" ASN1_ITEM_end(INT64)
260
261
71.4k
                           ASN1_ITEM_start(UINT64) ASN1_ITYPE_PRIMITIVE,
262
71.4k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64" ASN1_ITEM_end(UINT64)
263
264
286k
                                                ASN1_ITEM_start(ZINT32) ASN1_ITYPE_PRIMITIVE,
265
286k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
266
286k
    INTxx_FLAG_ZERO_DEFAULT | INTxx_FLAG_SIGNED, "ZINT32" ASN1_ITEM_end(ZINT32)
267
268
627k
                                                     ASN1_ITEM_start(ZUINT32) ASN1_ITYPE_PRIMITIVE,
269
627k
    V_ASN1_INTEGER, NULL, 0, &uint32_pf,
270
627k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT32" ASN1_ITEM_end(ZUINT32)
271
272
447k
                                 ASN1_ITEM_start(ZINT64) ASN1_ITYPE_PRIMITIVE,
273
447k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
274
447k
    INTxx_FLAG_ZERO_DEFAULT | INTxx_FLAG_SIGNED, "ZINT64" ASN1_ITEM_end(ZINT64)
275
276
443k
                                                     ASN1_ITEM_start(ZUINT64) ASN1_ITYPE_PRIMITIVE,
277
443k
    V_ASN1_INTEGER, NULL, 0, &uint64_pf,
278
443k
    INTxx_FLAG_ZERO_DEFAULT, "ZUINT64" ASN1_ITEM_end(ZUINT64)