Integer overflow in ASN1_STRING_set.
[openssl.git] / crypto / asn1 / asn1_lib.c
1 /*
2  * Copyright 1995-2016 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
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
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
41 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
42 {
43     return _asn1_check_infinite_end(p, len);
44 }
45
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 (!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;
64         len = 0;
65         while (*p & 0x80) {
66             len <<= 7L;
67             len |= *(p++) & 0x7f;
68             if (--max == 0)
69                 goto err;
70             if (len > (INT_MAX >> 7L))
71                 goto err;
72         }
73         len <<= 7L;
74         len |= *(p++) & 0x7f;
75         tag = (int)len;
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;
86     if (!asn1_get_length(&p, &inf, plength, max))
87         goto err;
88
89     if (inf && !(ret & V_ASN1_CONSTRUCTED))
90         goto err;
91
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;
101     return ret | inf;
102  err:
103     ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
104     return 0x80;
105 }
106
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  */
114 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
115                            long max)
116 {
117     const unsigned char *p = *pp;
118     unsigned long ret = 0;
119     int i;
120
121     if (max-- < 1)
122         return 0;
123     if (*p == 0x80) {
124         *inf = 1;
125         p++;
126     } else {
127         *inf = 0;
128         i = *p & 0x7f;
129         if (*p++ & 0x80) {
130             if (max < i + 1)
131                 return 0;
132             /* Skip leading zeroes */
133             while (i > 0 && *p == 0) {
134                 p++;
135                 i--;
136             }
137             if (i > (int)sizeof(long))
138                 return 0;
139             while (i > 0) {
140                 ret <<= 8;
141                 ret |= *p++;
142                 i--;
143             }
144             if (ret > LONG_MAX)
145                 return 0;
146         } else {
147             ret = i;
148         }
149     }
150     *pp = p;
151     *rl = (long)ret;
152     return 1;
153 }
154
155 /*
156  * class 0 is constructed constructed == 2 for indefinite length constructed
157  */
158 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
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);
166     if (tag < 31) {
167         *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
168     } else {
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 }
187
188 int ASN1_put_eoc(unsigned char **pp)
189 {
190     unsigned char *p = *pp;
191
192     *p++ = 0;
193     *p++ = 0;
194     *pp = p;
195     return 2;
196 }
197
198 static void asn1_put_length(unsigned char **pp, int length)
199 {
200     unsigned char *p = *pp;
201     int i, len;
202
203     if (length <= 127) {
204         *(p++) = (unsigned char)length;
205     } else {
206         len = length;
207         for (i = 0; len > 0; i++)
208             len >>= 8;
209         *(p++) = i | 0x80;
210         len = i;
211         while (i-- > 0) {
212             p[i] = length & 0xff;
213             length >>= 8;
214         }
215         p += len;
216     }
217     *pp = p;
218 }
219
220 int ASN1_object_size(int constructed, int length, int tag)
221 {
222     int ret = 1;
223
224     if (length < 0)
225         return -1;
226     if (tag >= 31) {
227         while (tag > 0) {
228             tag >>= 7;
229             ret++;
230         }
231     }
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             }
242         }
243     }
244     if (ret >= INT_MAX - length)
245         return -1;
246     return ret + length;
247 }
248
249 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
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;
256     /* Copy flags but preserve embed value */
257     dst->flags &= ASN1_STRING_FLAG_EMBED;
258     dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
259     return 1;
260 }
261
262 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
263 {
264     ASN1_STRING *ret;
265
266     if (!str)
267         return NULL;
268     ret = ASN1_STRING_new();
269     if (ret == NULL)
270         return NULL;
271     if (!ASN1_STRING_copy(ret, str)) {
272         ASN1_STRING_free(ret);
273         return NULL;
274     }
275     return ret;
276 }
277
278 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in)
279 {
280     unsigned char *c;
281     const char *data = _data;
282     size_t len;
283
284     if (len_in < 0) {
285         if (data == NULL)
286             return 0;
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;
299     }
300     if ((size_t)str->length <= len || str->data == NULL) {
301         c = str->data;
302         str->data = OPENSSL_realloc(c, len + 1);
303         if (str->data == NULL) {
304             ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
305             str->data = c;
306             return 0;
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     }
315     return 1;
316 }
317
318 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
319 {
320     OPENSSL_free(str->data);
321     str->data = data;
322     str->length = len;
323 }
324
325 ASN1_STRING *ASN1_STRING_new(void)
326 {
327     return ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
328 }
329
330 ASN1_STRING *ASN1_STRING_type_new(int type)
331 {
332     ASN1_STRING *ret;
333
334     ret = OPENSSL_zalloc(sizeof(*ret));
335     if (ret == NULL) {
336         ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
337         return NULL;
338     }
339     ret->type = type;
340     return ret;
341 }
342
343 void asn1_string_embed_free(ASN1_STRING *a, int embed)
344 {
345     if (a == NULL)
346         return;
347     if (!(a->flags & ASN1_STRING_FLAG_NDEF))
348         OPENSSL_free(a->data);
349     if (embed == 0)
350         OPENSSL_free(a);
351 }
352
353 void 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
360 void ASN1_STRING_clear_free(ASN1_STRING *a)
361 {
362     if (a == NULL)
363         return;
364     if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
365         OPENSSL_cleanse(a->data, a->length);
366     ASN1_STRING_free(a);
367 }
368
369 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
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)
377             return a->type - b->type;
378         else
379             return i;
380     } else {
381         return i;
382     }
383 }
384
385 int ASN1_STRING_length(const ASN1_STRING *x)
386 {
387     return x->length;
388 }
389
390 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
391 {
392     x->length = len;
393 }
394
395 int ASN1_STRING_type(const ASN1_STRING *x)
396 {
397     return x->type;
398 }
399
400 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
401 {
402     return x->data;
403 }
404
405 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
406 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
407 {
408     return x->data;
409 }
410 #endif
411
412 char *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) {
440             strncpy(p, sep, sep_len + 1);
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 }