Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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