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.
 
 
 
 
 
 

287 regels
9.0 KiB

  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 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. #include <openssl/mem.h>
  57. #include <openssl/obj.h>
  58. #include <openssl/thread.h>
  59. #include <openssl/x509.h>
  60. #include <openssl/x509v3.h>
  61. #include "pcy_int.h"
  62. #include "../internal.h"
  63. static int policy_data_cmp(const X509_POLICY_DATA **a,
  64. const X509_POLICY_DATA **b);
  65. static int policy_cache_set_int(long *out, ASN1_INTEGER *value);
  66. /*
  67. * Set cache entry according to CertificatePolicies extension. Note: this
  68. * destroys the passed CERTIFICATEPOLICIES structure.
  69. */
  70. static int policy_cache_create(X509 *x,
  71. CERTIFICATEPOLICIES *policies, int crit)
  72. {
  73. size_t i;
  74. int ret = 0;
  75. X509_POLICY_CACHE *cache = x->policy_cache;
  76. X509_POLICY_DATA *data = NULL;
  77. POLICYINFO *policy;
  78. if (sk_POLICYINFO_num(policies) == 0)
  79. goto bad_policy;
  80. cache->data = sk_X509_POLICY_DATA_new(policy_data_cmp);
  81. if (!cache->data)
  82. goto bad_policy;
  83. for (i = 0; i < sk_POLICYINFO_num(policies); i++) {
  84. policy = sk_POLICYINFO_value(policies, i);
  85. data = policy_data_new(policy, NULL, crit);
  86. if (!data)
  87. goto bad_policy;
  88. /*
  89. * Duplicate policy OIDs are illegal: reject if matches found.
  90. */
  91. sk_X509_POLICY_DATA_sort(cache->data);
  92. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
  93. if (cache->anyPolicy) {
  94. ret = -1;
  95. goto bad_policy;
  96. }
  97. cache->anyPolicy = data;
  98. } else if (sk_X509_POLICY_DATA_find(cache->data, NULL, data)) {
  99. ret = -1;
  100. goto bad_policy;
  101. } else if (!sk_X509_POLICY_DATA_push(cache->data, data))
  102. goto bad_policy;
  103. data = NULL;
  104. }
  105. ret = 1;
  106. bad_policy:
  107. if (ret == -1)
  108. x->ex_flags |= EXFLAG_INVALID_POLICY;
  109. if (data)
  110. policy_data_free(data);
  111. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  112. if (ret <= 0) {
  113. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  114. cache->data = NULL;
  115. }
  116. return ret;
  117. }
  118. static int policy_cache_new(X509 *x)
  119. {
  120. X509_POLICY_CACHE *cache;
  121. ASN1_INTEGER *ext_any = NULL;
  122. POLICY_CONSTRAINTS *ext_pcons = NULL;
  123. CERTIFICATEPOLICIES *ext_cpols = NULL;
  124. POLICY_MAPPINGS *ext_pmaps = NULL;
  125. int i;
  126. cache = OPENSSL_malloc(sizeof(X509_POLICY_CACHE));
  127. if (!cache)
  128. return 0;
  129. cache->anyPolicy = NULL;
  130. cache->data = NULL;
  131. cache->any_skip = -1;
  132. cache->explicit_skip = -1;
  133. cache->map_skip = -1;
  134. x->policy_cache = cache;
  135. /*
  136. * Handle requireExplicitPolicy *first*. Need to process this even if we
  137. * don't have any policies.
  138. */
  139. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  140. if (!ext_pcons) {
  141. if (i != -1)
  142. goto bad_cache;
  143. } else {
  144. if (!ext_pcons->requireExplicitPolicy
  145. && !ext_pcons->inhibitPolicyMapping)
  146. goto bad_cache;
  147. if (!policy_cache_set_int(&cache->explicit_skip,
  148. ext_pcons->requireExplicitPolicy))
  149. goto bad_cache;
  150. if (!policy_cache_set_int(&cache->map_skip,
  151. ext_pcons->inhibitPolicyMapping))
  152. goto bad_cache;
  153. }
  154. /* Process CertificatePolicies */
  155. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  156. /*
  157. * If no CertificatePolicies extension or problem decoding then there is
  158. * no point continuing because the valid policies will be NULL.
  159. */
  160. if (!ext_cpols) {
  161. /* If not absent some problem with extension */
  162. if (i != -1)
  163. goto bad_cache;
  164. return 1;
  165. }
  166. i = policy_cache_create(x, ext_cpols, i);
  167. /* NB: ext_cpols freed by policy_cache_set_policies */
  168. if (i <= 0)
  169. return i;
  170. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  171. if (!ext_pmaps) {
  172. /* If not absent some problem with extension */
  173. if (i != -1)
  174. goto bad_cache;
  175. } else {
  176. i = policy_cache_set_mapping(x, ext_pmaps);
  177. if (i <= 0)
  178. goto bad_cache;
  179. }
  180. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  181. if (!ext_any) {
  182. if (i != -1)
  183. goto bad_cache;
  184. } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  185. goto bad_cache;
  186. if (0) {
  187. bad_cache:
  188. x->ex_flags |= EXFLAG_INVALID_POLICY;
  189. }
  190. if (ext_pcons)
  191. POLICY_CONSTRAINTS_free(ext_pcons);
  192. if (ext_any)
  193. ASN1_INTEGER_free(ext_any);
  194. return 1;
  195. }
  196. void policy_cache_free(X509_POLICY_CACHE *cache)
  197. {
  198. if (!cache)
  199. return;
  200. if (cache->anyPolicy)
  201. policy_data_free(cache->anyPolicy);
  202. if (cache->data)
  203. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  204. OPENSSL_free(cache);
  205. }
  206. /*
  207. * g_x509_policy_cache_lock is used to protect against concurrent calls to
  208. * |policy_cache_new|. Ideally this would be done with a |CRYPTO_once_t| in
  209. * the |X509| structure, but |CRYPTO_once_t| isn't public.
  210. */
  211. static struct CRYPTO_STATIC_MUTEX g_x509_policy_cache_lock =
  212. CRYPTO_STATIC_MUTEX_INIT;
  213. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  214. {
  215. X509_POLICY_CACHE *cache;
  216. CRYPTO_STATIC_MUTEX_lock_read(&g_x509_policy_cache_lock);
  217. cache = x->policy_cache;
  218. CRYPTO_STATIC_MUTEX_unlock_read(&g_x509_policy_cache_lock);
  219. if (cache != NULL)
  220. return cache;
  221. CRYPTO_STATIC_MUTEX_lock_write(&g_x509_policy_cache_lock);
  222. if (x->policy_cache == NULL)
  223. policy_cache_new(x);
  224. cache = x->policy_cache;
  225. CRYPTO_STATIC_MUTEX_unlock_write(&g_x509_policy_cache_lock);
  226. return cache;
  227. }
  228. X509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache,
  229. const ASN1_OBJECT *id)
  230. {
  231. size_t idx;
  232. X509_POLICY_DATA tmp;
  233. tmp.valid_policy = (ASN1_OBJECT *)id;
  234. sk_X509_POLICY_DATA_sort(cache->data);
  235. if (!sk_X509_POLICY_DATA_find(cache->data, &idx, &tmp))
  236. return NULL;
  237. return sk_X509_POLICY_DATA_value(cache->data, idx);
  238. }
  239. static int policy_data_cmp(const X509_POLICY_DATA **a,
  240. const X509_POLICY_DATA **b)
  241. {
  242. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  243. }
  244. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  245. {
  246. if (value == NULL)
  247. return 1;
  248. if (value->type == V_ASN1_NEG_INTEGER)
  249. return 0;
  250. *out = ASN1_INTEGER_get(value);
  251. return 1;
  252. }