Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/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 OpenSSL license (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
47
{
18
47
    return ASN1_STRING_set(x, d, len);
19
47
}
20
21
int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
22
95.5k
{
23
95.5k
    int ret, j, bits, len;
24
95.5k
    unsigned char *p, *d;
25
26
95.5k
    if (a == NULL)
27
0
        return 0;
28
29
95.5k
    len = a->length;
30
31
95.5k
    if (len > 0) {
32
35.7k
        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
33
35.7k
            bits = (int)a->flags & 0x07;
34
35.7k
        } 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
35.7k
    } else
60
59.7k
        bits = 0;
61
62
95.5k
    ret = 1 + len;
63
95.5k
    if (pp == NULL)
64
72.0k
        return ret;
65
66
23.4k
    p = *pp;
67
68
23.4k
    *(p++) = (unsigned char)bits;
69
23.4k
    d = a->data;
70
23.4k
    if (len > 0) {
71
8.71k
        memcpy(p, d, len);
72
8.71k
        p += len;
73
8.71k
        p[-1] &= (0xff << bits);
74
8.71k
    }
75
23.4k
    *pp = p;
76
23.4k
    return ret;
77
95.5k
}
78
79
ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
80
                                     const unsigned char **pp, long len)
81
104k
{
82
104k
    ASN1_BIT_STRING *ret = NULL;
83
104k
    const unsigned char *p;
84
104k
    unsigned char *s;
85
104k
    int i;
86
87
104k
    if (len < 1) {
88
250
        i = ASN1_R_STRING_TOO_SHORT;
89
250
        goto err;
90
250
    }
91
92
104k
    if (len > INT_MAX) {
93
0
        i = ASN1_R_STRING_TOO_LONG;
94
0
        goto err;
95
0
    }
96
97
104k
    if ((a == NULL) || ((*a) == NULL)) {
98
10.8k
        if ((ret = ASN1_BIT_STRING_new()) == NULL)
99
0
            return NULL;
100
10.8k
    } else
101
93.2k
        ret = (*a);
102
103
104k
    p = *pp;
104
104k
    i = *(p++);
105
104k
    if (i > 7) {
106
116
        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
107
116
        goto err;
108
116
    }
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
103k
    ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
114
103k
    ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
115
116
103k
    if (len-- > 1) {            /* using one because of the bits left byte */
117
47.7k
        s = OPENSSL_malloc((int)len);
118
47.7k
        if (s == NULL) {
119
0
            i = ERR_R_MALLOC_FAILURE;
120
0
            goto err;
121
0
        }
122
47.7k
        memcpy(s, p, (int)len);
123
47.7k
        s[len - 1] &= (0xff << i);
124
47.7k
        p += len;
125
47.7k
    } else
126
56.2k
        s = NULL;
127
128
103k
    ret->length = (int)len;
129
103k
    OPENSSL_free(ret->data);
130
103k
    ret->data = s;
131
103k
    ret->type = V_ASN1_BIT_STRING;
132
103k
    if (a != NULL)
133
103k
        (*a) = ret;
134
103k
    *pp = p;
135
103k
    return ret;
136
366
 err:
137
366
    ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
138
366
    if ((a == NULL) || (*a != ret))
139
115
        ASN1_BIT_STRING_free(ret);
140
366
    return NULL;
141
103k
}
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
0
{
148
0
    int w, v, iv;
149
0
    unsigned char *c;
150
151
0
    if (n < 0)
152
0
        return 0;
153
154
0
    w = n / 8;
155
0
    v = 1 << (7 - (n & 0x07));
156
0
    iv = ~v;
157
0
    if (!value)
158
0
        v = 0;
159
160
0
    if (a == NULL)
161
0
        return 0;
162
163
0
    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
164
165
0
    if ((a->length < (w + 1)) || (a->data == NULL)) {
166
0
        if (!value)
167
0
            return 1;         /* Don't need to set */
168
0
        c = OPENSSL_clear_realloc(a->data, a->length, w + 1);
169
0
        if (c == NULL) {
170
0
            ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
171
0
            return 0;
172
0
        }
173
0
        if (w + 1 - a->length > 0)
174
0
            memset(c + a->length, 0, w + 1 - a->length);
175
0
        a->data = c;
176
0
        a->length = w + 1;
177
0
    }
178
0
    a->data[w] = ((a->data[w]) & iv) | v;
179
0
    while ((a->length > 0) && (a->data[a->length - 1] == 0))
180
0
        a->length--;
181
0
    return 1;
182
0
}
183
184
int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
185
14.1k
{
186
14.1k
    int w, v;
187
188
14.1k
    if (n < 0)
189
0
        return 0;
190
191
14.1k
    w = n / 8;
192
14.1k
    v = 1 << (7 - (n & 0x07));
193
14.1k
    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
194
2.44k
        return 0;
195
11.7k
    return ((a->data[w] & v) != 0);
196
14.1k
}
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
}