a_object.c 12 KB

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