Coverage Report

Created: 2024-07-24 06:31

/src/openssl/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
0
{
18
0
    return ASN1_STRING_set(x, d, len);
19
0
}
20
21
int ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22
65.6k
{
23
65.6k
    int ret, j, bits, len;
24
65.6k
    unsigned char *p, *d;
25
26
65.6k
    if (a == NULL)
27
0
        return 0;
28
29
65.6k
    len = a->length;
30
31
65.6k
    if (len > 0) {
32
65.6k
        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33
65.6k
            bits = (int)a->flags & 0x07;
34
65.6k
        } else {
35
0
            for (; len > 0; len--) {
36
0
                if (a->data[len - 1])
37
0
                    break;
38
0
            }
39
0
            j = a->data[len - 1];
40
0
            if (j & 0x01)
41
0
                bits = 0;
42
0
            else if (j & 0x02)
43
0
                bits = 1;
44
0
            else if (j & 0x04)
45
0
                bits = 2;
46
0
            else if (j & 0x08)
47
0
                bits = 3;
48
0
            else if (j & 0x10)
49
0
                bits = 4;
50
0
            else if (j & 0x20)
51
0
                bits = 5;
52
0
            else if (j & 0x40)
53
0
                bits = 6;
54
0
            else if (j & 0x80)
55
0
                bits = 7;
56
0
            else
57
0
                bits = 0;       /* should not happen */
58
0
        }
59
65.6k
    } else
60
0
        bits = 0;
61
62
65.6k
    ret = 1 + len;
63
65.6k
    if (pp == NULL)
64
49.2k
        return ret;
65
66
16.4k
    p = *pp;
67
68
16.4k
    *(p++) = (unsigned char)bits;
69
16.4k
    d = a->data;
70
16.4k
    if (len > 0) {
71
16.4k
        memcpy(p, d, len);
72
16.4k
        p += len;
73
16.4k
        p[-1] &= (0xff << bits);
74
16.4k
    }
75
16.4k
    *pp = p;
76
16.4k
    return ret;
77
65.6k
}
78
79
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
80
                                          const unsigned char **pp, long len)
81
68.4k
{
82
68.4k
    ASN1_BIT_STRING *ret = NULL;
83
68.4k
    const unsigned char *p;
84
68.4k
    unsigned char *s;
85
68.4k
    int i = 0;
86
87
68.4k
    if (len < 1) {
88
0
        i = ASN1_R_STRING_TOO_SHORT;
89
0
        goto err;
90
0
    }
91
92
68.4k
    if (len > INT_MAX) {
93
0
        i = ASN1_R_STRING_TOO_LONG;
94
0
        goto err;
95
0
    }
96
97
68.4k
    if ((a == NULL) || ((*a) == NULL)) {
98
27.3k
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
99
0
            return NULL;
100
27.3k
    } else
101
41.0k
        ret = (*a);
102
103
68.4k
    p = *pp;
104
68.4k
    i = *(p++);
105
68.4k
    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
68.4k
    ossl_asn1_string_set_bits_left(ret, i);
114
115
68.4k
    if (len-- > 1) {            /* using one because of the bits left byte */
116
68.4k
        s = OPENSSL_malloc((int)len);
117
68.4k
        if (s == NULL) {
118
0
            goto err;
119
0
        }
120
68.4k
        memcpy(s, p, (int)len);
121
68.4k
        s[len - 1] &= (0xff << i);
122
68.4k
        p += len;
123
68.4k
    } else
124
0
        s = NULL;
125
126
68.4k
    ASN1_STRING_set0(ret, s, (int)len);
127
68.4k
    ret->type = V_ASN1_BIT_STRING;
128
68.4k
    if (a != NULL)
129
68.4k
        (*a) = ret;
130
68.4k
    *pp = p;
131
68.4k
    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
68.4k
}
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
0
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
175
0
        a->length--;
176
0
    return 1;
177
0
}
178
179
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
180
0
{
181
0
    int w, v;
182
183
0
    if (n < 0)
184
0
        return 0;
185
186
0
    w = n / 8;
187
0
    v = 1 << (7 - (n & 0x07));
188
0
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
189
0
        return 0;
190
0
    return ((a->data[w] & v) != 0);
191
0
}
192
193
/*
194
 * Checks if the given bit string contains only bits specified by
195
 * the flags vector. Returns 0 if there is at least one bit set in 'a'
196
 * which is not specified in 'flags', 1 otherwise.
197
 * 'len' is the length of 'flags'.
198
 */
199
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
200
                          const unsigned char *flags, int flags_len)
201
0
{
202
0
    int i, ok;
203
    /* Check if there is one bit set at all. */
204
0
    if (!a || !a->data)
205
0
        return 1;
206
207
    /*
208
     * Check each byte of the internal representation of the bit string.
209
     */
210
0
    ok = 1;
211
0
    for (i = 0; i < a->length && ok; ++i) {
212
0
        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
213
        /* We are done if there is an unneeded bit set. */
214
0
        ok = (a->data[i] & mask) == 0;
215
0
    }
216
0
    return ok;
217
0
}