pcy_cache.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2004-2018 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 "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, 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. X509V3err(X509V3_F_POLICY_CACHE_CREATE, ERR_R_MALLOC_FAILURE);
  33. goto just_cleanup;
  34. }
  35. for (i = 0; i < num; i++) {
  36. policy = sk_POLICYINFO_value(policies, i);
  37. data = policy_data_new(policy, NULL, crit);
  38. if (data == NULL) {
  39. X509V3err(X509V3_F_POLICY_CACHE_CREATE, ERR_R_MALLOC_FAILURE);
  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. X509V3err(X509V3_F_POLICY_CACHE_CREATE, ERR_R_MALLOC_FAILURE);
  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. 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, 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. X509V3err(X509V3_F_POLICY_CACHE_NEW, ERR_R_MALLOC_FAILURE);
  86. return 0;
  87. }
  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 = 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 policy_cache_free(X509_POLICY_CACHE *cache)
  154. {
  155. if (!cache)
  156. return;
  157. policy_data_free(cache->anyPolicy);
  158. sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free);
  159. OPENSSL_free(cache);
  160. }
  161. const X509_POLICY_CACHE *policy_cache_set(X509 *x)
  162. {
  163. if (x->policy_cache == NULL) {
  164. CRYPTO_THREAD_write_lock(x->lock);
  165. policy_cache_new(x);
  166. CRYPTO_THREAD_unlock(x->lock);
  167. }
  168. return x->policy_cache;
  169. }
  170. X509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache,
  171. const ASN1_OBJECT *id)
  172. {
  173. int idx;
  174. X509_POLICY_DATA tmp;
  175. tmp.valid_policy = (ASN1_OBJECT *)id;
  176. idx = sk_X509_POLICY_DATA_find(cache->data, &tmp);
  177. return sk_X509_POLICY_DATA_value(cache->data, idx);
  178. }
  179. static int policy_data_cmp(const X509_POLICY_DATA *const *a,
  180. const X509_POLICY_DATA *const *b)
  181. {
  182. return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy);
  183. }
  184. static int policy_cache_set_int(long *out, ASN1_INTEGER *value)
  185. {
  186. if (value == NULL)
  187. return 1;
  188. if (value->type == V_ASN1_NEG_INTEGER)
  189. return 0;
  190. *out = ASN1_INTEGER_get(value);
  191. return 1;
  192. }