pcy_cache.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 2004-2020 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. DEFINE_STACK_OF(POLICYINFO)
  15. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  16. const X509_POLICY_DATA *const *b);
  17. static int policy_cache_set_int(long *out, ASN1_INTEGER *value);
  18. /*
  19. * Set cache entry according to CertificatePolicies extension. Note: this
  20. * destroys the passed CERTIFICATEPOLICIES structure.
  21. */
  22. static int policy_cache_create(X509 *x,
  23. CERTIFICATEPOLICIES *policies, int crit)
  24. {
  25. int i, num, ret = 0;
  26. X509_POLICY_CACHE *cache = x->policy_cache;
  27. X509_POLICY_DATA *data = NULL;
  28. POLICYINFO *policy;
  29. if ((num = sk_POLICYINFO_num(policies)) <= 0)
  30. goto bad_policy;
  31. cache->data = sk_X509_POLICY_DATA_new(policy_data_cmp);
  32. if (cache->data == NULL) {
  33. X509V3err(X509V3_F_POLICY_CACHE_CREATE, ERR_R_MALLOC_FAILURE);
  34. goto just_cleanup;
  35. }
  36. for (i = 0; i < num; i++) {
  37. policy = sk_POLICYINFO_value(policies, i);
  38. data = policy_data_new(policy, NULL, crit);
  39. if (data == NULL) {
  40. X509V3err(X509V3_F_POLICY_CACHE_CREATE, ERR_R_MALLOC_FAILURE);
  41. goto just_cleanup;
  42. }
  43. /*
  44. * Duplicate policy OIDs are illegal: reject if matches found.
  45. */
  46. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
  47. if (cache->anyPolicy) {
  48. ret = -1;
  49. goto bad_policy;
  50. }
  51. cache->anyPolicy = data;
  52. } else if (sk_X509_POLICY_DATA_find(cache->data, data) >=0 ) {
  53. ret = -1;
  54. goto bad_policy;
  55. } else if (!sk_X509_POLICY_DATA_push(cache->data, data)) {
  56. X509V3err(X509V3_F_POLICY_CACHE_CREATE, ERR_R_MALLOC_FAILURE);
  57. goto bad_policy;
  58. }
  59. data = NULL;
  60. }
  61. ret = 1;
  62. bad_policy:
  63. if (ret == -1)
  64. x->ex_flags |= EXFLAG_INVALID_POLICY;
  65. policy_data_free(data);
  66. just_cleanup:
  67. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  68. if (ret <= 0) {
  69. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  70. cache->data = NULL;
  71. }
  72. return ret;
  73. }
  74. static int policy_cache_new(X509 *x)
  75. {
  76. X509_POLICY_CACHE *cache;
  77. ASN1_INTEGER *ext_any = NULL;
  78. POLICY_CONSTRAINTS *ext_pcons = NULL;
  79. CERTIFICATEPOLICIES *ext_cpols = NULL;
  80. POLICY_MAPPINGS *ext_pmaps = NULL;
  81. int i;
  82. if (x->policy_cache != NULL)
  83. return 1;
  84. cache = OPENSSL_malloc(sizeof(*cache));
  85. if (cache == NULL) {
  86. X509V3err(X509V3_F_POLICY_CACHE_NEW, ERR_R_MALLOC_FAILURE);
  87. return 0;
  88. }
  89. cache->anyPolicy = NULL;
  90. cache->data = NULL;
  91. cache->any_skip = -1;
  92. cache->explicit_skip = -1;
  93. cache->map_skip = -1;
  94. x->policy_cache = cache;
  95. /*
  96. * Handle requireExplicitPolicy *first*. Need to process this even if we
  97. * don't have any policies.
  98. */
  99. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  100. if (!ext_pcons) {
  101. if (i != -1)
  102. goto bad_cache;
  103. } else {
  104. if (!ext_pcons->requireExplicitPolicy
  105. && !ext_pcons->inhibitPolicyMapping)
  106. goto bad_cache;
  107. if (!policy_cache_set_int(&cache->explicit_skip,
  108. ext_pcons->requireExplicitPolicy))
  109. goto bad_cache;
  110. if (!policy_cache_set_int(&cache->map_skip,
  111. ext_pcons->inhibitPolicyMapping))
  112. goto bad_cache;
  113. }
  114. /* Process CertificatePolicies */
  115. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  116. /*
  117. * If no CertificatePolicies extension or problem decoding then there is
  118. * no point continuing because the valid policies will be NULL.
  119. */
  120. if (!ext_cpols) {
  121. /* If not absent some problem with extension */
  122. if (i != -1)
  123. goto bad_cache;
  124. return 1;
  125. }
  126. i = policy_cache_create(x, ext_cpols, i);
  127. /* NB: ext_cpols freed by policy_cache_set_policies */
  128. if (i <= 0)
  129. return i;
  130. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  131. if (!ext_pmaps) {
  132. /* If not absent some problem with extension */
  133. if (i != -1)
  134. goto bad_cache;
  135. } else {
  136. i = policy_cache_set_mapping(x, ext_pmaps);
  137. if (i <= 0)
  138. goto bad_cache;
  139. }
  140. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  141. if (!ext_any) {
  142. if (i != -1)
  143. goto bad_cache;
  144. } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  145. goto bad_cache;
  146. goto just_cleanup;
  147. bad_cache:
  148. x->ex_flags |= EXFLAG_INVALID_POLICY;
  149. just_cleanup:
  150. POLICY_CONSTRAINTS_free(ext_pcons);
  151. ASN1_INTEGER_free(ext_any);
  152. return 1;
  153. }
  154. void policy_cache_free(X509_POLICY_CACHE *cache)
  155. {
  156. if (!cache)
  157. return;
  158. policy_data_free(cache->anyPolicy);
  159. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  160. OPENSSL_free(cache);
  161. }
  162. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  163. {
  164. if (x->policy_cache == NULL) {
  165. CRYPTO_THREAD_write_lock(x->lock);
  166. policy_cache_new(x);
  167. CRYPTO_THREAD_unlock(x->lock);
  168. }
  169. return x->policy_cache;
  170. }
  171. X509_POLICY_DATA *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. }