Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/asn1/a_bitstr.c
Line
Count
Source (jump to first uncovered line)
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
488
{
18
488
    return ASN1_STRING_set(x, d, len);
19
488
}
20
21
int ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22
11.9M
{
23
11.9M
    int ret, j, bits, len;
24
11.9M
    unsigned char *p, *d;
25
26
11.9M
    if (a == NULL)
27
0
        return 0;
28
29
11.9M
    len = a->length;
30
31
11.9M
    if (len > 0) {
32
5.32M
        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33
5.21M
            bits = (int)a->flags & 0x07;
34
5.21M
        } else {
35
113k
            for (; len > 0; len--) {
36
113k
                if (a->data[len - 1])
37
113k
                    break;
38
113k
            }
39
40
113k
            if (len == 0) {
41
0
                bits = 0;
42
113k
            } else {
43
113k
                j = a->data[len - 1];
44
113k
                if (j & 0x01)
45
0
                    bits = 0;
46
113k
                else if (j & 0x02)
47
0
                    bits = 1;
48
113k
                else if (j & 0x04)
49
0
                    bits = 2;
50
113k
                else if (j & 0x08)
51
0
                    bits = 3;
52
113k
                else if (j & 0x10)
53
0
                    bits = 4;
54
113k
                else if (j & 0x20)
55
113k
                    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
113k
            }
63
113k
        }
64
5.32M
    } else
65
6.59M
        bits = 0;
66
67
11.9M
    ret = 1 + len;
68
11.9M
    if (pp == NULL)
69
9.60M
        return ret;
70
71
2.32M
    p = *pp;
72
73
2.32M
    *(p++) = (unsigned char)bits;
74
2.32M
    d = a->data;
75
2.32M
    if (len > 0) {
76
995k
        memcpy(p, d, len);
77
995k
        p += len;
78
995k
        p[-1] &= (0xff << bits);
79
995k
    }
80
2.32M
    *pp = p;
81
2.32M
    return ret;
82
11.9M
}
83
84
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
85
                                          const unsigned char **pp, long len)
86
2.87M
{
87
2.87M
    ASN1_BIT_STRING *ret = NULL;
88
2.87M
    const unsigned char *p;
89
2.87M
    unsigned char *s;
90
2.87M
    int i;
91
92
2.87M
    if (len < 1) {
93
5.85k
        i = ASN1_R_STRING_TOO_SHORT;
94
5.85k
        goto err;
95
5.85k
    }
96
97
2.86M
    if (len > INT_MAX) {
98
0
        i = ASN1_R_STRING_TOO_LONG;
99
0
        goto err;
100
0
    }
101
102
2.86M
    if ((a == NULL) || ((*a) == NULL)) {
103
562k
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
104
0
            return NULL;
105
562k
    } else
106
2.30M
        ret = (*a);
107
108
2.86M
    p = *pp;
109
2.86M
    i = *(p++);
110
2.86M
    if (i > 7) {
111
3.72k
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
112
3.72k
        goto err;
113
3.72k
    }
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
2.86M
    ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
119
2.86M
    ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
120
121
2.86M
    if (len-- > 1) {            /* using one because of the bits left byte */
122
1.27M
        s = OPENSSL_malloc((int)len);
123
1.27M
        if (s == NULL) {
124
0
            i = ERR_R_MALLOC_FAILURE;
125
0
            goto err;
126
0
        }
127
1.27M
        memcpy(s, p, (int)len);
128
1.27M
        s[len - 1] &= (0xff << i);
129
1.27M
        p += len;
130
1.27M
    } else
131
1.58M
        s = NULL;
132
133
2.86M
    ret->length = (int)len;
134
2.86M
    OPENSSL_free(ret->data);
135
2.86M
    ret->data = s;
136
2.86M
    ret->type = V_ASN1_BIT_STRING;
137
2.86M
    if (a != NULL)
138
2.86M
        (*a) = ret;
139
2.86M
    *pp = p;
140
2.86M
    return ret;
141
9.57k
 err:
142
9.57k
    ERR_raise(ERR_LIB_ASN1, i);
143
9.57k
    if ((a == NULL) || (*a != ret))
144
5.37k
        ASN1_BIT_STRING_free(ret);
145
9.57k
    return NULL;
146
2.86M
}
147
148
/*
149
 * These next 2 functions from Goetz Babin-Ebell.
150
 */
151
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
152
28.3k
{
153
28.3k
    int w, v, iv;
154
28.3k
    unsigned char *c;
155
156
28.3k
    if (n < 0)
157
0
        return 0;
158
159
28.3k
    w = n / 8;
160
28.3k
    v = 1 << (7 - (n & 0x07));
161
28.3k
    iv = ~v;
162
28.3k
    if (!value)
163
0
        v = 0;
164
165
28.3k
    if (a == NULL)
166
0
        return 0;
167
168
28.3k
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
169
170
28.3k
    if ((a->length < (w + 1)) || (a->data == NULL)) {
171
28.3k
        if (!value)
172
0
            return 1;         /* Don't need to set */
173
28.3k
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
174
28.3k
        if (c == NULL) {
175
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
176
0
            return 0;
177
0
        }
178
28.3k
        if (w + 1 - a->length > 0)
179
28.3k
            memset(c + a->length, 0, w + 1 - a->length);
180
28.3k
        a->data = c;
181
28.3k
        a->length = w + 1;
182
28.3k
    }
183
28.3k
    a->data[w] = ((a->data[w]) & iv) | v;
184
28.3k
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
185
0
        a->length--;
186
28.3k
    return 1;
187
28.3k
}
188
189
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
190
236k
{
191
236k
    int w, v;
192
193
236k
    if (n < 0)
194
0
        return 0;
195
196
236k
    w = n / 8;
197
236k
    v = 1 << (7 - (n & 0x07));
198
236k
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
199
42.1k
        return 0;
200
194k
    return ((a->data[w] & v) != 0);
201
236k
}
202
203
/*
204
 * Checks if the given bit string contains only bits specified by
205
 * the flags vector. Returns 0 if there is at least one bit set in 'a'
206
 * which is not specified in 'flags', 1 otherwise.
207
 * 'len' is the length of 'flags'.
208
 */
209
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
210
                          const unsigned char *flags, int flags_len)
211
0
{
212
0
    int i, ok;
213
    /* Check if there is one bit set at all. */
214
0
    if (!a || !a->data)
215
0
        return 1;
216
217
    /*
218
     * Check each byte of the internal representation of the bit string.
219
     */
220
0
    ok = 1;
221
0
    for (i = 0; i < a->length && ok; ++i) {
222
0
        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
223
        /* We are done if there is an unneeded bit set. */
224
0
        ok = (a->data[i] & mask) == 0;
225
0
    }
226
0
    return ok;
227
0
}