1160814Ssimon/* pcy_map.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
66160814Ssimonstatic int ref_cmp(const X509_POLICY_REF * const *a,
67296465Sdelphij                   const X509_POLICY_REF * const *b)
68296465Sdelphij{
69296465Sdelphij    return OBJ_cmp((*a)->subjectDomainPolicy, (*b)->subjectDomainPolicy);
70296465Sdelphij}
71160814Ssimon
72296465Sdelphijstatic void policy_map_free(X509_POLICY_REF * map)
73296465Sdelphij{
74296465Sdelphij    OPENSSL_free(map);
75296465Sdelphij}
76160814Ssimon
77296465Sdelphijstatic X509_POLICY_REF *policy_map_find(X509_POLICY_CACHE *cache,
78296465Sdelphij                                        ASN1_OBJECT *id)
79296465Sdelphij{
80296465Sdelphij    X509_POLICY_REF tmp;
81296465Sdelphij    int idx;
82296465Sdelphij    tmp.subjectDomainPolicy = id;
83160814Ssimon
84296465Sdelphij    idx = sk_X509_POLICY_REF_find(cache->maps, &tmp);
85296465Sdelphij    if (idx == -1)
86296465Sdelphij        return NULL;
87296465Sdelphij    return sk_X509_POLICY_REF_value(cache->maps, idx);
88296465Sdelphij}
89160814Ssimon
90296465Sdelphij/*
91296465Sdelphij * Set policy mapping entries in cache. Note: this modifies the passed
92296465Sdelphij * POLICY_MAPPINGS structure
93160814Ssimon */
94160814Ssimon
95160814Ssimonint policy_cache_set_mapping(X509 *x, POLICY_MAPPINGS *maps)
96296465Sdelphij{
97296465Sdelphij    POLICY_MAPPING *map;
98296465Sdelphij    X509_POLICY_REF *ref = NULL;
99296465Sdelphij    ASN1_OBJECT *subjectDomainPolicyRef;
100296465Sdelphij    X509_POLICY_DATA *data;
101296465Sdelphij    X509_POLICY_CACHE *cache = x->policy_cache;
102296465Sdelphij    int i;
103296465Sdelphij    int ret = 0;
104296465Sdelphij    if (sk_POLICY_MAPPING_num(maps) == 0) {
105296465Sdelphij        ret = -1;
106296465Sdelphij        goto bad_mapping;
107296465Sdelphij    }
108296465Sdelphij    cache->maps = sk_X509_POLICY_REF_new(ref_cmp);
109296465Sdelphij    for (i = 0; i < sk_POLICY_MAPPING_num(maps); i++) {
110296465Sdelphij        map = sk_POLICY_MAPPING_value(maps, i);
111296465Sdelphij        /* Reject if map to or from anyPolicy */
112296465Sdelphij        if ((OBJ_obj2nid(map->subjectDomainPolicy) == NID_any_policy)
113296465Sdelphij            || (OBJ_obj2nid(map->issuerDomainPolicy) == NID_any_policy)) {
114296465Sdelphij            ret = -1;
115296465Sdelphij            goto bad_mapping;
116296465Sdelphij        }
117160814Ssimon
118296465Sdelphij        /* If we've already mapped from this OID bad mapping */
119296465Sdelphij        if (policy_map_find(cache, map->subjectDomainPolicy) != NULL) {
120296465Sdelphij            ret = -1;
121296465Sdelphij            goto bad_mapping;
122296465Sdelphij        }
123160814Ssimon
124296465Sdelphij        /* Attempt to find matching policy data */
125296465Sdelphij        data = policy_cache_find_data(cache, map->issuerDomainPolicy);
126296465Sdelphij        /* If we don't have anyPolicy can't map */
127296465Sdelphij        if (!data && !cache->anyPolicy)
128296465Sdelphij            continue;
129160814Ssimon
130296465Sdelphij        /* Create a NODE from anyPolicy */
131296465Sdelphij        if (!data) {
132296465Sdelphij            data = policy_data_new(NULL, map->issuerDomainPolicy,
133296465Sdelphij                                   cache->anyPolicy->flags
134296465Sdelphij                                   & POLICY_DATA_FLAG_CRITICAL);
135296465Sdelphij            if (!data)
136296465Sdelphij                goto bad_mapping;
137296465Sdelphij            data->qualifier_set = cache->anyPolicy->qualifier_set;
138296465Sdelphij            map->issuerDomainPolicy = NULL;
139296465Sdelphij            data->flags |= POLICY_DATA_FLAG_MAPPED_ANY;
140296465Sdelphij            data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
141296465Sdelphij            if (!sk_X509_POLICY_DATA_push(cache->data, data)) {
142296465Sdelphij                policy_data_free(data);
143296465Sdelphij                goto bad_mapping;
144296465Sdelphij            }
145296465Sdelphij        } else
146296465Sdelphij            data->flags |= POLICY_DATA_FLAG_MAPPED;
147160814Ssimon
148296465Sdelphij        if (!sk_ASN1_OBJECT_push(data->expected_policy_set,
149296465Sdelphij                                 map->subjectDomainPolicy))
150296465Sdelphij            goto bad_mapping;
151296465Sdelphij        /*
152296465Sdelphij         * map->subjectDomainPolicy will be freed when cache->data is freed.
153296465Sdelphij         * Set it to NULL to avoid double-free.
154296465Sdelphij         */
155296465Sdelphij        subjectDomainPolicyRef = map->subjectDomainPolicy;
156296465Sdelphij        map->subjectDomainPolicy = NULL;
157160814Ssimon
158296465Sdelphij        ref = OPENSSL_malloc(sizeof(X509_POLICY_REF));
159296465Sdelphij        if (!ref)
160296465Sdelphij            goto bad_mapping;
161160814Ssimon
162296465Sdelphij        ref->subjectDomainPolicy = subjectDomainPolicyRef;
163296465Sdelphij        ref->data = data;
164160814Ssimon
165296465Sdelphij        if (!sk_X509_POLICY_REF_push(cache->maps, ref))
166296465Sdelphij            goto bad_mapping;
167160814Ssimon
168296465Sdelphij        ref = NULL;
169160814Ssimon
170296465Sdelphij    }
171160814Ssimon
172296465Sdelphij    ret = 1;
173296465Sdelphij bad_mapping:
174296465Sdelphij    if (ret == -1)
175296465Sdelphij        x->ex_flags |= EXFLAG_INVALID_POLICY;
176296465Sdelphij    if (ref)
177296465Sdelphij        policy_map_free(ref);
178296465Sdelphij    if (ret <= 0) {
179296465Sdelphij        sk_X509_POLICY_REF_pop_free(cache->maps, policy_map_free);
180296465Sdelphij        cache->maps = NULL;
181296465Sdelphij    }
182296465Sdelphij    sk_POLICY_MAPPING_pop_free(maps, POLICY_MAPPING_free);
183296465Sdelphij    return ret;
184296465Sdelphij
185296465Sdelphij}
186