/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 | 13.9k | { |
23 | 13.9k | int ret, j, bits, len; |
24 | 13.9k | unsigned char *p, *d; |
25 | | |
26 | 13.9k | if (a == NULL) |
27 | 0 | return 0; |
28 | | |
29 | 13.9k | len = a->length; |
30 | | |
31 | 13.9k | if (len > 0) { |
32 | 13.9k | if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) { |
33 | 13.9k | bits = (int)a->flags & 0x07; |
34 | 13.9k | } else { |
35 | 0 | for (; len > 0; len--) { |
36 | 0 | if (a->data[len - 1]) |
37 | 0 | break; |
38 | 0 | } |
39 | |
|
40 | 0 | if (len == 0) { |
41 | 0 | bits = 0; |
42 | 0 | } else { |
43 | 0 | j = a->data[len - 1]; |
44 | 0 | if (j & 0x01) |
45 | 0 | bits = 0; |
46 | 0 | else if (j & 0x02) |
47 | 0 | bits = 1; |
48 | 0 | else if (j & 0x04) |
49 | 0 | bits = 2; |
50 | 0 | else if (j & 0x08) |
51 | 0 | bits = 3; |
52 | 0 | else if (j & 0x10) |
53 | 0 | bits = 4; |
54 | 0 | else if (j & 0x20) |
55 | 0 | 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 | 0 | } |
63 | 0 | } |
64 | 13.9k | } else |
65 | 5 | bits = 0; |
66 | | |
67 | 13.9k | ret = 1 + len; |
68 | 13.9k | if (pp == NULL) |
69 | 10.4k | return ret; |
70 | | |
71 | 3.48k | p = *pp; |
72 | | |
73 | 3.48k | *(p++) = (unsigned char)bits; |
74 | 3.48k | d = a->data; |
75 | 3.48k | if (len > 0) { |
76 | 3.48k | memcpy(p, d, len); |
77 | 3.48k | p += len; |
78 | 3.48k | p[-1] &= (0xff << bits); |
79 | 3.48k | } |
80 | 3.48k | *pp = p; |
81 | 3.48k | return ret; |
82 | 13.9k | } |
83 | | |
84 | | ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, |
85 | | const unsigned char **pp, long len) |
86 | 15.2k | { |
87 | 15.2k | ASN1_BIT_STRING *ret = NULL; |
88 | 15.2k | const unsigned char *p; |
89 | 15.2k | unsigned char *s; |
90 | 15.2k | int i = 0; |
91 | | |
92 | 15.2k | if (len < 1) { |
93 | 2 | i = ASN1_R_STRING_TOO_SHORT; |
94 | 2 | goto err; |
95 | 2 | } |
96 | | |
97 | 15.2k | if (len > INT_MAX) { |
98 | 0 | i = ASN1_R_STRING_TOO_LONG; |
99 | 0 | goto err; |
100 | 0 | } |
101 | | |
102 | 15.2k | if ((a == NULL) || ((*a) == NULL)) { |
103 | 3.83k | if ((ret = ASN1_BIT_STRING_new()) == NULL) |
104 | 0 | return NULL; |
105 | 3.83k | } else |
106 | 11.4k | ret = (*a); |
107 | | |
108 | 15.2k | p = *pp; |
109 | 15.2k | i = *(p++); |
110 | 15.2k | if (i > 7) { |
111 | 6 | i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT; |
112 | 6 | goto err; |
113 | 6 | } |
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 | 15.2k | ossl_asn1_string_set_bits_left(ret, i); |
119 | | |
120 | 15.2k | if (len-- > 1) { /* using one because of the bits left byte */ |
121 | 15.2k | s = OPENSSL_malloc((int)len); |
122 | 15.2k | if (s == NULL) { |
123 | 0 | goto err; |
124 | 0 | } |
125 | 15.2k | memcpy(s, p, (int)len); |
126 | 15.2k | s[len - 1] &= (0xff << i); |
127 | 15.2k | p += len; |
128 | 15.2k | } else |
129 | 4 | s = NULL; |
130 | | |
131 | 15.2k | ASN1_STRING_set0(ret, s, (int)len); |
132 | 15.2k | ret->type = V_ASN1_BIT_STRING; |
133 | 15.2k | if (a != NULL) |
134 | 15.2k | (*a) = ret; |
135 | 15.2k | *pp = p; |
136 | 15.2k | return ret; |
137 | 8 | err: |
138 | 8 | if (i != 0) |
139 | 8 | ERR_raise(ERR_LIB_ASN1, i); |
140 | 8 | if ((a == NULL) || (*a != ret)) |
141 | 7 | ASN1_BIT_STRING_free(ret); |
142 | 8 | return NULL; |
143 | 15.2k | } |
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 | 0 | { |
150 | 0 | int w, v, iv; |
151 | 0 | unsigned char *c; |
152 | |
|
153 | 0 | if (n < 0) |
154 | 0 | return 0; |
155 | | |
156 | 0 | w = n / 8; |
157 | 0 | v = 1 << (7 - (n & 0x07)); |
158 | 0 | iv = ~v; |
159 | 0 | if (!value) |
160 | 0 | v = 0; |
161 | |
|
162 | 0 | if (a == NULL) |
163 | 0 | return 0; |
164 | | |
165 | 0 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ |
166 | |
|
167 | 0 | if ((a->length < (w + 1)) || (a->data == NULL)) { |
168 | 0 | if (!value) |
169 | 0 | return 1; /* Don't need to set */ |
170 | 0 | c = OPENSSL_clear_realloc(a->data, a->length, w + 1); |
171 | 0 | if (c == NULL) |
172 | 0 | return 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 | 0 | { |
186 | 0 | int w, v; |
187 | |
|
188 | 0 | if (n < 0) |
189 | 0 | return 0; |
190 | | |
191 | 0 | w = n / 8; |
192 | 0 | v = 1 << (7 - (n & 0x07)); |
193 | 0 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) |
194 | 0 | return 0; |
195 | 0 | return ((a->data[w] & v) != 0); |
196 | 0 | } |
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 | } |