Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
17
983
{
18
983
    return ASN1_STRING_set(x, d, len);
19
983
}
20
21
int ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22
16.4M
{
23
16.4M
    int ret, j, bits, len;
24
16.4M
    unsigned char *p, *d;
25
26
16.4M
    if (a == NULL)
27
0
        return 0;
28
29
16.4M
    len = a->length;
30
31
16.4M
    if (len > 0) {
32
8.87M
        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33
8.71M
            bits = (int)a->flags & 0x07;
34
8.71M
        } else {
35
154k
            for (; len > 0; len--) {
36
154k
                if (a->data[len - 1])
37
154k
                    break;
38
154k
            }
39
40
154k
            if (len == 0) {
41
0
                bits = 0;
42
154k
            } else {
43
154k
                j = a->data[len - 1];
44
154k
                if (j & 0x01)
45
0
                    bits = 0;
46
154k
                else if (j & 0x02)
47
0
                    bits = 1;
48
154k
                else if (j & 0x04)
49
0
                    bits = 2;
50
154k
                else if (j & 0x08)
51
0
                    bits = 3;
52
154k
                else if (j & 0x10)
53
0
                    bits = 4;
54
154k
                else if (j & 0x20)
55
154k
                    bits = 5;
56
0
                else if (j & 0x40)
57
0
                    bits = 6;
58
0
                else if (j & 0x80)
59
0
                    bits = 7;
60
0
                else
61
0
                    bits = 0;       /* should not happen */
62
154k
            }
63
154k
        }
64
8.87M
    } else
65
7.53M
        bits = 0;
66
67
16.4M
    ret = 1 + len;
68
16.4M
    if (pp == NULL)
69
13.1M
        return ret;
70
71
3.21M
    p = *pp;
72
73
3.21M
    *(p++) = (unsigned char)bits;
74
3.21M
    d = a->data;
75
3.21M
    if (len > 0) {
76
1.68M
        memcpy(p, d, len);
77
1.68M
        p += len;
78
1.68M
        p[-1] &= (0xff << bits);
79
1.68M
    }
80
3.21M
    *pp = p;
81
3.21M
    return ret;
82
16.4M
}
83
84
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
85
                                          const unsigned char **pp, long len)
86
5.24M
{
87
5.24M
    ASN1_BIT_STRING *ret = NULL;
88
5.24M
    const unsigned char *p;
89
5.24M
    unsigned char *s;
90
5.24M
    int i = 0;
91
92
5.24M
    if (len < 1) {
93
32.1k
        i = ASN1_R_STRING_TOO_SHORT;
94
32.1k
        goto err;
95
32.1k
    }
96
97
5.20M
    if (len > INT_MAX) {
98
0
        i = ASN1_R_STRING_TOO_LONG;
99
0
        goto err;
100
0
    }
101
102
5.20M
    if ((a == NULL) || ((*a) == NULL)) {
103
1.32M
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
104
0
            return NULL;
105
1.32M
    } else
106
3.88M
        ret = (*a);
107
108
5.20M
    p = *pp;
109
5.20M
    i = *(p++);
110
5.20M
    if (i > 7) {
111
19.9k
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
112
19.9k
        goto err;
113
19.9k
    }
114
    /*
115
     * We do this to preserve the settings.  If we modify the settings, via
116
     * the _set_bit function, we will recalculate on output
117
     */
118
5.18M
    ossl_asn1_string_set_bits_left(ret, i);
119
120
5.18M
    if (len-- > 1) {            /* using one because of the bits left byte */
121
3.62M
        s = OPENSSL_malloc((int)len);
122
3.62M
        if (s == NULL) {
123
0
            goto err;
124
0
        }
125
3.62M
        memcpy(s, p, (int)len);
126
3.62M
        s[len - 1] &= (0xff << i);
127
3.62M
        p += len;
128
3.62M
    } else
129
1.56M
        s = NULL;
130
131
5.18M
    ASN1_STRING_set0(ret, s, (int)len);
132
5.18M
    ret->type = V_ASN1_BIT_STRING;
133
5.18M
    if (a != NULL)
134
5.18M
        (*a) = ret;
135
5.18M
    *pp = p;
136
5.18M
    return ret;
137
52.0k
 err:
138
52.0k
    if (i != 0)
139
52.0k
        ERR_raise(ERR_LIB_ASN1, i);
140
52.0k
    if ((a == NULL) || (*a != ret))
141
40.6k
        ASN1_BIT_STRING_free(ret);
142
52.0k
    return NULL;
143
5.18M
}
144
145
/*
146
 * These next 2 functions from Goetz Babin-Ebell.
147
 */
148
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
149
38.5k
{
150
38.5k
    int w, v, iv;
151
38.5k
    unsigned char *c;
152
153
38.5k
    if (n < 0)
154
0
        return 0;
155
156
38.5k
    w = n / 8;
157
38.5k
    v = 1 << (7 - (n & 0x07));
158
38.5k
    iv = ~v;
159
38.5k
    if (!value)
160
0
        v = 0;
161
162
38.5k
    if (a == NULL)
163
0
        return 0;
164
165
38.5k
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
166
167
38.5k
    if ((a->length < (w + 1)) || (a->data == NULL)) {
168
38.5k
        if (!value)
169
0
            return 1;         /* Don't need to set */
170
38.5k
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
171
38.5k
        if (c == NULL)
172
0
            return 0;
173
38.5k
        if (w + 1 - a->length > 0)
174
38.5k
            memset(c + a->length, 0, w + 1 - a->length);
175
38.5k
        a->data = c;
176
38.5k
        a->length = w + 1;
177
38.5k
    }
178
38.5k
    a->data[w] = ((a->data[w]) & iv) | v;
179
38.5k
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
180
0
        a->length--;
181
38.5k
    return 1;
182
38.5k
}
183
184
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
185
860k
{
186
860k
    int w, v;
187
188
860k
    if (n < 0)
189
0
        return 0;
190
191
860k
    w = n / 8;
192
860k
    v = 1 << (7 - (n & 0x07));
193
860k
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
194
146k
        return 0;
195
713k
    return ((a->data[w] & v) != 0);
196
860k
}
197
198
/*
199
 * Checks if the given bit string contains only bits specified by
200
 * the flags vector. Returns 0 if there is at least one bit set in 'a'
201
 * which is not specified in 'flags', 1 otherwise.
202
 * 'len' is the length of 'flags'.
203
 */
204
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
205
                          const unsigned char *flags, int flags_len)
206
0
{
207
0
    int i, ok;
208
    /* Check if there is one bit set at all. */
209
0
    if (!a || !a->data)
210
0
        return 1;
211
212
    /*
213
     * Check each byte of the internal representation of the bit string.
214
     */
215
0
    ok = 1;
216
0
    for (i = 0; i < a->length && ok; ++i) {
217
0
        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
218
        /* We are done if there is an unneeded bit set. */
219
0
        ok = (a->data[i] & mask) == 0;
220
0
    }
221
0
    return ok;
222
0
}