Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/asn1/a_bitstr.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2023 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
0
{
21
0
    abs->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
22
0
}
23
24
static int asn1_bit_string_set_unused_bits(ASN1_BIT_STRING *abs,
25
    uint8_t unused_bits)
26
0
{
27
0
    if (unused_bits > 7)
28
0
        return 0;
29
30
0
    asn1_bit_string_clear_unused_bits(abs);
31
32
0
    abs->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
33
34
0
    return 1;
35
0
}
36
37
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
38
0
{
39
0
    return ASN1_STRING_set(x, d, len);
40
0
}
41
42
int ossl_i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
43
0
{
44
0
    int ret = 0, bits = 0, len;
45
0
    unsigned char *p, *d;
46
47
0
    if (a == NULL)
48
0
        goto err;
49
50
0
    len = a->length;
51
52
0
    if (len > INT_MAX - 1)
53
0
        goto err;
54
55
0
    if ((len > 0) && (a->flags & ASN1_STRING_FLAG_BITS_LEFT))
56
0
        bits = (int)a->flags & 0x07;
57
58
0
    if (pp == NULL)
59
0
        goto done;
60
61
0
    p = *pp;
62
63
0
    *(p++) = (unsigned char)bits;
64
0
    d = a->data;
65
0
    if (len > 0) {
66
0
        memcpy(p, d, len);
67
0
        p += len;
68
0
        p[-1] &= (0xff << bits);
69
0
    }
70
0
    *pp = p;
71
72
0
done:
73
0
    ret = len + 1;
74
75
0
err:
76
0
    return ret;
77
0
}
78
79
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
80
    const unsigned char **pp, long len)
81
0
{
82
0
    ASN1_BIT_STRING *ret = NULL;
83
0
    const unsigned char *p;
84
0
    unsigned char *s;
85
0
    int i = 0;
86
87
0
    if (len < 1) {
88
0
        i = ASN1_R_STRING_TOO_SHORT;
89
0
        goto err;
90
0
    }
91
92
0
    if (len > INT_MAX) {
93
0
        i = ASN1_R_STRING_TOO_LONG;
94
0
        goto err;
95
0
    }
96
97
0
    if ((a == NULL) || ((*a) == NULL)) {
98
0
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
99
0
            return NULL;
100
0
    } else
101
0
        ret = (*a);
102
103
0
    p = *pp;
104
0
    i = *(p++);
105
0
    if (i > 7) {
106
0
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
107
0
        goto err;
108
0
    }
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
0
    ossl_asn1_string_set_bits_left(ret, i);
114
115
0
    if (len-- > 1) { /* using one because of the bits left byte */
116
0
        s = OPENSSL_malloc((int)len);
117
0
        if (s == NULL) {
118
0
            goto err;
119
0
        }
120
0
        memcpy(s, p, (int)len);
121
0
        s[len - 1] &= (0xff << i);
122
0
        p += len;
123
0
    } else
124
0
        s = NULL;
125
126
0
    ASN1_STRING_set0(ret, s, (int)len);
127
0
    ret->type = V_ASN1_BIT_STRING;
128
0
    if (a != NULL)
129
0
        (*a) = ret;
130
0
    *pp = p;
131
0
    return ret;
132
0
err:
133
0
    if (i != 0)
134
0
        ERR_raise(ERR_LIB_ASN1, i);
135
0
    if ((a == NULL) || (*a != ret))
136
0
        ASN1_BIT_STRING_free(ret);
137
0
    return NULL;
138
0
}
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
0
{
145
0
    int w, v, iv;
146
0
    unsigned char *c;
147
148
0
    if (n < 0)
149
0
        return 0;
150
151
0
    w = n / 8;
152
0
    v = 1 << (7 - (n & 0x07));
153
0
    iv = ~v;
154
0
    if (!value)
155
0
        v = 0;
156
157
0
    if (a == NULL)
158
0
        return 0;
159
160
0
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
161
162
0
    if ((a->length < (w + 1)) || (a->data == NULL)) {
163
0
        if (!value)
164
0
            return 1; /* Don't need to set */
165
0
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
166
0
        if (c == NULL)
167
0
            return 0;
168
0
        if (w + 1 - a->length > 0)
169
0
            memset(c + a->length, 0, w + 1 - a->length);
170
0
        a->data = c;
171
0
        a->length = w + 1;
172
0
    }
173
0
    a->data[w] = ((a->data[w]) & iv) | v;
174
175
0
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
176
0
        a->length--;
177
178
0
    if (a->length > 0) {
179
0
        uint8_t u8 = a->data[a->length - 1];
180
0
        uint8_t unused_bits = 7;
181
182
        /* Only keep least significant bit; count trailing zeroes. */
183
0
        u8 &= 0x100 - u8;
184
0
        if ((u8 & 0x0f) != 0)
185
0
            unused_bits -= 4;
186
0
        if ((u8 & 0x33) != 0)
187
0
            unused_bits -= 2;
188
0
        if ((u8 & 0x55) != 0)
189
0
            unused_bits -= 1;
190
191
0
        if (!asn1_bit_string_set_unused_bits(a, unused_bits))
192
0
            return 0;
193
0
    }
194
0
    return 1;
195
0
}
196
197
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
198
0
{
199
0
    int w, v;
200
201
0
    if (n < 0)
202
0
        return 0;
203
204
0
    w = n / 8;
205
0
    v = 1 << (7 - (n & 0x07));
206
0
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
207
0
        return 0;
208
0
    return ((a->data[w] & v) != 0);
209
0
}
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
0
{
273
0
    if (abs == NULL)
274
0
        return 0;
275
276
0
    if (length > INT_MAX || unused_bits < 0 || unused_bits > 7)
277
0
        return 0;
278
279
0
    if (length == 0 && unused_bits != 0)
280
0
        return 0;
281
282
0
    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
0
    if (!ASN1_STRING_set(abs, data, (int)length))
292
0
        return 0;
293
0
    abs->type = V_ASN1_BIT_STRING;
294
295
0
    return asn1_bit_string_set_unused_bits(abs, unused_bits);
296
0
}