1 /* crypto/asn1/a_object.c */
2 /* Copyright (C) 1995-1997 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_I2A_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 i2a_ASN1_OBJECT(bp,a)
187 int j,i,idx=0,n=0,len,nid,reason=ERR_R_BUF_LIB;
193 if ((a == NULL) || (a->data == NULL))
195 return(BIO_write(bp,"NULL",4));
199 if (nid == NID_undef)
206 while (idx < a->length)
209 if (!(p[idx] & 0x80)) break;
218 sprintf(buf,"%d.%ld",i,l);
220 if (BIO_write(bp,buf,i) != i)
225 for (; idx<len; idx++)
228 if (!(p[idx] & 0x80))
230 sprintf(buf,".%ld",l);
232 if (BIO_write(bp,buf,i) != i) goto err;
241 s=(char *)OBJ_nid2ln(nid);
243 s=(char *)OBJ_nid2sn(nid);
245 if (BIO_write(bp,s,j) != j) goto err;
250 ASN1err(ASN1_F_I2A_ASN1_OBJECT,reason);
254 ASN1_OBJECT *d2i_ASN1_OBJECT(a, pp, length)
259 ASN1_OBJECT *ret=NULL;
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))
270 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
276 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
279 i=ASN1_R_BAD_OBJECT_HEADER;
283 if (tag != V_ASN1_OBJECT)
285 i=ASN1_R_EXPECTING_AN_OBJECT;
288 if ((ret->data == NULL) || (ret->length < len))
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; }
296 memcpy(ret->data,p,(int)len);
297 ret->length=(int)len;
300 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
303 if (a != NULL) (*a)=ret;
307 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
308 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
309 ASN1_OBJECT_free(ret);
313 ASN1_OBJECT *ASN1_OBJECT_new()
317 ret=(ASN1_OBJECT *)Malloc(sizeof(ASN1_OBJECT));
320 ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
328 ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
332 void ASN1_OBJECT_free(a)
335 if (a == NULL) return;
336 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
338 if (a->sn != NULL) Free(a->sn);
339 if (a->ln != NULL) Free(a->ln);
342 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
344 if (a->data != NULL) Free(a->data);
348 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
352 ASN1_OBJECT *ASN1_OBJECT_create(nid,data,len,sn,ln)
365 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|
366 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA;