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.
 
 
 
 
 
 

637 lines
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. 1. If a
  178. * child structure needs to have its values initialized from a parent they are
  179. * simply copied across. For example SSL_CTX copied to SSL. 2. If the
  180. * structure should take on values only if they are currently unset. For
  181. * example the values in an SSL structure will take appropriate value for SSL
  182. * servers or clients but only if the application has not set new ones. The
  183. * "inh_flags" field determines how this function behaves. Normally any
  184. * values which are set in the default are not copied from the destination and
  185. * verify flags are ORed together. If X509_VP_FLAG_DEFAULT is set then
  186. * anything set in the source is copied to the destination. Effectively the
  187. * values in "to" become default values which will be used only if nothing new
  188. * is set in "from". If X509_VP_FLAG_OVERWRITE is set then all value are
  189. * copied across whether they are set or not. Flags is still Ored though. If
  190. * X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead of
  191. * ORed. If X509_VP_FLAG_LOCKED is set then no values are copied. If
  192. * X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed after
  193. * the next call.
  194. */
  195. /* Macro to test if a field should be copied from src to dest */
  196. #define test_x509_verify_param_copy(field, def) \
  197. (to_overwrite || \
  198. ((src->field != def) && (to_default || (dest->field == def))))
  199. /* As above but for ID fields */
  200. #define test_x509_verify_param_copy_id(idf, def) \
  201. test_x509_verify_param_copy(id->idf, def)
  202. /* Macro to test and copy a field if necessary */
  203. #define x509_verify_param_copy(field, def) \
  204. if (test_x509_verify_param_copy(field, def)) \
  205. dest->field = src->field
  206. int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
  207. const X509_VERIFY_PARAM *src)
  208. {
  209. unsigned long inh_flags;
  210. int to_default, to_overwrite;
  211. X509_VERIFY_PARAM_ID *id;
  212. if (!src)
  213. return 1;
  214. id = src->id;
  215. inh_flags = dest->inh_flags | src->inh_flags;
  216. if (inh_flags & X509_VP_FLAG_ONCE)
  217. dest->inh_flags = 0;
  218. if (inh_flags & X509_VP_FLAG_LOCKED)
  219. return 1;
  220. if (inh_flags & X509_VP_FLAG_DEFAULT)
  221. to_default = 1;
  222. else
  223. to_default = 0;
  224. if (inh_flags & X509_VP_FLAG_OVERWRITE)
  225. to_overwrite = 1;
  226. else
  227. to_overwrite = 0;
  228. x509_verify_param_copy(purpose, 0);
  229. x509_verify_param_copy(trust, 0);
  230. x509_verify_param_copy(depth, -1);
  231. /* If overwrite or check time not set, copy across */
  232. if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
  233. dest->check_time = src->check_time;
  234. dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
  235. /* Don't need to copy flag: that is done below */
  236. }
  237. if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
  238. dest->flags = 0;
  239. dest->flags |= src->flags;
  240. if (test_x509_verify_param_copy(policies, NULL)) {
  241. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  242. return 0;
  243. }
  244. /* Copy the host flags if and only if we're copying the host list */
  245. if (test_x509_verify_param_copy_id(hosts, NULL)) {
  246. if (dest->id->hosts) {
  247. string_stack_free(dest->id->hosts);
  248. dest->id->hosts = NULL;
  249. }
  250. if (id->hosts) {
  251. dest->id->hosts =
  252. sk_OPENSSL_STRING_deep_copy(id->hosts, str_copy, str_free);
  253. if (dest->id->hosts == NULL)
  254. return 0;
  255. dest->id->hostflags = id->hostflags;
  256. }
  257. }
  258. if (test_x509_verify_param_copy_id(email, NULL)) {
  259. if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
  260. return 0;
  261. }
  262. if (test_x509_verify_param_copy_id(ip, NULL)) {
  263. if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  269. const X509_VERIFY_PARAM *from)
  270. {
  271. unsigned long save_flags = to->inh_flags;
  272. int ret;
  273. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  274. ret = X509_VERIFY_PARAM_inherit(to, from);
  275. to->inh_flags = save_flags;
  276. return ret;
  277. }
  278. static int int_x509_param_set1(char **pdest, size_t *pdestlen,
  279. const char *src, size_t srclen)
  280. {
  281. void *tmp;
  282. if (src) {
  283. if (srclen == 0) {
  284. tmp = BUF_strdup(src);
  285. srclen = strlen(src);
  286. } else
  287. tmp = BUF_memdup(src, srclen);
  288. if (!tmp)
  289. return 0;
  290. } else {
  291. tmp = NULL;
  292. srclen = 0;
  293. }
  294. if (*pdest)
  295. OPENSSL_free(*pdest);
  296. *pdest = tmp;
  297. if (pdestlen)
  298. *pdestlen = srclen;
  299. return 1;
  300. }
  301. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  302. {
  303. if (param->name)
  304. OPENSSL_free(param->name);
  305. param->name = BUF_strdup(name);
  306. if (param->name)
  307. return 1;
  308. return 0;
  309. }
  310. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  311. {
  312. param->flags |= flags;
  313. if (flags & X509_V_FLAG_POLICY_MASK)
  314. param->flags |= X509_V_FLAG_POLICY_CHECK;
  315. return 1;
  316. }
  317. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
  318. unsigned long flags)
  319. {
  320. param->flags &= ~flags;
  321. return 1;
  322. }
  323. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  324. {
  325. return param->flags;
  326. }
  327. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  328. {
  329. return X509_PURPOSE_set(&param->purpose, purpose);
  330. }
  331. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  332. {
  333. return X509_TRUST_set(&param->trust, trust);
  334. }
  335. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  336. {
  337. param->depth = depth;
  338. }
  339. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  340. {
  341. param->check_time = t;
  342. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  343. }
  344. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
  345. ASN1_OBJECT *policy)
  346. {
  347. if (!param->policies) {
  348. param->policies = sk_ASN1_OBJECT_new_null();
  349. if (!param->policies)
  350. return 0;
  351. }
  352. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  353. return 0;
  354. return 1;
  355. }
  356. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  357. STACK_OF(ASN1_OBJECT) *policies)
  358. {
  359. size_t i;
  360. ASN1_OBJECT *oid, *doid;
  361. if (!param)
  362. return 0;
  363. if (param->policies)
  364. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  365. if (!policies) {
  366. param->policies = NULL;
  367. return 1;
  368. }
  369. param->policies = sk_ASN1_OBJECT_new_null();
  370. if (!param->policies)
  371. return 0;
  372. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
  373. oid = sk_ASN1_OBJECT_value(policies, i);
  374. doid = OBJ_dup(oid);
  375. if (!doid)
  376. return 0;
  377. if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
  378. ASN1_OBJECT_free(doid);
  379. return 0;
  380. }
  381. }
  382. param->flags |= X509_V_FLAG_POLICY_CHECK;
  383. return 1;
  384. }
  385. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
  386. const char *name, size_t namelen)
  387. {
  388. return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen);
  389. }
  390. int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
  391. const char *name, size_t namelen)
  392. {
  393. return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen);
  394. }
  395. void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
  396. unsigned int flags)
  397. {
  398. param->id->hostflags = flags;
  399. }
  400. char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
  401. {
  402. return param->id->peername;
  403. }
  404. int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
  405. const char *email, size_t emaillen)
  406. {
  407. return int_x509_param_set1(&param->id->email, &param->id->emaillen,
  408. email, emaillen);
  409. }
  410. int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
  411. const unsigned char *ip, size_t iplen)
  412. {
  413. if (iplen != 0 && iplen != 4 && iplen != 16)
  414. return 0;
  415. return int_x509_param_set1((char **)&param->id->ip, &param->id->iplen,
  416. (char *)ip, iplen);
  417. }
  418. int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
  419. {
  420. unsigned char ipout[16];
  421. size_t iplen;
  422. iplen = (size_t)a2i_ipadd(ipout, ipasc);
  423. if (iplen == 0)
  424. return 0;
  425. return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
  426. }
  427. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  428. {
  429. return param->depth;
  430. }
  431. const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
  432. {
  433. return param->name;
  434. }
  435. static const X509_VERIFY_PARAM_ID _empty_id =
  436. { NULL, 0U, NULL, NULL, 0, NULL, 0 };
  437. #define vpm_empty_id (X509_VERIFY_PARAM_ID *)&_empty_id
  438. /*
  439. * Default verify parameters: these are used for various applications and can
  440. * be overridden by the user specified table. NB: the 'name' field *must* be
  441. * in alphabetical order because it will be searched using OBJ_search.
  442. */
  443. static const X509_VERIFY_PARAM default_table[] = {
  444. {
  445. (char *)"default", /* X509 default parameters */
  446. 0, /* Check time */
  447. 0, /* internal flags */
  448. 0, /* flags */
  449. 0, /* purpose */
  450. 0, /* trust */
  451. 100, /* depth */
  452. NULL, /* policies */
  453. vpm_empty_id},
  454. {
  455. (char *)"pkcs7", /* S/MIME sign parameters */
  456. 0, /* Check time */
  457. 0, /* internal flags */
  458. 0, /* flags */
  459. X509_PURPOSE_SMIME_SIGN, /* purpose */
  460. X509_TRUST_EMAIL, /* trust */
  461. -1, /* depth */
  462. NULL, /* policies */
  463. vpm_empty_id},
  464. {
  465. (char *)"smime_sign", /* S/MIME sign parameters */
  466. 0, /* Check time */
  467. 0, /* internal flags */
  468. 0, /* flags */
  469. X509_PURPOSE_SMIME_SIGN, /* purpose */
  470. X509_TRUST_EMAIL, /* trust */
  471. -1, /* depth */
  472. NULL, /* policies */
  473. vpm_empty_id},
  474. {
  475. (char *)"ssl_client", /* SSL/TLS client parameters */
  476. 0, /* Check time */
  477. 0, /* internal flags */
  478. 0, /* flags */
  479. X509_PURPOSE_SSL_CLIENT, /* purpose */
  480. X509_TRUST_SSL_CLIENT, /* trust */
  481. -1, /* depth */
  482. NULL, /* policies */
  483. vpm_empty_id},
  484. {
  485. (char *)"ssl_server", /* SSL/TLS server parameters */
  486. 0, /* Check time */
  487. 0, /* internal flags */
  488. 0, /* flags */
  489. X509_PURPOSE_SSL_SERVER, /* purpose */
  490. X509_TRUST_SSL_SERVER, /* trust */
  491. -1, /* depth */
  492. NULL, /* policies */
  493. vpm_empty_id}
  494. };
  495. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  496. static int param_cmp(const X509_VERIFY_PARAM **a, const X509_VERIFY_PARAM **b)
  497. {
  498. return strcmp((*a)->name, (*b)->name);
  499. }
  500. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  501. {
  502. X509_VERIFY_PARAM *ptmp;
  503. if (!param_table) {
  504. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  505. if (!param_table)
  506. return 0;
  507. } else {
  508. size_t idx;
  509. if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param)) {
  510. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  511. X509_VERIFY_PARAM_free(ptmp);
  512. (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
  513. }
  514. }
  515. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  516. return 0;
  517. return 1;
  518. }
  519. int X509_VERIFY_PARAM_get_count(void)
  520. {
  521. int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
  522. if (param_table)
  523. num += sk_X509_VERIFY_PARAM_num(param_table);
  524. return num;
  525. }
  526. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
  527. {
  528. int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
  529. if (id < num)
  530. return default_table + id;
  531. return sk_X509_VERIFY_PARAM_value(param_table, id - num);
  532. }
  533. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  534. {
  535. X509_VERIFY_PARAM pm;
  536. unsigned i, limit;
  537. pm.name = (char *)name;
  538. if (param_table) {
  539. size_t idx;
  540. if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm))
  541. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  542. }
  543. limit = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
  544. for (i = 0; i < limit; i++) {
  545. if (strcmp(default_table[i].name, name) == 0) {
  546. return &default_table[i];
  547. }
  548. }
  549. return NULL;
  550. }
  551. void X509_VERIFY_PARAM_table_cleanup(void)
  552. {
  553. if (param_table)
  554. sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free);
  555. param_table = NULL;
  556. }