Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/asn1/a_bitstr.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <limits.h>
11
#include <stdio.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/asn1.h>
14
#include "asn1_local.h"
15
16
#include <crypto/asn1.h>
17
18
static void
19
asn1_bit_string_clear_unused_bits(ASN1_BIT_STRING *abs)
20
4.92k
{
21
4.92k
    abs->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
22
4.92k
}
23
24
static int asn1_bit_string_set_unused_bits(ASN1_BIT_STRING *abs,
25
    uint8_t unused_bits)
26
4.92k
{
27
4.92k
    if (unused_bits > 7)
28
0
        return 0;
29
30
4.92k
    asn1_bit_string_clear_unused_bits(abs);
31
32
4.92k
    abs->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
33
34
4.92k
    return 1;
35
4.92k
}
36
37
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
38
663
{
39
663
    return ASN1_STRING_set(x, d, len);
40
663
}
41
42
int ossl_i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
43
6.84M
{
44
6.84M
    int ret = 0, bits = 0, len;
45
6.84M
    unsigned char *p, *d;
46
47
6.84M
    if (a == NULL)
48
0
        goto err;
49
50
6.84M
    len = a->length;
51
52
6.84M
    if (len > INT_MAX - 1)
53
0
        goto err;
54
55
6.84M
    if ((len > 0) && (a->flags & ASN1_STRING_FLAG_BITS_LEFT))
56
4.61M
        bits = (int)a->flags & 0x07;
57
58
6.84M
    if (pp == NULL)
59
5.53M
        goto done;
60
61
1.30M
    p = *pp;
62
63
1.30M
    *(p++) = (unsigned char)bits;
64
1.30M
    d = a->data;
65
1.30M
    if (len > 0) {
66
859k
        memcpy(p, d, len);
67
859k
        p += len;
68
859k
        p[-1] &= (0xff << bits);
69
859k
    }
70
1.30M
    *pp = p;
71
72
6.84M
done:
73
6.84M
    ret = len + 1;
74
75
6.84M
err:
76
6.84M
    return ret;
77
6.84M
}
78
79
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
80
    const unsigned char **pp, long len)
81
5.25M
{
82
5.25M
    ASN1_BIT_STRING *ret = NULL;
83
5.25M
    const unsigned char *p;
84
5.25M
    unsigned char *s;
85
5.25M
    int i = 0;
86
87
5.25M
    if (len < 1) {
88
20.1k
        i = ASN1_R_STRING_TOO_SHORT;
89
20.1k
        goto err;
90
20.1k
    }
91
92
5.23M
    if (len > INT_MAX) {
93
0
        i = ASN1_R_STRING_TOO_LONG;
94
0
        goto err;
95
0
    }
96
97
5.23M
    if ((a == NULL) || ((*a) == NULL)) {
98
1.31M
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
99
0
            return NULL;
100
1.31M
    } else
101
3.91M
        ret = (*a);
102
103
5.23M
    p = *pp;
104
5.23M
    i = *(p++);
105
5.23M
    if (i > 7) {
106
15.2k
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
107
15.2k
        goto err;
108
15.2k
    }
109
    /*
110
     * We do this to preserve the settings.  If we modify the settings, via
111
     * the _set_bit function, we will recalculate on output
112
     */
113
5.22M
    ossl_asn1_string_set_bits_left(ret, i);
114
115
5.22M
    if (len-- > 1) { /* using one because of the bits left byte */
116
3.79M
        s = OPENSSL_malloc((int)len);
117
3.79M
        if (s == NULL) {
118
0
            goto err;
119
0
        }
120
3.79M
        memcpy(s, p, (int)len);
121
3.79M
        s[len - 1] &= (0xff << i);
122
3.79M
        p += len;
123
3.79M
    } else
124
1.42M
        s = NULL;
125
126
5.22M
    ASN1_STRING_set0(ret, s, (int)len);
127
5.22M
    ret->type = V_ASN1_BIT_STRING;
128
5.22M
    if (a != NULL)
129
5.22M
        (*a) = ret;
130
5.22M
    *pp = p;
131
5.22M
    return ret;
132
35.4k
err:
133
35.4k
    if (i != 0)
134
35.4k
        ERR_raise(ERR_LIB_ASN1, i);
135
35.4k
    if ((a == NULL) || (*a != ret))
136
25.7k
        ASN1_BIT_STRING_free(ret);
137
35.4k
    return NULL;
138
5.22M
}
139
140
/*
141
 * These next 2 functions from Goetz Babin-Ebell.
142
 */
