1160814Ssimon/* pcy_cache.c */ 2296465Sdelphij/* 3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4296465Sdelphij * 2004. 5160814Ssimon */ 6160814Ssimon/* ==================================================================== 7160814Ssimon * Copyright (c) 2004 The OpenSSL Project. All rights reserved. 8160814Ssimon * 9160814Ssimon * Redistribution and use in source and binary forms, with or without 10160814Ssimon * modification, are permitted provided that the following conditions 11160814Ssimon * are met: 12160814Ssimon * 13160814Ssimon * 1. Redistributions of source code must retain the above copyright 14296465Sdelphij * notice, this list of conditions and the following disclaimer. 15160814Ssimon * 16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 17160814Ssimon * notice, this list of conditions and the following disclaimer in 18160814Ssimon * the documentation and/or other materials provided with the 19160814Ssimon * distribution. 20160814Ssimon * 21160814Ssimon * 3. All advertising materials mentioning features or use of this 22160814Ssimon * software must display the following acknowledgment: 23160814Ssimon * "This product includes software developed by the OpenSSL Project 24160814Ssimon * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25160814Ssimon * 26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27160814Ssimon * endorse or promote products derived from this software without 28160814Ssimon * prior written permission. For written permission, please contact 29160814Ssimon * licensing@OpenSSL.org. 30160814Ssimon * 31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL" 32160814Ssimon * nor may "OpenSSL" appear in their names without prior written 33160814Ssimon * permission of the OpenSSL Project. 34160814Ssimon * 35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following 36160814Ssimon * acknowledgment: 37160814Ssimon * "This product includes software developed by the OpenSSL Project 38160814Ssimon * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39160814Ssimon * 40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43160814Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 52160814Ssimon * ==================================================================== 53160814Ssimon * 54160814Ssimon * This product includes cryptographic software written by Eric Young 55160814Ssimon * (eay@cryptsoft.com). This product includes software written by Tim 56160814Ssimon * Hudson (tjh@cryptsoft.com). 57160814Ssimon * 58160814Ssimon */ 59160814Ssimon 60160814Ssimon#include "cryptlib.h" 61160814Ssimon#include <openssl/x509.h> 62160814Ssimon#include <openssl/x509v3.h> 63160814Ssimon 64160814Ssimon#include "pcy_int.h" 65160814Ssimon 66296465Sdelphijstatic int policy_data_cmp(const X509_POLICY_DATA *const *a, 67296465Sdelphij const X509_POLICY_DATA *const *b); 68160814Ssimonstatic int policy_cache_set_int(long *out, ASN1_INTEGER *value); 69160814Ssimon 70296465Sdelphij/* 71296465Sdelphij * Set cache entry according to CertificatePolicies extension. Note: this 72296465Sdelphij * destroys the passed CERTIFICATEPOLICIES structure. 73160814Ssimon */ 74160814Ssimon 75160814Ssimonstatic int policy_cache_create(X509 *x, 76296465Sdelphij CERTIFICATEPOLICIES *policies, int crit) 77296465Sdelphij{ 78296465Sdelphij int i; 79296465Sdelphij int ret = 0; 80296465Sdelphij X509_POLICY_CACHE *cache = x->policy_cache; 81296465Sdelphij X509_POLICY_DATA *data = NULL; 82296465Sdelphij POLICYINFO *policy; 83296465Sdelphij if (sk_POLICYINFO_num(policies) == 0) 84296465Sdelphij goto bad_policy; 85296465Sdelphij cache->data = sk_X509_POLICY_DATA_new(policy_data_cmp); 86296465Sdelphij if (!cache->data) 87296465Sdelphij goto bad_policy; 88296465Sdelphij for (i = 0; i < sk_POLICYINFO_num(policies); i++) { 89296465Sdelphij policy = sk_POLICYINFO_value(policies, i); 90296465Sdelphij data = policy_data_new(policy, NULL, crit); 91296465Sdelphij if (!data) 92296465Sdelphij goto bad_policy; 93296465Sdelphij /* 94296465Sdelphij * Duplicate policy OIDs are illegal: reject if matches found. 95296465Sdelphij */ 96296465Sdelphij if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) { 97296465Sdelphij if (cache->anyPolicy) { 98296465Sdelphij ret = -1; 99296465Sdelphij goto bad_policy; 100296465Sdelphij } 101296465Sdelphij cache->anyPolicy = data; 102296465Sdelphij } else if (sk_X509_POLICY_DATA_find(cache->data, data) != -1) { 103296465Sdelphij ret = -1; 104296465Sdelphij goto bad_policy; 105296465Sdelphij } else if (!sk_X509_POLICY_DATA_push(cache->data, data)) 106296465Sdelphij goto bad_policy; 107296465Sdelphij data = NULL; 108296465Sdelphij } 109296465Sdelphij ret = 1; 110296465Sdelphij bad_policy: 111296465Sdelphij if (ret == -1) 112296465Sdelphij x->ex_flags |= EXFLAG_INVALID_POLICY; 113296465Sdelphij if (data) 114296465Sdelphij policy_data_free(data); 115296465Sdelphij sk_POLICYINFO_pop_free(policies, POLICYINFO_free); 116296465Sdelphij if (ret <= 0) { 117296465Sdelphij sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free); 118296465Sdelphij cache->data = NULL; 119296465Sdelphij } 120296465Sdelphij return ret; 121296465Sdelphij} 122160814Ssimon 123160814Ssimonstatic int policy_cache_new(X509 *x) 124296465Sdelphij{ 125296465Sdelphij X509_POLICY_CACHE *cache; 126296465Sdelphij ASN1_INTEGER *ext_any = NULL; 127296465Sdelphij POLICY_CONSTRAINTS *ext_pcons = NULL; 128296465Sdelphij CERTIFICATEPOLICIES *ext_cpols = NULL; 129296465Sdelphij POLICY_MAPPINGS *ext_pmaps = NULL; 130296465Sdelphij int i; 131296465Sdelphij cache = OPENSSL_malloc(sizeof(X509_POLICY_CACHE)); 132296465Sdelphij if (!cache) 133296465Sdelphij return 0; 134296465Sdelphij cache->anyPolicy = NULL; 135296465Sdelphij cache->data = NULL; 136296465Sdelphij cache->maps = NULL; 137296465Sdelphij cache->any_skip = -1; 138296465Sdelphij cache->explicit_skip = -1; 139296465Sdelphij cache->map_skip = -1; 140160814Ssimon 141296465Sdelphij x->policy_cache = cache; 142160814Ssimon 143296465Sdelphij /* 144296465Sdelphij * Handle requireExplicitPolicy *first*. Need to process this even if we 145296465Sdelphij * don't have any policies. 146296465Sdelphij */ 147296465Sdelphij ext_pcons = X509_get_ext_d2i(x, NID_policy_constraints, &i, NULL); 148160814Ssimon 149296465Sdelphij if (!ext_pcons) { 150296465Sdelphij if (i != -1) 151296465Sdelphij goto bad_cache; 152296465Sdelphij } else { 153296465Sdelphij if (!ext_pcons->requireExplicitPolicy 154296465Sdelphij && !ext_pcons->inhibitPolicyMapping) 155296465Sdelphij goto bad_cache; 156296465Sdelphij if (!policy_cache_set_int(&cache->explicit_skip, 157296465Sdelphij ext_pcons->requireExplicitPolicy)) 158296465Sdelphij goto bad_cache; 159296465Sdelphij if (!policy_cache_set_int(&cache->map_skip, 160296465Sdelphij ext_pcons->inhibitPolicyMapping)) 161296465Sdelphij goto bad_cache; 162296465Sdelphij } 163160814Ssimon 164296465Sdelphij /* Process CertificatePolicies */ 165160814Ssimon 166296465Sdelphij ext_cpols = X509_get_ext_d2i(x, NID_certificate_policies, &i, NULL); 167296465Sdelphij /* 168296465Sdelphij * If no CertificatePolicies extension or problem decoding then there is 169296465Sdelphij * no point continuing because the valid policies will be NULL. 170296465Sdelphij */ 171296465Sdelphij if (!ext_cpols) { 172296465Sdelphij /* If not absent some problem with extension */ 173296465Sdelphij if (i != -1) 174296465Sdelphij goto bad_cache; 175296465Sdelphij return 1; 176296465Sdelphij } 177160814Ssimon 178296465Sdelphij i = policy_cache_create(x, ext_cpols, i); 179160814Ssimon 180296465Sdelphij /* NB: ext_cpols freed by policy_cache_set_policies */ 181160814Ssimon 182296465Sdelphij if (i <= 0) 183296465Sdelphij return i; 184160814Ssimon 185296465Sdelphij ext_pmaps = X509_get_ext_d2i(x, NID_policy_mappings, &i, NULL); 186160814Ssimon 187296465Sdelphij if (!ext_pmaps) { 188296465Sdelphij /* If not absent some problem with extension */ 189296465Sdelphij if (i != -1) 190296465Sdelphij goto bad_cache; 191296465Sdelphij } else { 192296465Sdelphij i = policy_cache_set_mapping(x, ext_pmaps); 193296465Sdelphij if (i <= 0) 194296465Sdelphij goto bad_cache; 195296465Sdelphij } 196160814Ssimon 197296465Sdelphij ext_any = X509_get_ext_d2i(x, NID_inhibit_any_policy, &i, NULL); 198160814Ssimon 199296465Sdelphij if (!ext_any) { 200296465Sdelphij if (i != -1) 201296465Sdelphij goto bad_cache; 202296465Sdelphij } else if (!policy_cache_set_int(&cache->any_skip, ext_any)) 203296465Sdelphij goto bad_cache; 204160814Ssimon 205296465Sdelphij if (0) { 206296465Sdelphij bad_cache: 207296465Sdelphij x->ex_flags |= EXFLAG_INVALID_POLICY; 208296465Sdelphij } 209160814Ssimon 210296465Sdelphij if (ext_pcons) 211296465Sdelphij POLICY_CONSTRAINTS_free(ext_pcons); 212160814Ssimon 213296465Sdelphij if (ext_any) 214296465Sdelphij ASN1_INTEGER_free(ext_any); 215160814Ssimon 216296465Sdelphij return 1; 217160814Ssimon 218160814Ssimon} 219160814Ssimon 220160814Ssimonvoid policy_cache_free(X509_POLICY_CACHE *cache) 221296465Sdelphij{ 222296465Sdelphij if (!cache) 223296465Sdelphij return; 224296465Sdelphij if (cache->anyPolicy) 225296465Sdelphij policy_data_free(cache->anyPolicy); 226296465Sdelphij if (cache->data) 227296465Sdelphij sk_X509_POLICY_DATA_pop_free(cache->data, policy_data_free); 228296465Sdelphij OPENSSL_free(cache); 229296465Sdelphij} 230160814Ssimon 231160814Ssimonconst X509_POLICY_CACHE *policy_cache_set(X509 *x) 232296465Sdelphij{ 233160814Ssimon 234296465Sdelphij if (x->policy_cache == NULL) { 235296465Sdelphij CRYPTO_w_lock(CRYPTO_LOCK_X509); 236296465Sdelphij policy_cache_new(x); 237296465Sdelphij CRYPTO_w_unlock(CRYPTO_LOCK_X509); 238296465Sdelphij } 239160814Ssimon 240296465Sdelphij return x->policy_cache; 241160814Ssimon 242296465Sdelphij} 243160814Ssimon 244160814SsimonX509_POLICY_DATA *policy_cache_find_data(const X509_POLICY_CACHE *cache, 245296465Sdelphij const ASN1_OBJECT *id) 246296465Sdelphij{ 247296465Sdelphij int idx; 248296465Sdelphij X509_POLICY_DATA tmp; 249296465Sdelphij tmp.valid_policy = (ASN1_OBJECT *)id; 250296465Sdelphij idx = sk_X509_POLICY_DATA_find(cache->data, &tmp); 251296465Sdelphij if (idx == -1) 252296465Sdelphij return NULL; 253296465Sdelphij return sk_X509_POLICY_DATA_value(cache->data, idx); 254296465Sdelphij} 255160814Ssimon 256296465Sdelphijstatic int policy_data_cmp(const X509_POLICY_DATA *const *a, 257296465Sdelphij const X509_POLICY_DATA *const *b) 258296465Sdelphij{ 259296465Sdelphij return OBJ_cmp((*a)->valid_policy, (*b)->valid_policy); 260296465Sdelphij} 261160814Ssimon 262160814Ssimonstatic int policy_cache_set_int(long *out, ASN1_INTEGER *value) 263296465Sdelphij{ 264296465Sdelphij if (value == NULL) 265296465Sdelphij return 1; 266296465Sdelphij if (value->type == V_ASN1_NEG_INTEGER) 267296465Sdelphij return 0; 268296465Sdelphij *out = ASN1_INTEGER_get(value); 269296465Sdelphij return 1; 270296465Sdelphij} 271