Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/asn1/x_long.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2020 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 <openssl/asn1t.h>
13
14
161k
#define COPY_SIZE(a, b) (sizeof(a) < sizeof(b) ? sizeof(a) : sizeof(b))
15
16
/*
17
 * Custom primitive type for long handling. This converts between an
18
 * ASN1_INTEGER and a long directly.
19
 */
20
21
static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
22
static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
23
24
static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
25
    const ASN1_ITEM *it);
26
static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
27
    int utype, char *free_cont, const ASN1_ITEM *it);
28
static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
29
    int indent, const ASN1_PCTX *pctx);
30
31
static ASN1_PRIMITIVE_FUNCS long_pf = {
32
    NULL, 0,
33
    long_new,
34
    long_free,
35
    long_free, /* Clear should set to initial value */
36
    long_c2i,
37
    long_i2c,
38
    long_print
39
};
40
41
71.4k
ASN1_ITEM_start(LONG)
42
71.4k
    ASN1_ITYPE_PRIMITIVE,
43
71.4k
    V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG" ASN1_ITEM_end(LONG)
44
45
71.4k
                                                            ASN1_ITEM_start(ZLONG) ASN1_ITYPE_PRIMITIVE,
46
71.4k
    V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG" ASN1_ITEM_end(ZLONG)
47
48
                                              static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
49
0
{
50
0
    memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
51
0
    return 1;
52
0
}
53
54
static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
55
142k
{
56
142k
    memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
57
142k
}
58
59
/*
60
 * Originally BN_num_bits_word was called to perform this operation, but
61
 * trouble is that there is no guarantee that sizeof(long) equals to
62
 * sizeof(BN_ULONG). BN_ULONG is a configurable type that can be as wide
63
 * as long, but also double or half...
64
 */
65
static int num_bits_ulong(unsigned long value)
66
11.3k
{
67
11.3k
    size_t i;
68
11.3k
    unsigned long ret = 0;
69
70
    /*
71
     * It is argued that *on average* constant counter loop performs
72
     * not worse [if not better] than one with conditional break or
73
     * mask-n-table-lookup-style, because of branch misprediction
74
     * penalties.
75
     */
76
736k
    for (i = 0; i < sizeof(value) * 8; i++) {
77
725k
        ret += (value != 0);
78
725k
        value >>= 1;
79
725k
    }
80
81
11.3k
    return (int)ret;
82
11.3k
}
83
84
static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
85
    const ASN1_ITEM *it)
86
11.3k
{
87
11.3k
    long ltmp;
88
11.3k
    unsigned long utmp, sign;
89
11.3k
    int clen, pad, i;
90
91
11.3k
    memcpy(&ltmp, pval, COPY_SIZE(*pval, ltmp));
92
11.3k
    if (ltmp == it->size)
93
0
        return -1;
94
    /*
95
     * Convert the long to positive: we subtract one if negative so we can
96
     * cleanly handle the padding if only the MSB of the leading octet is
97
     * set.
98
     */
99
11.3k
    if (ltmp < 0) {
100
5.24k
        sign = 0xff;
101
5.24k
        utmp = 0 - (unsigned long)ltmp - 1;
102
6.09k
    } else {
103
6.09k
        sign = 0;
104
6.09k
        utmp = ltmp;
105
6.09k
    }
106
11.3k
    clen = num_bits_ulong(utmp);
107
    /* If MSB of leading octet set we need to pad */
108
11.3k
    if (!(clen & 0x7))
109
2.06k
        pad = 1;
110
9.27k
    else
111
9.27k
        pad = 0;
112
113
    /* Convert number of bits to number of octets */
114
11.3k
    clen = (clen + 7) >> 3;
115
116
11.3k
    if (cont != NULL) {
117
3.77k
        if (pad)
118
688
            *cont++ = (unsigned char)sign;
119
20.8k
        for (i = clen - 1; i >= 0; i--) {
120
17.1k
            cont[i] = (unsigned char)(utmp ^ sign);
121
17.1k
            utmp >>= 8;
122
17.1k
        }
123
3.77k
    }
124
11.3k
    return clen + pad;
125
11.3k
}
126
127
static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
128
    int utype, char *free_cont, const ASN1_ITEM *it)
129
6.26k
{
130
6.26k
    int i;
131
6.26k
    long ltmp;
132
6.26k
    unsigned long utmp = 0, sign = 0x100;
133
134
6.26k
    if (len > 1) {
135
        /*
136
         * Check possible pad byte.  Worst case, we're skipping past actual
137
         * content, but since that's only with 0x00 and 0xff and we set neg
138
         * accordingly, the result will be correct in the end anyway.
139
         */
140
5.95k
        switch (cont[0]) {
141
1.41k
        case 0xff:
142
1.41k
            cont++;
143
1.41k
            len--;
144
1.41k
            sign = 0xff;
145
1.41k
            break;
146
1.28k
        case 0:
147
1.28k
            cont++;
148
1.28k
            len--;
149
1.28k
            sign = 0;
150
1.28k
            break;
151
5.95k
        }
152
5.95k
    }
153
6.26k
    if (len > (int)sizeof(long)) {
154
544
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
155
544
        return 0;
156
544
    }
157
158
5.72k
    if (sign == 0x100) {
159
        /* Is it negative? */
160
3.13k
        if (len && (cont[0] & 0x80))
161
1.53k
            sign = 0xff;
162
1.60k
        else
163
1.60k
            sign = 0;
164
3.13k
    } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */
165
740
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
166
740
        return 0;
167
740
    }
168
4.98k
    utmp = 0;
169
31.5k
    for (i = 0; i < len; i++) {
170
26.5k
        utmp <<= 8;
171
26.5k
        utmp |= cont[i] ^ sign;
172
26.5k
    }
173
4.98k
    ltmp = (long)utmp;
174
4.98k
    if (ltmp < 0) {
175
1.17k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
176
1.17k
        return 0;
177
1.17k
    }
178
3.81k
    if (sign)
179
1.74k
        ltmp = -ltmp - 1;
180
3.81k
    if (ltmp == it->size) {
181
20
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
182
20
        return 0;
183
20
    }
184
3.79k
    memcpy(pval, &ltmp, COPY_SIZE(*pval, ltmp));
185
3.79k
    return 1;
186
3.81k
}
187
188
static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
189
    int indent, const ASN1_PCTX *pctx)
190
3.77k
{
191
3.77k
    long l;
192
193
3.77k
    memcpy(&l, pval, COPY_SIZE(*pval, l));
194
3.77k
    return BIO_printf(out, "%ld\n", l);
195
3.77k
}