a_object.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/buffer.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/objects.h>
  63. int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
  64. {
  65. unsigned char *p;
  66. int objsize;
  67. if ((a == NULL) || (a->data == NULL)) return(0);
  68. objsize = ASN1_object_size(0,a->length,V_ASN1_OBJECT);
  69. if (pp == NULL) return objsize;
  70. p= *pp;
  71. ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
  72. memcpy(p,a->data,a->length);
  73. p+=a->length;
  74. *pp=p;
  75. return(objsize);
  76. }
  77. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
  78. {
  79. int i,first,len=0,c;
  80. char tmp[24];
  81. const char *p;
  82. unsigned long l;
  83. if (num == 0)
  84. return(0);
  85. else if (num == -1)
  86. num=strlen(buf);
  87. p=buf;
  88. c= *(p++);
  89. num--;
  90. if ((c >= '0') && (c <= '2'))
  91. {
  92. first=(c-'0')*40;
  93. }
  94. else
  95. {
  96. ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);
  97. goto err;
  98. }
  99. if (num <= 0)
  100. {
  101. ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);
  102. goto err;
  103. }
  104. c= *(p++);
  105. num--;
  106. for (;;)
  107. {
  108. if (num <= 0) break;
  109. if ((c != '.') && (c != ' '))
  110. {
  111. ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);
  112. goto err;
  113. }
  114. l=0;
  115. for (;;)
  116. {
  117. if (num <= 0) break;
  118. num--;
  119. c= *(p++);
  120. if ((c == ' ') || (c == '.'))
  121. break;
  122. if ((c < '0') || (c > '9'))
  123. {
  124. ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
  125. goto err;
  126. }
  127. l=l*10L+(long)(c-'0');
  128. }
  129. if (len == 0)
  130. {
  131. if ((first < 2) && (l >= 40))
  132. {
  133. ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
  134. goto err;
  135. }
  136. l+=(long)first;
  137. }
  138. i=0;
  139. for (;;)
  140. {
  141. tmp[i++]=(unsigned char)l&0x7f;
  142. l>>=7L;
  143. if (l == 0L) break;
  144. }
  145. if (out != NULL)
  146. {
  147. if (len+i > olen)
  148. {
  149. ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);
  150. goto err;
  151. }
  152. while (--i > 0)
  153. out[len++]=tmp[i]|0x80;
  154. out[len++]=tmp[0];
  155. }
  156. else
  157. len+=i;
  158. }
  159. return(len);
  160. err:
  161. return(0);
  162. }
  163. int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
  164. {
  165. return OBJ_obj2txt(buf, buf_len, a, 0);
  166. }
  167. int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
  168. {
  169. char buf[80];
  170. int i;
  171. if ((a == NULL) || (a->data == NULL))
  172. return(BIO_write(bp,"NULL",4));
  173. i=i2t_ASN1_OBJECT(buf,80,a);
  174. if (i > 80) i=80;
  175. BIO_write(bp,buf,i);
  176. return(i);
  177. }
  178. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, unsigned char **pp,
  179. long length)
  180. {
  181. unsigned char *p;
  182. long len;
  183. int tag,xclass;
  184. int inf,i;
  185. ASN1_OBJECT *ret = NULL;
  186. p= *pp;
  187. inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
  188. if (inf & 0x80)
  189. {
  190. i=ASN1_R_BAD_OBJECT_HEADER;
  191. goto err;
  192. }
  193. if (tag != V_ASN1_OBJECT)
  194. {
  195. i=ASN1_R_EXPECTING_AN_OBJECT;
  196. goto err;
  197. }
  198. ret = c2i_ASN1_OBJECT(a, &p, len);
  199. if(ret) *pp = p;
  200. return ret;
  201. err:
  202. ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
  203. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  204. ASN1_OBJECT_free(ret);
  205. return(NULL);
  206. }
  207. ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, unsigned char **pp,
  208. long len)
  209. {
  210. ASN1_OBJECT *ret=NULL;
  211. unsigned char *p;
  212. int i;
  213. /* only the ASN1_OBJECTs from the 'table' will have values
  214. * for ->sn or ->ln */
  215. if ((a == NULL) || ((*a) == NULL) ||
  216. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
  217. {
  218. if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
  219. }
  220. else ret=(*a);
  221. p= *pp;
  222. if ((ret->data == NULL) || (ret->length < len))
  223. {
  224. if (ret->data != NULL) OPENSSL_free(ret->data);
  225. ret->data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1);
  226. ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  227. if (ret->data == NULL)
  228. { i=ERR_R_MALLOC_FAILURE; goto err; }
  229. }
  230. memcpy(ret->data,p,(int)len);
  231. ret->length=(int)len;
  232. ret->sn=NULL;
  233. ret->ln=NULL;
  234. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  235. p+=len;
  236. if (a != NULL) (*a)=ret;
  237. *pp=p;
  238. return(ret);
  239. err:
  240. ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
  241. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  242. ASN1_OBJECT_free(ret);
  243. return(NULL);
  244. }
  245. ASN1_OBJECT *ASN1_OBJECT_new(void)
  246. {
  247. ASN1_OBJECT *ret;
  248. ret=(ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
  249. if (ret == NULL)
  250. {
  251. ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE);
  252. return(NULL);
  253. }
  254. ret->length=0;
  255. ret->data=NULL;
  256. ret->nid=0;
  257. ret->sn=NULL;
  258. ret->ln=NULL;
  259. ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
  260. return(ret);
  261. }
  262. void ASN1_OBJECT_free(ASN1_OBJECT *a)
  263. {
  264. if (a == NULL) return;
  265. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
  266. {
  267. #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */
  268. if (a->sn != NULL) OPENSSL_free((void *)a->sn);
  269. if (a->ln != NULL) OPENSSL_free((void *)a->ln);
  270. #endif
  271. a->sn=a->ln=NULL;
  272. }
  273. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
  274. {
  275. if (a->data != NULL) OPENSSL_free(a->data);
  276. a->data=NULL;
  277. a->length=0;
  278. }
  279. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  280. OPENSSL_free(a);
  281. }
  282. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  283. const char *sn, const char *ln)
  284. {
  285. ASN1_OBJECT o;
  286. o.sn=sn;
  287. o.ln=ln;
  288. o.data=data;
  289. o.nid=nid;
  290. o.length=len;
  291. o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
  292. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  293. return(OBJ_dup(&o));
  294. }
  295. IMPLEMENT_STACK_OF(ASN1_OBJECT)
  296. IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT)