Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
158k
#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
70.0k
ASN1_ITEM_start(LONG)
42
70.0k
    ASN1_ITYPE_PRIMITIVE,
43
70.0k
    V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG" ASN1_ITEM_end(LONG)
44
45
70.0k
                                                            ASN1_ITEM_start(ZLONG) ASN1_ITYPE_PRIMITIVE,
46
70.0k
    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
140k
{
56
140k
    memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
57
140k
}
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
10.9k
{
67
10.9k
    size_t i;
68
10.9k
    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
709k
    for (i = 0; i < sizeof(value) * 8; i++) {
77
698k
        ret += (value != 0);
78
698k
        value >>= 1;
79
698k
    }
80
81
10.9k
    return (int)ret;
82
10.9k
}
83
84
static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
85
    const ASN1_ITEM *it)
86
10.9k
{
87
10.9k
    long ltmp;
88
10.9k
    unsigned long utmp, sign;
89
10.9k
    int clen, pad, i;
90
91
10.9k
    memcpy(&ltmp, pval, COPY_SIZE(*pval, ltmp));
92
10.9k
    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
10.9k
    if (ltmp < 0) {
100
4.92k
        sign = 0xff;
101
4.92k
        utmp = 0 - (unsigned long)ltmp - 1;
102
5.98k
    } else {
103
5.98k
        sign = 0;
104
5.98k
        utmp = ltmp;
105
5.98k
    }
106
10.9k
    clen = num_bits_ulong(utmp);
107
    /* If MSB of leading octet set we need to pad */
108
10.9k
    if (!(clen & 0x7))
109
1.88k
        pad = 1;
110
9.03k
    else
111
9.03k
        pad = 0;
112
113
    /* Convert number of bits to number of octets */
114
10.9k
    clen = (clen + 7) >> 3;
115
116
10.9k
    if (cont != NULL) {
117
3.63k
        if (pad)
118
628
            *cont++ = (unsigned char)sign;
119
20.0k
        for (i = clen - 1; i >= 0; i--) {
120
16.4k
            cont[i] = (unsigned char)(utmp ^ sign);
121
16.4k
            utmp >>= 8;
122
16.4k
        }
123
3.63k
    }
124
10.9k
    return clen + pad;
125
10.9k
}
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.03k
{
130
6.03k
    int i;
131
6.03k
    long ltmp;
132
6.03k
    unsigned long utmp = 0, sign = 0x100;
133
134
6.03k
    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.72k
        switch (cont[0]) {
141
1.39k
        case 0xff:
142
1.39k
            cont++;
143
1.39k
            len--;
144
1.39k
            sign = 0xff;
145
1.39k
            break;
146
1.21k
        case 0:
147
1.21k
            cont++;
148
1.21k
            len--;
149
1.21k
            sign = 0;
150
1.21k
            break;
151
5.72k
        }
152
5.72k
    }
153
6.03k
    if (len > (int)sizeof(long)) {
154
464
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
155
464
        return 0;
156
464
    }
157
158
5.57k
    if (sign == 0x100) {
159
        /* Is it negative? */
160
3.05k
        if (len && (cont[0] & 0x80))
161
1.44k
            sign = 0xff;
162
1.61k
        else
163
1.61k
            sign = 0;
164
3.05k
    } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */
165
782
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
166
782
        return 0;
167
782
    }
168
4.78k
    utmp = 0;
169
30.1k
    for (i = 0; i < len; i++) {
170
25.3k
        utmp <<= 8;
171
25.3k
        utmp |= cont[i] ^ sign;
172
25.3k
    }
173
4.78k
    ltmp = (long)utmp;
174
4.78k
    if (ltmp < 0) {
175
1.11k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
176
1.11k
        return 0;
177
1.11k
    }
178
3.67k
    if (sign)
179
1.64k
        ltmp = -ltmp - 1;
180
3.67k
    if (ltmp == it->size) {
181
19
        ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
182
19
        return 0;
183
19
    }
184
3.65k
    memcpy(pval, &ltmp, COPY_SIZE(*pval, ltmp));
185
3.65k
    return 1;
186
3.67k
}
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.63k
{
191
3.63k
    long l;
192
193
3.63k
    memcpy(&l, pval, COPY_SIZE(*pval, l));
194
3.63k
    return BIO_printf(out, "%ld\n", l);
195
3.63k
}