1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58
59 #include <limits.h>
60 #include <string.h>
61
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64
65 #include "../internal.h"
66 #include "internal.h"
67
68
ASN1_BIT_STRING_set(ASN1_BIT_STRING * x,const unsigned char * d,int len)69 int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len)
70 {
71 return ASN1_STRING_set(x, d, len);
72 }
73
asn1_bit_string_length(const ASN1_BIT_STRING * str,uint8_t * out_padding_bits)74 int asn1_bit_string_length(const ASN1_BIT_STRING *str,
75 uint8_t *out_padding_bits) {
76 int len = str->length;
77 if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) {
78 // If the string is already empty, it cannot have padding bits.
79 *out_padding_bits = len == 0 ? 0 : str->flags & 0x07;
80 return len;
81 }
82
83 // TODO(https://crbug.com/boringssl/447): If we move this logic to
84 // |ASN1_BIT_STRING_set_bit|, can we remove this representation?
85 while (len > 0 && str->data[len - 1] == 0) {
86 len--;
87 }
88 uint8_t padding_bits = 0;
89 if (len > 0) {
90 uint8_t last = str->data[len - 1];
91 assert(last != 0);
92 for (; padding_bits < 7; padding_bits++) {
93 if (last & (1 << padding_bits)) {
94 break;
95 }
96 }
97 }
98 *out_padding_bits = padding_bits;
99 return len;
100 }
101
ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING * str,size_t * out)102 int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out) {
103 uint8_t padding_bits;
104 int len = asn1_bit_string_length(str, &padding_bits);
105 if (padding_bits != 0) {
106 return 0;
107 }
108 *out = len;
109 return 1;
110 }
111
i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING * a,unsigned char ** pp)112 int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
113 {
114 if (a == NULL) {
115 return 0;
116 }
117
118 uint8_t bits;
119 int len = asn1_bit_string_length(a, &bits);
120 int ret = 1 + len;
121 if (pp == NULL) {
122 return ret;
123 }
124
125 uint8_t *p = *pp;
126 *(p++) = bits;
127 OPENSSL_memcpy(p, a->data, len);
128 if (len > 0) {
129 p[len - 1] &= (0xff << bits);
130 }
131 p += len;
132 *pp = p;
133 return (ret);
134 }
135
c2i_ASN1_BIT_STRING(ASN1_BIT_STRING ** a,const unsigned char ** pp,long len)136 ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
137 const unsigned char **pp, long len)
138 {
139 ASN1_BIT_STRING *ret = NULL;
140 const unsigned char *p;
141 unsigned char *s;
142 int padding;
143
144 if (len < 1) {
145 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
146 goto err;
147 }
148
149 if (len > INT_MAX) {
150 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
151 goto err;
152 }
153
154 if ((a == NULL) || ((*a) == NULL)) {
155 if ((ret = ASN1_BIT_STRING_new()) == NULL)
156 return (NULL);
157 } else
158 ret = (*a);
159
160 p = *pp;
161 padding = *(p++);
162 len--;
163 if (padding > 7) {
164 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
165 goto err;
166 }
167
168 /* Unused bits in a BIT STRING must be zero. */
169 uint8_t padding_mask = (1 << padding) - 1;
170 if (padding != 0 &&
171 (len < 1 || (p[len - 1] & padding_mask) != 0)) {
172 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BIT_STRING_PADDING);
173 goto err;
174 }
175
176 /*
177 * We do this to preserve the settings. If we modify the settings, via
178 * the _set_bit function, we will recalculate on output
179 */
180 ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
181 ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | padding); /* set */
182
183 if (len > 0) {
184 s = OPENSSL_memdup(p, len);
185 if (s == NULL) {
186 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
187 goto err;
188 }
189 p += len;
190 } else {
191 s = NULL;
192 }
193
194 ret->length = (int)len;
195 OPENSSL_free(ret->data);
196 ret->data = s;
197 ret->type = V_ASN1_BIT_STRING;
198 if (a != NULL)
199 (*a) = ret;
200 *pp = p;
201 return (ret);
202 err:
203 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
204 ASN1_BIT_STRING_free(ret);
205 return (NULL);
206 }
207
208 /*
209 * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
210 */
ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING * a,int n,int value)211 int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
212 {
213 int w, v, iv;
214 unsigned char *c;
215
216 w = n / 8;
217 v = 1 << (7 - (n & 0x07));
218 iv = ~v;
219 if (!value)
220 v = 0;
221
222 if (a == NULL)
223 return 0;
224
225 a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
226
227 if ((a->length < (w + 1)) || (a->data == NULL)) {
228 if (!value)
229 return (1); /* Don't need to set */
230 if (a->data == NULL)
231 c = (unsigned char *)OPENSSL_malloc(w + 1);
232 else
233 c = (unsigned char *)OPENSSL_realloc(a->data, w + 1);
234 if (c == NULL) {
235 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
236 return 0;
237 }
238 if (w + 1 - a->length > 0)
239 OPENSSL_memset(c + a->length, 0, w + 1 - a->length);
240 a->data = c;
241 a->length = w + 1;
242 }
243 a->data[w] = ((a->data[w]) & iv) | v;
244 while ((a->length > 0) && (a->data[a->length - 1] == 0))
245 a->length--;
246 return (1);
247 }
248
ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING * a,int n)249 int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
250 {
251 int w, v;
252
253 w = n / 8;
254 v = 1 << (7 - (n & 0x07));
255 if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
256 return (0);
257 return ((a->data[w] & v) != 0);
258 }
259
260 /*
261 * Checks if the given bit string contains only bits specified by
262 * the flags vector. Returns 0 if there is at least one bit set in 'a'
263 * which is not specified in 'flags', 1 otherwise.
264 * 'len' is the length of 'flags'.
265 */
ASN1_BIT_STRING_check(const ASN1_BIT_STRING * a,const unsigned char * flags,int flags_len)266 int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
267 const unsigned char *flags, int flags_len)
268 {
269 int i, ok;
270 /* Check if there is one bit set at all. */
271 if (!a || !a->data)
272 return 1;
273
274 /*
275 * Check each byte of the internal representation of the bit string.
276 */
277 ok = 1;
278 for (i = 0; i < a->length && ok; ++i) {
279 unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
280 /* We are done if there is an unneeded bit set. */
281 ok = (a->data[i] & mask) == 0;
282 }
283 return ok;
284 }
285