1 /* crypto/asn1/a_object.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
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);
70 int i2d_ASN1_OBJECT(a, pp)
76 if ((a == NULL) || (a->data == NULL)) return(0);
79 return(ASN1_object_size(0,a->length,V_ASN1_OBJECT));
82 ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
83 memcpy(p,a->data,a->length);
90 int a2d_ASN1_OBJECT(out,olen,buf,num)
108 if ((c >= '0') && (c <= '2'))
114 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);
120 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);
128 if ((c != '.') && (c != ' '))
130 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);
139 if ((c == ' ') || (c == '.'))
141 if ((c < '0') || (c > '9'))
143 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
146 l=l*10L+(long)(c-'0');
150 if ((first < 2) && (l >= 40))
152 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
160 tmp[i++]=(unsigned char)l&0x7f;
168 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);
172 out[len++]=tmp[i]|0x80;
183 int i2t_ASN1_OBJECT(buf,buf_len,a)
188 int i,idx=0,n=0,len,nid;
194 if (buf_len <= 0) return(0);
196 if ((a == NULL) || (a->data == NULL))
203 if (nid == NID_undef)
210 while (idx < a->length)
213 if (!(p[idx] & 0x80)) break;
222 sprintf(tbuf,"%d.%ld",i,l);
224 strncpy(buf,tbuf,buf_len);
230 for (; idx<len; idx++)
233 if (!(p[idx] & 0x80))
235 sprintf(tbuf,".%ld",l);
238 strncpy(buf,tbuf,buf_len);
249 s=(char *)OBJ_nid2ln(nid);
251 s=(char *)OBJ_nid2sn(nid);
252 strncpy(buf,s,buf_len);
259 int i2a_ASN1_OBJECT(bp,a)
266 if ((a == NULL) || (a->data == NULL))
267 return(BIO_write(bp,"NULL",4));
268 i=i2t_ASN1_OBJECT(buf,80,a);
274 ASN1_OBJECT *d2i_ASN1_OBJECT(a, pp, length)
279 ASN1_OBJECT *ret=NULL;
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))
290 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
296 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
299 i=ASN1_R_BAD_OBJECT_HEADER;
303 if (tag != V_ASN1_OBJECT)
305 i=ASN1_R_EXPECTING_AN_OBJECT;
308 if ((ret->data == NULL) || (ret->length < len))
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; }
316 memcpy(ret->data,p,(int)len);
317 ret->length=(int)len;
320 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
323 if (a != NULL) (*a)=ret;
327 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
328 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
329 ASN1_OBJECT_free(ret);
333 ASN1_OBJECT *ASN1_OBJECT_new()
337 ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT));
340 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
348 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
352 void ASN1_OBJECT_free(a)
355 if (a == NULL) return;
356 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
358 if (a->sn != NULL) Free(a->sn);
359 if (a->ln != NULL) Free(a->ln);
362 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
364 if (a->data != NULL) Free(a->data);
368 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
372 ASN1_OBJECT *ASN1_OBJECT_create(nid,data,len,sn,ln)
385 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
386 ASN1_OBJECT_FLAG_DYNAMIC_DATA;