Rename lh_xxx,sk_xxx tp OPENSSL_{LH,SK}_xxx
[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;
210
211     ret = length;
212     ret++;
213     if (tag >= 31) {
214         while (tag > 0) {
215             tag >>= 7;
216             ret++;
217         }
218     }
219     if (constructed == 2)
220         return ret + 3;
221     ret++;
222     if (length > 127) {
223         while (length > 0) {
224             length >>= 8;
225             ret++;
226         }
227     }
228     return (ret);
229 }
230
231 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
232 {
233     if (str == NULL)
234         return 0;
235     dst->type = str->type;
236     if (!ASN1_STRING_set(dst, str->data, str->length))
237         return 0;
238     /* Copy flags but preserve embed value */
239     dst->flags &= ASN1_STRING_FLAG_EMBED;
240     dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED;
241     return 1;
242 }
243
244 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
245 {
246     ASN1_STRING *ret;
247     if (!str)
248         return NULL;
249     ret = ASN1_STRING_new();
250     if (ret == NULL)
251         return NULL;
252     if (!ASN1_STRING_copy(ret, str)) {
253         ASN1_STRING_free(ret);
254         return NULL;
255     }
256     return ret;
257 }
258
259 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
260 {
261     unsigned char *c;
262     const char *data = _data;
263
264     if (len < 0) {
265         if (data == NULL)
266             return (0);
267         else
268             len = strlen(data);
269     }
270     if ((str->length < len) || (str->data == NULL)) {
271         c = str->data;
272         str->data = OPENSSL_realloc(c, len + 1);
273         if (str->data == NULL) {
274             ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
275             str->data = c;
276             return (0);
277         }
278     }
279     str->length = len;
280     if (data != NULL) {
281         memcpy(str->data, data, len);
282         /* an allowance for strings :-) */
283         str->data[len] = '\0';
284     }
285     return (1);
286 }
287
288 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
289 {
290     OPENSSL_free(str->data);
291     str->data = data;
292     str->length = len;
293 }
294
295 ASN1_STRING *ASN1_STRING_new(void)
296 {
297     return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
298 }
299
300 ASN1_STRING *ASN1_STRING_type_new(int type)
301 {
302     ASN1_STRING *ret;
303
304     ret = OPENSSL_zalloc(sizeof(*ret));
305     if (ret == NULL) {
306         ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
307         return (NULL);
308     }
309     ret->type = type;
310     return (ret);
311 }
312
313 void ASN1_STRING_free(ASN1_STRING *a)
314 {
315     if (a == NULL)
316         return;
317     if (!(a->flags & ASN1_STRING_FLAG_NDEF))
318         OPENSSL_free(a->data);
319     if (!(a->flags & ASN1_STRING_FLAG_EMBED))
320         OPENSSL_free(a);
321 }
322
323 void ASN1_STRING_clear_free(ASN1_STRING *a)
324 {
325     if (a == NULL)
326         return;
327     if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
328         OPENSSL_cleanse(a->data, a->length);
329     ASN1_STRING_free(a);
330 }
331
332 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
333 {
334     int i;
335
336     i = (a->length - b->length);
337     if (i == 0) {
338         i = memcmp(a->data, b->data, a->length);
339         if (i == 0)
340             return (a->type - b->type);
341         else
342             return (i);
343     } else
344         return (i);
345 }
346
347 int ASN1_STRING_length(const ASN1_STRING *x)
348 {
349     return x->length;
350 }
351
352 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
353 {
354     x->length = len;
355 }
356
357 int ASN1_STRING_type(ASN1_STRING *x)
358 {
359     return x->type;
360 }
361
362 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
363 {
364     return x->data;
365 }