1160814Ssimon/* pcy_node.c */ 2296341Sdelphij/* 3296341Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4296341Sdelphij * 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 14296341Sdelphij * 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 66296341Sdelphijstatic int node_cmp(const X509_POLICY_NODE *const *a, 67296341Sdelphij const X509_POLICY_NODE *const *b) 68296341Sdelphij{ 69296341Sdelphij return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy); 70296341Sdelphij} 71160814Ssimon 72160814SsimonSTACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void) 73296341Sdelphij{ 74296341Sdelphij return sk_X509_POLICY_NODE_new(node_cmp); 75296341Sdelphij} 76160814Ssimon 77160814SsimonX509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes, 78296341Sdelphij const ASN1_OBJECT *id) 79296341Sdelphij{ 80296341Sdelphij X509_POLICY_DATA n; 81296341Sdelphij X509_POLICY_NODE l; 82296341Sdelphij int idx; 83160814Ssimon 84296341Sdelphij n.valid_policy = (ASN1_OBJECT *)id; 85296341Sdelphij l.data = &n; 86160814Ssimon 87296341Sdelphij idx = sk_X509_POLICY_NODE_find(nodes, &l); 88296341Sdelphij if (idx == -1) 89296341Sdelphij return NULL; 90160814Ssimon 91296341Sdelphij return sk_X509_POLICY_NODE_value(nodes, idx); 92160814Ssimon 93296341Sdelphij} 94160814Ssimon 95160814SsimonX509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level, 96296341Sdelphij const X509_POLICY_NODE *parent, 97296341Sdelphij const ASN1_OBJECT *id) 98296341Sdelphij{ 99296341Sdelphij X509_POLICY_NODE *node; 100296341Sdelphij int i; 101296341Sdelphij for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) { 102296341Sdelphij node = sk_X509_POLICY_NODE_value(level->nodes, i); 103296341Sdelphij if (node->parent == parent) { 104296341Sdelphij if (!OBJ_cmp(node->data->valid_policy, id)) 105296341Sdelphij return node; 106296341Sdelphij } 107296341Sdelphij } 108296341Sdelphij return NULL; 109296341Sdelphij} 110160814Ssimon 111160814SsimonX509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level, 112296341Sdelphij const X509_POLICY_DATA *data, 113296341Sdelphij X509_POLICY_NODE *parent, 114296341Sdelphij X509_POLICY_TREE *tree) 115296341Sdelphij{ 116296341Sdelphij X509_POLICY_NODE *node; 117296341Sdelphij node = OPENSSL_malloc(sizeof(X509_POLICY_NODE)); 118296341Sdelphij if (!node) 119296341Sdelphij return NULL; 120296341Sdelphij node->data = data; 121296341Sdelphij node->parent = parent; 122296341Sdelphij node->nchild = 0; 123296341Sdelphij if (level) { 124296341Sdelphij if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) { 125296341Sdelphij if (level->anyPolicy) 126296341Sdelphij goto node_error; 127296341Sdelphij level->anyPolicy = node; 128296341Sdelphij } else { 129160814Ssimon 130296341Sdelphij if (!level->nodes) 131296341Sdelphij level->nodes = policy_node_cmp_new(); 132296341Sdelphij if (!level->nodes) 133296341Sdelphij goto node_error; 134296341Sdelphij if (!sk_X509_POLICY_NODE_push(level->nodes, node)) 135296341Sdelphij goto node_error; 136296341Sdelphij } 137296341Sdelphij } 138160814Ssimon 139296341Sdelphij if (tree) { 140296341Sdelphij if (!tree->extra_data) 141296341Sdelphij tree->extra_data = sk_X509_POLICY_DATA_new_null(); 142296341Sdelphij if (!tree->extra_data) 143296341Sdelphij goto node_error; 144296341Sdelphij if (!sk_X509_POLICY_DATA_push(tree->extra_data, data)) 145296341Sdelphij goto node_error; 146296341Sdelphij } 147160814Ssimon 148296341Sdelphij if (parent) 149296341Sdelphij parent->nchild++; 150160814Ssimon 151296341Sdelphij return node; 152160814Ssimon 153296341Sdelphij node_error: 154296341Sdelphij policy_node_free(node); 155296341Sdelphij return 0; 156160814Ssimon 157296341Sdelphij} 158160814Ssimon 159160814Ssimonvoid policy_node_free(X509_POLICY_NODE *node) 160296341Sdelphij{ 161296341Sdelphij OPENSSL_free(node); 162296341Sdelphij} 163160814Ssimon 164296341Sdelphij/* 165296341Sdelphij * 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, 170296341Sdelphij const X509_POLICY_NODE *node, const ASN1_OBJECT *oid) 171296341Sdelphij{ 172296341Sdelphij int i; 173296341Sdelphij ASN1_OBJECT *policy_oid; 174296341Sdelphij const X509_POLICY_DATA *x = node->data; 175238405Sjkim 176296341Sdelphij if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP) 177296341Sdelphij || !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) { 178296341Sdelphij if (!OBJ_cmp(x->valid_policy, oid)) 179296341Sdelphij return 1; 180296341Sdelphij return 0; 181296341Sdelphij } 182238405Sjkim 183296341Sdelphij for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) { 184296341Sdelphij policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i); 185296341Sdelphij if (!OBJ_cmp(policy_oid, oid)) 186296341Sdelphij return 1; 187296341Sdelphij } 188296341Sdelphij return 0; 189238405Sjkim 190296341Sdelphij} 191