pcy_cache.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/x509_int.h"
  13. #include "pcy_int.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;
  25. int ret = 0;
  26. X509_POLICY_CACHE *cache = x->policy_cache;
  27. X509_POLICY_DATA *data = NULL;
  28. POLICYINFO *policy;
  29. if (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. goto bad_policy;
  34. for (i = 0; i < sk_POLICYINFO_num(policies); i++) {
  35. policy = sk_POLICYINFO_value(policies, i);
  36. data = policy_data_new(policy, NULL, crit);
  37. if (data == NULL)
  38. goto bad_policy;
  39. /*
  40. * Duplicate policy OIDs are illegal: reject if matches found.
  41. */
  42. if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
  43. if (cache->anyPolicy) {
  44. ret = -1;
  45. goto bad_policy;
  46. }
  47. cache->anyPolicy = data;
  48. } else if (sk_X509_POLICY_DATA_find(cache->data, data) != -1) {
  49. ret = -1;
  50. goto bad_policy;
  51. } else if (!sk_X509_POLICY_DATA_push(cache->data, data))
  52. goto bad_policy;
  53. data = NULL;
  54. }
  55. ret = 1;
  56. bad_policy:
  57. if (ret == -1)
  58. x->ex_flags |= EXFLAG_INVALID_POLICY;
  59. policy_data_free(data);
  60. sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
  61. if (ret <= 0) {
  62. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  63. cache->data = NULL;
  64. }
  65. return ret;
  66. }
  67. static int policy_cache_new(X509 *x)
  68. {
  69. X509_POLICY_CACHE *cache;
  70. ASN1_INTEGER *ext_any = NULL;
  71. POLICY_CONSTRAINTS *ext_pcons = NULL;
  72. CERTIFICATEPOLICIES *ext_cpols = NULL;
  73. POLICY_MAPPINGS *ext_pmaps = NULL;
  74. int i;
  75. if (x->policy_cache != NULL)
  76. return 1;
  77. cache = OPENSSL_malloc(sizeof(*cache));
  78. if (cache == NULL)
  79. return 0;
  80. cache->anyPolicy = NULL;
  81. cache->data = NULL;
  82. cache->any_skip = -1;
  83. cache->explicit_skip = -1;
  84. cache->map_skip = -1;
  85. x->policy_cache = cache;
  86. /*
  87. * Handle requireExplicitPolicy *first*. Need to process this even if we
  88. * don't have any policies.
  89. */
  90. ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL);
  91. if (!ext_pcons) {
  92. if (i != -1)
  93. goto bad_cache;
  94. } else {
  95. if (!ext_pcons->requireExplicitPolicy
  96. && !ext_pcons->inhibitPolicyMapping)
  97. goto bad_cache;
  98. if (!policy_cache_set_int(&cache->explicit_skip,
  99. ext_pcons->requireExplicitPolicy))
  100. goto bad_cache;
  101. if (!policy_cache_set_int(&cache->map_skip,
  102. ext_pcons->inhibitPolicyMapping))
  103. goto bad_cache;
  104. }
  105. /* Process CertificatePolicies */
  106. ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL);
  107. /*
  108. * If no CertificatePolicies extension or problem decoding then there is
  109. * no point continuing because the valid policies will be NULL.
  110. */
  111. if (!ext_cpols) {
  112. /* If not absent some problem with extension */
  113. if (i != -1)
  114. goto bad_cache;
  115. return 1;
  116. }
  117. i = policy_cache_create(x, ext_cpols, i);
  118. /* NB: ext_cpols freed by policy_cache_set_policies */
  119. if (i <= 0)
  120. return i;
  121. ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL);
  122. if (!ext_pmaps) {
  123. /* If not absent some problem with extension */
  124. if (i != -1)
  125. goto bad_cache;
  126. } else {
  127. i = policy_cache_set_mapping(x, ext_pmaps);
  128. if (i <= 0)
  129. goto bad_cache;
  130. }
  131. ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL);
  132. if (!ext_any) {
  133. if (i != -1)
  134. goto bad_cache;
  135. } else if (!policy_cache_set_int(&cache->any_skip, ext_any))
  136. goto bad_cache;
  137. goto just_cleanup;
  138. bad_cache:
  139. x->ex_flags |= EXFLAG_INVALID_POLICY;
  140. just_cleanup:
  141. POLICY_CONSTRAINTS_free(ext_pcons);
  142. ASN1_INTEGER_free(ext_any);
  143. return 1;
  144. }
  145. void policy_cache_free(X509_POLICY_CACHE *cache)
  146. {
  147. if (!cache)
  148. return;
  149. policy_data_free(cache->anyPolicy);
  150. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  151. OPENSSL_free(cache);
  152. }
  153. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  154. {
  155. if (x->policy_cache == NULL) {
  156. CRYPTO_THREAD_write_lock(x->lock);
  157. policy_cache_new(x);
  158. CRYPTO_THREAD_unlock(x->lock);
  159. }
  160. return x->policy_cache;
  161. }
  162. X509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache,
  163. const ASN1_OBJECT *id)
  164. {
  165. int idx;
  166. X509_POLICY_DATA tmp;
  167. tmp.valid_policy = (ASN1_OBJECT *)id;
  168. idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
  169. if (idx == -1)
  170. return NULL;
  171. return sk_X509_POLICY_DATA_value(cache->data, idx);
  172. }
  173. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  174. const X509_POLICY_DATA *const *b)
  175. {
  176. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  177. }
  178. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  179. {
  180. if (value == NULL)
  181. return 1;
  182. if (value->type == V_ASN1_NEG_INTEGER)
  183. return 0;
  184. *out = ASN1_INTEGER_get(value);
  185. return 1;
  186. }