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