a_object.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* crypto/asn1/a_object.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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, *allocated = NULL;
  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. if (*pp == NULL) {
  75. if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
  76. ASN1err(ASN1_F_I2D_ASN1_OBJECT, ERR_R_MALLOC_FAILURE);
  77. return 0;
  78. }
  79. } else {
  80. p = *pp;
  81. }
  82. ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  83. memcpy(p, a->data, a->length);
  84. /*
  85. * If a new buffer was allocated, just return it back.
  86. * If not, return the incremented buffer pointer.
  87. */
  88. *pp = allocated != NULL ? allocated : p + a->length;
  89. return objsize;
  90. }
  91. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
  92. {
  93. int i, first, len = 0, c, use_bn;
  94. char ftmp[24], *tmp = ftmp;
  95. int tmpsize = sizeof(ftmp);
  96. const char *p;
  97. unsigned long l;
  98. BIGNUM *bl = NULL;
  99. if (num == 0)
  100. return (0);
  101. else if (num == -1)
  102. num = strlen(buf);
  103. p = buf;
  104. c = *(p++);
  105. num--;
  106. if ((c >= '0') && (c <= '2')) {
  107. first = c - '0';
  108. } else {
  109. ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
  110. goto err;
  111. }
  112. if (num <= 0) {
  113. ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
  114. goto err;
  115. }
  116. c = *(p++);
  117. num--;
  118. for (;;) {
  119. if (num <= 0)
  120. break;
  121. if ((c != '.') && (c != ' ')) {
  122. ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
  123. goto err;
  124. }
  125. l = 0;
  126. use_bn = 0;
  127. for (;;) {
  128. if (num <= 0)
  129. break;
  130. num--;
  131. c = *(p++);
  132. if ((c == ' ') || (c == '.'))
  133. break;
  134. if ((c < '0') || (c > '9')) {
  135. ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
  136. goto err;
  137. }
  138. if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
  139. use_bn = 1;
  140. if (!bl)
  141. bl = BN_new();
  142. if (!bl || !BN_set_word(bl, l))
  143. goto err;
  144. }
  145. if (use_bn) {
  146. if (!BN_mul_word(bl, 10L)
  147. || !BN_add_word(bl, c - '0'))
  148. goto err;
  149. } else
  150. l = l * 10L + (long)(c - '0');
  151. }
  152. if (len == 0) {
  153. if ((first < 2) && (l >= 40)) {
  154. ASN1err(ASN1_F_A2D_ASN1_OBJECT,
  155. ASN1_R_SECOND_NUMBER_TOO_LARGE);
  156. goto err;
  157. }
  158. if (use_bn) {
  159. if (!BN_add_word(bl, first * 40))
  160. goto err;
  161. } else
  162. l += (long)first *40;
  163. }
  164. i = 0;
  165. if (use_bn) {
  166. int blsize;
  167. blsize = BN_num_bits(bl);
  168. blsize = (blsize + 6) / 7;
  169. if (blsize > tmpsize) {
  170. if (tmp != ftmp)
  171. OPENSSL_free(tmp);
  172. tmpsize = blsize + 32;
  173. tmp = OPENSSL_malloc(tmpsize);
  174. if (!tmp)
  175. goto err;
  176. }
  177. while (blsize--) {
  178. BN_ULONG t = BN_div_word(bl, 0x80L);
  179. if (t == (BN_ULONG)-1)
  180. goto err;
  181. tmp[i++] = (unsigned char)t;
  182. }
  183. } else {
  184. for (;;) {
  185. tmp[i++] = (unsigned char)l & 0x7f;
  186. l >>= 7L;
  187. if (l == 0L)
  188. break;
  189. }
  190. }
  191. if (out != NULL) {
  192. if (len + i > olen) {
  193. ASN1err(ASN1_F_A2D_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
  194. goto err;
  195. }
  196. while (--i > 0)
  197. out[len++] = tmp[i] | 0x80;
  198. out[len++] = tmp[0];
  199. } else
  200. len += i;
  201. }
  202. if (tmp != ftmp)
  203. OPENSSL_free(tmp);
  204. if (bl)
  205. BN_free(bl);
  206. return (len);
  207. err:
  208. if (tmp != ftmp)
  209. OPENSSL_free(tmp);
  210. if (bl)
  211. BN_free(bl);
  212. return (0);
  213. }
  214. int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
  215. {
  216. return OBJ_obj2txt(buf, buf_len, a, 0);
  217. }
  218. int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
  219. {
  220. char buf[80], *p = buf;
  221. int i;
  222. if ((a == NULL) || (a->data == NULL))
  223. return (BIO_write(bp, "NULL", 4));
  224. i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
  225. if (i > (int)(sizeof(buf) - 1)) {
  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. i = ASN1_R_BAD_OBJECT_HEADER;
  250. goto err;
  251. }
  252. if (tag != V_ASN1_OBJECT) {
  253. i = ASN1_R_EXPECTING_AN_OBJECT;
  254. goto err;
  255. }
  256. ret = c2i_ASN1_OBJECT(a, &p, len);
  257. if (ret)
  258. *pp = p;
  259. return ret;
  260. err:
  261. ASN1err(ASN1_F_D2I_ASN1_OBJECT, i);
  262. return (NULL);
  263. }
  264. ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  265. long len)
  266. {
  267. ASN1_OBJECT *ret = NULL;
  268. const unsigned char *p;
  269. unsigned char *data;
  270. int i, length;
  271. /*
  272. * Sanity check OID encoding. Need at least one content octet. MSB must
  273. * be clear in the last octet. can't have leading 0x80 in subidentifiers,
  274. * see: X.690 8.19.2
  275. */
  276. if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  277. p[len - 1] & 0x80) {
  278. ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  279. return NULL;
  280. }
  281. /* Now 0 < len <= INT_MAX, so the cast is safe. */
  282. length = (int)len;
  283. for (i = 0; i < length; i++, p++) {
  284. if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
  285. ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  286. return NULL;
  287. }
  288. }
  289. /*
  290. * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
  291. * ->ln
  292. */
  293. if ((a == NULL) || ((*a) == NULL) ||
  294. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  295. if ((ret = ASN1_OBJECT_new()) == NULL)
  296. return (NULL);
  297. } else
  298. ret = (*a);
  299. p = *pp;
  300. /* detach data from object */
  301. data = (unsigned char *)ret->data;
  302. ret->data = NULL;
  303. /* once detached we can change it */
  304. if ((data == NULL) || (ret->length < length)) {
  305. ret->length = 0;
  306. if (data != NULL)
  307. OPENSSL_free(data);
  308. data = (unsigned char *)OPENSSL_malloc(length);
  309. if (data == NULL) {
  310. i = ERR_R_MALLOC_FAILURE;
  311. goto err;
  312. }
  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)
  324. (*a) = ret;
  325. *pp = p;
  326. return (ret);
  327. err:
  328. ASN1err(ASN1_F_C2I_ASN1_OBJECT, i);
  329. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  330. ASN1_OBJECT_free(ret);
  331. return (NULL);
  332. }
  333. ASN1_OBJECT *ASN1_OBJECT_new(void)
  334. {
  335. ASN1_OBJECT *ret;
  336. ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
  337. if (ret == NULL) {
  338. ASN1err(ASN1_F_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)
  352. return;
  353. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
  354. #ifndef CONST_STRICT /* disable purely for compile-time strict
  355. * const checking. Doing this on a "real"
  356. * compile will cause memory leaks */
  357. if (a->sn != NULL)
  358. OPENSSL_free((void *)a->sn);
  359. if (a->ln != NULL)
  360. OPENSSL_free((void *)a->ln);
  361. #endif
  362. a->sn = a->ln = NULL;
  363. }
  364. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
  365. if (a->data != NULL)
  366. OPENSSL_free((void *)a->data);
  367. a->data = NULL;
  368. a->length = 0;
  369. }
  370. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  371. OPENSSL_free(a);
  372. }
  373. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  374. const char *sn, const char *ln)
  375. {
  376. ASN1_OBJECT o;
  377. o.sn = sn;
  378. o.ln = ln;
  379. o.data = data;
  380. o.nid = nid;
  381. o.length = len;
  382. o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  383. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  384. return (OBJ_dup(&o));
  385. }
  386. IMPLEMENT_STACK_OF(ASN1_OBJECT)
  387. IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT)