embed support for CHOICE type
[openssl.git] / crypto / asn1 / asn1_lib.c
1 /* crypto/asn1/asn1_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <limits.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/asn1.h>
63
64 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
65                            int max);
66 static void asn1_put_length(unsigned char **pp, int length);
67
68 static int _asn1_check_infinite_end(const unsigned char **p, long len)
69 {
70     /*
71      * If there is 0 or 1 byte left, the length check should pick things up
72      */
73     if (len <= 0)
74         return (1);
75     else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
76         (*p) += 2;
77         return (1);
78     }
79     return (0);
80 }
81
82 int ASN1_check_infinite_end(unsigned char **p, long len)
83 {
84     return _asn1_check_infinite_end((const unsigned char **)p, len);
85 }
86
87 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
88 {
89     return _asn1_check_infinite_end(p, len);
90 }
91
92 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
93                     int *pclass, long omax)
94 {
95     int i, ret;
96     long l;
97     const unsigned char *p = *pp;
98     int tag, xclass, inf;
99     long max = omax;
100
101     if (!max)
102         goto err;
103     ret = (*p & V_ASN1_CONSTRUCTED);
104     xclass = (*p & V_ASN1_PRIVATE);
105     i = *p & V_ASN1_PRIMITIVE_TAG;
106     if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
107         p++;
108         if (--max == 0)
109             goto err;
110         l = 0;
111         while (*p & 0x80) {
112             l <<= 7L;
113             l |= *(p++) & 0x7f;
114             if (--max == 0)
115                 goto err;
116             if (l > (INT_MAX >> 7L))
117                 goto err;
118         }
119         l <<= 7L;
120         l |= *(p++) & 0x7f;
121         tag = (int)l;
122         if (--max == 0)
123             goto err;
124     } else {
125         tag = i;
126         p++;
127         if (--max == 0)
128             goto err;
129     }
130     *ptag = tag;
131     *pclass = xclass;
132     if (!asn1_get_length(&p, &inf, plength, (int)max))
133         goto err;
134
135     if (inf && !(ret & V_ASN1_CONSTRUCTED))
136         goto err;
137
138     if (*plength > (omax - (p - *pp))) {
139         ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);
140         /*
141          * Set this so that even if things are not long enough the values are
142          * set correctly
143          */
144         ret |= 0x80;
145     }
146     *pp = p;
147     return (ret | inf);
148  err:
149     ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);
150     return (0x80);
151 }
152
153 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
154                            int max)
155 {
156     const unsigned char *p = *pp;
157     unsigned long ret = 0;
158     unsigned int i;
159
160     if (max-- < 1)
161         return (0);
162     if (*p == 0x80) {
163         *inf = 1;
164         ret = 0;
165         p++;
166     } else {
167         *inf = 0;
168         i = *p & 0x7f;
169         if (*(p++) & 0x80) {
170             if (max < (int)i)
171                 return 0;
172             /* Skip leading zeroes */
173             while (i && *p == 0) {
174                 p++;
175                 i--;
176             }
177             if (i > sizeof(long))
178                 return 0;
179             while (i-- > 0) {
180                 ret <<= 8L;
181                 ret |= *(p++);
182             }
183         } else
184             ret = i;
185     }
186     if (ret > LONG_MAX)
187         return 0;
188     *pp = p;
189     *rl = (long)ret;
190     return (1);
191 }
192
193 /*
194  * class 0 is constructed constructed == 2 for indefinite length constructed
195  */
196 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
197                      int xclass)
198 {
199     unsigned char *p = *pp;
200     int i, ttag;
201
202     i = (constructed) ? V_ASN1_CONSTRUCTED : 0;
203     i |= (xclass & V_ASN1_PRIVATE);
204     if (tag < 31)
205         *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG);
206     else {
207         *(p++) = i | V_ASN1_PRIMITIVE_TAG;
208         for (i = 0, ttag = tag; ttag > 0; i++)
209             ttag >>= 7;
210         ttag = i;
211         while (i-- > 0) {
212             p[i] = tag & 0x7f;
213             if (i != (ttag - 1))
214                 p[i] |= 0x80;
215             tag >>= 7;
216         }
217         p += ttag;
218     }
219     if (constructed == 2)
220         *(p++) = 0x80;
221     else
222         asn1_put_length(&p, length);
223     *pp = p;
224 }
225
226 int ASN1_put_eoc(unsigned char **pp)
227 {
228     unsigned char *p = *pp;
229     *p++ = 0;
230     *p++ = 0;
231     *pp = p;
232     return 2;
233 }
234
235 static void asn1_put_length(unsigned char **pp, int length)
236 {
237     unsigned char *p = *pp;
238     int i, l;
239     if (length <= 127)
240         *(p++) = (unsigned char)length;
241     else {
242         l = length;
243         for (i = 0; l > 0; i++)
244             l >>= 8;
245         *(p++) = i | 0x80;
246         l = i;
247         while (i-- > 0) {
248             p[i] = length & 0xff;
249             length >>= 8;
250         }
251         p += l;
252     }
253     *pp = p;
254 }
255
256 int ASN1_object_size(int constructed, int length, int tag)
257 {
258     int ret;
259
260     ret = length;
261     ret++;
262     if (tag >= 31) {
263         while (tag > 0) {
264             tag >>= 7;
265             ret++;
266         }
267     }
268     if (constructed == 2)
269         return ret + 3;
270     ret++;
271     if (length > 127) {
272         while (length > 0) {
273             length >>= 8;
274             ret++;
275         }
276     }
277     return (ret);
278 }
279
280 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
281 {
282     if (str == NULL)
283         return 0;
284     dst->type = str->type;
285     if (!ASN1_STRING_set(dst, str->data, str->length))
286         return 0;
287     dst->flags = str->flags;
288     return 1;
289 }
290
291 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
292 {
293     ASN1_STRING *ret;
294     if (!str)
295         return NULL;
296     ret = ASN1_STRING_new();
297     if (!ret)
298         return NULL;
299     if (!ASN1_STRING_copy(ret, str)) {
300         ASN1_STRING_free(ret);
301         return NULL;
302     }
303     return ret;
304 }
305
306 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
307 {
308     unsigned char *c;
309     const char *data = _data;
310
311     if (len < 0) {
312         if (data == NULL)
313             return (0);
314         else
315             len = strlen(data);
316     }
317     if ((str->length < len) || (str->data == NULL)) {
318         c = str->data;
319         str->data = OPENSSL_realloc(c, len + 1);
320         if (str->data == NULL) {
321             ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
322             str->data = c;
323             return (0);
324         }
325     }
326     str->length = len;
327     if (data != NULL) {
328         memcpy(str->data, data, len);
329         /* an allowance for strings :-) */
330         str->data[len] = '\0';
331     }
332     return (1);
333 }
334
335 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
336 {
337     OPENSSL_free(str->data);
338     str->data = data;
339     str->length = len;
340 }
341
342 ASN1_STRING *ASN1_STRING_new(void)
343 {
344     return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
345 }
346
347 ASN1_STRING *ASN1_STRING_type_new(int type)
348 {
349     ASN1_STRING *ret;
350
351     ret = OPENSSL_zalloc(sizeof(*ret));
352     if (ret == NULL) {
353         ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);
354         return (NULL);
355     }
356     ret->type = type;
357     return (ret);
358 }
359
360 void ASN1_STRING_free(ASN1_STRING *a)
361 {
362     if (a == NULL)
363         return;
364     if (!(a->flags & ASN1_STRING_FLAG_NDEF))
365         OPENSSL_free(a->data);
366     OPENSSL_free(a);
367 }
368
369 void ASN1_STRING_clear_free(ASN1_STRING *a)
370 {
371     if (a == NULL)
372         return;
373     if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
374         OPENSSL_cleanse(a->data, a->length);
375     ASN1_STRING_free(a);
376 }
377
378 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
379 {
380     int i;
381
382     i = (a->length - b->length);
383     if (i == 0) {
384         i = memcmp(a->data, b->data, a->length);
385         if (i == 0)
386             return (a->type - b->type);
387         else
388             return (i);
389     } else
390         return (i);
391 }
392
393 int ASN1_STRING_length(const ASN1_STRING *x)
394 {
395     return x->length;
396 }
397
398 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
399 {
400     x->length = len;
401 }
402
403 int ASN1_STRING_type(ASN1_STRING *x)
404 {
405     return x->type;
406 }
407
408 unsigned char *ASN1_STRING_data(ASN1_STRING *x)
409 {
410     return x->data;
411 }