Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

586 wiersze
15 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 <openssl/buf.h>
  55. #include <openssl/lhash.h>
  56. #include <openssl/mem.h>
  57. #include <openssl/obj.h>
  58. #include <openssl/x509.h>
  59. #include <openssl/x509v3.h>
  60. #include "vpm_int.h"
  61. /* X509_VERIFY_PARAM functions */
  62. static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
  63. {
  64. X509_VERIFY_PARAM_ID *paramid;
  65. if (!param)
  66. return;
  67. param->name = NULL;
  68. param->purpose = 0;
  69. param->trust = 0;
  70. /*param->inh_flags = X509_VP_FLAG_DEFAULT;*/
  71. param->inh_flags = 0;
  72. param->flags = 0;
  73. param->depth = -1;
  74. if (param->policies)
  75. {
  76. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  77. param->policies = NULL;
  78. }
  79. paramid = param->id;
  80. if (paramid->host)
  81. {
  82. OPENSSL_free(paramid->host);
  83. paramid->host = NULL;
  84. paramid->hostlen = 0;
  85. }
  86. if (paramid->email)
  87. {
  88. OPENSSL_free(paramid->email);
  89. paramid->email = NULL;
  90. paramid->emaillen = 0;
  91. }
  92. if (paramid->ip)
  93. {
  94. OPENSSL_free(paramid->ip);
  95. paramid->ip = NULL;
  96. paramid->iplen = 0;
  97. }
  98. }
  99. X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
  100. {
  101. X509_VERIFY_PARAM *param;
  102. X509_VERIFY_PARAM_ID *paramid;
  103. param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
  104. if (!param)
  105. return NULL;
  106. paramid = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
  107. if (!paramid)
  108. {
  109. OPENSSL_free(param);
  110. return NULL;
  111. }
  112. memset(param, 0, sizeof(X509_VERIFY_PARAM));
  113. memset(paramid, 0, sizeof(X509_VERIFY_PARAM_ID));
  114. param->id = paramid;
  115. x509_verify_param_zero(param);
  116. return param;
  117. }
  118. void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
  119. {
  120. x509_verify_param_zero(param);
  121. OPENSSL_free(param->id);
  122. OPENSSL_free(param);
  123. }
  124. /* This function determines how parameters are "inherited" from one structure
  125. * to another. There are several different ways this can happen.
  126. *
  127. * 1. If a child structure needs to have its values initialized from a parent
  128. * they are simply copied across. For example SSL_CTX copied to SSL.
  129. * 2. If the structure should take on values only if they are currently unset.
  130. * For example the values in an SSL structure will take appropriate value
  131. * for SSL servers or clients but only if the application has not set new
  132. * ones.
  133. *
  134. * The "inh_flags" field determines how this function behaves.
  135. *
  136. * Normally any values which are set in the default are not copied from the
  137. * destination and verify flags are ORed together.
  138. *
  139. * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
  140. * to the destination. Effectively the values in "to" become default values
  141. * which will be used only if nothing new is set in "from".
  142. *
  143. * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
  144. * they are set or not. Flags is still Ored though.
  145. *
  146. * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
  147. * of ORed.
  148. *
  149. * If X509_VP_FLAG_LOCKED is set then no values are copied.
  150. *
  151. * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
  152. * after the next call.
  153. */
  154. /* Macro to test if a field should be copied from src to dest */
  155. #define test_x509_verify_param_copy(field, def) \
  156. (to_overwrite || \
  157. ((src->field != def) && (to_default || (dest->field == def))))
  158. /* As above but for ID fields */
  159. #define test_x509_verify_param_copy_id(idf, def) \
  160. test_x509_verify_param_copy(id->idf, def)
  161. /* Macro to test and copy a field if necessary */
  162. #define x509_verify_param_copy(field, def) \
  163. if (test_x509_verify_param_copy(field, def)) \
  164. dest->field = src->field
  165. int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
  166. const X509_VERIFY_PARAM *src)
  167. {
  168. unsigned long inh_flags;
  169. int to_default, to_overwrite;
  170. X509_VERIFY_PARAM_ID *id;
  171. if (!src)
  172. return 1;
  173. id = src->id;
  174. inh_flags = dest->inh_flags | src->inh_flags;
  175. if (inh_flags & X509_VP_FLAG_ONCE)
  176. dest->inh_flags = 0;
  177. if (inh_flags & X509_VP_FLAG_LOCKED)
  178. return 1;
  179. if (inh_flags & X509_VP_FLAG_DEFAULT)
  180. to_default = 1;
  181. else
  182. to_default = 0;
  183. if (inh_flags & X509_VP_FLAG_OVERWRITE)
  184. to_overwrite = 1;
  185. else
  186. to_overwrite = 0;
  187. x509_verify_param_copy(purpose, 0);
  188. x509_verify_param_copy(trust, 0);
  189. x509_verify_param_copy(depth, -1);
  190. /* If overwrite or check time not set, copy across */
  191. if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME))
  192. {
  193. dest->check_time = src->check_time;
  194. dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
  195. /* Don't need to copy flag: that is done below */
  196. }
  197. if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
  198. dest->flags = 0;
  199. dest->flags |= src->flags;
  200. if (test_x509_verify_param_copy(policies, NULL))
  201. {
  202. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  203. return 0;
  204. }
  205. if (test_x509_verify_param_copy_id(host, NULL))
  206. {
  207. if (!X509_VERIFY_PARAM_set1_host(dest, id->host, id->hostlen))
  208. return 0;
  209. dest->id->hostflags = id->hostflags;
  210. }
  211. if (test_x509_verify_param_copy_id(email, NULL))
  212. {
  213. if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
  214. return 0;
  215. }
  216. if (test_x509_verify_param_copy_id(ip, NULL))
  217. {
  218. if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
  219. return 0;
  220. }
  221. return 1;
  222. }
  223. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  224. const X509_VERIFY_PARAM *from)
  225. {
  226. unsigned long save_flags = to->inh_flags;
  227. int ret;
  228. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  229. ret = X509_VERIFY_PARAM_inherit(to, from);
  230. to->inh_flags = save_flags;
  231. return ret;
  232. }
  233. static int int_x509_param_set1(unsigned char **pdest, size_t *pdestlen,
  234. const unsigned char *src, size_t srclen)
  235. {
  236. void *tmp;
  237. if (src)
  238. {
  239. if (srclen == 0)
  240. {
  241. tmp = BUF_strdup((char *)src);
  242. srclen = strlen((char *)src);
  243. }
  244. else
  245. tmp = BUF_memdup(src, srclen);
  246. if (!tmp)
  247. return 0;
  248. }
  249. else
  250. {
  251. tmp = NULL;
  252. srclen = 0;
  253. }
  254. if (*pdest)
  255. OPENSSL_free(*pdest);
  256. *pdest = tmp;
  257. if (pdestlen)
  258. *pdestlen = srclen;
  259. return 1;
  260. }
  261. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  262. {
  263. if (param->name)
  264. OPENSSL_free(param->name);
  265. param->name = BUF_strdup(name);
  266. if (param->name)
  267. return 1;
  268. return 0;
  269. }
  270. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  271. {
  272. param->flags |= flags;
  273. if (flags & X509_V_FLAG_POLICY_MASK)
  274. param->flags |= X509_V_FLAG_POLICY_CHECK;
  275. return 1;
  276. }
  277. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  278. {
  279. param->flags &= ~flags;
  280. return 1;
  281. }
  282. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  283. {
  284. return param->flags;
  285. }
  286. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  287. {
  288. return X509_PURPOSE_set(&param->purpose, purpose);
  289. }
  290. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  291. {
  292. return X509_TRUST_set(&param->trust, trust);
  293. }
  294. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  295. {
  296. param->depth = depth;
  297. }
  298. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  299. {
  300. param->check_time = t;
  301. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  302. }
  303. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
  304. {
  305. if (!param->policies)
  306. {
  307. param->policies = sk_ASN1_OBJECT_new_null();
  308. if (!param->policies)
  309. return 0;
  310. }
  311. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  312. return 0;
  313. return 1;
  314. }
  315. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  316. STACK_OF(ASN1_OBJECT) *policies)
  317. {
  318. size_t i;
  319. ASN1_OBJECT *oid, *doid;
  320. if (!param)
  321. return 0;
  322. if (param->policies)
  323. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  324. if (!policies)
  325. {
  326. param->policies = NULL;
  327. return 1;
  328. }
  329. param->policies = sk_ASN1_OBJECT_new_null();
  330. if (!param->policies)
  331. return 0;
  332. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++)
  333. {
  334. oid = sk_ASN1_OBJECT_value(policies, i);
  335. doid = OBJ_dup(oid);
  336. if (!doid)
  337. return 0;
  338. if (!sk_ASN1_OBJECT_push(param->policies, doid))
  339. {
  340. ASN1_OBJECT_free(doid);
  341. return 0;
  342. }
  343. }
  344. param->flags |= X509_V_FLAG_POLICY_CHECK;
  345. return 1;
  346. }
  347. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
  348. const unsigned char *name, size_t namelen)
  349. {
  350. return int_x509_param_set1(&param->id->host, &param->id->hostlen,
  351. name, namelen);
  352. }
  353. void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
  354. unsigned int flags)
  355. {
  356. param->id->hostflags = flags;
  357. }
  358. int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
  359. const unsigned char *email, size_t emaillen)
  360. {
  361. return int_x509_param_set1(&param->id->email, &param->id->emaillen,
  362. email, emaillen);
  363. }
  364. int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
  365. const unsigned char *ip, size_t iplen)
  366. {
  367. if (iplen != 0 && iplen != 4 && iplen != 16)
  368. return 0;
  369. return int_x509_param_set1(&param->id->ip, &param->id->iplen, ip, iplen);
  370. }
  371. int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
  372. {
  373. unsigned char ipout[16];
  374. int iplen;
  375. iplen = a2i_ipadd(ipout, ipasc);
  376. if (iplen == 0)
  377. return 0;
  378. return X509_VERIFY_PARAM_set1_ip(param, ipout, (size_t)iplen);
  379. }
  380. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  381. {
  382. return param->depth;
  383. }
  384. const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
  385. {
  386. return param->name;
  387. }
  388. static X509_VERIFY_PARAM_ID _empty_id = {NULL, 0, 0U, NULL, 0, NULL, 0};
  389. #define vpm_empty_id (X509_VERIFY_PARAM_ID *)&_empty_id
  390. /* Default verify parameters: these are used for various
  391. * applications and can be overridden by the user specified table.
  392. * NB: the 'name' field *must* be in alphabetical order because it
  393. * will be searched using OBJ_search.
  394. */
  395. static const X509_VERIFY_PARAM default_table[] = {
  396. {
  397. (char *) "default", /* X509 default parameters */
  398. 0, /* Check time */
  399. 0, /* internal flags */
  400. 0, /* flags */
  401. 0, /* purpose */
  402. 0, /* trust */
  403. 100, /* depth */
  404. NULL, /* policies */
  405. vpm_empty_id
  406. },
  407. {
  408. (char *) "pkcs7", /* S/MIME sign parameters */
  409. 0, /* Check time */
  410. 0, /* internal flags */
  411. 0, /* flags */
  412. X509_PURPOSE_SMIME_SIGN, /* purpose */
  413. X509_TRUST_EMAIL, /* trust */
  414. -1, /* depth */
  415. NULL, /* policies */
  416. vpm_empty_id
  417. },
  418. {
  419. (char *) "smime_sign", /* S/MIME sign parameters */
  420. 0, /* Check time */
  421. 0, /* internal flags */
  422. 0, /* flags */
  423. X509_PURPOSE_SMIME_SIGN, /* purpose */
  424. X509_TRUST_EMAIL, /* trust */
  425. -1, /* depth */
  426. NULL, /* policies */
  427. vpm_empty_id
  428. },
  429. {
  430. (char *) "ssl_client", /* SSL/TLS client parameters */
  431. 0, /* Check time */
  432. 0, /* internal flags */
  433. 0, /* flags */
  434. X509_PURPOSE_SSL_CLIENT, /* purpose */
  435. X509_TRUST_SSL_CLIENT, /* trust */
  436. -1, /* depth */
  437. NULL, /* policies */
  438. vpm_empty_id
  439. },
  440. {
  441. (char *) "ssl_server", /* SSL/TLS server parameters */
  442. 0, /* Check time */
  443. 0, /* internal flags */
  444. 0, /* flags */
  445. X509_PURPOSE_SSL_SERVER, /* purpose */
  446. X509_TRUST_SSL_SERVER, /* trust */
  447. -1, /* depth */
  448. NULL, /* policies */
  449. vpm_empty_id
  450. }};
  451. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  452. static int param_cmp(const X509_VERIFY_PARAM **a,
  453. const X509_VERIFY_PARAM **b)
  454. {
  455. return strcmp((*a)->name, (*b)->name);
  456. }
  457. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  458. {
  459. X509_VERIFY_PARAM *ptmp;
  460. if (!param_table)
  461. {
  462. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  463. if (!param_table)
  464. return 0;
  465. }
  466. else
  467. {
  468. size_t idx;
  469. if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param))
  470. {
  471. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  472. X509_VERIFY_PARAM_free(ptmp);
  473. (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
  474. }
  475. }
  476. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  477. return 0;
  478. return 1;
  479. }
  480. int X509_VERIFY_PARAM_get_count(void)
  481. {
  482. int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
  483. if (param_table)
  484. num += sk_X509_VERIFY_PARAM_num(param_table);
  485. return num;
  486. }
  487. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
  488. {
  489. int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
  490. if (id < num)
  491. return default_table + id;
  492. return sk_X509_VERIFY_PARAM_value(param_table, id - num);
  493. }
  494. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  495. {
  496. X509_VERIFY_PARAM pm;
  497. unsigned i, limit;
  498. pm.name = (char *)name;
  499. if (param_table)
  500. {
  501. size_t idx;
  502. if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm))
  503. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  504. }
  505. limit = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
  506. for (i = 0; i < limit; i++) {
  507. if (strcmp(default_table[i].name, name) == 0) {
  508. return &default_table[i];
  509. }
  510. }
  511. return NULL;
  512. }
  513. void X509_VERIFY_PARAM_table_cleanup(void)
  514. {
  515. if (param_table)
  516. sk_X509_VERIFY_PARAM_pop_free(param_table,
  517. X509_VERIFY_PARAM_free);
  518. param_table = NULL;
  519. }