a_object.c 12 KB

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