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

673 строки
17 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2004. */
  3. /* ====================================================================
  4. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * licensing@OpenSSL.org.
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This product includes cryptographic software written by Eric Young
  52. * (eay@cryptsoft.com). This product includes software written by Tim
  53. * Hudson (tjh@cryptsoft.com). */
  54. #include <string.h>
  55. #include <openssl/buf.h>
  56. #include <openssl/lhash.h>
  57. #include <openssl/mem.h>
  58. #include <openssl/obj.h>
  59. #include <openssl/stack.h>
  60. #include <openssl/x509.h>
  61. #include <openssl/x509v3.h>
  62. #include "vpm_int.h"
  63. /* X509_VERIFY_PARAM functions */
  64. #define SET_HOST 0
  65. #define ADD_HOST 1
  66. static char *str_copy(char *s) { return OPENSSL_strdup(s); }
  67. static void str_free(char *s) { OPENSSL_free(s); }
  68. #define string_stack_free(sk) sk_OPENSSL_STRING_pop_free(sk, str_free)
  69. static int int_x509_param_set_hosts(X509_VERIFY_PARAM_ID *id, int mode,
  70. const char *name, size_t namelen)
  71. {
  72. char *copy;
  73. /*
  74. * Refuse names with embedded NUL bytes.
  75. * XXX: Do we need to push an error onto the error stack?
  76. */
  77. if (name && memchr(name, '\0', namelen))
  78. return 0;
  79. if (mode == SET_HOST && id->hosts)
  80. {
  81. string_stack_free(id->hosts);
  82. id->hosts = NULL;
  83. }
  84. if (name == NULL || namelen == 0)
  85. return 1;
  86. copy = BUF_strndup(name, namelen);
  87. if (copy == NULL)
  88. return 0;
  89. if (id->hosts == NULL &&
  90. (id->hosts = sk_OPENSSL_STRING_new_null()) == NULL)
  91. {
  92. OPENSSL_free(copy);
  93. return 0;
  94. }
  95. if (!sk_OPENSSL_STRING_push(id->hosts, copy))
  96. {
  97. OPENSSL_free(copy);
  98. if (sk_OPENSSL_STRING_num(id->hosts) == 0)
  99. {
  100. sk_OPENSSL_STRING_free(id->hosts);
  101. id->hosts = NULL;
  102. }
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
  108. {
  109. X509_VERIFY_PARAM_ID *paramid;
  110. if (!param)
  111. return;
  112. param->name = NULL;
  113. param->purpose = 0;
  114. param->trust = 0;
  115. /*param->inh_flags = X509_VP_FLAG_DEFAULT;*/
  116. param->inh_flags = 0;
  117. param->flags = 0;
  118. param->depth = -1;
  119. if (param->policies)
  120. {
  121. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  122. param->policies = NULL;
  123. }
  124. paramid = param->id;
  125. if (paramid->hosts)
  126. {
  127. string_stack_free(paramid->hosts);
  128. paramid->hosts = NULL;
  129. }
  130. if (paramid->peername)
  131. {
  132. OPENSSL_free(paramid->peername);
  133. paramid->peername = NULL;
  134. }
  135. if (paramid->email)
  136. {
  137. OPENSSL_free(paramid->email);
  138. paramid->email = NULL;
  139. paramid->emaillen = 0;
  140. }
  141. if (paramid->ip)
  142. {
  143. OPENSSL_free(paramid->ip);
  144. paramid->ip = NULL;
  145. paramid->iplen = 0;
  146. }
  147. }
  148. X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
  149. {
  150. X509_VERIFY_PARAM *param;
  151. X509_VERIFY_PARAM_ID *paramid;
  152. param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
  153. if (!param)
  154. return NULL;
  155. paramid = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM_ID));
  156. if (!paramid)
  157. {
  158. OPENSSL_free(param);
  159. return NULL;
  160. }
  161. memset(param, 0, sizeof(X509_VERIFY_PARAM));
  162. memset(paramid, 0, sizeof(X509_VERIFY_PARAM_ID));
  163. param->id = paramid;
  164. x509_verify_param_zero(param);
  165. return param;
  166. }
  167. void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
  168. {
  169. if (param == NULL)
  170. return;
  171. x509_verify_param_zero(param);
  172. OPENSSL_free(param->id);
  173. OPENSSL_free(param);
  174. }
  175. /* This function determines how parameters are "inherited" from one structure
  176. * to another. There are several different ways this can happen.
  177. *
  178. * 1. If a child structure needs to have its values initialized from a parent
  179. * they are simply copied across. For example SSL_CTX copied to SSL.
  180. * 2. If the structure should take on values only if they are currently unset.
  181. * For example the values in an SSL structure will take appropriate value
  182. * for SSL servers or clients but only if the application has not set new
  183. * ones.
  184. *
  185. * The "inh_flags" field determines how this function behaves.
  186. *
  187. * Normally any values which are set in the default are not copied from the
  188. * destination and verify flags are ORed together.
  189. *
  190. * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
  191. * to the destination. Effectively the values in "to" become default values
  192. * which will be used only if nothing new is set in "from".
  193. *
  194. * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
  195. * they are set or not. Flags is still Ored though.
  196. *
  197. * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
  198. * of ORed.
  199. *
  200. * If X509_VP_FLAG_LOCKED is set then no values are copied.
  201. *
  202. * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
  203. * after the next call.
  204. */
  205. /* Macro to test if a field should be copied from src to dest */
  206. #define test_x509_verify_param_copy(field, def) \
  207. (to_overwrite || \
  208. ((src->field != def) && (to_default || (dest->field == def))))
  209. /* As above but for ID fields */
  210. #define test_x509_verify_param_copy_id(idf, def) \
  211. test_x509_verify_param_copy(id->idf, def)
  212. /* Macro to test and copy a field if necessary */
  213. #define x509_verify_param_copy(field, def) \
  214. if (test_x509_verify_param_copy(field, def)) \
  215. dest->field = src->field
  216. int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
  217. const X509_VERIFY_PARAM *src)
  218. {
  219. unsigned long inh_flags;
  220. int to_default, to_overwrite;
  221. X509_VERIFY_PARAM_ID *id;
  222. if (!src)
  223. return 1;
  224. id = src->id;
  225. inh_flags = dest->inh_flags | src->inh_flags;
  226. if (inh_flags & X509_VP_FLAG_ONCE)
  227. dest->inh_flags = 0;
  228. if (inh_flags & X509_VP_FLAG_LOCKED)
  229. return 1;
  230. if (inh_flags & X509_VP_FLAG_DEFAULT)
  231. to_default = 1;
  232. else
  233. to_default = 0;
  234. if (inh_flags & X509_VP_FLAG_OVERWRITE)
  235. to_overwrite = 1;
  236. else
  237. to_overwrite = 0;
  238. x509_verify_param_copy(purpose, 0);
  239. x509_verify_param_copy(trust, 0);
  240. x509_verify_param_copy(depth, -1);
  241. /* If overwrite or check time not set, copy across */
  242. if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME))
  243. {
  244. dest->check_time = src->check_time;
  245. dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
  246. /* Don't need to copy flag: that is done below */
  247. }
  248. if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
  249. dest->flags = 0;
  250. dest->flags |= src->flags;
  251. if (test_x509_verify_param_copy(policies, NULL))
  252. {
  253. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  254. return 0;
  255. }
  256. /* Copy the host flags if and only if we're copying the host list */
  257. if (test_x509_verify_param_copy_id(hosts, NULL))
  258. {
  259. if (dest->id->hosts)
  260. {
  261. string_stack_free(dest->id->hosts);
  262. dest->id->hosts = NULL;
  263. }
  264. if (id->hosts)
  265. {
  266. dest->id->hosts =
  267. sk_OPENSSL_STRING_deep_copy(id->hosts,
  268. str_copy, str_free);
  269. if (dest->id->hosts == NULL)
  270. return 0;
  271. dest->id->hostflags = id->hostflags;
  272. }
  273. }
  274. if (test_x509_verify_param_copy_id(email, NULL))
  275. {
  276. if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
  277. return 0;
  278. }
  279. if (test_x509_verify_param_copy_id(ip, NULL))
  280. {
  281. if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
  282. return 0;
  283. }
  284. return 1;
  285. }
  286. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  287. const X509_VERIFY_PARAM *from)
  288. {
  289. unsigned long save_flags = to->inh_flags;
  290. int ret;
  291. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  292. ret = X509_VERIFY_PARAM_inherit(to, from);
  293. to->inh_flags = save_flags;
  294. return ret;
  295. }
  296. static int int_x509_param_set1(char **pdest, size_t *pdestlen,
  297. const char *src, size_t srclen)
  298. {
  299. void *tmp;
  300. if (src)
  301. {
  302. if (srclen == 0)
  303. {
  304. tmp = BUF_strdup(src);
  305. srclen = strlen(src);
  306. }
  307. else
  308. tmp = BUF_memdup(src, srclen);
  309. if (!tmp)
  310. return 0;
  311. }
  312. else
  313. {
  314. tmp = NULL;
  315. srclen = 0;
  316. }
  317. if (*pdest)
  318. OPENSSL_free(*pdest);
  319. *pdest = tmp;
  320. if (pdestlen)
  321. *pdestlen = srclen;
  322. return 1;
  323. }
  324. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  325. {
  326. if (param->name)
  327. OPENSSL_free(param->name);
  328. param->name = BUF_strdup(name);
  329. if (param->name)
  330. return 1;
  331. return 0;
  332. }
  333. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  334. {
  335. param->flags |= flags;
  336. if (flags & X509_V_FLAG_POLICY_MASK)
  337. param->flags |= X509_V_FLAG_POLICY_CHECK;
  338. return 1;
  339. }
  340. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  341. {
  342. param->flags &= ~flags;
  343. return 1;
  344. }
  345. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  346. {
  347. return param->flags;
  348. }
  349. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  350. {
  351. return X509_PURPOSE_set(&param->purpose, purpose);
  352. }
  353. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  354. {
  355. return X509_TRUST_set(&param->trust, trust);
  356. }
  357. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  358. {
  359. param->depth = depth;
  360. }
  361. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  362. {
  363. param->check_time = t;
  364. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  365. }
  366. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
  367. {
  368. if (!param->policies)
  369. {
  370. param->policies = sk_ASN1_OBJECT_new_null();
  371. if (!param->policies)
  372. return 0;
  373. }
  374. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  375. return 0;
  376. return 1;
  377. }
  378. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  379. STACK_OF(ASN1_OBJECT) *policies)
  380. {
  381. size_t i;
  382. ASN1_OBJECT *oid, *doid;
  383. if (!param)
  384. return 0;
  385. if (param->policies)
  386. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  387. if (!policies)
  388. {
  389. param->policies = NULL;
  390. return 1;
  391. }
  392. param->policies = sk_ASN1_OBJECT_new_null();
  393. if (!param->policies)
  394. return 0;
  395. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++)
  396. {
  397. oid = sk_ASN1_OBJECT_value(policies, i);
  398. doid = OBJ_dup(oid);
  399. if (!doid)
  400. return 0;
  401. if (!sk_ASN1_OBJECT_push(param->policies, doid))
  402. {
  403. ASN1_OBJECT_free(doid);
  404. return 0;
  405. }
  406. }
  407. param->flags |= X509_V_FLAG_POLICY_CHECK;
  408. return 1;
  409. }
  410. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
  411. const char *name, size_t namelen)
  412. {
  413. return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen);
  414. }
  415. int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
  416. const char *name, size_t namelen)
  417. {
  418. return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen);
  419. }
  420. void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
  421. unsigned int flags)
  422. {
  423. param->id->hostflags = flags;
  424. }
  425. char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
  426. {
  427. return param->id->peername;
  428. }
  429. int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
  430. const char *email, size_t emaillen)
  431. {
  432. return int_x509_param_set1(&param->id->email, &param->id->emaillen,
  433. email, emaillen);
  434. }
  435. int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
  436. const unsigned char *ip, size_t iplen)
  437. {
  438. if (iplen != 0 && iplen != 4 && iplen != 16)
  439. return 0;
  440. return int_x509_param_set1((char **)&param->id->ip, &param->id->iplen,
  441. (char *)ip, iplen);
  442. }
  443. int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
  444. {
  445. unsigned char ipout[16];
  446. size_t iplen;
  447. iplen = (size_t) a2i_ipadd(ipout, ipasc);
  448. if (iplen == 0)
  449. return 0;
  450. return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
  451. }
  452. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  453. {
  454. return param->depth;
  455. }
  456. const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
  457. {
  458. return param->name;
  459. }
  460. static const X509_VERIFY_PARAM_ID _empty_id = {NULL, 0U, NULL, NULL, 0, NULL, 0};
  461. #define vpm_empty_id (X509_VERIFY_PARAM_ID *)&_empty_id
  462. /* Default verify parameters: these are used for various
  463. * applications and can be overridden by the user specified table.
  464. * NB: the 'name' field *must* be in alphabetical order because it
  465. * will be searched using OBJ_search.
  466. */
  467. static const X509_VERIFY_PARAM default_table[] = {
  468. {
  469. (char *) "default", /* X509 default parameters */
  470. 0, /* Check time */
  471. 0, /* internal flags */
  472. 0, /* flags */
  473. 0, /* purpose */
  474. 0, /* trust */
  475. 100, /* depth */
  476. NULL, /* policies */
  477. vpm_empty_id
  478. },
  479. {
  480. (char *) "pkcs7", /* S/MIME sign parameters */
  481. 0, /* Check time */
  482. 0, /* internal flags */
  483. 0, /* flags */
  484. X509_PURPOSE_SMIME_SIGN, /* purpose */
  485. X509_TRUST_EMAIL, /* trust */
  486. -1, /* depth */
  487. NULL, /* policies */
  488. vpm_empty_id
  489. },
  490. {
  491. (char *) "smime_sign", /* S/MIME sign parameters */
  492. 0, /* Check time */
  493. 0, /* internal flags */
  494. 0, /* flags */
  495. X509_PURPOSE_SMIME_SIGN, /* purpose */
  496. X509_TRUST_EMAIL, /* trust */
  497. -1, /* depth */
  498. NULL, /* policies */
  499. vpm_empty_id
  500. },
  501. {
  502. (char *) "ssl_client", /* SSL/TLS client parameters */
  503. 0, /* Check time */
  504. 0, /* internal flags */
  505. 0, /* flags */
  506. X509_PURPOSE_SSL_CLIENT, /* purpose */
  507. X509_TRUST_SSL_CLIENT, /* trust */
  508. -1, /* depth */
  509. NULL, /* policies */
  510. vpm_empty_id
  511. },
  512. {
  513. (char *) "ssl_server", /* SSL/TLS server parameters */
  514. 0, /* Check time */
  515. 0, /* internal flags */
  516. 0, /* flags */
  517. X509_PURPOSE_SSL_SERVER, /* purpose */
  518. X509_TRUST_SSL_SERVER, /* trust */
  519. -1, /* depth */
  520. NULL, /* policies */
  521. vpm_empty_id
  522. }};
  523. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  524. static int param_cmp(const X509_VERIFY_PARAM **a,
  525. const X509_VERIFY_PARAM **b)
  526. {
  527. return strcmp((*a)->name, (*b)->name);
  528. }
  529. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  530. {
  531. X509_VERIFY_PARAM *ptmp;
  532. if (!param_table)
  533. {
  534. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  535. if (!param_table)
  536. return 0;
  537. }
  538. else
  539. {
  540. size_t idx;
  541. if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param))
  542. {
  543. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  544. X509_VERIFY_PARAM_free(ptmp);
  545. (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
  546. }
  547. }
  548. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  549. return 0;
  550. return 1;
  551. }
  552. int X509_VERIFY_PARAM_get_count(void)
  553. {
  554. int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
  555. if (param_table)
  556. num += sk_X509_VERIFY_PARAM_num(param_table);
  557. return num;
  558. }
  559. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
  560. {
  561. int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
  562. if (id < num)
  563. return default_table + id;
  564. return sk_X509_VERIFY_PARAM_value(param_table, id - num);
  565. }
  566. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  567. {
  568. X509_VERIFY_PARAM pm;
  569. unsigned i, limit;
  570. pm.name = (char *)name;
  571. if (param_table)
  572. {
  573. size_t idx;
  574. if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm))
  575. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  576. }
  577. limit = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
  578. for (i = 0; i < limit; i++) {
  579. if (strcmp(default_table[i].name, name) == 0) {
  580. return &default_table[i];
  581. }
  582. }
  583. return NULL;
  584. }
  585. void X509_VERIFY_PARAM_table_cleanup(void)
  586. {
  587. if (param_table)
  588. sk_X509_VERIFY_PARAM_pop_free(param_table,
  589. X509_VERIFY_PARAM_free);
  590. param_table = NULL;
  591. }