pcy_cache.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2004-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <openssl/x509.h>
  11. #include <openssl/x509v3.h>
  12. #include "crypto/x509.h"
  13. #include "pcy_local.h"
  14. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  15. const X509_POLICY_DATA *const *b);
  16. static int policy_cache_set_int(long *out, ASN1_INTEGER *value);
  17. /*
  18. * Set cache entry according to CertificatePolicies extension. Note: this
  19. * destroys the passed CERTIFICATEPOLICIES structure.
  20. */
  21. static int policy_cache_create(X509 *x,
  22. CERTIFICATEPOLICIES *policies, int crit)
  23. {
  24. int i, num, ret = 0;
  25. X509_POLICY_CACHE *cache = x->policy_cache;
  26. X509_POLICY_DATA *data = NULL;
  27. POLICYINFO *policy;
  28. if ((num = sk_POLICYINFO_num(policies)) <= 0)
  29. goto bad_policy;
  30. cache->data = sk_X509_POLICY_DATA_new(policy_data_cmp);
  31. if (cache->data == NULL) {
  32. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  33. goto just_cleanup;
  34. }
  35. for (i = 0; i < num; i++) {
  36. policy = sk_POLICYINFO_value(policies, i);
  37. data = ossl_policy_data_new(policy, NULL, crit);
  38. if (data == NULL) {
  39. ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
  40. goto just_cleanup;
  41. }
  42. /*
  43. * Duplicate policy OIDs are illegal: reject if matches found.
  44. */
  45. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
  46. if (cache->anyPolicy) {
  47. ret = -1;
  48. goto bad_policy;
  49. }
  50. cache->anyPolicy = data;
  51. } else if (sk_X509_POLICY_DATA_find(cache->data, data) >=0) {
  52. ret = -1;
  53. goto bad_policy;
  54. } else if (!sk_X509_POLICY_DATA_push(cache->data, data)) {
  55. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  56. goto bad_policy;
  57. }
  58. data = NULL;
  59. }
  60. /* Sort so we can find more quickly */
  61. sk_X509_POLICY_DATA_sort(cache->data);
  62. ret = 1;
  63. bad_policy:
  64. if (ret == -1)
  65. x->ex_flags |= EXFLAG_INVALID_POLICY;
  66. ossl_policy_data_free(data);
  67. just_cleanup:
  68. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  69. if (ret <= 0) {
  70. sk_X509_POLICY_DATA_pop_free(cache->data, ossl_policy_data_free);
  71. cache->data = NULL;
  72. }
  73. return ret;
  74. }
  75. static int policy_cache_new(X509 *x)
  76. {
  77. X509_POLICY_CACHE *cache;
  78. ASN1_INTEGER *ext_any = NULL;
  79. POLICY_CONSTRAINTS *ext_pcons = NULL;
  80. CERTIFICATEPOLICIES *ext_cpols = NULL;
  81. POLICY_MAPPINGS *ext_pmaps = NULL;
  82. int i;
  83. if (x->policy_cache != NULL)
  84. return 1;
  85. cache = OPENSSL_malloc(sizeof(*cache));
  86. if (cache == NULL)
  87. return 0;
  88. cache->anyPolicy = NULL;
  89. cache->data = NULL;
  90. cache->any_skip = -1;
  91. cache->explicit_skip = -1;
  92. cache->map_skip = -1;
  93. x->policy_cache = cache;
  94. /*
  95. * Handle requireExplicitPolicy *first*. Need to process this even if we
  96. * don't have any policies.
  97. */
  98. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  99. if (!ext_pcons) {
  100. if (i != -1)
  101. goto bad_cache;
  102. } else {
  103. if (!ext_pcons->requireExplicitPolicy
  104. && !ext_pcons->inhibitPolicyMapping)
  105. goto bad_cache;
  106. if (!policy_cache_set_int(&cache->explicit_skip,
  107. ext_pcons->requireExplicitPolicy))
  108. goto bad_cache;
  109. if (!policy_cache_set_int(&cache->map_skip,
  110. ext_pcons->inhibitPolicyMapping))
  111. goto bad_cache;
  112. }
  113. /* Process CertificatePolicies */
  114. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  115. /*
  116. * If no CertificatePolicies extension or problem decoding then there is
  117. * no point continuing because the valid policies will be NULL.
  118. */
  119. if (!ext_cpols) {
  120. /* If not absent some problem with extension */
  121. if (i != -1)
  122. goto bad_cache;
  123. return 1;
  124. }
  125. i = policy_cache_create(x, ext_cpols, i);
  126. /* NB: ext_cpols freed by policy_cache_set_policies */
  127. if (i <= 0)
  128. return i;
  129. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  130. if (!ext_pmaps) {
  131. /* If not absent some problem with extension */
  132. if (i != -1)
  133. goto bad_cache;
  134. } else {
  135. i = ossl_policy_cache_set_mapping(x, ext_pmaps);
  136. if (i <= 0)
  137. goto bad_cache;
  138. }
  139. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  140. if (!ext_any) {
  141. if (i != -1)
  142. goto bad_cache;
  143. } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  144. goto bad_cache;
  145. goto just_cleanup;
  146. bad_cache:
  147. x->ex_flags |= EXFLAG_INVALID_POLICY;
  148. just_cleanup:
  149. POLICY_CONSTRAINTS_free(ext_pcons);
  150. ASN1_INTEGER_free(ext_any);
  151. return 1;
  152. }
  153. void ossl_policy_cache_free(X509_POLICY_CACHE *cache)
  154. {
  155. if (!cache)
  156. return;
  157. ossl_policy_data_free(cache->anyPolicy);
  158. sk_X509_POLICY_DATA_pop_free(cache->data, ossl_policy_data_free);
  159. OPENSSL_free(cache);
  160. }
  161. const X509_POLICY_CACHE *ossl_policy_cache_set(X509 *x)
  162. {
  163. if (x->policy_cache == NULL) {
  164. if (!CRYPTO_THREAD_write_lock(x->lock))
  165. return NULL;
  166. policy_cache_new(x);
  167. CRYPTO_THREAD_unlock(x->lock);
  168. }
  169. return x->policy_cache;
  170. }
  171. X509_POLICY_DATA *ossl_policy_cache_find_data(const X509_POLICY_CACHE *cache,
  172. const ASN1_OBJECT *id)
  173. {
  174. int idx;
  175. X509_POLICY_DATA tmp;
  176. tmp.valid_policy = (ASN1_OBJECT *)id;
  177. idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
  178. return sk_X509_POLICY_DATA_value(cache->data, idx);
  179. }
  180. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  181. const X509_POLICY_DATA *const *b)
  182. {
  183. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  184. }
  185. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  186. {
  187. if (value == NULL)
  188. return 1;
  189. if (value->type == V_ASN1_NEG_INTEGER)
  190. return 0;
  191. *out = ASN1_INTEGER_get(value);
  192. return 1;
  193. }