pcy_cache.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2004-2021 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. ret = 1;
  61. bad_policy:
  62. if (ret == -1)
  63. x->ex_flags |= EXFLAG_INVALID_POLICY;
  64. ossl_policy_data_free(data);
  65. just_cleanup:
  66. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  67. if (ret <= 0) {
  68. sk_X509_POLICY_DATA_pop_free(cache->data, ossl_policy_data_free);
  69. cache->data = NULL;
  70. }
  71. return ret;
  72. }
  73. static int policy_cache_new(X509 *x)
  74. {
  75. X509_POLICY_CACHE *cache;
  76. ASN1_INTEGER *ext_any = NULL;
  77. POLICY_CONSTRAINTS *ext_pcons = NULL;
  78. CERTIFICATEPOLICIES *ext_cpols = NULL;
  79. POLICY_MAPPINGS *ext_pmaps = NULL;
  80. int i;
  81. if (x->policy_cache != NULL)
  82. return 1;
  83. cache = OPENSSL_malloc(sizeof(*cache));
  84. if (cache == NULL)
  85. return 0;
  86. cache->anyPolicy = NULL;
  87. cache->data = NULL;
  88. cache->any_skip = -1;
  89. cache->explicit_skip = -1;
  90. cache->map_skip = -1;
  91. x->policy_cache = cache;
  92. /*
  93. * Handle requireExplicitPolicy *first*. Need to process this even if we
  94. * don't have any policies.
  95. */
  96. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  97. if (!ext_pcons) {
  98. if (i != -1)
  99. goto bad_cache;
  100. } else {
  101. if (!ext_pcons->requireExplicitPolicy
  102. && !ext_pcons->inhibitPolicyMapping)
  103. goto bad_cache;
  104. if (!policy_cache_set_int(&cache->explicit_skip,
  105. ext_pcons->requireExplicitPolicy))
  106. goto bad_cache;
  107. if (!policy_cache_set_int(&cache->map_skip,
  108. ext_pcons->inhibitPolicyMapping))
  109. goto bad_cache;
  110. }
  111. /* Process CertificatePolicies */
  112. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  113. /*
  114. * If no CertificatePolicies extension or problem decoding then there is
  115. * no point continuing because the valid policies will be NULL.
  116. */
  117. if (!ext_cpols) {
  118. /* If not absent some problem with extension */
  119. if (i != -1)
  120. goto bad_cache;
  121. return 1;
  122. }
  123. i = policy_cache_create(x, ext_cpols, i);
  124. /* NB: ext_cpols freed by policy_cache_set_policies */
  125. if (i <= 0)
  126. return i;
  127. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  128. if (!ext_pmaps) {
  129. /* If not absent some problem with extension */
  130. if (i != -1)
  131. goto bad_cache;
  132. } else {
  133. i = ossl_policy_cache_set_mapping(x, ext_pmaps);
  134. if (i <= 0)
  135. goto bad_cache;
  136. }
  137. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  138. if (!ext_any) {
  139. if (i != -1)
  140. goto bad_cache;
  141. } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  142. goto bad_cache;
  143. goto just_cleanup;
  144. bad_cache:
  145. x->ex_flags |= EXFLAG_INVALID_POLICY;
  146. just_cleanup:
  147. POLICY_CONSTRAINTS_free(ext_pcons);
  148. ASN1_INTEGER_free(ext_any);
  149. return 1;
  150. }
  151. void ossl_policy_cache_free(X509_POLICY_CACHE *cache)
  152. {
  153. if (!cache)
  154. return;
  155. ossl_policy_data_free(cache->anyPolicy);
  156. sk_X509_POLICY_DATA_pop_free(cache->data, ossl_policy_data_free);
  157. OPENSSL_free(cache);
  158. }
  159. const X509_POLICY_CACHE *ossl_policy_cache_set(X509 *x)
  160. {
  161. if (x->policy_cache == NULL) {
  162. if (!CRYPTO_THREAD_write_lock(x->lock))
  163. return NULL;
  164. policy_cache_new(x);
  165. CRYPTO_THREAD_unlock(x->lock);
  166. }
  167. return x->policy_cache;
  168. }
  169. X509_POLICY_DATA *ossl_policy_cache_find_data(const X509_POLICY_CACHE *cache,
  170. const ASN1_OBJECT *id)
  171. {
  172. int idx;
  173. X509_POLICY_DATA tmp;
  174. tmp.valid_policy = (ASN1_OBJECT *)id;
  175. idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
  176. return sk_X509_POLICY_DATA_value(cache->data, idx);
  177. }
  178. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  179. const X509_POLICY_DATA *const *b)
  180. {
  181. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  182. }
  183. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  184. {
  185. if (value == NULL)
  186. return 1;
  187. if (value->type == V_ASN1_NEG_INTEGER)
  188. return 0;
  189. *out = ASN1_INTEGER_get(value);
  190. return 1;
  191. }