Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

659 строки
16 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/obj.h>
  57. #include <limits.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/bytestring.h>
  61. #include <openssl/err.h>
  62. #include <openssl/lhash.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/thread.h>
  65. #include "obj_dat.h"
  66. /* These globals are protected by CRYPTO_LOCK_OBJ. */
  67. static LHASH_OF(ASN1_OBJECT) *global_added_by_data = NULL;
  68. static LHASH_OF(ASN1_OBJECT) *global_added_by_nid = NULL;
  69. static LHASH_OF(ASN1_OBJECT) *global_added_by_short_name = NULL;
  70. static LHASH_OF(ASN1_OBJECT) *global_added_by_long_name = NULL;
  71. static unsigned global_next_nid = NUM_NID;
  72. static int obj_next_nid() {
  73. int ret;
  74. CRYPTO_w_lock(CRYPTO_LOCK_OBJ);
  75. ret = global_next_nid++;
  76. CRYPTO_w_unlock(CRYPTO_LOCK_OBJ);
  77. return ret;
  78. }
  79. ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *o) {
  80. ASN1_OBJECT *r;
  81. unsigned char *data = NULL;
  82. char *sn = NULL, *ln = NULL;
  83. if (o == NULL) {
  84. return NULL;
  85. }
  86. if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  87. /* TODO(fork): this is a little dangerous. */
  88. return (ASN1_OBJECT *)o;
  89. }
  90. r = ASN1_OBJECT_new();
  91. if (r == NULL) {
  92. OPENSSL_PUT_ERROR(OBJ, OBJ_dup, ERR_R_ASN1_LIB);
  93. return NULL;
  94. }
  95. r->ln = r->sn = NULL;
  96. data = OPENSSL_malloc(o->length);
  97. if (data == NULL) {
  98. goto err;
  99. }
  100. if (o->data != NULL) {
  101. memcpy(data, o->data, o->length);
  102. }
  103. /* once data is attached to an object, it remains const */
  104. r->data = data;
  105. r->length = o->length;
  106. r->nid = o->nid;
  107. if (o->ln != NULL) {
  108. ln = OPENSSL_strdup(o->ln);
  109. if (ln == NULL) {
  110. goto err;
  111. }
  112. }
  113. if (o->sn != NULL) {
  114. sn = OPENSSL_strdup(o->sn);
  115. if (sn) {
  116. goto err;
  117. }
  118. }
  119. r->sn = sn;
  120. r->ln = ln;
  121. r->flags =
  122. o->flags | (ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  123. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  124. return r;
  125. err:
  126. OPENSSL_PUT_ERROR(OBJ, OBJ_dup, ERR_R_MALLOC_FAILURE);
  127. if (ln != NULL) {
  128. OPENSSL_free(ln);
  129. }
  130. if (sn != NULL) {
  131. OPENSSL_free(sn);
  132. }
  133. if (data != NULL) {
  134. OPENSSL_free(data);
  135. }
  136. OPENSSL_free(r);
  137. return NULL;
  138. }
  139. int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  140. int ret;
  141. ret = a->length - b->length;
  142. if (ret) {
  143. return ret;
  144. }
  145. return memcmp(a->data, b->data, a->length);
  146. }
  147. /* nids_cmp is called to search the kNIDsInOIDOrder array. The |key| argument
  148. * is an |ASN1_OBJECT|* that we're looking for and |element| is a pointer to an
  149. * unsigned int in the array. */
  150. static int obj_cmp(const void *key, const void *element) {
  151. int j;
  152. unsigned nid = *((unsigned*) element);
  153. const ASN1_OBJECT *a = key;
  154. const ASN1_OBJECT *b = &kObjects[nid];
  155. j = a->length - b->length;
  156. if (j) {
  157. return j;
  158. }
  159. return memcmp(a->data, b->data, a->length);
  160. }
  161. int OBJ_obj2nid(const ASN1_OBJECT *obj) {
  162. const unsigned int *nid_ptr;
  163. if (obj == NULL) {
  164. return NID_undef;
  165. }
  166. if (obj->nid != 0) {
  167. return obj->nid;
  168. }
  169. CRYPTO_r_lock(CRYPTO_LOCK_OBJ);
  170. if (global_added_by_data != NULL) {
  171. ASN1_OBJECT *match;
  172. match = lh_ASN1_OBJECT_retrieve(global_added_by_data, obj);
  173. if (match != NULL) {
  174. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  175. return match->nid;
  176. }
  177. }
  178. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  179. nid_ptr = bsearch(obj, kNIDsInOIDOrder, NUM_OBJ, sizeof(unsigned), obj_cmp);
  180. if (nid_ptr == NULL) {
  181. return NID_undef;
  182. }
  183. return kObjects[*nid_ptr].nid;
  184. }
  185. int OBJ_cbs2nid(const CBS *cbs) {
  186. ASN1_OBJECT obj;
  187. memset(&obj, 0, sizeof(obj));
  188. obj.data = CBS_data(cbs);
  189. obj.length = CBS_len(cbs);
  190. return OBJ_obj2nid(&obj);
  191. }
  192. /* short_name_cmp is called to search the kNIDsInShortNameOrder array. The
  193. * |key| argument is name that we're looking for and |element| is a pointer to
  194. * an unsigned int in the array. */
  195. static int short_name_cmp(const void *key, const void *element) {
  196. const char *name = (const char *) key;
  197. unsigned nid = *((unsigned*) element);
  198. return strcmp(name, kObjects[nid].sn);
  199. }
  200. int OBJ_sn2nid(const char *short_name) {
  201. const unsigned int *nid_ptr;
  202. CRYPTO_r_lock(CRYPTO_LOCK_OBJ);
  203. if (global_added_by_short_name != NULL) {
  204. ASN1_OBJECT *match, template;
  205. template.sn = short_name;
  206. match = lh_ASN1_OBJECT_retrieve(global_added_by_short_name, &template);
  207. if (match != NULL) {
  208. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  209. return match->nid;
  210. }
  211. }
  212. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  213. nid_ptr = bsearch(short_name, kNIDsInShortNameOrder, NUM_SN, sizeof(unsigned), short_name_cmp);
  214. if (nid_ptr == NULL) {
  215. return NID_undef;
  216. }
  217. return kObjects[*nid_ptr].nid;
  218. }
  219. /* long_name_cmp is called to search the kNIDsInLongNameOrder array. The
  220. * |key| argument is name that we're looking for and |element| is a pointer to
  221. * an unsigned int in the array. */
  222. static int long_name_cmp(const void *key, const void *element) {
  223. const char *name = (const char *) key;
  224. unsigned nid = *((unsigned*) element);
  225. return strcmp(name, kObjects[nid].ln);
  226. }
  227. int OBJ_ln2nid(const char *long_name) {
  228. const unsigned int *nid_ptr;
  229. CRYPTO_r_lock(CRYPTO_LOCK_OBJ);
  230. if (global_added_by_long_name != NULL) {
  231. ASN1_OBJECT *match, template;
  232. template.ln = long_name;
  233. match = lh_ASN1_OBJECT_retrieve(global_added_by_long_name, &template);
  234. if (match != NULL) {
  235. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  236. return match->nid;
  237. }
  238. }
  239. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  240. nid_ptr = bsearch(long_name, kNIDsInLongNameOrder, NUM_LN, sizeof(unsigned), long_name_cmp);
  241. if (nid_ptr == NULL) {
  242. return NID_undef;
  243. }
  244. return kObjects[*nid_ptr].nid;
  245. }
  246. int OBJ_txt2nid(const char *s) {
  247. ASN1_OBJECT *obj;
  248. int nid;
  249. obj = OBJ_txt2obj(s, 0 /* search names */);
  250. nid = OBJ_obj2nid(obj);
  251. ASN1_OBJECT_free(obj);
  252. return nid;
  253. }
  254. const ASN1_OBJECT *OBJ_nid2obj(int nid) {
  255. if (nid >= 0 && nid < NUM_NID) {
  256. if (nid != NID_undef && kObjects[nid].nid == NID_undef) {
  257. goto err;
  258. }
  259. return &kObjects[nid];
  260. }
  261. CRYPTO_r_lock(CRYPTO_LOCK_OBJ);
  262. if (global_added_by_nid != NULL) {
  263. ASN1_OBJECT *match, template;
  264. template.nid = nid;
  265. match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
  266. if (match != NULL) {
  267. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  268. return match;
  269. }
  270. }
  271. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  272. err:
  273. OPENSSL_PUT_ERROR(OBJ, OBJ_nid2obj, OBJ_R_UNKNOWN_NID);
  274. return NULL;
  275. }
  276. const char *OBJ_nid2sn(int nid) {
  277. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  278. if (obj == NULL) {
  279. return NULL;
  280. }
  281. return obj->sn;
  282. }
  283. const char *OBJ_nid2ln(int nid) {
  284. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  285. if (obj == NULL) {
  286. return NULL;
  287. }
  288. return obj->ln;
  289. }
  290. ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
  291. int nid = NID_undef;
  292. ASN1_OBJECT *op = NULL;
  293. unsigned char *buf;
  294. unsigned char *p;
  295. const unsigned char *bufp;
  296. int contents_len, total_len;
  297. if (!dont_search_names) {
  298. nid = OBJ_sn2nid(s);
  299. if (nid == NID_undef) {
  300. nid = OBJ_ln2nid(s);
  301. }
  302. if (nid != NID_undef) {
  303. return (ASN1_OBJECT*) OBJ_nid2obj(nid);
  304. }
  305. }
  306. /* Work out size of content octets */
  307. contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  308. if (contents_len <= 0) {
  309. return NULL;
  310. }
  311. /* Work out total size */
  312. total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT);
  313. buf = OPENSSL_malloc(total_len);
  314. if (buf == NULL) {
  315. OPENSSL_PUT_ERROR(OBJ, OBJ_txt2obj, ERR_R_MALLOC_FAILURE);
  316. return NULL;
  317. }
  318. p = buf;
  319. /* Write out tag+length */
  320. ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  321. /* Write out contents */
  322. a2d_ASN1_OBJECT(p, contents_len, s, -1);
  323. bufp = buf;
  324. op = d2i_ASN1_OBJECT(NULL, &bufp, total_len);
  325. OPENSSL_free(buf);
  326. return op;
  327. }
  328. int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, int dont_return_name) {
  329. int i, n = 0, len, nid, first, use_bn;
  330. BIGNUM *bl;
  331. unsigned long l;
  332. const unsigned char *p;
  333. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  334. if (out && out_len > 0) {
  335. out[0] = 0;
  336. }
  337. if (obj == NULL || obj->data == NULL) {
  338. return 0;
  339. }
  340. if (!dont_return_name && (nid = OBJ_obj2nid(obj)) != NID_undef) {
  341. const char *s;
  342. s = OBJ_nid2ln(nid);
  343. if (s == NULL) {
  344. s = OBJ_nid2sn(nid);
  345. }
  346. if (s) {
  347. if (out) {
  348. BUF_strlcpy(out, s, out_len);
  349. }
  350. return strlen(s);
  351. }
  352. }
  353. len = obj->length;
  354. p = obj->data;
  355. first = 1;
  356. bl = NULL;
  357. while (len > 0) {
  358. l = 0;
  359. use_bn = 0;
  360. for (;;) {
  361. unsigned char c = *p++;
  362. len--;
  363. if (len == 0 && (c & 0x80)) {
  364. goto err;
  365. }
  366. if (use_bn) {
  367. if (!BN_add_word(bl, c & 0x7f)) {
  368. goto err;
  369. }
  370. } else {
  371. l |= c & 0x7f;
  372. }
  373. if (!(c & 0x80)) {
  374. break;
  375. }
  376. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  377. if (!bl && !(bl = BN_new())) {
  378. goto err;
  379. }
  380. if (!BN_set_word(bl, l)) {
  381. goto err;
  382. }
  383. use_bn = 1;
  384. }
  385. if (use_bn) {
  386. if (!BN_lshift(bl, bl, 7)) {
  387. goto err;
  388. }
  389. } else {
  390. l <<= 7L;
  391. }
  392. }
  393. if (first) {
  394. first = 0;
  395. if (l >= 80) {
  396. i = 2;
  397. if (use_bn) {
  398. if (!BN_sub_word(bl, 80)) {
  399. goto err;
  400. }
  401. } else {
  402. l -= 80;
  403. }
  404. } else {
  405. i = (int)(l / 40);
  406. l -= (long)(i * 40);
  407. }
  408. if (out && out_len > 1) {
  409. *out++ = i + '0';
  410. *out = '0';
  411. out_len--;
  412. }
  413. n++;
  414. }
  415. if (use_bn) {
  416. char *bndec;
  417. bndec = BN_bn2dec(bl);
  418. if (!bndec) {
  419. goto err;
  420. }
  421. i = strlen(bndec);
  422. if (out) {
  423. if (out_len > 1) {
  424. *out++ = '.';
  425. *out = 0;
  426. out_len--;
  427. }
  428. BUF_strlcpy(out, bndec, out_len);
  429. if (i > out_len) {
  430. out += out_len;
  431. out_len = 0;
  432. } else {
  433. out += i;
  434. out_len -= i;
  435. }
  436. }
  437. n++;
  438. n += i;
  439. OPENSSL_free(bndec);
  440. } else {
  441. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  442. i = strlen(tbuf);
  443. if (out && out_len > 0) {
  444. BUF_strlcpy(out, tbuf, out_len);
  445. if (i > out_len) {
  446. out += out_len;
  447. out_len = 0;
  448. } else {
  449. out += i;
  450. out_len -= i;
  451. }
  452. }
  453. n += i;
  454. }
  455. }
  456. if (bl) {
  457. BN_free(bl);
  458. }
  459. return n;
  460. err:
  461. if (bl) {
  462. BN_free(bl);
  463. }
  464. return -1;
  465. }
  466. static uint32_t hash_nid(const ASN1_OBJECT *obj) {
  467. return obj->nid;
  468. }
  469. static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  470. return a->nid - b->nid;
  471. }
  472. static uint32_t hash_data(const ASN1_OBJECT *obj) {
  473. return OPENSSL_hash32(obj->data, obj->length);
  474. }
  475. static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  476. int i = a->length - b->length;
  477. if (i) {
  478. return i;
  479. }
  480. return memcmp(a->data, b->data, a->length);
  481. }
  482. static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
  483. return lh_strhash(obj->sn);
  484. }
  485. static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  486. return strcmp(a->sn, b->sn);
  487. }
  488. static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
  489. return lh_strhash(obj->ln);
  490. }
  491. static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  492. return strcmp(a->ln, b->ln);
  493. }
  494. /* obj_add_object inserts |obj| into the various global hashes for run-time
  495. * added objects. It returns one on success or zero otherwise. */
  496. static int obj_add_object(ASN1_OBJECT *obj) {
  497. int ok;
  498. ASN1_OBJECT *old_object;
  499. obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  500. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  501. CRYPTO_w_lock(CRYPTO_LOCK_OBJ);
  502. if (global_added_by_nid == NULL) {
  503. global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
  504. global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
  505. global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
  506. global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
  507. }
  508. /* We don't pay attention to |old_object| (which contains any previous object
  509. * that was evicted from the hashes) because we don't have a reference count
  510. * on ASN1_OBJECT values. Also, we should never have duplicates nids and so
  511. * should always have objects in |global_added_by_nid|. */
  512. ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
  513. if (obj->length != 0 && obj->data != NULL) {
  514. ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
  515. }
  516. if (obj->sn != NULL) {
  517. ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
  518. }
  519. if (obj->ln != NULL) {
  520. ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
  521. }
  522. CRYPTO_w_unlock(CRYPTO_LOCK_OBJ);
  523. return ok;
  524. }
  525. int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
  526. int ret = NID_undef;
  527. ASN1_OBJECT *op = NULL;
  528. unsigned char *buf = NULL;
  529. int len;
  530. len = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
  531. if (len <= 0) {
  532. goto err;
  533. }
  534. buf = OPENSSL_malloc(len);
  535. if (buf == NULL) {
  536. OPENSSL_PUT_ERROR(OBJ, OBJ_create, ERR_R_MALLOC_FAILURE);
  537. goto err;
  538. }
  539. len = a2d_ASN1_OBJECT(buf, len, oid, -1);
  540. if (len == 0) {
  541. goto err;
  542. }
  543. op = (ASN1_OBJECT *)ASN1_OBJECT_create(obj_next_nid(), buf, len, short_name,
  544. long_name);
  545. if (op == NULL) {
  546. goto err;
  547. }
  548. if (obj_add_object(op)) {
  549. ret = op->nid;
  550. }
  551. op = NULL;
  552. err:
  553. if (op != NULL) {
  554. ASN1_OBJECT_free(op);
  555. }
  556. if (buf != NULL) {
  557. OPENSSL_free(buf);
  558. }
  559. return ret;
  560. }