Change #include filenames from <foo.h> to <openssl.h>.
[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 "cryptlib.h"
61 #include <openssl/buffer.h>
62 #include <openssl/asn1.h>
63 #include <openssl/objects.h>
64
65 /* ASN1err(ASN1_F_ASN1_OBJECT_NEW,ASN1_R_EXPECTING_AN_OBJECT); 
66  * ASN1err(ASN1_F_D2I_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER); 
67  * ASN1err(ASN1_F_I2T_ASN1_OBJECT,ASN1_R_BAD_OBJECT_HEADER);
68  */
69
70 int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
71         {
72         unsigned char *p;
73
74         if ((a == NULL) || (a->data == NULL)) return(0);
75
76         if (pp == NULL)
77                 return(ASN1_object_size(0,a->length,V_ASN1_OBJECT));
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(a->length);
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;
91         char tmp[24];
92         const char *p;
93         unsigned long l;
94
95         if (num == 0)
96                 return(0);
97         else if (num == -1)
98                 num=strlen(buf);
99
100         p=buf;
101         c= *(p++);
102         num--;
103         if ((c >= '0') && (c <= '2'))
104                 {
105                 first=(c-'0')*40;
106                 }
107         else
108                 {
109                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);
110                 goto err;
111                 }
112
113         if (num <= 0)
114                 {
115                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);
116                 goto err;
117                 }
118         c= *(p++);
119         num--;
120         for (;;)
121                 {
122                 if (num <= 0) break;
123                 if ((c != '.') && (c != ' '))
124                         {
125                         ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);
126                         goto err;
127                         }
128                 l=0;
129                 for (;;)
130                         {
131                         if (num <= 0) break;
132                         num--;
133                         c= *(p++);
134                         if ((c == ' ') || (c == '.'))
135                                 break;
136                         if ((c < '0') || (c > '9'))
137                                 {
138                                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
139                                 goto err;
140                                 }
141                         l=l*10L+(long)(c-'0');
142                         }
143                 if (len == 0)
144                         {
145                         if ((first < 2) && (l >= 40))
146                                 {
147                                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
148                                 goto err;
149                                 }
150                         l+=(long)first;
151                         }
152                 i=0;
153                 for (;;)
154                         {
155                         tmp[i++]=(unsigned char)l&0x7f;
156                         l>>=7L;
157                         if (l == 0L) break;
158                         }
159                 if (out != NULL)
160                         {
161                         if (len+i > olen)
162                                 {
163                                 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);
164                                 goto err;
165                                 }
166                         while (--i > 0)
167                                 out[len++]=tmp[i]|0x80;
168                         out[len++]=tmp[0];
169                         }
170                 else
171                         len+=i;
172                 }
173         return(len);
174 err:
175         return(0);
176         }
177
178 int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
179         {
180         int i,idx=0,n=0,len,nid;
181         unsigned long l;
182         unsigned char *p;
183         const char *s;
184         char tbuf[32];
185
186         if (buf_len <= 0) return(0);
187
188         if ((a == NULL) || (a->data == NULL))
189                 {
190                 buf[0]='\0';
191                 return(0);
192                 }
193
194         nid=OBJ_obj2nid(a);
195         if (nid == NID_undef)
196                 {
197                 len=a->length;
198                 p=a->data;
199
200                 idx=0;
201                 l=0;
202                 while (idx < a->length)
203                         {
204                         l|=(p[idx]&0x7f);
205                         if (!(p[idx] & 0x80)) break;
206                         l<<=7L;
207                         idx++;
208                         }
209                 idx++;
210                 i=(int)(l/40);
211                 if (i > 2) i=2;
212                 l-=(long)(i*40);
213
214                 sprintf(tbuf,"%d.%ld",i,l);
215                 i=strlen(tbuf);
216                 strncpy(buf,tbuf,buf_len);
217                 buf_len-=i;
218                 buf+=i;
219                 n+=i;
220
221                 l=0;
222                 for (; idx<len; idx++)
223                         {
224                         l|=p[idx]&0x7f;
225                         if (!(p[idx] & 0x80))
226                                 {
227                                 sprintf(tbuf,".%ld",l);
228                                 i=strlen(tbuf);
229                                 if (buf_len > 0)
230                                         strncpy(buf,tbuf,buf_len);
231                                 buf_len-=i;
232                                 buf+=i;
233                                 n+=i;
234                                 l=0;
235                                 }
236                         l<<=7L;
237                         }
238                 }
239         else
240                 {
241                 s=OBJ_nid2ln(nid);
242                 if (s == NULL)
243                         s=OBJ_nid2sn(nid);
244                 strncpy(buf,s,buf_len);
245                 n=strlen(s);
246                 }
247         buf[buf_len-1]='\0';
248         return(n);
249         }
250
251 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
252         {
253         char buf[80];
254         int i;
255
256         if ((a == NULL) || (a->data == NULL))
257                 return(BIO_write(bp,"NULL",4));
258         i=i2t_ASN1_OBJECT(buf,80,a);
259         if (i > 80) i=80;
260         BIO_write(bp,buf,i);
261         return(i);
262         }
263
264 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, unsigned char **pp,
265              long length)
266         {
267         ASN1_OBJECT *ret=NULL;
268         unsigned char *p;
269         long len;
270         int tag,xclass;
271         int inf,i;
272
273         /* only the ASN1_OBJECTs from the 'table' will have values
274          * for ->sn or ->ln */
275         if ((a == NULL) || ((*a) == NULL) ||
276                 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
277                 {
278                 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
279                 }
280         else    ret=(*a);
281
282         p= *pp;
283
284         inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
285         if (inf & 0x80)
286                 {
287                 i=ASN1_R_BAD_OBJECT_HEADER;
288                 goto err;
289                 }
290
291         if (tag != V_ASN1_OBJECT)
292                 {
293                 i=ASN1_R_EXPECTING_AN_OBJECT;
294                 goto err;
295                 }
296         if ((ret->data == NULL) || (ret->length < len))
297                 {
298                 if (ret->data != NULL) Free((char *)ret->data);
299                 ret->data=(unsigned char *)Malloc((int)len);
300                 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
301                 if (ret->data == NULL)
302                         { i=ERR_R_MALLOC_FAILURE; goto err; }
303                 }
304         memcpy(ret->data,p,(int)len);
305         ret->length=(int)len;
306         ret->sn=NULL;
307         ret->ln=NULL;
308         /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
309         p+=len;
310
311         if (a != NULL) (*a)=ret;
312         *pp=p;
313         return(ret);
314 err:
315         ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
316         if ((ret != NULL) && ((a == NULL) || (*a != ret)))
317                 ASN1_OBJECT_free(ret);
318         return(NULL);
319         }
320
321 ASN1_OBJECT *ASN1_OBJECT_new(void)
322         {
323         ASN1_OBJECT *ret;
324
325         ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT));
326         if (ret == NULL)
327                 {
328                 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
329                 return(NULL);
330                 }
331         ret->length=0;
332         ret->data=NULL;
333         ret->nid=0;
334         ret->sn=NULL;
335         ret->ln=NULL;
336         ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
337         return(ret);
338         }
339
340 void ASN1_OBJECT_free(ASN1_OBJECT *a)
341         {
342         if (a == NULL) return;
343         if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
344                 {
345 #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause mempory leaks */
346                 if (a->sn != NULL) Free((void *)a->sn);
347                 if (a->ln != NULL) Free((void *)a->ln);
348 #endif
349                 a->sn=a->ln=NULL;
350                 }
351         if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
352                 {
353                 if (a->data != NULL) Free(a->data);
354                 a->data=NULL;
355                 a->length=0;
356                 }
357         if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
358                 Free(a);
359         }
360
361 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
362              char *sn, char *ln)
363         {
364         ASN1_OBJECT o;
365
366         o.sn=sn;
367         o.ln=ln;
368         o.data=data;
369         o.nid=nid;
370         o.length=len;
371         o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
372                 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
373         return(OBJ_dup(&o));
374         }
375