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