pcy_cache.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* pcy_cache.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include "internal/cryptlib.h"
  60. #include <openssl/x509.h>
  61. #include <openssl/x509v3.h>
  62. #include "pcy_int.h"
  63. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  64. const X509_POLICY_DATA *const *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. int 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. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
  92. if (cache->anyPolicy) {
  93. ret = -1;
  94. goto bad_policy;
  95. }
  96. cache->anyPolicy = data;
  97. } else if (sk_X509_POLICY_DATA_find(cache->data, data) != -1) {
  98. ret = -1;
  99. goto bad_policy;
  100. } else if (!sk_X509_POLICY_DATA_push(cache->data, data))
  101. goto bad_policy;
  102. data = NULL;
  103. }
  104. ret = 1;
  105. bad_policy:
  106. if (ret == -1)
  107. x->ex_flags |= EXFLAG_INVALID_POLICY;
  108. policy_data_free(data);
  109. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  110. if (ret <= 0) {
  111. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  112. cache->data = NULL;
  113. }
  114. return ret;
  115. }
  116. static int policy_cache_new(X509 *x)
  117. {
  118. X509_POLICY_CACHE *cache;
  119. ASN1_INTEGER *ext_any = NULL;
  120. POLICY_CONSTRAINTS *ext_pcons = NULL;
  121. CERTIFICATEPOLICIES *ext_cpols = NULL;
  122. POLICY_MAPPINGS *ext_pmaps = NULL;
  123. int i;
  124. cache = OPENSSL_malloc(sizeof(*cache));
  125. if (!cache)
  126. return 0;
  127. cache->anyPolicy = NULL;
  128. cache->data = NULL;
  129. cache->any_skip = -1;
  130. cache->explicit_skip = -1;
  131. cache->map_skip = -1;
  132. x->policy_cache = cache;
  133. /*
  134. * Handle requireExplicitPolicy *first*. Need to process this even if we
  135. * don't have any policies.
  136. */
  137. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  138. if (!ext_pcons) {
  139. if (i != -1)
  140. goto bad_cache;
  141. } else {
  142. if (!ext_pcons->requireExplicitPolicy
  143. && !ext_pcons->inhibitPolicyMapping)
  144. goto bad_cache;
  145. if (!policy_cache_set_int(&cache->explicit_skip,
  146. ext_pcons->requireExplicitPolicy))
  147. goto bad_cache;
  148. if (!policy_cache_set_int(&cache->map_skip,
  149. ext_pcons->inhibitPolicyMapping))
  150. goto bad_cache;
  151. }
  152. /* Process CertificatePolicies */
  153. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  154. /*
  155. * If no CertificatePolicies extension or problem decoding then there is
  156. * no point continuing because the valid policies will be NULL.
  157. */
  158. if (!ext_cpols) {
  159. /* If not absent some problem with extension */
  160. if (i != -1)
  161. goto bad_cache;
  162. return 1;
  163. }
  164. i = policy_cache_create(x, ext_cpols, i);
  165. /* NB: ext_cpols freed by policy_cache_set_policies */
  166. if (i <= 0)
  167. return i;
  168. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  169. if (!ext_pmaps) {
  170. /* If not absent some problem with extension */
  171. if (i != -1)
  172. goto bad_cache;
  173. } else {
  174. i = policy_cache_set_mapping(x, ext_pmaps);
  175. if (i <= 0)
  176. goto bad_cache;
  177. }
  178. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  179. if (!ext_any) {
  180. if (i != -1)
  181. goto bad_cache;
  182. } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  183. goto bad_cache;
  184. goto just_cleanup;
  185. bad_cache:
  186. x->ex_flags |= EXFLAG_INVALID_POLICY;
  187. just_cleanup:
  188. POLICY_CONSTRAINTS_free(ext_pcons);
  189. ASN1_INTEGER_free(ext_any);
  190. return 1;
  191. }
  192. void policy_cache_free(X509_POLICY_CACHE *cache)
  193. {
  194. if (!cache)
  195. return;
  196. policy_data_free(cache->anyPolicy);
  197. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  198. OPENSSL_free(cache);
  199. }
  200. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  201. {
  202. if (x->policy_cache == NULL) {
  203. CRYPTO_w_lock(CRYPTO_LOCK_X509);
  204. policy_cache_new(x);
  205. CRYPTO_w_unlock(CRYPTO_LOCK_X509);
  206. }
  207. return x->policy_cache;
  208. }
  209. X509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache,
  210. const ASN1_OBJECT *id)
  211. {
  212. int idx;
  213. X509_POLICY_DATA tmp;
  214. tmp.valid_policy = (ASN1_OBJECT *)id;
  215. idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
  216. if (idx == -1)
  217. return NULL;
  218. return sk_X509_POLICY_DATA_value(cache->data, idx);
  219. }
  220. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  221. const X509_POLICY_DATA *const *b)
  222. {
  223. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  224. }
  225. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  226. {
  227. if (value == NULL)
  228. return 1;
  229. if (value->type == V_ASN1_NEG_INTEGER)
  230. return 0;
  231. *out = ASN1_INTEGER_get(value);
  232. return 1;
  233. }