Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/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
#ifndef OPENSSL_NO_DEPRECATED_4_1
19
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
20
595
{
21
595
    return ossl_asn1_string_set_internal(x, d, len, /*add_nul_byte=*/0);
22
595
}
23
#endif
24
25
int ossl_i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
26
5.23M
{
27
5.23M
    int ret = 0, bits = 0, len;
28
5.23M
    unsigned char *p, *d;
29
30
5.23M
    if (a == NULL)
31
0
        goto err;
32
33
5.23M
    len = a->length;
34
35
5.23M
    if (len > INT_MAX - 1 || len < 0)
36
0
        goto err;
37
38
5.23M
    if ((len > 0) && (a->flags & ASN1_STRING_FLAG_BITS_LEFT))
39
3.35M
        bits = (int)a->flags & 0x07;
40
41
5.23M
    if (pp == NULL)
42
4.23M
        goto done;
43
44
999k
    p = *pp;
45
46
999k
    *(p++) = (unsigned char)bits;
47
999k
    d = a->data;
48
999k
    if (len > 0) {
49
621k
        memcpy(p, d, len);
50
621k
        p += len;
51
621k
        p[-1] &= (0xff << bits);
52
621k
    }
53
999k
    *pp = p;
54
55
5.23M
done:
56
5.23M
    ret = len + 1;
57
58
5.23M
err:
59
5.23M
    return ret;
60
5.23M
}
61
62
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
63
    const unsigned char **pp, long len)
64
5.99M
{
65
5.99M
    ASN1_BIT_STRING *ret = NULL;
66
5.99M
    const unsigned char *p;
67
5.99M
    unsigned char *s;
68
5.99M
    int i = 0;
69
70
5.99M
    if (len < 1) {
71
27.5k
        i = ASN1_R_STRING_TOO_SHORT;
72
27.5k
        goto err;
73
27.5k
    }
74
75
5.97M
    if (len > INT_MAX) {
76
0
        i = ASN1_R_STRING_TOO_LONG;
77
0
        goto err;
78
0
    }
79
80
5.97M
    if ((a == NULL) || ((*a) == NULL)) {
81
1.62M
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
82
0
            return NULL;
83
1.62M
    } else
84
4.34M
        ret = (*a);
85
86
5.97M
    p = *pp;
87
5.97M
    i = *(p++);
88
5.97M
    if (i > 7) {
89
18.0k
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
90
18.0k
        goto err;
91
18.0k
    }
92
    /*
93
     * We do this to preserve the settings.  If we modify the settings, via
94
     * the _set_bit function, we will recalculate on output
95
     */
96
5.95M
    ossl_asn1_bit_string_set_unused_bits(ret, i);
97
98
5.95M
    if (len-- > 1) { /* using one because of the bits left byte */
99
4.44M
        s = OPENSSL_malloc((int)len);
100
4.44M
        if (s == NULL) {
101
0
            goto err;
102
0
        }
103
4.44M
        memcpy(s, p, (int)len);
104
4.44M
        s[len - 1] &= (0xff << i);
105
4.44M
        p += len;
106
4.44M
    } else
107
1.51M
        s = NULL;
108
109
5.95M
    ASN1_STRING_set0(ret, s, (int)len);
110
5.95M
    ret->type = V_ASN1_BIT_STRING;
111
5.95M
    if (a != NULL)
112
5.95M
        (*a) = ret;
113
5.95M
    *pp = p;
114
5.95M
    return ret;
115
45.5k
err:
116
45.5k
    if (i != 0)
117
45.5k
        ERR_raise(ERR_LIB_ASN1, i);
118
45.5k
    if ((a == NULL) || (*a != ret))
119
33.7k
        ASN1_BIT_STRING_free(ret);
120
45.5k
    return NULL;
121
5.95M
}
122
123
/*
124
 * These next 2 functions from Goetz Babin-Ebell.
125
 */
