Coverage Report

Created: 2023-09-25 06:45

/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
277
{
18
277
    return ASN1_STRING_set(x, d, len);
19
277
}
20
21
int ossl_i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22
9.62M
{
23
9.62M
    int ret, j, bits, len;
24
9.62M
    unsigned char *p, *d;
25
26
9.62M
    if (a == NULL)
27
0
        return 0;
28
29
9.62M
    len = a->length;
30
31
9.62M
    if (len > 0) {
32
8.88M
        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33
8.83M
            bits = (int)a->flags & 0x07;
34
8.83M
        } else {
35
46.5k
            for (; len > 0; len--) {
36
46.5k
                if (a->data[len - 1])
37
46.5k
                    break;
38
46.5k
            }
39
46.5k
            j = a->data[len - 1];
40
46.5k
            if (j & 0x01)
41
0
                bits = 0;
42
46.5k
            else if (j & 0x02)
43
0
                bits = 1;
44
46.5k
            else if (j & 0x04)
45
0
                bits = 2;
46
46.5k
            else if (j & 0x08)
47
0
                bits = 3;
48
46.5k
            else if (j & 0x10)
49
0
                bits = 4;
50
46.5k
            else if (j & 0x20)
51
46.5k
                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
46.5k
        }
59
8.88M
    } else
60
740k
        bits = 0;
61
62
9.62M
    ret = 1 + len;
63
9.62M
    if (pp == NULL)
64
7.78M
        return ret;
65
66
1.83M
    p = *pp;
67
68
1.83M
    *(p++) = (unsigned char)bits;
69
1.83M
    d = a->data;
70
1.83M
    if (len > 0) {
71
1.68M
        memcpy(p, d, len);
72
1.68M
        p += len;
73
1.68M
        p[-1] &= (0xff << bits);
74
1.68M
    }
75
1.83M
    *pp = p;
76
1.83M
    return ret;
77
9.62M
}
78
79
ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
80
                                          const unsigned char **pp, long len)
81
1.23M
{
82
1.23M
    ASN1_BIT_STRING *ret = NULL;
83
1.23M
    const unsigned char *p;
84
1.23M
    unsigned char *s;
85
1.23M
    int i;
86
87
1.23M
    if (len < 1) {
88
1.43k
        i = ASN1_R_STRING_TOO_SHORT;
89
1.43k
        goto err;
90
1.43k
    }
91
92
1.23M
    if (len > INT_MAX) {
93
0
        i = ASN1_R_STRING_TOO_LONG;
94
0
        goto err;
95
0
    }
96
97
1.23M
    if ((a == NULL) || ((*a) == NULL)) {
98
260k
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
99
0
            return NULL;
100
260k
    } else
101
975k
        ret = (*a);
102
103
1.23M
    p = *pp;
104
1.23M
    i = *(p++);
105
1.23M
    if (i > 7) {
106
1.00k
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
107
1.00k
        goto err;
108
1.00k
    }
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
1.23M
    ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
114
1.23M
    ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
115
116
1.23M
    if (len-- > 1) {            /* using one because of the bits left byte */
117
909k
        s = OPENSSL_malloc((int)len);
118
909k
        if (s == NULL) {
119
0
            i = ERR_R_MALLOC_FAILURE;
120
0
            goto err;
121
0
        }
122
909k
        memcpy(s, p, (int)len);
123
909k
        s[len - 1] &= (0xff << i);
124
909k
        p += len;
125
909k
    } else
126
325k
        s = NULL;
127
128
1.23M
    ret->length = (int)len;
129
1.23M
    OPENSSL_free(ret->data);
130
1.23M
    ret->data = s;
131
1.23M
    ret->type = V_ASN1_BIT_STRING;
132
1.23M
    if (a != NULL)
133
1.23M
        (*a) = ret;
134
1.23M
    *pp = p;
135
1.23M
    return ret;
136
2.44k
 err:
137
2.44k
    ERR_raise(ERR_LIB_ASN1, i);
138
2.44k
    if ((a == NULL) || (*a != ret))
139
1.15k
        ASN1_BIT_STRING_free(ret);
140
2.44k
    return NULL;
141
1.23M
}
142
143
/*
144
 * These next 2 functions from Goetz Babin-Ebell.
145
 */
146
int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
147
11.6k
{
148
11.6k
    int w, v, iv;
149
11.6k
    unsigned char *c;
150
151
11.6k
    if (n < 0)
152
0
        return 0;
153
154
11.6k
    w = n / 8;
155
11.6k
    v = 1 << (7 - (n & 0x07));
156
11.6k
    iv = ~v;
157
11.6k
    if (!value)
158
0
        v = 0;
159
160
11.6k
    if (a == NULL)
161
0
        return 0;
162
163
11.6k
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
164
165
11.6k
    if ((a->length < (w + 1)) || (a->data == NULL)) {
166
11.6k
        if (!value)
167
0
            return 1;         /* Don't need to set */
168
11.6k
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
169
11.6k
        if (c == NULL) {
170
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
171
0
            return 0;
172
0
        }
173
11.6k
        if (w + 1 - a->length > 0)
174
11.6k
            memset(c + a->length, 0, w + 1 - a->length);
175
11.6k
        a->data = c;
176
11.6k
        a->length = w + 1;
177
11.6k
    }
178
11.6k
    a->data[w] = ((a->data[w]) & iv) | v;
179
11.6k
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
180
0
        a->length--;
181
11.6k
    return 1;
182
11.6k
}
183
184
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
185
198k
{
186
198k
    int w, v;
187
188
198k
    if (n < 0)
189
0
        return 0;
190
191
198k
    w = n / 8;
192
198k
    v = 1 << (7 - (n & 0x07));
193
198k
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
194
27.9k
        return 0;
195
170k
    return ((a->data[w] & v) != 0);
196
198k
}
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
}