1160814Ssimon/* pcy_node.c */
2280304Sjkim/*
3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280304Sjkim * 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
14280304Sjkim *    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 <openssl/asn1.h>
61160814Ssimon#include <openssl/x509.h>
62160814Ssimon#include <openssl/x509v3.h>
63160814Ssimon
64160814Ssimon#include "pcy_int.h"
65160814Ssimon
66280304Sjkimstatic int node_cmp(const X509_POLICY_NODE *const *a,
67280304Sjkim                    const X509_POLICY_NODE *const *b)
68280304Sjkim{
69280304Sjkim    return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
70280304Sjkim}
71160814Ssimon
72160814SsimonSTACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
73280304Sjkim{
74280304Sjkim    return sk_X509_POLICY_NODE_new(node_cmp);
75280304Sjkim}
76160814Ssimon
77160814SsimonX509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
78280304Sjkim                               const ASN1_OBJECT *id)
79280304Sjkim{
80280304Sjkim    X509_POLICY_DATA n;
81280304Sjkim    X509_POLICY_NODE l;
82280304Sjkim    int idx;
83160814Ssimon
84280304Sjkim    n.valid_policy = (ASN1_OBJECT *)id;
85280304Sjkim    l.data = &n;
86160814Ssimon
87280304Sjkim    idx = sk_X509_POLICY_NODE_find(nodes, &l);
88280304Sjkim    if (idx == -1)
89280304Sjkim        return NULL;
90160814Ssimon
91280304Sjkim    return sk_X509_POLICY_NODE_value(nodes, idx);
92160814Ssimon
93280304Sjkim}
94160814Ssimon
95160814SsimonX509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
96280304Sjkim                                  const X509_POLICY_NODE *parent,
97280304Sjkim                                  const ASN1_OBJECT *id)
98280304Sjkim{
99280304Sjkim    X509_POLICY_NODE *node;
100280304Sjkim    int i;
101280304Sjkim    for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
102280304Sjkim        node = sk_X509_POLICY_NODE_value(level->nodes, i);
103280304Sjkim        if (node->parent == parent) {
104280304Sjkim            if (!OBJ_cmp(node->data->valid_policy, id))
105280304Sjkim                return node;
106280304Sjkim        }
107280304Sjkim    }
108280304Sjkim    return NULL;
109280304Sjkim}
110160814Ssimon
111160814SsimonX509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
112280304Sjkim                                 const X509_POLICY_DATA *data,
113280304Sjkim                                 X509_POLICY_NODE *parent,
114280304Sjkim                                 X509_POLICY_TREE *tree)
115280304Sjkim{
116280304Sjkim    X509_POLICY_NODE *node;
117280304Sjkim    node = OPENSSL_malloc(sizeof(X509_POLICY_NODE));
118280304Sjkim    if (!node)
119280304Sjkim        return NULL;
120280304Sjkim    node->data = data;
121280304Sjkim    node->parent = parent;
122280304Sjkim    node->nchild = 0;
123280304Sjkim    if (level) {
124280304Sjkim        if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
125280304Sjkim            if (level->anyPolicy)
126280304Sjkim                goto node_error;
127280304Sjkim            level->anyPolicy = node;
128280304Sjkim        } else {
129160814Ssimon
130280304Sjkim            if (!level->nodes)
131280304Sjkim                level->nodes = policy_node_cmp_new();
132280304Sjkim            if (!level->nodes)
133280304Sjkim                goto node_error;
134280304Sjkim            if (!sk_X509_POLICY_NODE_push(level->nodes, node))
135280304Sjkim                goto node_error;
136280304Sjkim        }
137280304Sjkim    }
138160814Ssimon
139280304Sjkim    if (tree) {
140280304Sjkim        if (!tree->extra_data)
141280304Sjkim            tree->extra_data = sk_X509_POLICY_DATA_new_null();
142280304Sjkim        if (!tree->extra_data)
143280304Sjkim            goto node_error;
144280304Sjkim        if (!sk_X509_POLICY_DATA_push(tree->extra_data, data))
145280304Sjkim            goto node_error;
146280304Sjkim    }
147160814Ssimon
148280304Sjkim    if (parent)
149280304Sjkim        parent->nchild++;
150160814Ssimon
151280304Sjkim    return node;
152160814Ssimon
153280304Sjkim node_error:
154280304Sjkim    policy_node_free(node);
155280304Sjkim    return 0;
156160814Ssimon
157280304Sjkim}
158160814Ssimon
159160814Ssimonvoid policy_node_free(X509_POLICY_NODE *node)
160280304Sjkim{
161280304Sjkim    OPENSSL_free(node);
162280304Sjkim}
163160814Ssimon
164280304Sjkim/*
165280304Sjkim * See if a policy node matches a policy OID. If mapping enabled look through
166238405Sjkim * expected policy set otherwise just valid policy.
167238405Sjkim */
168160814Ssimon
169238405Sjkimint policy_node_match(const X509_POLICY_LEVEL *lvl,
170280304Sjkim                      const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
171280304Sjkim{
172280304Sjkim    int i;
173280304Sjkim    ASN1_OBJECT *policy_oid;
174280304Sjkim    const X509_POLICY_DATA *x = node->data;
175238405Sjkim
176280304Sjkim    if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP)
177280304Sjkim        || !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) {
178280304Sjkim        if (!OBJ_cmp(x->valid_policy, oid))
179280304Sjkim            return 1;
180280304Sjkim        return 0;
181280304Sjkim    }
182238405Sjkim
183280304Sjkim    for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) {
184280304Sjkim        policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i);
185280304Sjkim        if (!OBJ_cmp(policy_oid, oid))
186280304Sjkim            return 1;
187280304Sjkim    }
188280304Sjkim    return 0;
189238405Sjkim
190280304Sjkim}
191