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