1 /*
2 * Copyright 1995-2021 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 <stdio.h>
11 #include <limits.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include "asn1_local.h"
15
16 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
17 long max);
18 static void asn1_put_length(unsigned char **pp, int length);
19
_asn1_check_infinite_end(const unsigned char ** p,long len)20 static int _asn1_check_infinite_end(const unsigned char **p, long len)
21 {
22 /*
23 * If there is 0 or 1 byte left, the length check should pick things up
24 */
25 if (len <= 0) {
26 return 1;
27 } else {
28 if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
29 (*p) += 2;
30 return 1;
31 }
32 }
33 return 0;
34 }
35
ASN1_check_infinite_end(unsigned char ** p,long len)36 int ASN1_check_infinite_end(unsigned char **p, long len)
37 {
38 return _asn1_check_infinite_end((const unsigned char **)p, len);
39 }
40
ASN1_const_check_infinite_end(const unsigned char ** p,long len)41 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
42 {
43 return _asn1_check_infinite_end(p, len);
44 }
45
ASN1_get_object(const unsigned char ** pp,long * plength,int * ptag,int * pclass,long omax)46 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
47 int *pclass, long omax)
48 {
49 int i, ret;
50 long len;
51 const unsigned char *p = *pp;
52 int tag, xclass, inf;
53 long max = omax;
54
55 if (omax <= 0) {
56 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
57 return 0x80;
58 }
59 ret = (*p & V_ASN1_CONSTRUCTED);
60 xclass = (*p & V_ASN1_PRIVATE);
61 i = *p & V_ASN1_PRIMITIVE_TAG;
62 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
63 p++;
64 if (--max == 0)
65 goto err;
66 len = 0;
67 while (*p & 0x80) {
68 len <<= 7L;
69 len |= *(p++) & 0x7f;
70 if (--max == 0)
71 goto err;
72 if (len > (INT_MAX >> 7L))
73 goto err;
74 }
75 len <<= 7L;
76 len |= *(p++) & 0x7f;
77 tag = (int)len;
78 if (--max == 0)
79 goto err;
80 } else {
81 tag = i;
82 p++;
83 if (--max == 0)
84 goto err;
85 }
86 *ptag = tag;
87 *pclass = xclass;
88 if (!asn1_get_length(&p, &inf, plength, max))
89 goto err;
90
91 if (inf && !(ret & V_ASN1_CONSTRUCTED))
92 goto err;
93
94 if (*plength > (omax - (p - *pp))) {
95 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
96 /*
97 * Set this so that even if things are not long enough the values are
98 * set correctly
99 */
100 ret |= 0x80;
101 }
102 *pp = p;
103 return ret | inf;
104 err:
105 ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG);
106 return 0x80;
107 }
108
109 /*
110 * Decode a length field.
111 * The short form is a single byte defining a length 0 - 127.
112 * The long form is a byte 0 - 127 with the top bit set and this indicates
113 * the number of following octets that contain the length. These octets
114 * are stored most significant digit first.
115 */
asn1_get_length(const unsigned char ** pp,int * inf,long * rl,long max)116 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
117 long max)
118 {
119 const unsigned char *p = *pp;
120 unsigned long ret = 0;
121 int i;
122
123 if (max-- < 1)
124 return 0;
125 if (*p == 0x80) {
126 *inf = 1;
127 p++;
128 } else {
129 *inf = 0;
130 i = *p & 0x7f;
131 if (*p++ & 0x80) {
132 if (max < i + 1)
133 return 0;
134 /* Skip leading zeroes */
135 while (i > 0 && *p == 0) {
136 p++;
137 i--;
138 }
139 if (i > (int)sizeof(long))
140 return 0;
141 while (i > 0) {
142 ret <<= 8;
143 ret |= *p++;
144 i--;
145 }
146 if (ret > LONG_MAX)
147 return 0;
148 } else {
149 ret = i;
150 }
151 }
152 *pp = p;
153 *rl = (long)ret;
154 return 1;
155 }
156
157 /*
158 * constructed == 2 for indefinite length constructed
159 */
ASN1_put_object(unsigned char ** pp,int constructed,int length,int tag,int xclass)160 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
161 int xclass)
162 {
163 unsigned char *p = *pp;
164 int i, ttag;
165
166 i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
167 i |= (xclass & V_ASN1_PRIVATE);
168 if (tag < 31) {
169 *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
170 } else {
171 *(p++) = i | V_ASN1_PRIMITIVE_TAG;
172 for (i = 0, ttag = tag; ttag > 0; i++)
173 ttag >>= 7;
174 ttag = i;
175 while (i-- > 0) {
176 p[i] = tag & 0x7f;
177 if (i != (ttag - 1))
178 p[i] |= 0x80;
179 tag >>= 7;
180 }
181 p += ttag;
182 }
183 if (constructed == 2)
184 *(p++) = 0x80;
185 else
186 asn1_put_length(&p, length);
187 *pp = p;
188 }
189
ASN1_put_eoc(unsigned char ** pp)190 int ASN1_put_eoc(unsigned char **pp)
191 {
192 unsigned char *p = *pp;
193
194 *p++ = 0;
195 *p++ = 0;
196 *pp = p;
197 return 2;
198 }
199
asn1_put_length(unsigned char ** pp,int length)200 static void asn1_put_length(unsigned char **pp, int length)
201 {
202 unsigned char *p = *pp;
203 int i, len;
204
205 if (length <= 127) {
206 *(p++) = (unsigned char)length;
207 } else {
208 len = length;
209 for (i = 0; len > 0; i++)
210 len >>= 8;
211 *(p++) = i | 0x80;
212 len = i;
213 while (i-- > 0) {
214 p[i] = length & 0xff;
215 length >>= 8;
216 }
217 p += len;
218 }
219 *pp = p;
220 }
221
ASN1_object_size(int constructed,int length,int tag)222 int ASN1_object_size(int constructed, int length, int tag)
223 {
224 int ret = 1;
225
226 if (length < 0)
227 return -1;
228 if (tag >= 31) {
229 while (tag > 0) {
230 tag >>= 7;
231 ret++;
232 }
233 }
234 if (constructed == 2) {
235 ret += 3;
236 } else {
237 ret++;
238 if (length > 127) {
239 int tmplen = length;
240 while (tmplen > 0) {
241 tmplen >>= 8;
242 ret++;
243 }
244 }
245 }
246 if (ret >= INT_MAX - length)
247 return -1;
248 return ret + length;
249 }
250
ossl_asn1_string_set_bits_left(ASN1_STRING * str,unsigned int num)251 void ossl_asn1_string_set_bits_left(ASN1_STRING *str, unsigned int num)
252 {
253 str->flags &= ~0x07;
254 str->flags |= ASN1_STRING_FLAG_BITS_LEFT | (num & 0x07);
255 }
256
ASN1_STRING_copy(ASN1_STRING * dst,const ASN1_STRING * str)257 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
258 {
259 if (str == NULL)
260 return 0;
261 dst->type = str->type;
262 if (!ASN1_STRING_set(dst, str->data, str->length))
263 return 0;
264 /* Copy flags but preserve embed value */
265 dst->flags &= ASN1_STRING_FLAG_EMBED;
266 dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
267 return 1;
268 }
269
ASN1_STRING_dup(const ASN1_STRING * str)270 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
271 {
272 ASN1_STRING *ret;
273
274 if (!str)
275 return NULL;
276 ret = ASN1_STRING_new();
277 if (ret == NULL)
278 return NULL;
279 if (!ASN1_STRING_copy(ret, str)) {
280 ASN1_STRING_free(ret);
281 return NULL;
282 }
283 return ret;
284 }
285
ASN1_STRING_set(ASN1_STRING * str,const void * _data,int len_in)286 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
287 {
288 unsigned char *c;
289 const char *data = _data;
290 size_t len;
291
292 if (len_in < 0) {
293 if (data == NULL)
294 return 0;
295 len = strlen(data);
296 } else {
297 len = (size_t)len_in;
298 }
299 /*
300 * Verify that the length fits within an integer for assignment to
301 * str->length below. The additional 1 is subtracted to allow for the
302 * '\0' terminator even though this isn't strictly necessary.
303 */
304 if (len > INT_MAX - 1) {
305 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
306 return 0;
307 }
308 if ((size_t)str->length <= len || str->data == NULL) {
309 c = str->data;
310 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
311 /* No NUL terminator in fuzzing builds */
312 str->data = OPENSSL_realloc(c, len != 0 ? len : 1);
313 #else
314 str->data = OPENSSL_realloc(c, len + 1);
315 #endif
316 if (str->data == NULL) {
317 str->data = c;
318 return 0;
319 }
320 }
321 str->length = len;
322 if (data != NULL) {
323 memcpy(str->data, data, len);
324 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
325 /* Set the unused byte to something non NUL and printable. */
326 if (len == 0)
327 str->data[len] = '~';
328 #else
329 /*
330 * Add a NUL terminator. This should not be necessary - but we add it as
331 * a safety precaution
332 */
333 str->data[len] = '\0';
334 #endif
335 }
336 return 1;
337 }
338
ASN1_STRING_set0(ASN1_STRING * str,void * data,int len)339 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
340 {
341 OPENSSL_free(str->data);
342 str->data = data;
343 str->length = len;
344 }
345
ASN1_STRING_new(void)346 ASN1_STRING *ASN1_STRING_new(void)
347 {
348 return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
349 }
350
ASN1_STRING_type_new(int type)351 ASN1_STRING *ASN1_STRING_type_new(int type)
352 {
353 ASN1_STRING *ret;
354
355 ret = OPENSSL_zalloc(sizeof(*ret));
356 if (ret == NULL)
357 return NULL;
358 ret->type = type;
359 return ret;
360 }
361
ossl_asn1_string_embed_free(ASN1_STRING * a,int embed)362 void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed)
363 {
364 if (a == NULL)
365 return;
366 if (!(a->flags & ASN1_STRING_FLAG_NDEF))
367 OPENSSL_free(a->data);
368 if (embed == 0)
369 OPENSSL_free(a);
370 }
371
ASN1_STRING_free(ASN1_STRING * a)372 void ASN1_STRING_free(ASN1_STRING *a)
373 {
374 if (a == NULL)
375 return;
376 ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED);
377 }
378
ASN1_STRING_clear_free(ASN1_STRING * a)379 void ASN1_STRING_clear_free(ASN1_STRING *a)
380 {
381 if (a == NULL)
382 return;
383 if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
384 OPENSSL_cleanse(a->data, a->length);
385 ASN1_STRING_free(a);
386 }
387
ASN1_STRING_cmp(const ASN1_STRING * a,const ASN1_STRING * b)388 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
389 {
390 int i;
391
392 i = (a->length - b->length);
393 if (i == 0) {
394 if (a->length != 0)
395 i = memcmp(a->data, b->data, a->length);
396 if (i == 0)
397 return a->type - b->type;
398 else
399 return i;
400 } else {
401 return i;
402 }
403 }
404
ASN1_STRING_length(const ASN1_STRING * x)405 int ASN1_STRING_length(const ASN1_STRING *x)
406 {
407 return x->length;
408 }
409
410 #ifndef OPENSSL_NO_DEPRECATED_3_0
ASN1_STRING_length_set(ASN1_STRING * x,int len)411 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
412 {
413 x->length = len;
414 }
415 #endif
416
ASN1_STRING_type(const ASN1_STRING * x)417 int ASN1_STRING_type(const ASN1_STRING *x)
418 {
419 return x->type;
420 }
421
ASN1_STRING_get0_data(const ASN1_STRING * x)422 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
423 {
424 return x->data;
425 }
426
427 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
ASN1_STRING_data(ASN1_STRING * x)428 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
429 {
430 return x->data;
431 }
432 #endif
433
434 /* |max_len| excludes NUL terminator and may be 0 to indicate no restriction */
ossl_sk_ASN1_UTF8STRING2text(STACK_OF (ASN1_UTF8STRING)* text,const char * sep,size_t max_len)435 char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text,
436 const char *sep, size_t max_len)
437 {
438 int i;
439 ASN1_UTF8STRING *current;
440 size_t length = 0, sep_len;
441 char *result = NULL;
442 char *p;
443
444 if (sep == NULL)
445 sep = "";
446 sep_len = strlen(sep);
447
448 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
449 current = sk_ASN1_UTF8STRING_value(text, i);
450 if (i > 0)
451 length += sep_len;
452 length += ASN1_STRING_length(current);
453 if (max_len != 0 && length > max_len)
454 return NULL;
455 }
456 if ((result = OPENSSL_malloc(length + 1)) == NULL)
457 return NULL;
458
459 p = result;
460 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
461 current = sk_ASN1_UTF8STRING_value(text, i);
462 length = ASN1_STRING_length(current);
463 if (i > 0 && sep_len > 0) {
464 strncpy(p, sep, sep_len + 1); /* using + 1 to silence gcc warning */
465 p += sep_len;
466 }
467 strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);
468 p += length;
469 }
470 *p = '\0';
471
472 return result;
473 }
474