25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

648 satır
19 KiB

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