Copyright consolidation: perl files
[openssl.git] / crypto / asn1 / a_object.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #include <stdio.h>
59 #include <limits.h>
60 #include "internal/cryptlib.h"
61 #include <openssl/buffer.h>
62 #include <openssl/asn1.h>
63 #include <openssl/objects.h>
64 #include <openssl/bn.h>
65 #include "internal/asn1_int.h"
66 #include "asn1_locl.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 == NULL)
142                     bl = BN_new();
143                 if (bl == NULL || !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 == NULL)
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     BN_free(bl);
204     return (len);
205  err:
206     if (tmp != ftmp)
207         OPENSSL_free(tmp);
208     BN_free(bl);
209     return (0);
210 }
211
212 int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
213 {
214     return OBJ_obj2txt(buf, buf_len, a, 0);
215 }
216
217 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
218 {
219     char buf[80], *p = buf;
220     int i;
221
222     if ((a == NULL) || (a->data == NULL))
223         return (BIO_write(bp, "NULL", 4));
224     i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
225     if (i > (int)(sizeof(buf) - 1)) {
226         p = OPENSSL_malloc(i + 1);
227         if (p == NULL)
228             return -1;
229         i2t_ASN1_OBJECT(p, i + 1, a);
230     }
231     if (i <= 0) {
232         i = BIO_write(bp, "<INVALID>", 9);
233         i += BIO_dump(bp, (const char *)a->data, a->length);
234         return i;
235     }
236     BIO_write(bp, p, i);
237     if (p != buf)
238         OPENSSL_free(p);
239     return (i);
240 }
241
242 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
243                              long length)
244 {
245     const unsigned char *p;
246     long len;
247     int tag, xclass;
248     int inf, i;
249     ASN1_OBJECT *ret = NULL;
250     p = *pp;
251     inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
252     if (inf & 0x80) {
253         i = ASN1_R_BAD_OBJECT_HEADER;
254         goto err;
255     }
256
257     if (tag != V_ASN1_OBJECT) {
258         i = ASN1_R_EXPECTING_AN_OBJECT;
259         goto err;
260     }
261     ret = c2i_ASN1_OBJECT(a, &p, len);
262     if (ret)
263         *pp = p;
264     return ret;
265  err:
266     ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
267     return (NULL);
268 }
269
270 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
271                              long len)
272 {
273     ASN1_OBJECT *ret = NULL, tobj;
274     const unsigned char *p;
275     unsigned char *data;
276     int i, length;
277
278     /*
279      * Sanity check OID encoding. Need at least one content octet. MSB must
280      * be clear in the last octet. can't have leading 0x80 in subidentifiers,
281      * see: X.690 8.19.2
282      */
283     if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
284         p[len - 1] & 0x80) {
285         ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
286         return NULL;
287     }
288     /* Now 0 < len <= INT_MAX, so the cast is safe. */
289     length = (int)len;
290     /*
291      * Try to lookup OID in table: these are all valid encodings so if we get
292      * a match we know the OID is valid.
293      */
294     tobj.nid = NID_undef;
295     tobj.data = p;
296     tobj.length = length;
297     tobj.flags = 0;
298     i = OBJ_obj2nid(&tobj);
299     if (i != NID_undef) {
300         /*
301          * Return shared registered OID object: this improves efficiency
302          * because we don't have to return a dynamically allocated OID
303          * and NID lookups can use the cached value.
304          */
305         ret = OBJ_nid2obj(i);
306         if (a) {
307             ASN1_OBJECT_free(*a);
308             *a = ret;
309         }
310         *pp += len;
311         return ret;
312     }
313     for (i = 0; i < length; i++, p++) {
314         if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
315             ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
316             return NULL;
317         }
318     }
319
320     /*
321      * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
322      * ->ln
323      */
324     if ((a == NULL) || ((*a) == NULL) ||
325         !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
326         if ((ret = ASN1_OBJECT_new()) == NULL)
327             return (NULL);
328     } else
329         ret = (*a);
330
331     p = *pp;
332     /* detach data from object */
333     data = (unsigned char *)ret->data;
334     ret->data = NULL;
335     /* once detached we can change it */
336     if ((data == NULL) || (ret->length < length)) {
337         ret->length = 0;
338         OPENSSL_free(data);
339         data = OPENSSL_malloc(length);
340         if (data == NULL) {
341             i = ERR_R_MALLOC_FAILURE;
342             goto err;
343         }
344         ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
345     }
346     memcpy(data, p, length);
347     /* reattach data to object, after which it remains const */
348     ret->data = data;
349     ret->length = length;
350     ret->sn = NULL;
351     ret->ln = NULL;
352     /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
353     p += length;
354
355     if (a != NULL)
356         (*a) = ret;
357     *pp = p;
358     return (ret);
359  err:
360     ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
361     if ((a == NULL) || (*a != ret))
362         ASN1_OBJECT_free(ret);
363     return (NULL);
364 }
365
366 ASN1_OBJECT *ASN1_OBJECT_new(void)
367 {
368     ASN1_OBJECT *ret;
369
370     ret = OPENSSL_zalloc(sizeof(*ret));
371     if (ret == NULL) {
372         ASN1err(ASN1_F_ASN1_OBJECT_NEW, ERR_R_MALLOC_FAILURE);
373         return (NULL);
374     }
375     ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
376     return (ret);
377 }
378
379 void ASN1_OBJECT_free(ASN1_OBJECT *a)
380 {
381     if (a == NULL)
382         return;
383     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
384 #ifndef CONST_STRICT            /* disable purely for compile-time strict
385                                  * const checking. Doing this on a "real"
386                                  * compile will cause memory leaks */
387         OPENSSL_free((void*)a->sn);
388         OPENSSL_free((void*)a->ln);
389 #endif
390         a->sn = a->ln = NULL;
391     }
392     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
393         OPENSSL_free((void*)a->data);
394         a->data = NULL;
395         a->length = 0;
396     }
397     if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
398         OPENSSL_free(a);
399 }
400
401 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
402                                 const char *sn, const char *ln)
403 {
404     ASN1_OBJECT o;
405
406     o.sn = sn;
407     o.ln = ln;
408     o.data = data;
409     o.nid = nid;
410     o.length = len;
411     o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
412         ASN1_OBJECT_FLAG_DYNAMIC_DATA;
413     return (OBJ_dup(&o));
414 }