a_object.c 12 KB

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