Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

673 рядки
17 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(void) {
  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. OPENSSL_EXPORT int OBJ_nid2cbb(CBB *out, int nid) {
  255. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  256. CBB oid;
  257. if (obj == NULL ||
  258. !CBB_add_asn1(out, &oid, CBS_ASN1_OBJECT) ||
  259. !CBB_add_bytes(&oid, obj->data, obj->length) ||
  260. !CBB_flush(out)) {
  261. return 0;
  262. }
  263. return 1;
  264. }
  265. const ASN1_OBJECT *OBJ_nid2obj(int nid) {
  266. if (nid >= 0 && nid < NUM_NID) {
  267. if (nid != NID_undef && kObjects[nid].nid == NID_undef) {
  268. goto err;
  269. }
  270. return &kObjects[nid];
  271. }
  272. CRYPTO_r_lock(CRYPTO_LOCK_OBJ);
  273. if (global_added_by_nid != NULL) {
  274. ASN1_OBJECT *match, template;
  275. template.nid = nid;
  276. match = lh_ASN1_OBJECT_retrieve(global_added_by_nid, &template);
  277. if (match != NULL) {
  278. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  279. return match;
  280. }
  281. }
  282. CRYPTO_r_unlock(CRYPTO_LOCK_OBJ);
  283. err:
  284. OPENSSL_PUT_ERROR(OBJ, OBJ_nid2obj, OBJ_R_UNKNOWN_NID);
  285. return NULL;
  286. }
  287. const char *OBJ_nid2sn(int nid) {
  288. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  289. if (obj == NULL) {
  290. return NULL;
  291. }
  292. return obj->sn;
  293. }
  294. const char *OBJ_nid2ln(int nid) {
  295. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  296. if (obj == NULL) {
  297. return NULL;
  298. }
  299. return obj->ln;
  300. }
  301. ASN1_OBJECT *OBJ_txt2obj(const char *s, int dont_search_names) {
  302. int nid = NID_undef;
  303. ASN1_OBJECT *op = NULL;
  304. unsigned char *buf;
  305. unsigned char *p;
  306. const unsigned char *bufp;
  307. int contents_len, total_len;
  308. if (!dont_search_names) {
  309. nid = OBJ_sn2nid(s);
  310. if (nid == NID_undef) {
  311. nid = OBJ_ln2nid(s);
  312. }
  313. if (nid != NID_undef) {
  314. return (ASN1_OBJECT*) OBJ_nid2obj(nid);
  315. }
  316. }
  317. /* Work out size of content octets */
  318. contents_len = a2d_ASN1_OBJECT(NULL, 0, s, -1);
  319. if (contents_len <= 0) {
  320. return NULL;
  321. }
  322. /* Work out total size */
  323. total_len = ASN1_object_size(0, contents_len, V_ASN1_OBJECT);
  324. buf = OPENSSL_malloc(total_len);
  325. if (buf == NULL) {
  326. OPENSSL_PUT_ERROR(OBJ, OBJ_txt2obj, ERR_R_MALLOC_FAILURE);
  327. return NULL;
  328. }
  329. p = buf;
  330. /* Write out tag+length */
  331. ASN1_put_object(&p, 0, contents_len, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  332. /* Write out contents */
  333. a2d_ASN1_OBJECT(p, contents_len, s, -1);
  334. bufp = buf;
  335. op = d2i_ASN1_OBJECT(NULL, &bufp, total_len);
  336. OPENSSL_free(buf);
  337. return op;
  338. }
  339. int OBJ_obj2txt(char *out, int out_len, const ASN1_OBJECT *obj, int dont_return_name) {
  340. int i, n = 0, len, nid, first, use_bn;
  341. BIGNUM *bl;
  342. unsigned long l;
  343. const unsigned char *p;
  344. char tbuf[DECIMAL_SIZE(i) + DECIMAL_SIZE(l) + 2];
  345. if (out && out_len > 0) {
  346. out[0] = 0;
  347. }
  348. if (obj == NULL || obj->data == NULL) {
  349. return 0;
  350. }
  351. if (!dont_return_name && (nid = OBJ_obj2nid(obj)) != NID_undef) {
  352. const char *s;
  353. s = OBJ_nid2ln(nid);
  354. if (s == NULL) {
  355. s = OBJ_nid2sn(nid);
  356. }
  357. if (s) {
  358. if (out) {
  359. BUF_strlcpy(out, s, out_len);
  360. }
  361. return strlen(s);
  362. }
  363. }
  364. len = obj->length;
  365. p = obj->data;
  366. first = 1;
  367. bl = NULL;
  368. while (len > 0) {
  369. l = 0;
  370. use_bn = 0;
  371. for (;;) {
  372. unsigned char c = *p++;
  373. len--;
  374. if (len == 0 && (c & 0x80)) {
  375. goto err;
  376. }
  377. if (use_bn) {
  378. if (!BN_add_word(bl, c & 0x7f)) {
  379. goto err;
  380. }
  381. } else {
  382. l |= c & 0x7f;
  383. }
  384. if (!(c & 0x80)) {
  385. break;
  386. }
  387. if (!use_bn && (l > (ULONG_MAX >> 7L))) {
  388. if (!bl && !(bl = BN_new())) {
  389. goto err;
  390. }
  391. if (!BN_set_word(bl, l)) {
  392. goto err;
  393. }
  394. use_bn = 1;
  395. }
  396. if (use_bn) {
  397. if (!BN_lshift(bl, bl, 7)) {
  398. goto err;
  399. }
  400. } else {
  401. l <<= 7L;
  402. }
  403. }
  404. if (first) {
  405. first = 0;
  406. if (l >= 80) {
  407. i = 2;
  408. if (use_bn) {
  409. if (!BN_sub_word(bl, 80)) {
  410. goto err;
  411. }
  412. } else {
  413. l -= 80;
  414. }
  415. } else {
  416. i = (int)(l / 40);
  417. l -= (long)(i * 40);
  418. }
  419. if (out && out_len > 1) {
  420. *out++ = i + '0';
  421. *out = '0';
  422. out_len--;
  423. }
  424. n++;
  425. }
  426. if (use_bn) {
  427. char *bndec;
  428. bndec = BN_bn2dec(bl);
  429. if (!bndec) {
  430. goto err;
  431. }
  432. i = strlen(bndec);
  433. if (out) {
  434. if (out_len > 1) {
  435. *out++ = '.';
  436. *out = 0;
  437. out_len--;
  438. }
  439. BUF_strlcpy(out, bndec, out_len);
  440. if (i > out_len) {
  441. out += out_len;
  442. out_len = 0;
  443. } else {
  444. out += i;
  445. out_len -= i;
  446. }
  447. }
  448. n++;
  449. n += i;
  450. OPENSSL_free(bndec);
  451. } else {
  452. BIO_snprintf(tbuf, sizeof(tbuf), ".%lu", l);
  453. i = strlen(tbuf);
  454. if (out && out_len > 0) {
  455. BUF_strlcpy(out, tbuf, out_len);
  456. if (i > out_len) {
  457. out += out_len;
  458. out_len = 0;
  459. } else {
  460. out += i;
  461. out_len -= i;
  462. }
  463. }
  464. n += i;
  465. }
  466. }
  467. if (bl) {
  468. BN_free(bl);
  469. }
  470. return n;
  471. err:
  472. if (bl) {
  473. BN_free(bl);
  474. }
  475. return -1;
  476. }
  477. static uint32_t hash_nid(const ASN1_OBJECT *obj) {
  478. return obj->nid;
  479. }
  480. static int cmp_nid(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  481. return a->nid - b->nid;
  482. }
  483. static uint32_t hash_data(const ASN1_OBJECT *obj) {
  484. return OPENSSL_hash32(obj->data, obj->length);
  485. }
  486. static int cmp_data(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  487. int i = a->length - b->length;
  488. if (i) {
  489. return i;
  490. }
  491. return memcmp(a->data, b->data, a->length);
  492. }
  493. static uint32_t hash_short_name(const ASN1_OBJECT *obj) {
  494. return lh_strhash(obj->sn);
  495. }
  496. static int cmp_short_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  497. return strcmp(a->sn, b->sn);
  498. }
  499. static uint32_t hash_long_name(const ASN1_OBJECT *obj) {
  500. return lh_strhash(obj->ln);
  501. }
  502. static int cmp_long_name(const ASN1_OBJECT *a, const ASN1_OBJECT *b) {
  503. return strcmp(a->ln, b->ln);
  504. }
  505. /* obj_add_object inserts |obj| into the various global hashes for run-time
  506. * added objects. It returns one on success or zero otherwise. */
  507. static int obj_add_object(ASN1_OBJECT *obj) {
  508. int ok;
  509. ASN1_OBJECT *old_object;
  510. obj->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  511. ASN1_OBJECT_FLAG_DYNAMIC_DATA);
  512. CRYPTO_w_lock(CRYPTO_LOCK_OBJ);
  513. if (global_added_by_nid == NULL) {
  514. global_added_by_nid = lh_ASN1_OBJECT_new(hash_nid, cmp_nid);
  515. global_added_by_data = lh_ASN1_OBJECT_new(hash_data, cmp_data);
  516. global_added_by_short_name = lh_ASN1_OBJECT_new(hash_short_name, cmp_short_name);
  517. global_added_by_long_name = lh_ASN1_OBJECT_new(hash_long_name, cmp_long_name);
  518. }
  519. /* We don't pay attention to |old_object| (which contains any previous object
  520. * that was evicted from the hashes) because we don't have a reference count
  521. * on ASN1_OBJECT values. Also, we should never have duplicates nids and so
  522. * should always have objects in |global_added_by_nid|. */
  523. ok = lh_ASN1_OBJECT_insert(global_added_by_nid, &old_object, obj);
  524. if (obj->length != 0 && obj->data != NULL) {
  525. ok &= lh_ASN1_OBJECT_insert(global_added_by_data, &old_object, obj);
  526. }
  527. if (obj->sn != NULL) {
  528. ok &= lh_ASN1_OBJECT_insert(global_added_by_short_name, &old_object, obj);
  529. }
  530. if (obj->ln != NULL) {
  531. ok &= lh_ASN1_OBJECT_insert(global_added_by_long_name, &old_object, obj);
  532. }
  533. CRYPTO_w_unlock(CRYPTO_LOCK_OBJ);
  534. return ok;
  535. }
  536. int OBJ_create(const char *oid, const char *short_name, const char *long_name) {
  537. int ret = NID_undef;
  538. ASN1_OBJECT *op = NULL;
  539. unsigned char *buf = NULL;
  540. int len;
  541. len = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
  542. if (len <= 0) {
  543. goto err;
  544. }
  545. buf = OPENSSL_malloc(len);
  546. if (buf == NULL) {
  547. OPENSSL_PUT_ERROR(OBJ, OBJ_create, ERR_R_MALLOC_FAILURE);
  548. goto err;
  549. }
  550. len = a2d_ASN1_OBJECT(buf, len, oid, -1);
  551. if (len == 0) {
  552. goto err;
  553. }
  554. op = (ASN1_OBJECT *)ASN1_OBJECT_create(obj_next_nid(), buf, len, short_name,
  555. long_name);
  556. if (op == NULL) {
  557. goto err;
  558. }
  559. if (obj_add_object(op)) {
  560. ret = op->nid;
  561. }
  562. op = NULL;
  563. err:
  564. if (op != NULL) {
  565. ASN1_OBJECT_free(op);
  566. }
  567. if (buf != NULL) {
  568. OPENSSL_free(buf);
  569. }
  570. return ret;
  571. }