143
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
144
4.92k
{
145
4.92k
    int w, v, iv;
146
4.92k
    unsigned char *c;
147
148
4.92k
    if (n < 0)
149
0
        return 0;
150
151
4.92k
    w = n / 8;
152
4.92k
    v = 1 << (7 - (n & 0x07));
153
4.92k
    iv = ~v;
154
4.92k
    if (!value)
155
0
        v = 0;
156
157
4.92k
    if (a == NULL)
158
0
        return 0;
159
160
4.92k
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
161
162
4.92k
    if ((a->length < (w + 1)) || (a->data == NULL)) {
163
4.92k
        if (!value)
164
0
            return 1; /* Don't need to set */
165
4.92k
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
166
4.92k
        if (c == NULL)
167
0
            return 0;
168
4.92k
        if (w + 1 - a->length > 0)
169
4.92k
            memset(c + a->length, 0, w + 1 - a->length);
170
4.92k
        a->data = c;
171
4.92k
        a->length = w + 1;
172
4.92k
    }
173
4.92k
    a->data[w] = ((a->data[w]) & iv) | v;
174
175
4.92k
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
176
0
        a->length--;
177
178
4.92k
    if (a->length > 0) {
179
4.92k
        uint8_t u8 = a->data[a->length - 1];
180
4.92k
        uint8_t unused_bits = 7;
181
182
        /* Only keep least significant bit; count trailing zeroes. */
183
4.92k
        u8 &= 0x100 - u8;
184
4.92k
        if ((u8 & 0x0f) != 0)
185
0
            unused_bits -= 4;
186
4.92k
        if ((u8 & 0x33) != 0)
187
4.92k
            unused_bits -= 2;
188
4.92k
        if ((u8 & 0x55) != 0)
189
0
            unused_bits -= 1;
190
191
4.92k
        if (!asn1_bit_string_set_unused_bits(a, unused_bits))
192
0
            return 0;
193
4.92k
    }
194
4.92k
    return 1;
195
4.92k
}
196
197
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
198
785k
{
199
785k
    int w, v;
200
201
785k
    if (n < 0)
202
0
        return 0;
203
204
785k
    w = n / 8;
205
785k
    v = 1 << (7 - (n & 0x07));
206
785k
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
207
287k
        return 0;
208
497k
    return ((a->data[w] & v) != 0);
209
785k
}
210
211
/*
212
 * Checks if the given bit string contains only bits specified by
213
 * the flags vector. Returns 0 if there is at least one bit set in 'a'
214
 * which is not specified in 'flags', 1 otherwise.
215
 * 'len' is the length of 'flags'.
216
 */
217
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
218
    const unsigned char *flags, int flags_len)
219
0
{
220
0
    int i, ok;
221
    /* Check if there is one bit set at all. */
222
0
    if (!a || !a->data)
223
0
        return 1;
224
225
    /*
226
     * Check each byte of the internal representation of the bit string.
227
     */
228
0
    ok = 1;
229
0
    for (i = 0; i < a->length && ok; ++i) {
230
0
        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
231
        /* We are done if there is an unneeded bit set. */
232
0
        ok = (a->data[i] & mask) == 0;
233
0
    }
234
0
    return ok;
235
0
}
236
237
int ASN1_BIT_STRING_get_length(const ASN1_BIT_STRING *abs, size_t *out_length,
238
    int *out_unused_bits)
239
0
{
240
0
    size_t length;
241
0
    int unused_bits;
242
243
0
    if (abs == NULL || abs->type != V_ASN1_BIT_STRING)
244
0
        return 0;
245
246
0
    if (out_length == NULL || out_unused_bits == NULL)
247
0
        return 0;
248
249
0
    length = abs->length;
250
0
    unused_bits = 0;
251
252
0
    if ((abs->flags & ASN1_STRING_FLAG_BITS_LEFT) != 0)
253
0
        unused_bits = abs->flags & 0x07;
254
255
0
    if (length == 0 && unused_bits != 0)
256
0
        return 0;
257
258
0
    if (unused_bits != 0) {
259
0
        unsigned char mask = (1 << unused_bits) - 1;
260
0
        if ((abs->data[length - 1] & mask) != 0)
261
0
            return 0;
262
0
    }
263
264
0
    *out_length = length;
265
0
    *out_unused_bits = unused_bits;
266
267
0
    return 1;
268
0
}
269
270
int ASN1_BIT_STRING_set1(ASN1_BIT_STRING *abs, const uint8_t *data, size_t length,
271
    int unused_bits)
272
11
{
273
11
    if (abs == NULL)
274
0
        return 0;
275
276
11
    if (length > INT_MAX || unused_bits < 0 || unused_bits > 7)
277
0
        return 0;
278
279
11
    if (length == 0 && unused_bits != 0)
280
0
        return 0;
281
282
11
    if (length > 0 && (data[length - 1] & ((1 << unused_bits) - 1)) != 0)
283
0
        return 0;
284
285
    /*
286
     * XXX - ASN1_STRING_set() and asn1_bit_string_set_unused_bits() preserve the
287
     * state of flags irrelevant to ASN1_BIT_STRING. Should we explicitly
288
     * clear them?
289
     */
290
291
11
    if (!ASN1_STRING_set(abs, data, (int)length))
292
0
        return 0;
293
11
    abs->type = V_ASN1_BIT_STRING;
294
295
11
    return asn1_bit_string_set_unused_bits(abs, unused_bits);
296
11
}