]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/a_object.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / crypto / asn1 / a_object.c
1 /* crypto/asn1/a_object.c */
2 /* Copyright (C) 1995-1997 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_I2A_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 i2a_ASN1_OBJECT(bp,a)
184 BIO *bp;
185 ASN1_OBJECT *a;
186 {
187 int j,i,idx=0,n=0,len,nid,reason=ERR_R_BUF_LIB;
188 unsigned long l;
189 unsigned char *p;
190 char buf[20];
191 char *s;
192
193 if ((a == NULL) || (a->data == NULL))
194 {
195 return(BIO_write(bp,"NULL",4));
196 }
197
198 nid=OBJ_obj2nid(a);
199 if (nid == NID_undef)
200 {
201 len=a->length;
202 p=a->data;
203
204 idx=0;
205 l=0;
206 while (idx < a->length)
207 {
208 l|=(p[idx]&0x7f);
209 if (!(p[idx] & 0x80)) break;
210 l<<=7L;
211 idx++;
212 }
213 idx++;
214 i=(int)(l/40);
215 if (i > 2) i=2;
216 l-=(long)(i*40);
217
218 sprintf(buf,"%d.%ld",i,l);
219 i=strlen(buf);
220 if (BIO_write(bp,buf,i) != i)
221 goto err;
222 n+=i;
223
224 l=0;
225 for (; idx<len; idx++)
226 {
227 l|=p[idx]&0x7f;
228 if (!(p[idx] & 0x80))
229 {
230 sprintf(buf,".%ld",l);
231 i=strlen(buf);
232 if (BIO_write(bp,buf,i) != i) goto err;
233 n+=i;
234 l=0;
235 }
236 l<<=7L;
237 }
238 }
239 else
240 {
241 s=(char *)OBJ_nid2ln(nid);
242 if (s == NULL)
243 s=(char *)OBJ_nid2sn(nid);
244 j=strlen(s);
245 if (BIO_write(bp,s,j) != j) goto err;
246 n=j;
247 }
248 return(n);
249 err:
250 ASN1err(ASN1_F_I2A_ASN1_OBJECT,reason);
251 return(-1);
252 }
253
254 ASN1_OBJECT *d2i_ASN1_OBJECT(a, pp, length)
255 ASN1_OBJECT **a;
256 unsigned char **pp;
257 long length;
258 {
259 ASN1_OBJECT *ret=NULL;
260 unsigned char *p;
261 long len;
262 int tag,xclass;
263 int inf,i;
264
265 /* only the ASN1_OBJECTs from the 'table' will have values
266 * for ->sn or ->ln */
267 if ((a == NULL) || ((*a) == NULL) ||
268 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
269 {
270 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
271 }
272 else ret=(*a);
273
274 p= *pp;
275
276 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
277 if (inf & 0x80)
278 {
279 i=ASN1_R_BAD_OBJECT_HEADER;
280 goto err;
281 }
282
283 if (tag != V_ASN1_OBJECT)
284 {
285 i=ASN1_R_EXPECTING_AN_OBJECT;
286 goto err;
287 }
288 if ((ret->data == NULL) || (ret->length < len))
289 {
290 if (ret->data != NULL) Free((char *)ret->data);
291 ret->data=(unsigned char *)Malloc((int)len);
292 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
293 if (ret->data == NULL)
294 { i=ERR_R_MALLOC_FAILURE; goto err; }
295 }
296 memcpy(ret->data,p,(int)len);
297 ret->length=(int)len;
298 ret->sn=NULL;
299 ret->ln=NULL;
300 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
301 p+=len;
302
303 if (a != NULL) (*a)=ret;
304 *pp=p;
305 return(ret);
306 err:
307 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
308 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
309 ASN1_OBJECT_free(ret);
310 return(NULL);
311 }
312
313 ASN1_OBJECT *ASN1_OBJECT_new()
314 {
315 ASN1_OBJECT *ret;
316
317 ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT));
318 if (ret == NULL)
319 {
320 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
321 return(NULL);
322 }
323 ret->length=0;
324 ret->data=NULL;
325 ret->nid=0;
326 ret->sn=NULL;
327 ret->ln=NULL;
328 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
329 return(ret);
330 }
331
332 void ASN1_OBJECT_free(a)
333 ASN1_OBJECT *a;
334 {
335 if (a == NULL) return;
336 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
337 {
338 if (a->sn != NULL) Free(a->sn);
339 if (a->ln != NULL) Free(a->ln);
340 a->sn=a->ln=NULL;
341 }
342 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
343 {
344 if (a->data != NULL) Free(a->data);
345 a->data=NULL;
346 a->length=0;
347 }
348 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
349 Free((char *)a);
350 }
351
352 ASN1_OBJECT *ASN1_OBJECT_create(nid,data,len,sn,ln)
353 int nid;
354 unsigned char *data;
355 int len;
356 char *sn,*ln;
357 {
358 ASN1_OBJECT o;
359
360 o.sn=sn;
361 o.ln=ln;
362 o.data=data;
363 o.nid=nid;
364 o.length=len;
365 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|
366 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA;
367 return(OBJ_dup(&o));
368 }
369