126
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
127
17.5k
{
128
17.5k
    int w, v, iv;
129
17.5k
    unsigned char *c;
130
131
17.5k
    if (n < 0)
132
112
        return 0;
133
134
17.4k
    w = n / 8;
135
17.4k
    v = 1 << (7 - (n & 0x07));
136
17.4k
    iv = ~v;
137
17.4k
    if (!value)
138
0
        v = 0;
139
140
17.4k
    if (a == NULL)
141
0
        return 0;
142
143
17.4k
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
144
145
17.4k
    if ((a->length < (w + 1)) || (a->data == NULL)) {
146
14.9k
        if (!value)
147
0
            return 1; /* Don't need to set */
148
14.9k
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
149
14.9k
        if (c == NULL)
150
0
            return 0;
151
14.9k
        if (w + 1 - a->length > 0)
152
14.9k
            memset(c + a->length, 0, w + 1 - a->length);
153
14.9k
        a->data = c;
154
14.9k
        a->length = w + 1;
155
14.9k
    }
156
17.4k
    a->data[w] = ((a->data[w]) & iv) | v;
157
158
17.4k
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
159
0
        a->length--;
160
161
17.4k
    if (a->length > 0) {
162
17.4k
        uint8_t u8 = a->data[a->length - 1];
163
17.4k
        uint8_t unused_bits = 7;
164
165
        /* Only keep least significant bit; count trailing zeroes. */
166
17.4k
        u8 &= 0x100 - u8;
167
17.4k
        if ((u8 & 0x0f) != 0)
168
2.42k
            unused_bits -= 4;
169
17.4k
        if ((u8 & 0x33) != 0)
170
14.0k
            unused_bits -= 2;
171
17.4k
        if ((u8 & 0x55) != 0)
172
3.22k
            unused_bits -= 1;
173
17.4k
        ossl_asn1_bit_string_set_unused_bits(a, unused_bits);
174
17.4k
    }
175
17.4k
    return 1;
176
17.4k
}
177
178
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
179
793k
{
180
793k
    int w, v;
181
182
793k
    if (n < 0)
183
0
        return 0;
184
185
793k
    w = n / 8;
186
793k
    v = 1 << (7 - (n & 0x07));
187
793k
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
188
173k
        return 0;
189
620k
    return ((a->data[w] & v) != 0);
190
793k
}
191
192
/*
193
 * Checks if the given bit string contains only bits specified by
194
 * the flags vector. Returns 0 if there is at least one bit set in 'a'
195
 * which is not specified in 'flags', 1 otherwise.
196
 * 'len' is the length of 'flags'.
197
 */
198
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
199
    const unsigned char *flags, int flags_len)
200
0
{
201
0
    int i, ok;
202
    /* Check if there is one bit set at all. */
203
0
    if (!a || !a->data)
204
0
        return 1;
205
206
    /*
207
     * Check each byte of the internal representation of the bit string.
208
     */
209
0
    ok = 1;
210
0
    for (i = 0; i < a->length && ok; ++i) {
211
0
        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
212
        /* We are done if there is an unneeded bit set. */
213
0
        ok = (a->data[i] & mask) == 0;
214
0
    }
215
0
    return ok;
216
0
}
217
218
int ASN1_BIT_STRING_get_length(const ASN1_BIT_STRING *abs, size_t *out_length,
219
    int *out_unused_bits)
220
0
{
221
0
    size_t length;
222
0
    int unused_bits;
223
224
0
    if (abs == NULL || abs->type != V_ASN1_BIT_STRING)
225
0
        return 0;
226
227
0
    if (out_length == NULL || out_unused_bits == NULL || abs->length < 0)
228
0
        return 0;
229
230
0
    length = abs->length;
231
0
    unused_bits = 0;
232
233
0
    if ((abs->flags & ASN1_STRING_FLAG_BITS_LEFT) != 0)
234
0
        unused_bits = abs->flags & 0x07;
235
236
0
    if (length == 0 && unused_bits != 0)
237
0
        return 0;
238
239
0
    if (unused_bits != 0) {
240
0
        unsigned char mask = (1 << unused_bits) - 1;
241
0
        if ((abs->data[length - 1] & mask) != 0)
242
0
            return 0;
243
0
    }
244
245
0
    *out_length = length;
246
0
    *out_unused_bits = unused_bits;
247
248
0
    return 1;
249
0
}
250
251
int ASN1_BIT_STRING_set1(ASN1_BIT_STRING *abs, const uint8_t *data, size_t length,
252
    int unused_bits)
253
12.9k
{
254
12.9k
    if (abs == NULL)
255
0
        return 0;
256
257
12.9k
    if (length > INT_MAX || unused_bits < 0 || unused_bits > 7)
258
0
        return 0;
259
260
12.9k
    if (length == 0 && unused_bits != 0)
261
0
        return 0;
262
263
12.9k
    if (length > 0 && (data[length - 1] & ((1 << unused_bits) - 1)) != 0)
264
0
        return 0;
265
266
12.9k
    if (!ossl_asn1_string_set_internal(abs, data, (int)length, /*add_nul_byte=*/0))
267
0
        return 0;
268
269
12.9k
    abs->type = V_ASN1_BIT_STRING;
270
271
12.9k
    ossl_asn1_bit_string_set_unused_bits(abs, unused_bits);
272
273
12.9k
    return 1;
274
12.9k
}