rand/randfile.c: restore fallback to $HOME for non-setuid programs.
[openssl.git] / crypto / asn1 / asn1_lib.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
15 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
16                            long max);
17 static void asn1_put_length(unsigned char **pp, int length);
18
19 static int _asn1_check_infinite_end(const unsigned char **p, long len)
20 {
21     /*
22      * If there is 0 or 1 byte left, the length check should pick things up
23      */
24     if (len <= 0)
25         return (1);
26     else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
27         (*p) += 2;
28         return (1);
29     }
30     return (0);
31 }
32
33 int ASN1_check_infinite_end(unsigned char **p, long len)
34 {
35     return _asn1_check_infinite_end((const unsigned char **)p, len);
36 }
37
38 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
39 {
40     return _asn1_check_infinite_end(p, len);
41 }
42
43 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
44                     int *pclass, long omax)
45 {
46     int i, ret;
47     long l;
48     const unsigned char *p = *pp;
49     int tag, xclass, inf;
50     long max = omax;
51
52     if (!max)
53         goto err;
54     ret = (*p & V_ASN1_CONSTRUCTED);
55     xclass = (*p & V_ASN1_PRIVATE);
56     i = *p & V_ASN1_PRIMITIVE_TAG;
57     if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
58         p++;
59         if (--max == 0)
60             goto err;
61         l = 0;
62         while (*p & 0x80) {
63             l <<= 7L;
64             l |= *(p++) & 0x7f;
65             if (--max == 0)
66                 goto err;
67             if (l > (INT_MAX >> 7L))
68                 goto err;
69         }
70         l <<= 7L;
71         l |= *(p++) & 0x7f;
72         tag = (int)l;
73         if (--max == 0)
74             goto err;
75     } else {
76         tag = i;
77         p++;
78         if (--max == 0)
79             goto err;
80     }
81     *ptag = tag;
82     *pclass = xclass;
83     if (!asn1_get_length(&p, &inf, plength, max))
84         goto err;
85
86     if (inf && !(ret & V_ASN1_CONSTRUCTED))
87         goto err;
88
89     if (*plength > (omax - (p - *pp))) {
90         ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
91         /*
92          * Set this so that even if things are not long enough the values are
93          * set correctly
94          */
95         ret |= 0x80;
96     }
97     *pp = p;
98     return (ret | inf);
99  err:
100     ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
101     return (0x80);
102 }
103
104 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
105                            long max)
106 {
107     const unsigned char *p = *pp;
108     unsigned long ret = 0;
109     unsigned long i;
110
111     if (max-- < 1)
112         return 0;
113     if (*p == 0x80) {
114         *inf = 1;
115         ret = 0;
116         p++;
117     } else {
118         *inf = 0;
119         i = *p & 0x7f;
120         if (*(p++) & 0x80) {
121             if (max < (long)i + 1)
122                 return 0;
123             /* Skip leading zeroes */
124             while (i && *p == 0) {
125                 p++;
126                 i--;
127             }
128             if (i > sizeof(long))
129                 return 0;
130             while (i-- > 0) {
131                 ret <<= 8L;
132                 ret |= *(p++);
133             }
134         } else
135             ret = i;
136     }
137     if (ret > LONG_MAX)
138         return 0;
139     *pp = p;
140     *rl = (long)ret;
141     return 1;
142 }
143
144 /*
145  * class 0 is constructed constructed == 2 for indefinite length constructed
146  */
147 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
148                      int xclass)
149 {
150     unsigned char *p = *pp;
151     int i, ttag;
152
153     i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
154     i |= (xclass & V_ASN1_PRIVATE);
155     if (tag < 31)
156         *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
157     else {
158         *(p++) = i | V_ASN1_PRIMITIVE_TAG;
159         for (i = 0, ttag = tag; ttag > 0; i++)
160             ttag >>= 7;
161         ttag = i;
162         while (i-- > 0) {
163             p[i] = tag & 0x7f;
164             if (i != (ttag - 1))
165                 p[i] |= 0x80;
166             tag >>= 7;
167         }
168         p += ttag;
169     }
170     if (constructed == 2)
171         *(p++) = 0x80;
172     else
173         asn1_put_length(&p, length);
174     *pp = p;
175 }
176
177 int ASN1_put_eoc(unsigned char **pp)
178 {
179     unsigned char *p = *pp;
180     *p++ = 0;
181     *p++ = 0;
182     *pp = p;
183     return 2;
184 }
185
186 static void asn1_put_length(unsigned char **pp, int length)
187 {
188     unsigned char *p = *pp;
189     int i, l;
190     if (length <= 127)
191         *(p++) = (unsigned char)length;
192     else {
193         l = length;
194         for (i = 0; l > 0; i++)
195             l >>= 8;
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
207 int ASN1_object_size(int constructed, int length, int tag)
208 {
209     int ret = 1;
210     if (length < 0)
211         return -1;
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     return ret + length;
233 }
234
235 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
236 {
237     if (str == NULL)
238         return 0;
239     dst->type = str->type;
240     if (!ASN1_STRING_set(dst, str->data, str->length))
241         return 0;
242     /* Copy flags but preserve embed value */
243     dst->flags &= ASN1_STRING_FLAG_EMBED;
244     dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
245     return 1;
246 }
247
248 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
249 {
250     ASN1_STRING *ret;
251     if (!str)
252         return NULL;
253     ret = ASN1_STRING_new();
254     if (ret == NULL)
255         return NULL;
256     if (!ASN1_STRING_copy(ret, str)) {
257         ASN1_STRING_free(ret);
258         return NULL;
259     }
260     return ret;
261 }
262
263 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
264 {
265     unsigned char *c;
266     const char *data = _data;
267
268     if (len < 0) {
269         if (data == NULL)
270             return (0);
271         else
272             len = strlen(data);
273     }
274     if ((str->length <= len) || (str->data == NULL)) {
275         c = str->data;
276         str->data = OPENSSL_realloc(c, len + 1);
277         if (str->data == NULL) {
278             ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
279             str->data = c;
280             return (0);
281         }
282     }
283     str->length = len;
284     if (data != NULL) {
285         memcpy(str->data, data, len);
286         /* an allowance for strings :-) */
287         str->data[len] = '\0';
288     }
289     return (1);
290 }
291
292 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
293 {
294     OPENSSL_free(str->data);
295     str->data = data;
296     str->length = len;
297 }
298
299 ASN1_STRING *ASN1_STRING_new(void)
300 {
301     return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
302 }
303
304 ASN1_STRING *ASN1_STRING_type_new(int type)
305 {
306     ASN1_STRING *ret;
307
308     ret = OPENSSL_zalloc(sizeof(*ret));
309     if (ret == NULL) {
310         ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
311         return (NULL);
312     }
313     ret->type = type;
314     return (ret);
315 }
316
317 void ASN1_STRING_free(ASN1_STRING *a)
318 {
319     if (a == NULL)
320         return;
321     if (!(a->flags & ASN1_STRING_FLAG_NDEF))
322         OPENSSL_free(a->data);
323     if (!(a->flags & ASN1_STRING_FLAG_EMBED))
324         OPENSSL_free(a);
325 }
326
327 void ASN1_STRING_clear_free(ASN1_STRING *a)
328 {
329     if (a == NULL)
330         return;
331     if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
332         OPENSSL_cleanse(a->data, a->length);
333     ASN1_STRING_free(a);
334 }
335
336 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
337 {
338     int i;
339
340     i = (a->length - b->length);
341     if (i == 0) {
342         i = memcmp(a->data, b->data, a->length);
343         if (i == 0)
344             return (a->type - b->type);
345         else
346             return (i);
347     } else
348         return (i);
349 }
350
351 int ASN1_STRING_length(const ASN1_STRING *x)
352 {
353     return x->length;
354 }
355
356 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
357 {
358     x->length = len;
359 }
360
361 int ASN1_STRING_type(const ASN1_STRING *x)
362 {
363     return x->type;
364 }
365
366 const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
367 {
368     return x->data;
369 }
370
371 # if OPENSSL_API_COMPAT < 0x10100000L
372 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
373 {
374     return x->data;
375 }
376 #endif