x509_vpm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 overwrite or check time not set, copy across */
  157. if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME))
  158. {
  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. {
  168. if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
  169. return 0;
  170. }
  171. return 1;
  172. }
  173. int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
  174. const X509_VERIFY_PARAM *from)
  175. {
  176. to->inh_flags |= X509_VP_FLAG_DEFAULT;
  177. return X509_VERIFY_PARAM_inherit(to, from);
  178. }
  179. int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
  180. {
  181. if (param->name)
  182. OPENSSL_free(param->name);
  183. param->name = BUF_strdup(name);
  184. if (param->name)
  185. return 1;
  186. return 0;
  187. }
  188. int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  189. {
  190. param->flags |= flags;
  191. if (flags & X509_V_FLAG_POLICY_MASK)
  192. param->flags |= X509_V_FLAG_POLICY_CHECK;
  193. return 1;
  194. }
  195. int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
  196. {
  197. param->flags &= ~flags;
  198. return 1;
  199. }
  200. unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
  201. {
  202. return param->flags;
  203. }
  204. int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
  205. {
  206. return X509_PURPOSE_set(&param->purpose, purpose);
  207. }
  208. int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
  209. {
  210. return X509_TRUST_set(&param->trust, trust);
  211. }
  212. void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
  213. {
  214. param->depth = depth;
  215. }
  216. void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
  217. {
  218. param->check_time = t;
  219. param->flags |= X509_V_FLAG_USE_CHECK_TIME;
  220. }
  221. int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
  222. {
  223. if (!param->policies)
  224. {
  225. param->policies = sk_ASN1_OBJECT_new_null();
  226. if (!param->policies)
  227. return 0;
  228. }
  229. if (!sk_ASN1_OBJECT_push(param->policies, policy))
  230. return 0;
  231. return 1;
  232. }
  233. int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
  234. STACK_OF(ASN1_OBJECT) *policies)
  235. {
  236. int i;
  237. ASN1_OBJECT *oid, *doid;
  238. if (!param)
  239. return 0;
  240. if (param->policies)
  241. sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
  242. if (!policies)
  243. {
  244. param->policies = NULL;
  245. return 1;
  246. }
  247. param->policies = sk_ASN1_OBJECT_new_null();
  248. if (!param->policies)
  249. return 0;
  250. for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++)
  251. {
  252. oid = sk_ASN1_OBJECT_value(policies, i);
  253. doid = OBJ_dup(oid);
  254. if (!doid)
  255. return 0;
  256. if (!sk_ASN1_OBJECT_push(param->policies, doid))
  257. {
  258. ASN1_OBJECT_free(doid);
  259. return 0;
  260. }
  261. }
  262. param->flags |= X509_V_FLAG_POLICY_CHECK;
  263. return 1;
  264. }
  265. int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
  266. {
  267. return param->depth;
  268. }
  269. /* Default verify parameters: these are used for various
  270. * applications and can be overridden by the user specified table.
  271. * NB: the 'name' field *must* be in alphabetical order because it
  272. * will be searched using OBJ_search.
  273. */
  274. static const X509_VERIFY_PARAM default_table[] = {
  275. {
  276. "default", /* X509 default parameters */
  277. 0, /* Check time */
  278. 0, /* internal flags */
  279. 0, /* flags */
  280. 0, /* purpose */
  281. 0, /* trust */
  282. 9, /* depth */
  283. NULL /* policies */
  284. },
  285. {
  286. "pkcs7", /* SSL/TLS client parameters */
  287. 0, /* Check time */
  288. 0, /* internal flags */
  289. 0, /* flags */
  290. X509_PURPOSE_SMIME_SIGN, /* purpose */
  291. X509_TRUST_EMAIL, /* trust */
  292. -1, /* depth */
  293. NULL /* policies */
  294. },
  295. {
  296. "ssl_client", /* SSL/TLS client parameters */
  297. 0, /* Check time */
  298. 0, /* internal flags */
  299. 0, /* flags */
  300. X509_PURPOSE_SSL_CLIENT, /* purpose */
  301. X509_TRUST_SSL_CLIENT, /* trust */
  302. -1, /* depth */
  303. NULL /* policies */
  304. },
  305. {
  306. "ssl_server", /* SSL/TLS server parameters */
  307. 0, /* Check time */
  308. 0, /* internal flags */
  309. 0, /* flags */
  310. X509_PURPOSE_SSL_SERVER, /* purpose */
  311. X509_TRUST_SSL_SERVER, /* trust */
  312. -1, /* depth */
  313. NULL /* policies */
  314. }};
  315. static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
  316. static int table_cmp(const void *pa, const void *pb)
  317. {
  318. const X509_VERIFY_PARAM *a = pa, *b = pb;
  319. return strcmp(a->name, b->name);
  320. }
  321. static int param_cmp(const X509_VERIFY_PARAM * const *a,
  322. const X509_VERIFY_PARAM * const *b)
  323. {
  324. return strcmp((*a)->name, (*b)->name);
  325. }
  326. int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
  327. {
  328. int idx;
  329. X509_VERIFY_PARAM *ptmp;
  330. if (!param_table)
  331. {
  332. param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
  333. if (!param_table)
  334. return 0;
  335. }
  336. else
  337. {
  338. idx = sk_X509_VERIFY_PARAM_find(param_table, param);
  339. if (idx != -1)
  340. {
  341. ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
  342. X509_VERIFY_PARAM_free(ptmp);
  343. sk_X509_VERIFY_PARAM_delete(param_table, idx);
  344. }
  345. }
  346. if (!sk_X509_VERIFY_PARAM_push(param_table, param))
  347. return 0;
  348. return 1;
  349. }
  350. const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
  351. {
  352. int idx;
  353. X509_VERIFY_PARAM pm;
  354. pm.name = (char *)name;
  355. if (param_table)
  356. {
  357. idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
  358. if (idx != -1)
  359. return sk_X509_VERIFY_PARAM_value(param_table, idx);
  360. }
  361. return (const X509_VERIFY_PARAM *) OBJ_bsearch((char *)&pm,
  362. (char *)&default_table,
  363. sizeof(default_table)/sizeof(X509_VERIFY_PARAM),
  364. sizeof(X509_VERIFY_PARAM),
  365. table_cmp);
  366. }
  367. void X509_VERIFY_PARAM_table_cleanup(void)
  368. {
  369. if (param_table)
  370. sk_X509_VERIFY_PARAM_pop_free(param_table,
  371. X509_VERIFY_PARAM_free);
  372. param_table = NULL;
  373. }