Coverage Report

Created: 2026-04-22 06:14

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