25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

397 lines
12 KiB

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