make ASN1_OBJECT opaque
[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 #include "internal/asn1_int.h"
67
68 int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
69 {
70     unsigned char *p;
71     int objsize;
72
73     if ((a == NULL) || (a->data == NULL))
74         return (0);
75
76     objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
77     if (pp == NULL)
78         return objsize;
79
80     p = *pp;
81     ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
82     memcpy(p, a->data, a->length);
83     p += a->length;
84
85     *pp = p;
86     return (objsize);
87 }
88
89 int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
90 {
91     int i, first, len = 0, c, use_bn;
92     char ftmp[24], *tmp = ftmp;
93     int tmpsize = sizeof ftmp;
94     const char *p;
95     unsigned long l;
96     BIGNUM *bl = NULL;
97
98     if (num == 0)
99         return (0);
100     else if (num == -1)
101         num = strlen(buf);
102
103     p = buf;
104     c = *(p++);
105     num--;
106     if ((c >= '0') && (c <= '2')) {
107         first = c - '0';
108     } else {
109         ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
110         goto err;
111     }
112
113     if (num <= 0) {
114         ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
115         goto err;
116     }
117     c = *(p++);
118     num--;
119     for (;;) {
120         if (num <= 0)
121             break;
122         if ((c != '.') && (c != ' ')) {
123             ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
124             goto err;
125         }
126         l = 0;
127         use_bn = 0;
128         for (;;) {
129             if (num <= 0)
130                 break;
131             num--;
132             c = *(p++);
133             if ((c == ' ') || (c == '.'))
134                 break;
135             if ((c < '0') || (c > '9')) {
136                 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
137                 goto err;
138             }
139             if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
140                 use_bn = 1;
141                 if (!bl)
142                     bl = BN_new();
143                 if (!bl || !BN_set_word(bl, l))
144                     goto err;
145             }
146             if (use_bn) {
147                 if (!BN_mul_word(bl, 10L)
148                     || !BN_add_word(bl, c - '0'))
149                     goto err;
150             } else
151                 l = l * 10L + (long)(c - '0');
152         }
153         if (len == 0) {
154             if ((first < 2) && (l >= 40)) {
155                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,
156                         ASN1_R_SECOND_NUMBER_TOO_LARGE);
157                 goto err;
158             }
159             if (use_bn) {
160                 if (!BN_add_word(bl, first * 40))
161                     goto err;
162             } else
163                 l += (long)first *40;
164         }
165         i = 0;
166         if (use_bn) {
167             int blsize;
168             blsize = BN_num_bits(bl);
169             blsize = (blsize + 6) / 7;
170             if (blsize > tmpsize) {
171                 if (tmp != ftmp)
172                     OPENSSL_free(tmp);
173                 tmpsize = blsize + 32;
174                 tmp = OPENSSL_malloc(tmpsize);
175                 if (!tmp)
176                     goto err;
177             }
178             while (blsize--)
179                 tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
180         } else {
181
182             for (;;) {
183                 tmp[i++] = (unsigned char)l & 0x7f;
184                 l >>= 7L;
185                 if (l == 0L)
186                     break;
187             }
188
189         }
190         if (out != NULL) {
191             if (len + i > olen) {
192                 ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
193                 goto err;
194             }
195             while (--i > 0)
196                 out[len++] = tmp[i] | 0x80;
197             out[len++] = tmp[0];
198         } else
199             len += i;
200     }
201     if (tmp != ftmp)
202         OPENSSL_free(tmp);
203     if (bl)
204         BN_free(bl);
205     return (len);
206  err:
207     if (tmp != ftmp)
208         OPENSSL_free(tmp);
209     if (bl)
210         BN_free(bl);
211     return (0);
212 }
213
214 int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
215 {
216     return OBJ_obj2txt(buf, buf_len, a, 0);
217 }
218
219 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
220 {
221     char buf[80], *p = buf;
222     int i;
223
224     if ((a == NULL) || (a->data == NULL))
225         return (BIO_write(bp, "NULL", 4));
226     i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
227     if (i > (int)(sizeof(buf) - 1)) {
228         p = OPENSSL_malloc(i + 1);
229         if (!p)
230             return -1;
231         i2t_ASN1_OBJECT(p, i + 1, a);
232     }
233     if (i <= 0) {
234         i = BIO_write(bp, "<INVALID>", 9);
235         i += BIO_dump(bp, (const char *)a->data, a->length);
236         return i;
237     }
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 }