pcy_node.c revision 160814
1160814Ssimon/* pcy_node.c */
2160814Ssimon/* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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,
95160814Ssimon					const ASN1_OBJECT *id)
96160814Ssimon	{
97160814Ssimon	return tree_find_sk(level->nodes, id);
98160814Ssimon	}
99160814Ssimon
100160814SsimonX509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
101160814Ssimon			X509_POLICY_DATA *data,
102160814Ssimon			X509_POLICY_NODE *parent,
103160814Ssimon			X509_POLICY_TREE *tree)
104160814Ssimon	{
105160814Ssimon	X509_POLICY_NODE *node;
106160814Ssimon	node = OPENSSL_malloc(sizeof(X509_POLICY_NODE));
107160814Ssimon	if (!node)
108160814Ssimon		return NULL;
109160814Ssimon	node->data = data;
110160814Ssimon	node->parent = parent;
111160814Ssimon	node->nchild = 0;
112160814Ssimon	if (level)
113160814Ssimon		{
114160814Ssimon		if (OBJ_obj2nid(data->valid_policy) == NID_any_policy)
115160814Ssimon			{
116160814Ssimon			if (level->anyPolicy)
117160814Ssimon				goto node_error;
118160814Ssimon			level->anyPolicy = node;
119160814Ssimon			}
120160814Ssimon		else
121160814Ssimon			{
122160814Ssimon
123160814Ssimon			if (!level->nodes)
124160814Ssimon				level->nodes = policy_node_cmp_new();
125160814Ssimon			if (!level->nodes)
126160814Ssimon				goto node_error;
127160814Ssimon			if (!sk_X509_POLICY_NODE_push(level->nodes, node))
128160814Ssimon				goto node_error;
129160814Ssimon			}
130160814Ssimon		}
131160814Ssimon
132160814Ssimon	if (tree)
133160814Ssimon		{
134160814Ssimon		if (!tree->extra_data)
135160814Ssimon			 tree->extra_data = sk_X509_POLICY_DATA_new_null();
136160814Ssimon		if (!tree->extra_data)
137160814Ssimon			goto node_error;
138160814Ssimon		if (!sk_X509_POLICY_DATA_push(tree->extra_data, data))
139160814Ssimon			goto node_error;
140160814Ssimon		}
141160814Ssimon
142160814Ssimon	if (parent)
143160814Ssimon		parent->nchild++;
144160814Ssimon
145160814Ssimon	return node;
146160814Ssimon
147160814Ssimon	node_error:
148160814Ssimon	policy_node_free(node);
149160814Ssimon	return 0;
150160814Ssimon
151160814Ssimon	}
152160814Ssimon
153160814Ssimonvoid policy_node_free(X509_POLICY_NODE *node)
154160814Ssimon	{
155160814Ssimon	OPENSSL_free(node);
156160814Ssimon	}
157160814Ssimon
158160814Ssimon
159