pcy_node.c revision 238405
1160814Ssimon/* pcy_node.c */
2194206Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3160814Ssimon * project 2004.
4160814Ssimon */
5160814Ssimon/* ====================================================================
6160814Ssimon * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
7160814Ssimon *
8160814Ssimon * Redistribution and use in source and binary forms, with or without
9160814Ssimon * modification, are permitted provided that the following conditions
10160814Ssimon * are met:
11160814Ssimon *
12160814Ssimon * 1. Redistributions of source code must retain the above copyright
13160814Ssimon *    notice, this list of conditions and the following disclaimer.
14160814Ssimon *
15160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
16160814Ssimon *    notice, this list of conditions and the following disclaimer in
17160814Ssimon *    the documentation and/or other materials provided with the
18160814Ssimon *    distribution.
19160814Ssimon *
20160814Ssimon * 3. All advertising materials mentioning features or use of this
21160814Ssimon *    software must display the following acknowledgment:
22160814Ssimon *    "This product includes software developed by the OpenSSL Project
23160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24160814Ssimon *
25160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26160814Ssimon *    endorse or promote products derived from this software without
27160814Ssimon *    prior written permission. For written permission, please contact
28160814Ssimon *    licensing@OpenSSL.org.
29160814Ssimon *
30160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
31160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
32160814Ssimon *    permission of the OpenSSL Project.
33160814Ssimon *
34160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
35160814Ssimon *    acknowledgment:
36160814Ssimon *    "This product includes software developed by the OpenSSL Project
37160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38160814Ssimon *
39160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
51160814Ssimon * ====================================================================
52160814Ssimon *
53160814Ssimon * This product includes cryptographic software written by Eric Young
54160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
55160814Ssimon * Hudson (tjh@cryptsoft.com).
56160814Ssimon *
57160814Ssimon */
58160814Ssimon
59160814Ssimon#include <openssl/asn1.h>
60160814Ssimon#include <openssl/x509.h>
61160814Ssimon#include <openssl/x509v3.h>
62160814Ssimon
63160814Ssimon#include "pcy_int.h"
64160814Ssimon
65160814Ssimonstatic int node_cmp(const X509_POLICY_NODE * const *a,
66160814Ssimon			const X509_POLICY_NODE * const *b)
67160814Ssimon	{
68160814Ssimon	return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
69160814Ssimon	}
70160814Ssimon
71160814SsimonSTACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
72160814Ssimon	{
73160814Ssimon	return sk_X509_POLICY_NODE_new(node_cmp);
74160814Ssimon	}
75160814Ssimon
76160814SsimonX509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
77160814Ssimon					const ASN1_OBJECT *id)
78160814Ssimon	{
79160814Ssimon	X509_POLICY_DATA n;
80160814Ssimon	X509_POLICY_NODE l;
81160814Ssimon	int idx;
82160814Ssimon
83160814Ssimon	n.valid_policy = (ASN1_OBJECT *)id;
84160814Ssimon	l.data = &n;
85160814Ssimon
86160814Ssimon	idx = sk_X509_POLICY_NODE_find(nodes, &l);
87160814Ssimon	if (idx == -1)
88160814Ssimon		return NULL;
89160814Ssimon
90160814Ssimon	return sk_X509_POLICY_NODE_value(nodes, idx);
91160814Ssimon
92160814Ssimon	}
93160814Ssimon
94160814SsimonX509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
95238405Sjkim					const X509_POLICY_NODE *parent,
96160814Ssimon					const ASN1_OBJECT *id)
97160814Ssimon	{
98238405Sjkim	X509_POLICY_NODE *node;
99238405Sjkim	int i;
100238405Sjkim	for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++)
101238405Sjkim		{
102238405Sjkim		node = sk_X509_POLICY_NODE_value(level->nodes, i);
103238405Sjkim		if (node->parent == parent)
104238405Sjkim			{
105238405Sjkim			if (!OBJ_cmp(node->data->valid_policy, id))
106238405Sjkim				return node;
107238405Sjkim			}
108238405Sjkim		}
109238405Sjkim	return NULL;
110160814Ssimon	}
111160814Ssimon
112160814SsimonX509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
113238405Sjkim			const X509_POLICY_DATA *data,
114160814Ssimon			X509_POLICY_NODE *parent,
115160814Ssimon			X509_POLICY_TREE *tree)
116160814Ssimon	{
117160814Ssimon	X509_POLICY_NODE *node;
118160814Ssimon	node = OPENSSL_malloc(sizeof(X509_POLICY_NODE));
119160814Ssimon	if (!node)
120160814Ssimon		return NULL;
121160814Ssimon	node->data = data;
122160814Ssimon	node->parent = parent;
123160814Ssimon	node->nchild = 0;
124160814Ssimon	if (level)
125160814Ssimon		{
126160814Ssimon		if (OBJ_obj2nid(data->valid_policy) == NID_any_policy)
127160814Ssimon			{
128160814Ssimon			if (level->anyPolicy)
129160814Ssimon				goto node_error;
130160814Ssimon			level->anyPolicy = node;
131160814Ssimon			}
132160814Ssimon		else
133160814Ssimon			{
134160814Ssimon
135160814Ssimon			if (!level->nodes)
136160814Ssimon				level->nodes = policy_node_cmp_new();
137160814Ssimon			if (!level->nodes)
138160814Ssimon				goto node_error;
139160814Ssimon			if (!sk_X509_POLICY_NODE_push(level->nodes, node))
140160814Ssimon				goto node_error;
141160814Ssimon			}
142160814Ssimon		}
143160814Ssimon
144160814Ssimon	if (tree)
145160814Ssimon		{
146160814Ssimon		if (!tree->extra_data)
147160814Ssimon			 tree->extra_data = sk_X509_POLICY_DATA_new_null();
148160814Ssimon		if (!tree->extra_data)
149160814Ssimon			goto node_error;
150160814Ssimon		if (!sk_X509_POLICY_DATA_push(tree->extra_data, data))
151160814Ssimon			goto node_error;
152160814Ssimon		}
153160814Ssimon
154160814Ssimon	if (parent)
155160814Ssimon		parent->nchild++;
156160814Ssimon
157160814Ssimon	return node;
158160814Ssimon
159160814Ssimon	node_error:
160160814Ssimon	policy_node_free(node);
161160814Ssimon	return 0;
162160814Ssimon
163160814Ssimon	}
164160814Ssimon
165160814Ssimonvoid policy_node_free(X509_POLICY_NODE *node)
166160814Ssimon	{
167160814Ssimon	OPENSSL_free(node);
168160814Ssimon	}
169160814Ssimon
170238405Sjkim/* See if a policy node matches a policy OID. If mapping enabled look through
171238405Sjkim * expected policy set otherwise just valid policy.
172238405Sjkim */
173160814Ssimon
174238405Sjkimint policy_node_match(const X509_POLICY_LEVEL *lvl,
175238405Sjkim		      const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
176238405Sjkim	{
177238405Sjkim	int i;
178238405Sjkim	ASN1_OBJECT *policy_oid;
179238405Sjkim	const X509_POLICY_DATA *x = node->data;
180238405Sjkim
181238405Sjkim	if (	    (lvl->flags & X509_V_FLAG_INHIBIT_MAP)
182238405Sjkim		|| !(x->flags & POLICY_DATA_FLAG_MAP_MASK))
183238405Sjkim		{
184238405Sjkim		if (!OBJ_cmp(x->valid_policy, oid))
185238405Sjkim			return 1;
186238405Sjkim		return 0;
187238405Sjkim		}
188238405Sjkim
189238405Sjkim	for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++)
190238405Sjkim		{
191238405Sjkim		policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i);
192238405Sjkim		if (!OBJ_cmp(policy_oid, oid))
193238405Sjkim			return 1;
194238405Sjkim		}
195238405Sjkim	return 0;
196238405Sjkim
197238405Sjkim	}
198