2
0

x509_vpm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* x509_vpm.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2004.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/crypto.h>
  62. #include <openssl/lhash.h>
  63. #include <openssl/buffer.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/x509v3.h>
  66. /* X509_VERIFY_PARAM functions */
  67. static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
  68. {
  69. if (!param)
  70. return;
  71. param->name = NULL;
  72. param->purpose = 0;
  73. param->trust = 0;
  74. param->inh_flags = 0;
  75. param->flags = 0;
  76. param->depth = -1;
  77. if (param->policies) {
  78. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  79. param->policies = NULL;
  80. }
  81. }
  82. X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
  83. {
  84. X509_VERIFY_PARAM *param;
  85. param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
  86. memset(param, 0, sizeof(X509_VERIFY_PARAM));
  87. x509_verify_param_zero(param);
  88. return param;
  89. }
  90. void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
  91. {
  92. x509_verify_param_zero(param);
  93. OPENSSL_free(param);
  94. }
  95. /*-
  96. * This function determines how parameters are "inherited" from one structure
  97. * to another. There are several different ways this can happen.
  98. *
  99. * 1. If a child structure needs to have its values initialized from a parent
  100. * they are simply copied across. For example SSL_CTX copied to SSL.
  101. * 2. If the structure should take on values only if they are currently unset.
  102. * For example the values in an SSL structure will take appropriate value
  103. * for SSL servers or clients but only if the application has not set new
  104. * ones.
  105. *
  106. * The "inh_flags" field determines how this function behaves.
  107. *
  108. * Normally any values which are set in the default are not copied from the
  109. * destination and verify flags are ORed together.
  110. *
  111. * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
  112. * to the destination. Effectively the values in "to" become default values
  113. * which will be used only if nothing new is set in "from".
  114. *
  115. * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
  116. * they are set or not. Flags is still Ored though.
  117. *
  118. * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
  119. * of ORed.
  120. *
  121. * If X509_VP_FLAG_LOCKED is set then no values are copied.
  122. *
  123. * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
  124. * after the next call.
  125. */
  126. /* Macro to test if a field should be copied from src to dest */
  127. #define test_x509_verify_param_copy(field, def) \
  128. (to_overwrite || \
  129. ((src->field != def) && (to_default || (dest->field == def))))
  130. /* Macro to test and copy a field if necessary */
  131. #define x509_verify_param_copy(field, def) \
  132. if (test_x509_verify_param_copy(field, def)) \
  133. dest->field = src->field
  134. int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
  135. const X509_VERIFY_PARAM *src)
  136. {
  137. unsigned long inh_flags;
  138. int to_default, to_overwrite;
  139. if (!src)
  140. return 1;
  141. inh_flags = dest->inh_flags | src->inh_flags;
  142. if (inh_flags & X509_VP_FLAG_ONCE)
  143. dest->inh_flags = 0;
  144. if (inh_flags & X509_VP_FLAG_LOCKED)
  145. return 1;
  146. if (inh_flags & X509_VP_FLAG_DEFAULT)
  147. to_default = 1;
  148. else
  149. to_default = 0;
  150. if (inh_flags & X509_VP_FLAG_OVERWRITE)
  151. to_overwrite = 1;
  152. else
  153. to_overwrite = 0;
  154. x509_verify_param_copy(purpose, 0);
  155. x509_verify_param_copy(trust, 0);
  156. x509_verify_param_copy(depth, -1);
  157. /* If overwrite or check time not set, copy across */
  158. if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
  159. dest->check_time = src->check_time;
  160. dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
  161. /* Don't need to copy flag: that is done below */
  162. }
  163. if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
  164. dest->flags = 0;
  165. dest->flags |= src->flags;
  166. if (test_x509_verify_param_copy(policies, NULL)) {
  167. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  173. const X509_VERIFY_PARAM *from)
  174. {
  175. unsigned long save_flags = to->inh_flags;
  176. int ret;
  177. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  178. ret = X509_VERIFY_PARAM_inherit(to, from);
  179. to->inh_flags = save_flags;
  180. return ret;
  181. }
  182. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  183. {
  184. if (param->name)
  185. OPENSSL_free(param->name);
  186. param->name = BUF_strdup(name);
  187. if (param->name)
  188. return 1;
  189. return 0;
  190. }
  191. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  192. {
  193. param->flags |= flags;
  194. if (flags & X509_V_FLAG_POLICY_MASK)
  195. param->flags |= X509_V_FLAG_POLICY_CHECK;
  196. return 1;
  197. }
  198. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
  199. unsigned long flags)
  200. {
  201. param->flags &= ~flags;
  202. return 1;
  203. }
  204. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  205. {
  206. return param->flags;
  207. }
  208. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  209. {
  210. return X509_PURPOSE_set(&param->purpose, purpose);
  211. }
  212. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  213. {
  214. return X509_TRUST_set(&param->trust, trust);
  215. }
  216. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  217. {
  218. param->depth = depth;
  219. }
  220. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  221. {
  222. param->check_time = t;
  223. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  224. }
  225. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
  226. ASN1_OBJECT *policy)
  227. {
  228. if (!param->policies) {
  229. param->policies = sk_ASN1_OBJECT_new_null();
  230. if (!param->policies)
  231. return 0;
  232. }
  233. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  234. return 0;
  235. return 1;
  236. }
  237. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  238. STACK_OF(ASN1_OBJECT) *policies)
  239. {
  240. int i;
  241. ASN1_OBJECT *oid, *doid;
  242. if (!param)
  243. return 0;
  244. if (param->policies)
  245. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  246. if (!policies) {
  247. param->policies = NULL;
  248. return 1;
  249. }
  250. param->policies = sk_ASN1_OBJECT_new_null();
  251. if (!param->policies)
  252. return 0;
  253. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
  254. oid = sk_ASN1_OBJECT_value(policies, i);
  255. doid = OBJ_dup(oid);
  256. if (!doid)
  257. return 0;
  258. if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
  259. ASN1_OBJECT_free(doid);
  260. return 0;
  261. }
  262. }
  263. param->flags |= X509_V_FLAG_POLICY_CHECK;
  264. return 1;
  265. }
  266. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  267. {
  268. return param->depth;
  269. }
  270. /*
  271. * Default verify parameters: these are used for various applications and can
  272. * be overridden by the user specified table. NB: the 'name' field *must* be
  273. * in alphabetical order because it will be searched using OBJ_search.
  274. */
  275. static const X509_VERIFY_PARAM default_table[] = {
  276. {
  277. "default", /* X509 default parameters */
  278. 0, /* Check time */
  279. 0, /* internal flags */
  280. 0, /* flags */
  281. 0, /* purpose */
  282. 0, /* trust */
  283. 100, /* depth */
  284. NULL /* policies */
  285. },
  286. {
  287. "pkcs7", /* S/MIME signing parameters */
  288. 0, /* Check time */
  289. 0, /* internal flags */
  290. 0, /* flags */
  291. X509_PURPOSE_SMIME_SIGN, /* purpose */
  292. X509_TRUST_EMAIL, /* trust */
  293. -1, /* depth */
  294. NULL /* policies */
  295. },
  296. {
  297. "smime_sign", /* S/MIME signing parameters */
  298. 0, /* Check time */
  299. 0, /* internal flags */
  300. 0, /* flags */
  301. X509_PURPOSE_SMIME_SIGN, /* purpose */
  302. X509_TRUST_EMAIL, /* trust */
  303. -1, /* depth */
  304. NULL /* policies */
  305. },
  306. {
  307. "ssl_client", /* SSL/TLS client parameters */
  308. 0, /* Check time */
  309. 0, /* internal flags */
  310. 0, /* flags */
  311. X509_PURPOSE_SSL_CLIENT, /* purpose */
  312. X509_TRUST_SSL_CLIENT, /* trust */
  313. -1, /* depth */
  314. NULL /* policies */
  315. },
  316. {
  317. "ssl_server", /* SSL/TLS server parameters */
  318. 0, /* Check time */
  319. 0, /* internal flags */
  320. 0, /* flags */
  321. X509_PURPOSE_SSL_SERVER, /* purpose */
  322. X509_TRUST_SSL_SERVER, /* trust */
  323. -1, /* depth */
  324. NULL /* policies */
  325. }
  326. };
  327. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  328. static int table_cmp(const void *pa, const void *pb)
  329. {
  330. const X509_VERIFY_PARAM *a = pa, *b = pb;
  331. return strcmp(a->name, b->name);
  332. }
  333. static int param_cmp(const X509_VERIFY_PARAM *const *a,
  334. const X509_VERIFY_PARAM *const *b)
  335. {
  336. return strcmp((*a)->name, (*b)->name);
  337. }
  338. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  339. {
  340. int idx;
  341. X509_VERIFY_PARAM *ptmp;
  342. if (!param_table) {
  343. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  344. if (!param_table)
  345. return 0;
  346. } else {
  347. idx = sk_X509_VERIFY_PARAM_find(param_table, param);
  348. if (idx != -1) {
  349. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  350. X509_VERIFY_PARAM_free(ptmp);
  351. (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
  352. }
  353. }
  354. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  355. return 0;
  356. return 1;
  357. }
  358. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  359. {
  360. int idx;
  361. X509_VERIFY_PARAM pm;
  362. pm.name = (char *)name;
  363. if (param_table) {
  364. idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
  365. if (idx != -1)
  366. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  367. }
  368. return (const X509_VERIFY_PARAM *)OBJ_bsearch((char *)&pm,
  369. (char *)&default_table,
  370. sizeof(default_table) /
  371. sizeof(X509_VERIFY_PARAM),
  372. sizeof(X509_VERIFY_PARAM),
  373. table_cmp);
  374. }
  375. void X509_VERIFY_PARAM_table_cleanup(void)
  376. {
  377. if (param_table)
  378. sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free);
  379. param_table = NULL;
  380. }