a_object.c 9.3 KB

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