1160814Ssimon/* pcy_map.c */
2280297Sjkim/*
3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280297Sjkim * 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
14280297Sjkim *    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
66280297Sjkim/*
67280297Sjkim * Set policy mapping entries in cache. Note: this modifies the passed
68280297Sjkim * POLICY_MAPPINGS structure
69160814Ssimon */
70160814Ssimon
71160814Ssimonint policy_cache_set_mapping(X509 *x, POLICY_MAPPINGS *maps)
72280297Sjkim{
73280297Sjkim    POLICY_MAPPING *map;
74280297Sjkim    X509_POLICY_DATA *data;
75280297Sjkim    X509_POLICY_CACHE *cache = x->policy_cache;
76280297Sjkim    int i;
77280297Sjkim    int ret = 0;
78280297Sjkim    if (sk_POLICY_MAPPING_num(maps) == 0) {
79280297Sjkim        ret = -1;
80280297Sjkim        goto bad_mapping;
81280297Sjkim    }
82280297Sjkim    for (i = 0; i < sk_POLICY_MAPPING_num(maps); i++) {
83280297Sjkim        map = sk_POLICY_MAPPING_value(maps, i);
84280297Sjkim        /* Reject if map to or from anyPolicy */
85280297Sjkim        if ((OBJ_obj2nid(map->subjectDomainPolicy) == NID_any_policy)
86280297Sjkim            || (OBJ_obj2nid(map->issuerDomainPolicy) == NID_any_policy)) {
87280297Sjkim            ret = -1;
88280297Sjkim            goto bad_mapping;
89280297Sjkim        }
90160814Ssimon
91280297Sjkim        /* Attempt to find matching policy data */
92280297Sjkim        data = policy_cache_find_data(cache, map->issuerDomainPolicy);
93280297Sjkim        /* If we don't have anyPolicy can't map */
94280297Sjkim        if (!data && !cache->anyPolicy)
95280297Sjkim            continue;
96160814Ssimon
97280297Sjkim        /* Create a NODE from anyPolicy */
98280297Sjkim        if (!data) {
99280297Sjkim            data = policy_data_new(NULL, map->issuerDomainPolicy,
100280297Sjkim                                   cache->anyPolicy->flags
101280297Sjkim                                   & POLICY_DATA_FLAG_CRITICAL);
102280297Sjkim            if (!data)
103280297Sjkim                goto bad_mapping;
104280297Sjkim            data->qualifier_set = cache->anyPolicy->qualifier_set;
105280297Sjkim            /*
106280297Sjkim             * map->issuerDomainPolicy = NULL;
107280297Sjkim             */
108280297Sjkim            data->flags |= POLICY_DATA_FLAG_MAPPED_ANY;
109280297Sjkim            data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
110280297Sjkim            if (!sk_X509_POLICY_DATA_push(cache->data, data)) {
111280297Sjkim                policy_data_free(data);
112280297Sjkim                goto bad_mapping;
113280297Sjkim            }
114280297Sjkim        } else
115280297Sjkim            data->flags |= POLICY_DATA_FLAG_MAPPED;
116280297Sjkim        if (!sk_ASN1_OBJECT_push(data->expected_policy_set,
117280297Sjkim                                 map->subjectDomainPolicy))
118280297Sjkim            goto bad_mapping;
119280297Sjkim        map->subjectDomainPolicy = NULL;
120160814Ssimon
121280297Sjkim    }
122160814Ssimon
123280297Sjkim    ret = 1;
124280297Sjkim bad_mapping:
125280297Sjkim    if (ret == -1)
126280297Sjkim        x->ex_flags |= EXFLAG_INVALID_POLICY;
127280297Sjkim    sk_POLICY_MAPPING_pop_free(maps, POLICY_MAPPING_free);
128280297Sjkim    return ret;
129160814Ssimon
130280297Sjkim}
131