x509_vpm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* x509_vpm.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 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. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/crypto.h>
  61. #include <openssl/lhash.h>
  62. #include <openssl/buffer.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/x509v3.h>
  65. /* X509_VERIFY_PARAM functions */
  66. static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
  67. {
  68. if (!param)
  69. return;
  70. param->name = NULL;
  71. param->purpose = 0;
  72. param->trust = 0;
  73. param->inh_flags = X509_VP_FLAG_DEFAULT;
  74. param->flags = 0;
  75. param->depth = -1;
  76. if (param->policies)
  77. {
  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. /* This function determines how parameters are "inherited" from one structure
  96. * to another. There are several different ways this can happen.
  97. *
  98. * 1. If a child structure needs to have its values initialized from a parent
  99. * they are simply copied across. For example SSL_CTX copied to SSL.
  100. * 2. If the structure should take on values only if they are currently unset.
  101. * For example the values in an SSL structure will take appropriate value
  102. * for SSL servers or clients but only if the application has not set new
  103. * ones.
  104. *
  105. * The "inh_flags" field determines how this function behaves.
  106. *
  107. * Normally any values which are set in the default are not copied from the
  108. * destination and verify flags are ORed together.
  109. *
  110. * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
  111. * to the destination. Effectively the values in "to" become default values
  112. * which will be used only if nothing new is set in "from".
  113. *
  114. * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
  115. * they are set or not. Flags is still Ored though.
  116. *
  117. * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
  118. * of ORed.
  119. *
  120. * If X509_VP_FLAG_LOCKED is set then no values are copied.
  121. *
  122. * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
  123. * after the next call.
  124. */
  125. /* Macro to test if a field should be copied from src to dest */
  126. #define test_x509_verify_param_copy(field, def) \
  127. (to_overwrite || \
  128. ((src->field != def) && (to_default || (dest->field == def))))
  129. /* Macro to test and copy a field if necessary */
  130. #define x509_verify_param_copy(field, def) \
  131. if (test_x509_verify_param_copy(field, def)) \
  132. dest->field = src->field
  133. int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
  134. const X509_VERIFY_PARAM *src)
  135. {
  136. unsigned long inh_flags;
  137. int to_default, to_overwrite;
  138. if (!src)
  139. return 1;
  140. inh_flags = dest->inh_flags | src->inh_flags;
  141. if (inh_flags & X509_VP_FLAG_ONCE)
  142. dest->inh_flags = 0;
  143. if (inh_flags & X509_VP_FLAG_LOCKED)
  144. return 1;
  145. if (inh_flags & X509_VP_FLAG_DEFAULT)
  146. to_default = 1;
  147. else
  148. to_default = 0;
  149. if (inh_flags & X509_VP_FLAG_OVERWRITE)
  150. to_overwrite = 1;
  151. else
  152. to_overwrite = 0;
  153. x509_verify_param_copy(purpose, 0);
  154. x509_verify_param_copy(trust, 0);
  155. x509_verify_param_copy(depth, -1);
  156. if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
  157. dest->flags = 0;
  158. dest->flags |= src->flags;
  159. if (test_x509_verify_param_copy(policies, NULL))
  160. {
  161. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  162. return 0;
  163. }
  164. return 1;
  165. }
  166. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  167. const X509_VERIFY_PARAM *from)
  168. {
  169. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  170. return X509_VERIFY_PARAM_inherit(to, from);
  171. }
  172. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  173. {
  174. if (param->name)
  175. OPENSSL_free(param->name);
  176. param->name = BUF_strdup(name);
  177. if (param->name)
  178. return 1;
  179. return 0;
  180. }
  181. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  182. {
  183. param->flags |= flags;
  184. if (flags & X509_V_FLAG_POLICY_MASK)
  185. param->flags |= X509_V_FLAG_POLICY_CHECK;
  186. return 1;
  187. }
  188. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  189. {
  190. param->flags &= ~flags;
  191. return 1;
  192. }
  193. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  194. {
  195. return param->flags;
  196. }
  197. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  198. {
  199. return X509_PURPOSE_set(&param->purpose, purpose);
  200. }
  201. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  202. {
  203. return X509_TRUST_set(&param->trust, trust);
  204. }
  205. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  206. {
  207. param->depth = depth;
  208. }
  209. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  210. {
  211. param->check_time = t;
  212. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  213. }
  214. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
  215. {
  216. if (!param->policies)
  217. {
  218. param->policies = sk_ASN1_OBJECT_new_null();
  219. if (!param->policies)
  220. return 0;
  221. }
  222. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  223. return 0;
  224. return 1;
  225. }
  226. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  227. STACK_OF(ASN1_OBJECT) *policies)
  228. {
  229. int i;
  230. ASN1_OBJECT *oid, *doid;
  231. if (!param)
  232. return 0;
  233. if (param->policies)
  234. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  235. if (!policies)
  236. {
  237. param->policies = NULL;
  238. return 1;
  239. }
  240. param->policies = sk_ASN1_OBJECT_new_null();
  241. if (!param->policies)
  242. return 0;
  243. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++)
  244. {
  245. oid = sk_ASN1_OBJECT_value(policies, i);
  246. doid = OBJ_dup(oid);
  247. if (!doid)
  248. return 0;
  249. if (!sk_ASN1_OBJECT_push(param->policies, doid))
  250. {
  251. ASN1_OBJECT_free(doid);
  252. return 0;
  253. }
  254. }
  255. param->flags |= X509_V_FLAG_POLICY_CHECK;
  256. return 1;
  257. }
  258. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  259. {
  260. return param->depth;
  261. }
  262. /* Default verify parameters: these are used for various
  263. * applications and can be overridden by the user specified table.
  264. * NB: the 'name' field *must* be in alphabetical order because it
  265. * will be searched using OBJ_search.
  266. */
  267. static const X509_VERIFY_PARAM default_table[] = {
  268. {
  269. "default", /* X509 default parameters */
  270. 0, /* Check time */
  271. 0, /* internal flags */
  272. 0, /* flags */
  273. 0, /* purpose */
  274. 0, /* trust */
  275. 9, /* depth */
  276. NULL /* policies */
  277. },
  278. {
  279. "pkcs7", /* SSL/TLS client parameters */
  280. 0, /* Check time */
  281. 0, /* internal flags */
  282. 0, /* flags */
  283. X509_PURPOSE_SMIME_SIGN, /* purpose */
  284. X509_TRUST_EMAIL, /* trust */
  285. -1, /* depth */
  286. NULL /* policies */
  287. },
  288. {
  289. "ssl_client", /* SSL/TLS client parameters */
  290. 0, /* Check time */
  291. 0, /* internal flags */
  292. 0, /* flags */
  293. X509_PURPOSE_SSL_CLIENT, /* purpose */
  294. X509_TRUST_SSL_CLIENT, /* trust */
  295. -1, /* depth */
  296. NULL /* policies */
  297. },
  298. {
  299. "ssl_server", /* SSL/TLS server parameters */
  300. 0, /* Check time */
  301. 0, /* internal flags */
  302. 0, /* flags */
  303. X509_PURPOSE_SSL_SERVER, /* purpose */
  304. X509_TRUST_SSL_SERVER, /* trust */
  305. -1, /* depth */
  306. NULL /* policies */
  307. }};
  308. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  309. static int table_cmp(const void *pa, const void *pb)
  310. {
  311. const X509_VERIFY_PARAM *a = pa, *b = pb;
  312. return strcmp(a->name, b->name);
  313. }
  314. static int param_cmp(const X509_VERIFY_PARAM * const *a,
  315. const X509_VERIFY_PARAM * const *b)
  316. {
  317. return strcmp((*a)->name, (*b)->name);
  318. }
  319. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  320. {
  321. int idx;
  322. X509_VERIFY_PARAM *ptmp;
  323. if (!param_table)
  324. {
  325. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  326. if (!param_table)
  327. return 0;
  328. }
  329. else
  330. {
  331. idx = sk_X509_VERIFY_PARAM_find(param_table, param);
  332. if (idx != -1)
  333. {
  334. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  335. X509_VERIFY_PARAM_free(ptmp);
  336. sk_X509_VERIFY_PARAM_delete(param_table, idx);
  337. }
  338. }
  339. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  340. return 0;
  341. return 1;
  342. }
  343. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  344. {
  345. int idx;
  346. X509_VERIFY_PARAM pm;
  347. pm.name = (char *)name;
  348. if (param_table)
  349. {
  350. idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
  351. if (idx != -1)
  352. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  353. }
  354. return (const X509_VERIFY_PARAM *) OBJ_bsearch((char *)&pm,
  355. (char *)&default_table,
  356. sizeof(default_table)/sizeof(X509_VERIFY_PARAM),
  357. sizeof(X509_VERIFY_PARAM),
  358. table_cmp);
  359. }
  360. void X509_VERIFY_PARAM_table_cleanup(void)
  361. {
  362. if (param_table)
  363. sk_X509_VERIFY_PARAM_pop_free(param_table,
  364. X509_VERIFY_PARAM_free);
  365. param_table = NULL;
  366. }