You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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