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/bytestring.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65
66 #include "../internal.h"
67 #include "internal.h"
68
69
70 // Cross-module errors from crypto/x509/i2d_pr.c.
71 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE)
72
73 // Cross-module errors from crypto/x509/algorithm.c.
74 OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED)
75 OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED)
76 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM)
77 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM)
78 OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE)
79 // Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove
80 // these once asn1_gen.c is gone.
81 OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED)
82 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT)
83 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN)
84 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT)
85 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX)
86 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG)
87 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER)
88 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING)
89 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE)
90 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT)
91 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE)
92 OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT)
93 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER)
94 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER)
95 OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR)
96 OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE)
97 OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT)
98 OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT)
99 OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG)
100 OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT)
101 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT)
102 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG)
103 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE)
104
105 static void asn1_put_length(unsigned char **pp, int length);
106
ASN1_get_object(const unsigned char ** inp,long * out_len,int * out_tag,int * out_class,long in_len)107 int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag,
108 int *out_class, long in_len) {
109 if (in_len < 0) {
110 OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
111 return 0x80;
112 }
113
114 CBS_ASN1_TAG tag;
115 CBS cbs, body;
116 CBS_init(&cbs, *inp, (size_t)in_len);
117 if (!CBS_get_any_asn1(&cbs, &body, &tag) ||
118 // Bound the length to comfortably fit in an int. Lengths in this
119 // module often switch between int and long without overflow checks.
120 CBS_len(&body) > INT_MAX / 2) {
121 OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
122 return 0x80;
123 }
124
125 // Convert between tag representations.
126 int tag_class = (tag & CBS_ASN1_CLASS_MASK) >> CBS_ASN1_TAG_SHIFT;
127 int constructed = (tag & CBS_ASN1_CONSTRUCTED) >> CBS_ASN1_TAG_SHIFT;
128 int tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
129
130 // To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags.
131 if (tag_class == V_ASN1_UNIVERSAL && tag_number > V_ASN1_MAX_UNIVERSAL) {
132 OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
133 return 0x80;
134 }
135
136 *inp = CBS_data(&body);
137 *out_len = CBS_len(&body);
138 *out_tag = tag_number;
139 *out_class = tag_class;
140 return constructed;
141 }
142
143 // class 0 is constructed constructed == 2 for indefinite length constructed
ASN1_put_object(unsigned char ** pp,int constructed,int length,int tag,int xclass)144 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
145 int xclass) {
146 unsigned char *p = *pp;
147 int i, ttag;
148
149 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
150 i |= (xclass & V_ASN1_PRIVATE);
151 if (tag < 31) {
152 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
153 } else {
154 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
155 for (i = 0, ttag = tag; ttag > 0; i++) {
156 ttag >>= 7;
157 }
158 ttag = i;
159 while (i-- > 0) {
160 p[i] = tag & 0x7f;
161 if (i != (ttag - 1)) {
162 p[i] |= 0x80;
163 }
164 tag >>= 7;
165 }
166 p += ttag;
167 }
168 if (constructed == 2) {
169 *(p++) = 0x80;
170 } else {
171 asn1_put_length(&p, length);
172 }
173 *pp = p;
174 }
175
ASN1_put_eoc(unsigned char ** pp)176 int ASN1_put_eoc(unsigned char **pp) {
177 // This function is no longer used in the library, but some external code
178 // uses it.
179 unsigned char *p = *pp;
180 *p++ = 0;
181 *p++ = 0;
182 *pp = p;
183 return 2;
184 }
185
asn1_put_length(unsigned char ** pp,int length)186 static void asn1_put_length(unsigned char **pp, int length) {
187 unsigned char *p = *pp;
188 int i, l;
189 if (length <= 127) {
190 *(p++) = (unsigned char)length;
191 } else {
192 l = length;
193 for (i = 0; l > 0; i++) {
194 l >>= 8;
195 }
196 *(p++) = i | 0x80;
197 l = i;
198 while (i-- > 0) {
199 p[i] = length & 0xff;
200 length >>= 8;
201 }
202 p += l;
203 }
204 *pp = p;
205 }
206
ASN1_object_size(int constructed,int length,int tag)207 int ASN1_object_size(int constructed, int length, int tag) {
208 int ret = 1;
209 if (length < 0) {
210 return -1;
211 }
212 if (tag >= 31) {
213 while (tag > 0) {
214 tag >>= 7;
215 ret++;
216 }
217 }
218 if (constructed == 2) {
219 ret += 3;
220 } else {
221 ret++;
222 if (length > 127) {
223 int tmplen = length;
224 while (tmplen > 0) {
225 tmplen >>= 8;
226 ret++;
227 }
228 }
229 }
230 if (ret >= INT_MAX - length) {
231 return -1;
232 }
233 return ret + length;
234 }
235
ASN1_STRING_copy(ASN1_STRING * dst,const ASN1_STRING * str)236 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) {
237 if (str == NULL) {
238 return 0;
239 }
240 if (!ASN1_STRING_set(dst, str->data, str->length)) {
241 return 0;
242 }
243 dst->type = str->type;
244 dst->flags = str->flags;
245 return 1;
246 }
247
ASN1_STRING_dup(const ASN1_STRING * str)248 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) {
249 ASN1_STRING *ret;
250 if (!str) {
251 return NULL;
252 }
253 ret = ASN1_STRING_new();
254 if (!ret) {
255 return NULL;
256 }
257 if (!ASN1_STRING_copy(ret, str)) {
258 ASN1_STRING_free(ret);
259 return NULL;
260 }
261 return ret;
262 }
263
ASN1_STRING_set(ASN1_STRING * str,const void * _data,ossl_ssize_t len_s)264 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, ossl_ssize_t len_s) {
265 const char *data = _data;
266 size_t len;
267 if (len_s < 0) {
268 if (data == NULL) {
269 return 0;
270 }
271 len = strlen(data);
272 } else {
273 len = (size_t)len_s;
274 }
275
276 // |ASN1_STRING| cannot represent strings that exceed |int|, and we must
277 // reserve space for a trailing NUL below.
278 if (len > INT_MAX || len + 1 < len) {
279 OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
280 return 0;
281 }
282
283 if (str->length <= (int)len || str->data == NULL) {
284 unsigned char *c = str->data;
285 if (c == NULL) {
286 str->data = OPENSSL_malloc(len + 1);
287 } else {
288 str->data = OPENSSL_realloc(c, len + 1);
289 }
290
291 if (str->data == NULL) {
292 str->data = c;
293 return 0;
294 }
295 }
296 str->length = (int)len;
297 if (data != NULL) {
298 OPENSSL_memcpy(str->data, data, len);
299 // Historically, OpenSSL would NUL-terminate most (but not all)
300 // |ASN1_STRING|s, in case anyone accidentally passed |str->data| into a
301 // function expecting a C string. We retain this behavior for compatibility,
302 // but code must not rely on this. See CVE-2021-3712.
303 str->data[len] = '\0';
304 }
305 return 1;
306 }
307
ASN1_STRING_set0(ASN1_STRING * str,void * data,int len)308 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) {
309 OPENSSL_free(str->data);
310 str->data = data;
311 str->length = len;
312 }
313
ASN1_STRING_new(void)314 ASN1_STRING *ASN1_STRING_new(void) {
315 return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
316 }
317
ASN1_STRING_type_new(int type)318 ASN1_STRING *ASN1_STRING_type_new(int type) {
319 ASN1_STRING *ret;
320
321 ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
322 if (ret == NULL) {
323 return NULL;
324 }
325 ret->length = 0;
326 ret->type = type;
327 ret->data = NULL;
328 ret->flags = 0;
329 return ret;
330 }
331
ASN1_STRING_free(ASN1_STRING * str)332 void ASN1_STRING_free(ASN1_STRING *str) {
333 if (str == NULL) {
334 return;
335 }
336 OPENSSL_free(str->data);
337 OPENSSL_free(str);
338 }
339
ASN1_STRING_cmp(const ASN1_STRING * a,const ASN1_STRING * b)340 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) {
341 // Capture padding bits and implicit truncation in BIT STRINGs.
342 int a_length = a->length, b_length = b->length;
343 uint8_t a_padding = 0, b_padding = 0;
344 if (a->type == V_ASN1_BIT_STRING) {
345 a_length = asn1_bit_string_length(a, &a_padding);
346 }
347 if (b->type == V_ASN1_BIT_STRING) {
348 b_length = asn1_bit_string_length(b, &b_padding);
349 }
350
351 if (a_length < b_length) {
352 return -1;
353 }
354 if (a_length > b_length) {
355 return 1;
356 }
357 // In a BIT STRING, the number of bits is 8 * length - padding. Invert this
358 // comparison so we compare by lengths.
359 if (a_padding > b_padding) {
360 return -1;
361 }
362 if (a_padding < b_padding) {
363 return 1;
364 }
365
366 int ret = OPENSSL_memcmp(a->data, b->data, a_length);
367 if (ret != 0) {
368 return ret;
369 }
370
371 // Comparing the type first is more natural, but this matches OpenSSL.
372 if (a->type < b->type) {
373 return -1;
374 }
375 if (a->type > b->type) {
376 return 1;
377 }
378 return 0;
379 }
380
ASN1_STRING_length(const ASN1_STRING * str)381 int ASN1_STRING_length(const ASN1_STRING *str) { return str->length; }
382
ASN1_STRING_type(const ASN1_STRING * str)383 int ASN1_STRING_type(const ASN1_STRING *str) { return str->type; }
384
ASN1_STRING_data(ASN1_STRING * str)385 unsigned char *ASN1_STRING_data(ASN1_STRING *str) { return str->data; }
386
ASN1_STRING_get0_data(const ASN1_STRING * str)387 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *str) {
388 return str->data;
389 }
390