pcy_cache.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* pcy_cache.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 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. *
  57. */
  58. #include "cryptlib.h"
  59. #include <openssl/x509.h>
  60. #include <openssl/x509v3.h>
  61. #include "pcy_int.h"
  62. static int policy_data_cmp(const X509_POLICY_DATA * const *a,
  63. const X509_POLICY_DATA * const *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. int 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, data) != -1)
  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->maps = NULL;
  136. cache->any_skip = -1;
  137. cache->explicit_skip = -1;
  138. cache->map_skip = -1;
  139. x->policy_cache = cache;
  140. /* Handle requireExplicitPolicy *first*. Need to process this
  141. * even if we don't have any policies.
  142. */
  143. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  144. if (!ext_pcons)
  145. {
  146. if (i != -1)
  147. goto bad_cache;
  148. }
  149. else
  150. {
  151. if (!ext_pcons->requireExplicitPolicy
  152. && !ext_pcons->inhibitPolicyMapping)
  153. goto bad_cache;
  154. if (!policy_cache_set_int(&cache->explicit_skip,
  155. ext_pcons->requireExplicitPolicy))
  156. goto bad_cache;
  157. if (!policy_cache_set_int(&cache->map_skip,
  158. ext_pcons->inhibitPolicyMapping))
  159. goto bad_cache;
  160. }
  161. /* Process CertificatePolicies */
  162. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  163. /* If no CertificatePolicies extension or problem decoding then
  164. * there is no point continuing because the valid policies will be
  165. * NULL.
  166. */
  167. if (!ext_cpols)
  168. {
  169. /* If not absent some problem with extension */
  170. if (i != -1)
  171. goto bad_cache;
  172. return 1;
  173. }
  174. i = policy_cache_create(x, ext_cpols, i);
  175. /* NB: ext_cpols freed by policy_cache_set_policies */
  176. if (i <= 0)
  177. return i;
  178. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  179. if (!ext_pmaps)
  180. {
  181. /* If not absent some problem with extension */
  182. if (i != -1)
  183. goto bad_cache;
  184. }
  185. else
  186. {
  187. i = policy_cache_set_mapping(x, ext_pmaps);
  188. if (i <= 0)
  189. goto bad_cache;
  190. }
  191. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  192. if (!ext_any)
  193. {
  194. if (i != -1)
  195. goto bad_cache;
  196. }
  197. else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  198. goto bad_cache;
  199. if (0)
  200. {
  201. bad_cache:
  202. x->ex_flags |= EXFLAG_INVALID_POLICY;
  203. }
  204. if(ext_pcons)
  205. POLICY_CONSTRAINTS_free(ext_pcons);
  206. if (ext_any)
  207. ASN1_INTEGER_free(ext_any);
  208. return 1;
  209. }
  210. void policy_cache_free(X509_POLICY_CACHE *cache)
  211. {
  212. if (!cache)
  213. return;
  214. if (cache->anyPolicy)
  215. policy_data_free(cache->anyPolicy);
  216. if (cache->data)
  217. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  218. OPENSSL_free(cache);
  219. }
  220. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  221. {
  222. if (x->policy_cache == NULL)
  223. {
  224. CRYPTO_w_lock(CRYPTO_LOCK_X509);
  225. policy_cache_new(x);
  226. CRYPTO_w_unlock(CRYPTO_LOCK_X509);
  227. }
  228. return x->policy_cache;
  229. }
  230. X509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache,
  231. const ASN1_OBJECT *id)
  232. {
  233. int idx;
  234. X509_POLICY_DATA tmp;
  235. tmp.valid_policy = (ASN1_OBJECT *)id;
  236. idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
  237. if (idx == -1)
  238. return NULL;
  239. return sk_X509_POLICY_DATA_value(cache->data, idx);
  240. }
  241. static int policy_data_cmp(const X509_POLICY_DATA * const *a,
  242. const X509_POLICY_DATA * const *b)
  243. {
  244. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  245. }
  246. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  247. {
  248. if (value == NULL)
  249. return 1;
  250. if (value->type == V_ASN1_NEG_INTEGER)
  251. return 0;
  252. *out = ASN1_INTEGER_get(value);
  253. return 1;
  254. }