No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

pcy_cache.c 7.5 KiB

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