x509_vpm.c 13 KB

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