You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

a_object.c 10 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <limits.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
  62. {
  63. unsigned char *p;
  64. int objsize;
  65. if ((a == NULL) || (a->data == NULL)) return(0);
  66. objsize = ASN1_object_size(0,a->length,V_ASN1_OBJECT);
  67. if (pp == NULL) return objsize;
  68. p= *pp;
  69. ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
  70. memcpy(p,a->data,a->length);
  71. p+=a->length;
  72. *pp=p;
  73. return(objsize);
  74. }
  75. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
  76. {
  77. int i,first,len=0,c, use_bn;
  78. char ftmp[24], *tmp = ftmp;
  79. int tmpsize = sizeof ftmp;
  80. const char *p;
  81. unsigned long l;
  82. BIGNUM *bl = NULL;
  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';
  93. }
  94. else
  95. {
  96. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
  97. goto err;
  98. }
  99. if (num <= 0)
  100. {
  101. OPENSSL_PUT_ERROR(ASN1, 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. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
  112. goto err;
  113. }
  114. l=0;
  115. use_bn = 0;
  116. for (;;)
  117. {
  118. if (num <= 0) break;
  119. num--;
  120. c= *(p++);
  121. if ((c == ' ') || (c == '.'))
  122. break;
  123. if ((c < '0') || (c > '9'))
  124. {
  125. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
  126. goto err;
  127. }
  128. if (!use_bn && l >= ((ULONG_MAX - 80) / 10L))
  129. {
  130. use_bn = 1;
  131. if (!bl)
  132. bl = BN_new();
  133. if (!bl || !BN_set_word(bl, l))
  134. goto err;
  135. }
  136. if (use_bn)
  137. {
  138. if (!BN_mul_word(bl, 10L)
  139. || !BN_add_word(bl, c-'0'))
  140. goto err;
  141. }
  142. else
  143. l=l*10L+(long)(c-'0');
  144. }
  145. if (len == 0)
  146. {
  147. if ((first < 2) && (l >= 40))
  148. {
  149. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_SECOND_NUMBER_TOO_LARGE);
  150. goto err;
  151. }
  152. if (use_bn)
  153. {
  154. if (!BN_add_word(bl, first * 40))
  155. goto err;
  156. }
  157. else
  158. l+=(long)first*40;
  159. }
  160. i=0;
  161. if (use_bn)
  162. {
  163. int blsize;
  164. blsize = BN_num_bits(bl);
  165. blsize = (blsize + 6)/7;
  166. if (blsize > tmpsize)
  167. {
  168. if (tmp != ftmp)
  169. OPENSSL_free(tmp);
  170. tmpsize = blsize + 32;
  171. tmp = OPENSSL_malloc(tmpsize);
  172. if (!tmp)
  173. goto err;
  174. }
  175. while(blsize--)
  176. tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
  177. }
  178. else
  179. {
  180. for (;;)
  181. {
  182. tmp[i++]=(unsigned char)l&0x7f;
  183. l>>=7L;
  184. if (l == 0L) break;
  185. }
  186. }
  187. if (out != NULL)
  188. {
  189. if (len+i > olen)
  190. {
  191. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
  192. goto err;
  193. }
  194. while (--i > 0)
  195. out[len++]=tmp[i]|0x80;
  196. out[len++]=tmp[0];
  197. }
  198. else
  199. len+=i;
  200. }
  201. if (tmp != ftmp)
  202. OPENSSL_free(tmp);
  203. if (bl)
  204. BN_free(bl);
  205. return(len);
  206. err:
  207. if (tmp != ftmp)
  208. OPENSSL_free(tmp);
  209. if (bl)
  210. BN_free(bl);
  211. return(0);
  212. }
  213. int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
  214. {
  215. return OBJ_obj2txt(buf, buf_len, a, 0);
  216. }
  217. int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
  218. {
  219. char buf[80], *p = buf;
  220. int i;
  221. if ((a == NULL) || (a->data == NULL))
  222. return(BIO_write(bp,"NULL",4));
  223. i=i2t_ASN1_OBJECT(buf,sizeof buf,a);
  224. if (i > (int)(sizeof(buf) - 1))
  225. {
  226. p = OPENSSL_malloc(i + 1);
  227. if (!p)
  228. return -1;
  229. i2t_ASN1_OBJECT(p,i + 1,a);
  230. }
  231. if (i <= 0)
  232. return BIO_write(bp, "<INVALID>", 9);
  233. BIO_write(bp,p,i);
  234. if (p != buf)
  235. OPENSSL_free(p);
  236. return(i);
  237. }
  238. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  239. long length)
  240. {
  241. const unsigned char *p;
  242. long len;
  243. int tag,xclass;
  244. int inf,i;
  245. ASN1_OBJECT *ret = NULL;
  246. p= *pp;
  247. inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
  248. if (inf & 0x80)
  249. {
  250. i=ASN1_R_BAD_OBJECT_HEADER;
  251. goto err;
  252. }
  253. if (tag != V_ASN1_OBJECT)
  254. {
  255. i=ASN1_R_EXPECTING_AN_OBJECT;
  256. goto err;
  257. }
  258. ret = c2i_ASN1_OBJECT(a, &p, len);
  259. if(ret) *pp = p;
  260. return ret;
  261. err:
  262. OPENSSL_PUT_ERROR(ASN1, d2i_ASN1_OBJECT, i);
  263. return(NULL);
  264. }
  265. ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  266. long len)
  267. {
  268. ASN1_OBJECT *ret=NULL;
  269. const unsigned char *p;
  270. unsigned char *data;
  271. int i, length;
  272. /* Sanity check OID encoding.
  273. * Need at least one content octet.
  274. * MSB must be clear in the last octet.
  275. * can't have leading 0x80 in subidentifiers, see: X.690 8.19.2
  276. */
  277. if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  278. p[len - 1] & 0x80)
  279. {
  280. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  281. return NULL;
  282. }
  283. /* Now 0 < len <= INT_MAX, so the cast is safe. */
  284. length = (int)len;
  285. for (i = 0; i < length; i++, p++)
  286. {
  287. if (*p == 0x80 && (!i || !(p[-1] & 0x80)))
  288. {
  289. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  290. return NULL;
  291. }
  292. }
  293. /* only the ASN1_OBJECTs from the 'table' will have values
  294. * for ->sn or ->ln */
  295. if ((a == NULL) || ((*a) == NULL) ||
  296. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
  297. {
  298. if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
  299. }
  300. else ret=(*a);
  301. p= *pp;
  302. /* detach data from object */
  303. data = (unsigned char *)ret->data;
  304. ret->data = NULL;
  305. /* once detached we can change it */
  306. if ((data == NULL) || (ret->length < length))
  307. {
  308. ret->length=0;
  309. if (data != NULL) OPENSSL_free(data);
  310. data=(unsigned char *)OPENSSL_malloc(length);
  311. if (data == NULL)
  312. { i=ERR_R_MALLOC_FAILURE; goto err; }
  313. ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  314. }
  315. memcpy(data,p,length);
  316. /* reattach data to object, after which it remains const */
  317. ret->data =data;
  318. ret->length=length;
  319. ret->sn=NULL;
  320. ret->ln=NULL;
  321. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  322. p+=length;
  323. if (a != NULL) (*a)=ret;
  324. *pp=p;
  325. return(ret);
  326. err:
  327. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_OBJECT, i);
  328. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  329. ASN1_OBJECT_free(ret);
  330. return(NULL);
  331. }
  332. ASN1_OBJECT *ASN1_OBJECT_new(void)
  333. {
  334. ASN1_OBJECT *ret;
  335. ret=(ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
  336. if (ret == NULL)
  337. {
  338. OPENSSL_PUT_ERROR(ASN1, ASN1_OBJECT_new, ERR_R_MALLOC_FAILURE);
  339. return(NULL);
  340. }
  341. ret->length=0;
  342. ret->data=NULL;
  343. ret->nid=0;
  344. ret->sn=NULL;
  345. ret->ln=NULL;
  346. ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
  347. return(ret);
  348. }
  349. void ASN1_OBJECT_free(ASN1_OBJECT *a)
  350. {
  351. if (a == NULL) return;
  352. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
  353. {
  354. #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */
  355. if (a->sn != NULL) OPENSSL_free((void *)a->sn);
  356. if (a->ln != NULL) OPENSSL_free((void *)a->ln);
  357. #endif
  358. a->sn=a->ln=NULL;
  359. }
  360. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
  361. {
  362. if (a->data != NULL) OPENSSL_free((void *)a->data);
  363. a->data=NULL;
  364. a->length=0;
  365. }
  366. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  367. OPENSSL_free(a);
  368. }
  369. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  370. const char *sn, const char *ln)
  371. {
  372. ASN1_OBJECT o;
  373. o.sn=sn;
  374. o.ln=ln;
  375. o.data=data;
  376. o.nid=nid;
  377. o.length=len;
  378. o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
  379. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  380. return(OBJ_dup(&o));
  381. }