Fix memory leak in GENERAL_NAME_set0_othername.
[openssl.git] / crypto / asn1 / a_object.c
1 /* crypto/asn1/a_object.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 "cryptlib.h"
62 #include <openssl/buffer.h>
63 #include <openssl/asn1.h>
64 #include <openssl/objects.h>
65 #include <openssl/bn.h>
66
67 int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
68 {
69     unsigned char *p;
70     int objsize;
71
72     if ((a == NULL) || (a->data == NULL))
73         return (0);
74
75     objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
76     if (pp == NULL || objsize == -1)
77         return objsize;
78
79     p = *pp;
80     ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
81     memcpy(p, a->data, a->length);
82     p += a->length;
83
84     *pp = p;
85     return (objsize);
86 }
87
88 int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
89 {
90     int i, first, len = 0, c, use_bn;
91     char ftmp[24], *tmp = ftmp;
92     int tmpsize = sizeof ftmp;
93     const char *p;
94     unsigned long l;
95     BIGNUM *bl = NULL;
96
97     if (num == 0)
98         return (0);
99     else if (num == -1)
100         num = strlen(buf);
101
102     p = buf;
103     c = *(p++);
104     num--;
105     if ((c >= '0') && (c <= '2')) {
106         first = c - '0';
107     } else {
108         ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
109         goto err;
110     }
111
112     if (num <= 0) {
113         ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
114         goto err;
115     }
116     c = *(p++);
117     num--;
118     for (;;) {
119         if (num <= 0)
120             break;
121         if ((c != '.') && (c != ' ')) {
122             ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
123             goto err;
124         }
125         l = 0;
126         use_bn = 0;
127         for (;;) {
128             if (num <= 0)
129                 break;
130             num--;
131             c = *(p++);
132             if ((c == ' ') || (c == '.'))
133                 break;
134             if ((c < '0') || (c > '9')) {
135                 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
136                 goto err;
137             }
138             if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
139                 use_bn = 1;
140                 if (!bl)
141                     bl = BN_new();
142                 if (!bl || !BN_set_word(bl, l))
143                     goto err;
144             }
145             if (use_bn) {
146                 if (!BN_mul_word(bl, 10L)
147                     || !BN_add_word(bl, c - '0'))
148                     goto err;
149             } else
150                 l = l * 10L + (long)(c - '0');
151         }
152         if (len == 0) {
153             if ((first < 2) && (l >= 40)) {
154                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,
155                         ASN1_R_SECOND_NUMBER_TOO_LARGE);
156                 goto err;
157             }
158             if (use_bn) {
159                 if (!BN_add_word(bl, first * 40))
160                     goto err;
161             } else
162                 l += (long)first *40;
163         }
164         i = 0;
165         if (use_bn) {
166             int blsize;
167             blsize = BN_num_bits(bl);
168             blsize = (blsize + 6) / 7;
169             if (blsize > tmpsize) {
170                 if (tmp != ftmp)
171                     OPENSSL_free(tmp);
172                 tmpsize = blsize + 32;
173                 tmp = OPENSSL_malloc(tmpsize);
174                 if (!tmp)
175                     goto err;
176             }
177             while (blsize--) {
178                 BN_ULONG t = BN_div_word(bl, 0x80L);
179                 if (t == (BN_ULONG)-1)
180                     goto err;
181                 tmp[i++] = (unsigned char)t;
182             }
183         } else {
184
185             for (;;) {
186                 tmp[i++] = (unsigned char)l & 0x7f;
187                 l >>= 7L;
188                 if (l == 0L)
189                     break;
190             }
191
192         }
193         if (out != NULL) {
194             if (len + i > olen) {
195                 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
196                 goto err;
197             }
198             while (--i > 0)
199                 out[len++] = tmp[i] | 0x80;
200             out[len++] = tmp[0];
201         } else
202             len += i;
203     }
204     if (tmp != ftmp)
205         OPENSSL_free(tmp);
206     if (bl)
207         BN_free(bl);
208     return (len);
209  err:
210     if (tmp != ftmp)
211         OPENSSL_free(tmp);
212     if (bl)
213         BN_free(bl);
214     return (0);
215 }
216
217 int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
218 {
219     return OBJ_obj2txt(buf, buf_len, a, 0);
220 }
221
222 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
223 {
224     char buf[80], *p = buf;
225     int i;
226
227     if ((a == NULL) || (a->data == NULL))
228         return (BIO_write(bp, "NULL", 4));
229     i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
230     if (i > (int)(sizeof(buf) - 1)) {
231         p = OPENSSL_malloc(i + 1);
232         if (!p)
233             return -1;
234         i2t_ASN1_OBJECT(p, i + 1, a);
235     }
236     if (i <= 0)
237         return BIO_write(bp, "<INVALID>", 9);
238     BIO_write(bp, p, i);
239     if (p != buf)
240         OPENSSL_free(p);
241     return (i);
242 }
243
244 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
245                              long length)
246 {
247     const unsigned char *p;
248     long len;
249     int tag, xclass;
250     int inf, i;
251     ASN1_OBJECT *ret = NULL;
252     p = *pp;
253     inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
254     if (inf & 0x80) {
255         i = ASN1_R_BAD_OBJECT_HEADER;
256         goto err;
257     }
258
259     if (tag != V_ASN1_OBJECT) {
260         i = ASN1_R_EXPECTING_AN_OBJECT;
261         goto err;
262     }
263     ret = c2i_ASN1_OBJECT(a, &p, len);
264     if (ret)
265         *pp = p;
266     return ret;
267  err:
268     ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
269     return (NULL);
270 }
271
272 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
273                              long len)
274 {
275     ASN1_OBJECT *ret = NULL;
276     const unsigned char *p;
277     unsigned char *data;
278     int i, length;
279
280     /*
281      * Sanity check OID encoding. Need at least one content octet. MSB must
282      * be clear in the last octet. can't have leading 0x80 in subidentifiers,
283      * see: X.690 8.19.2
284      */
285     if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
286         p[len - 1] & 0x80) {
287         ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
288         return NULL;
289     }
290     /* Now 0 < len <= INT_MAX, so the cast is safe. */
291     length = (int)len;
292     for (i = 0; i < length; i++, p++) {
293         if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
294             ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
295             return NULL;
296         }
297     }
298
299     /*
300      * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
301      * ->ln
302      */
303     if ((a == NULL) || ((*a) == NULL) ||
304         !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
305         if ((ret = ASN1_OBJECT_new()) == NULL)
306             return (NULL);
307     } else
308         ret = (*a);
309
310     p = *pp;
311     /* detach data from object */
312     data = (unsigned char *)ret->data;
313     ret->data = NULL;
314     /* once detached we can change it */
315     if ((data == NULL) || (ret->length < length)) {
316         ret->length = 0;
317         if (data != NULL)
318             OPENSSL_free(data);
319         data = (unsigned char *)OPENSSL_malloc(length);
320         if (data == NULL) {
321             i = ERR_R_MALLOC_FAILURE;
322             goto err;
323         }
324         ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
325     }
326     memcpy(data, p, length);
327     /* reattach data to object, after which it remains const */
328     ret->data = data;
329     ret->length = length;
330     ret->sn = NULL;
331     ret->ln = NULL;
332     /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
333     p += length;
334
335     if (a != NULL)
336         (*a) = ret;
337     *pp = p;
338     return (ret);
339  err:
340     ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
341     if ((ret != NULL) && ((a == NULL) || (*a != ret)))
342         ASN1_OBJECT_free(ret);
343     return (NULL);
344 }
345
346 ASN1_OBJECT *ASN1_OBJECT_new(void)
347 {
348     ASN1_OBJECT *ret;
349
350     ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
351     if (ret == NULL) {
352         ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
353         return (NULL);
354     }
355     ret->length = 0;
356     ret->data = NULL;
357     ret->nid = 0;
358     ret->sn = NULL;
359     ret->ln = NULL;
360     ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
361     return (ret);
362 }
363
364 void ASN1_OBJECT_free(ASN1_OBJECT *a)
365 {
366     if (a == NULL)
367         return;
368     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
369 #ifndef CONST_STRICT            /* disable purely for compile-time strict
370                                  * const checking. Doing this on a "real"
371                                  * compile will cause memory leaks */
372         if (a->sn != NULL)
373             OPENSSL_free((void *)a->sn);
374         if (a->ln != NULL)
375             OPENSSL_free((void *)a->ln);
376 #endif
377         a->sn = a->ln = NULL;
378     }
379     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
380         if (a->data != NULL)
381             OPENSSL_free((void *)a->data);
382         a->data = NULL;
383         a->length = 0;
384     }
385     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
386         OPENSSL_free(a);
387 }
388
389 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
390                                 const char *sn, const char *ln)
391 {
392     ASN1_OBJECT o;
393
394     o.sn = sn;
395     o.ln = ln;
396     o.data = data;
397     o.nid = nid;
398     o.length = len;
399     o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
400         ASN1_OBJECT_FLAG_DYNAMIC_DATA;
401     return (OBJ_dup(&o));
402 }
403
404 IMPLEMENT_STACK_OF(ASN1_OBJECT)
405
406 IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT)