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.
 
 
 
 
 
 

300 rivejä
8.1 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2004.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/mem.h>
  56. #include <openssl/obj.h>
  57. #include <openssl/thread.h>
  58. #include <openssl/x509.h>
  59. #include <openssl/x509v3.h>
  60. #include "pcy_int.h"
  61. #include "../internal.h"
  62. static int policy_data_cmp(const X509_POLICY_DATA **a,
  63. const X509_POLICY_DATA **b);
  64. static int policy_cache_set_int(long *out, ASN1_INTEGER *value);
  65. /* Set cache entry according to CertificatePolicies extension.
  66. * Note: this destroys the passed CERTIFICATEPOLICIES structure.
  67. */
  68. static int policy_cache_create(X509 *x,
  69. CERTIFICATEPOLICIES *policies, int crit)
  70. {
  71. size_t i;
  72. int ret = 0;
  73. X509_POLICY_CACHE *cache = x->policy_cache;
  74. X509_POLICY_DATA *data = NULL;
  75. POLICYINFO *policy;
  76. if (sk_POLICYINFO_num(policies) == 0)
  77. goto bad_policy;
  78. cache->data = sk_X509_POLICY_DATA_new(policy_data_cmp);
  79. if (!cache->data)
  80. goto bad_policy;
  81. for (i = 0; i < sk_POLICYINFO_num(policies); i++)
  82. {
  83. policy = sk_POLICYINFO_value(policies, i);
  84. data = policy_data_new(policy, NULL, crit);
  85. if (!data)
  86. goto bad_policy;
  87. /* Duplicate policy OIDs are illegal: reject if matches
  88. * found.
  89. */
  90. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy)
  91. {
  92. if (cache->anyPolicy)
  93. {
  94. ret = -1;
  95. goto bad_policy;
  96. }
  97. cache->anyPolicy = data;
  98. }
  99. else if (sk_X509_POLICY_DATA_find(cache->data, NULL, data))
  100. {
  101. ret = -1;
  102. goto bad_policy;
  103. }
  104. else if (!sk_X509_POLICY_DATA_push(cache->data, data))
  105. goto bad_policy;
  106. data = NULL;
  107. }
  108. ret = 1;
  109. bad_policy:
  110. if (ret == -1)
  111. x->ex_flags |= EXFLAG_INVALID_POLICY;
  112. if (data)
  113. policy_data_free(data);
  114. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  115. if (ret <= 0)
  116. {
  117. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  118. cache->data = NULL;
  119. }
  120. return ret;
  121. }
  122. static int policy_cache_new(X509 *x)
  123. {
  124. X509_POLICY_CACHE *cache;
  125. ASN1_INTEGER *ext_any = NULL;
  126. POLICY_CONSTRAINTS *ext_pcons = NULL;
  127. CERTIFICATEPOLICIES *ext_cpols = NULL;
  128. POLICY_MAPPINGS *ext_pmaps = NULL;
  129. int i;
  130. cache = OPENSSL_malloc(sizeof(X509_POLICY_CACHE));
  131. if (!cache)
  132. return 0;
  133. cache->anyPolicy = NULL;
  134. cache->data = NULL;
  135. cache->any_skip = -1;
  136. cache->explicit_skip = -1;
  137. cache->map_skip = -1;
  138. x->policy_cache = cache;
  139. /* Handle requireExplicitPolicy *first*. Need to process this
  140. * even if we don't have any policies.
  141. */
  142. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  143. if (!ext_pcons)
  144. {
  145. if (i != -1)
  146. goto bad_cache;
  147. }
  148. else
  149. {
  150. if (!ext_pcons->requireExplicitPolicy
  151. && !ext_pcons->inhibitPolicyMapping)
  152. goto bad_cache;
  153. if (!policy_cache_set_int(&cache->explicit_skip,
  154. ext_pcons->requireExplicitPolicy))
  155. goto bad_cache;
  156. if (!policy_cache_set_int(&cache->map_skip,
  157. ext_pcons->inhibitPolicyMapping))
  158. goto bad_cache;
  159. }
  160. /* Process CertificatePolicies */
  161. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  162. /* If no CertificatePolicies extension or problem decoding then
  163. * there is no point continuing because the valid policies will be
  164. * NULL.
  165. */
  166. if (!ext_cpols)
  167. {
  168. /* If not absent some problem with extension */
  169. if (i != -1)
  170. goto bad_cache;
  171. return 1;
  172. }
  173. i = policy_cache_create(x, ext_cpols, i);
  174. /* NB: ext_cpols freed by policy_cache_set_policies */
  175. if (i <= 0)
  176. return i;
  177. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  178. if (!ext_pmaps)
  179. {
  180. /* If not absent some problem with extension */
  181. if (i != -1)
  182. goto bad_cache;
  183. }
  184. else
  185. {
  186. i = policy_cache_set_mapping(x, ext_pmaps);
  187. if (i <= 0)
  188. goto bad_cache;
  189. }
  190. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  191. if (!ext_any)
  192. {
  193. if (i != -1)
  194. goto bad_cache;
  195. }
  196. else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  197. goto bad_cache;
  198. if (0)
  199. {
  200. bad_cache:
  201. x->ex_flags |= EXFLAG_INVALID_POLICY;
  202. }
  203. if(ext_pcons)
  204. POLICY_CONSTRAINTS_free(ext_pcons);
  205. if (ext_any)
  206. ASN1_INTEGER_free(ext_any);
  207. return 1;
  208. }
  209. void policy_cache_free(X509_POLICY_CACHE *cache)
  210. {
  211. if (!cache)
  212. return;
  213. if (cache->anyPolicy)
  214. policy_data_free(cache->anyPolicy);
  215. if (cache->data)
  216. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  217. OPENSSL_free(cache);
  218. }
  219. /* g_x509_policy_cache_lock is used to protect against concurrent calls to
  220. * |policy_cache_new|. Ideally this would be done with a |CRYPTO_once_t|
  221. * in the |X509| structure, but |CRYPTO_once_t| isn't public. */
  222. static struct CRYPTO_STATIC_MUTEX g_x509_policy_cache_lock =
  223. CRYPTO_STATIC_MUTEX_INIT;
  224. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  225. {
  226. X509_POLICY_CACHE *cache;
  227. CRYPTO_STATIC_MUTEX_lock_read(&g_x509_policy_cache_lock);
  228. cache = x->policy_cache;
  229. CRYPTO_STATIC_MUTEX_unlock(&g_x509_policy_cache_lock);
  230. if (cache != NULL)
  231. return cache;
  232. CRYPTO_STATIC_MUTEX_lock_write(&g_x509_policy_cache_lock);
  233. if (x->policy_cache == NULL)
  234. policy_cache_new(x);
  235. cache = x->policy_cache;
  236. CRYPTO_STATIC_MUTEX_unlock(&g_x509_policy_cache_lock);
  237. return cache;
  238. }
  239. X509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache,
  240. const ASN1_OBJECT *id)
  241. {
  242. size_t idx;
  243. X509_POLICY_DATA tmp;
  244. tmp.valid_policy = (ASN1_OBJECT *)id;
  245. if (!sk_X509_POLICY_DATA_find(cache->data, &idx, &tmp))
  246. return NULL;
  247. return sk_X509_POLICY_DATA_value(cache->data, idx);
  248. }
  249. static int policy_data_cmp(const X509_POLICY_DATA **a,
  250. const X509_POLICY_DATA **b)
  251. {
  252. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  253. }
  254. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  255. {
  256. if (value == NULL)
  257. return 1;
  258. if (value->type == V_ASN1_NEG_INTEGER)
  259. return 0;
  260. *out = ASN1_INTEGER_get(value);
  261. return 1;
  262. }