1160814Ssimon/* pcy_node.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 <openssl/asn1.h> 61160814Ssimon#include <openssl/x509.h> 62160814Ssimon#include <openssl/x509v3.h> 63160814Ssimon 64160814Ssimon#include "pcy_int.h" 65160814Ssimon 66280297Sjkimstatic int node_cmp(const X509_POLICY_NODE *const *a, 67280297Sjkim const X509_POLICY_NODE *const *b) 68280297Sjkim{ 69280297Sjkim return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy); 70280297Sjkim} 71160814Ssimon 72160814SsimonSTACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void) 73280297Sjkim{ 74280297Sjkim return sk_X509_POLICY_NODE_new(node_cmp); 75280297Sjkim} 76160814Ssimon 77160814SsimonX509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes, 78280297Sjkim const ASN1_OBJECT *id) 79280297Sjkim{ 80280297Sjkim X509_POLICY_DATA n; 81280297Sjkim X509_POLICY_NODE l; 82280297Sjkim int idx; 83160814Ssimon 84280297Sjkim n.valid_policy = (ASN1_OBJECT *)id; 85280297Sjkim l.data = &n; 86160814Ssimon 87280297Sjkim idx = sk_X509_POLICY_NODE_find(nodes, &l); 88280297Sjkim if (idx == -1) 89280297Sjkim return NULL; 90160814Ssimon 91280297Sjkim return sk_X509_POLICY_NODE_value(nodes, idx); 92160814Ssimon 93280297Sjkim} 94160814Ssimon 95160814SsimonX509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level, 96280297Sjkim const X509_POLICY_NODE *parent, 97280297Sjkim const ASN1_OBJECT *id) 98280297Sjkim{ 99280297Sjkim X509_POLICY_NODE *node; 100280297Sjkim int i; 101280297Sjkim for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) { 102280297Sjkim node = sk_X509_POLICY_NODE_value(level->nodes, i); 103280297Sjkim if (node->parent == parent) { 104280297Sjkim if (!OBJ_cmp(node->data->valid_policy, id)) 105280297Sjkim return node; 106280297Sjkim } 107280297Sjkim } 108280297Sjkim return NULL; 109280297Sjkim} 110160814Ssimon 111160814SsimonX509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level, 112280297Sjkim const X509_POLICY_DATA *data, 113280297Sjkim X509_POLICY_NODE *parent, 114280297Sjkim X509_POLICY_TREE *tree) 115280297Sjkim{ 116280297Sjkim X509_POLICY_NODE *node; 117280297Sjkim node = OPENSSL_malloc(sizeof(X509_POLICY_NODE)); 118280297Sjkim if (!node) 119280297Sjkim return NULL; 120280297Sjkim node->data = data; 121280297Sjkim node->parent = parent; 122280297Sjkim node->nchild = 0; 123280297Sjkim if (level) { 124280297Sjkim if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) { 125280297Sjkim if (level->anyPolicy) 126280297Sjkim goto node_error; 127280297Sjkim level->anyPolicy = node; 128280297Sjkim } else { 129160814Ssimon 130280297Sjkim if (!level->nodes) 131280297Sjkim level->nodes = policy_node_cmp_new(); 132280297Sjkim if (!level->nodes) 133280297Sjkim goto node_error; 134280297Sjkim if (!sk_X509_POLICY_NODE_push(level->nodes, node)) 135280297Sjkim goto node_error; 136280297Sjkim } 137280297Sjkim } 138160814Ssimon 139280297Sjkim if (tree) { 140280297Sjkim if (!tree->extra_data) 141280297Sjkim tree->extra_data = sk_X509_POLICY_DATA_new_null(); 142280297Sjkim if (!tree->extra_data) 143280297Sjkim goto node_error; 144280297Sjkim if (!sk_X509_POLICY_DATA_push(tree->extra_data, data)) 145280297Sjkim goto node_error; 146280297Sjkim } 147160814Ssimon 148280297Sjkim if (parent) 149280297Sjkim parent->nchild++; 150160814Ssimon 151280297Sjkim return node; 152160814Ssimon 153280297Sjkim node_error: 154280297Sjkim policy_node_free(node); 155280297Sjkim return 0; 156160814Ssimon 157280297Sjkim} 158160814Ssimon 159160814Ssimonvoid policy_node_free(X509_POLICY_NODE *node) 160280297Sjkim{ 161280297Sjkim OPENSSL_free(node); 162280297Sjkim} 163160814Ssimon 164280297Sjkim/* 165280297Sjkim * 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, 170280297Sjkim const X509_POLICY_NODE *node, const ASN1_OBJECT *oid) 171280297Sjkim{ 172280297Sjkim int i; 173280297Sjkim ASN1_OBJECT *policy_oid; 174280297Sjkim const X509_POLICY_DATA *x = node->data; 175238405Sjkim 176280297Sjkim if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP) 177280297Sjkim || !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) { 178280297Sjkim if (!OBJ_cmp(x->valid_policy, oid)) 179280297Sjkim return 1; 180280297Sjkim return 0; 181280297Sjkim } 182238405Sjkim 183280297Sjkim for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) { 184280297Sjkim policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i); 185280297Sjkim if (!OBJ_cmp(policy_oid, oid)) 186280297Sjkim return 1; 187280297Sjkim } 188280297Sjkim return 0; 189238405Sjkim 190280297Sjkim} 191