2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2018-05-01 20:34:30 +08:00
|
|
|
* Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2004-03-23 22:14:35 +08:00
|
|
|
*
|
2018-12-06 21:00:54 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2004-03-23 22:14:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/x509v3.h>
|
2018-03-29 04:32:31 +08:00
|
|
|
#include <openssl/err.h>
|
2004-03-23 22:14:35 +08:00
|
|
|
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "pcy_local.h"
|
2004-03-23 22:14:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static int node_cmp(const X509_POLICY_NODE *const *a,
|
|
|
|
const X509_POLICY_NODE *const *b)
|
|
|
|
{
|
|
|
|
return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
|
|
|
|
}
|
2004-03-23 22:14:35 +08:00
|
|
|
|
|
|
|
STACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return sk_X509_POLICY_NODE_new(node_cmp);
|
|
|
|
}
|
2004-03-23 22:14:35 +08:00
|
|
|
|
|
|
|
X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
|
2015-01-22 11:40:55 +08:00
|
|
|
const ASN1_OBJECT *id)
|
|
|
|
{
|
|
|
|
X509_POLICY_DATA n;
|
|
|
|
X509_POLICY_NODE l;
|
|
|
|
int idx;
|
2004-03-23 22:14:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
n.valid_policy = (ASN1_OBJECT *)id;
|
|
|
|
l.data = &n;
|
2004-03-23 22:14:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
idx = sk_X509_POLICY_NODE_find(nodes, &l);
|
|
|
|
return sk_X509_POLICY_NODE_value(nodes, idx);
|
2004-03-23 22:14:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2004-03-23 22:14:35 +08:00
|
|
|
|
|
|
|
X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
|
2015-01-22 11:40:55 +08:00
|
|
|
const X509_POLICY_NODE *parent,
|
|
|
|
const ASN1_OBJECT *id)
|
|
|
|
{
|
|
|
|
X509_POLICY_NODE *node;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
|
|
|
|
node = sk_X509_POLICY_NODE_value(level->nodes, i);
|
|
|
|
if (node->parent == parent) {
|
|
|
|
if (!OBJ_cmp(node->data->valid_policy, id))
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-23 22:14:35 +08:00
|
|
|
|
|
|
|
X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
|
2015-12-23 08:47:28 +08:00
|
|
|
X509_POLICY_DATA *data,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_POLICY_NODE *parent,
|
|
|
|
X509_POLICY_TREE *tree)
|
|
|
|
{
|
|
|
|
X509_POLICY_NODE *node;
|
2015-09-03 21:15:26 +08:00
|
|
|
|
|
|
|
node = OPENSSL_zalloc(sizeof(*node));
|
2018-03-29 04:32:31 +08:00
|
|
|
if (node == NULL) {
|
|
|
|
X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2018-03-29 04:32:31 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
node->data = data;
|
|
|
|
node->parent = parent;
|
|
|
|
if (level) {
|
|
|
|
if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
|
|
|
|
if (level->anyPolicy)
|
|
|
|
goto node_error;
|
|
|
|
level->anyPolicy = node;
|
|
|
|
} else {
|
|
|
|
|
2015-10-30 19:12:26 +08:00
|
|
|
if (level->nodes == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
level->nodes = policy_node_cmp_new();
|
2018-03-29 04:32:31 +08:00
|
|
|
if (level->nodes == NULL) {
|
|
|
|
X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto node_error;
|
2018-03-29 04:32:31 +08:00
|
|
|
}
|
|
|
|
if (!sk_X509_POLICY_NODE_push(level->nodes, node)) {
|
|
|
|
X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto node_error;
|
2018-03-29 04:32:31 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree) {
|
2015-10-30 19:12:26 +08:00
|
|
|
if (tree->extra_data == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
tree->extra_data = sk_X509_POLICY_DATA_new_null();
|
2018-03-29 04:32:31 +08:00
|
|
|
if (tree->extra_data == NULL){
|
|
|
|
X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto node_error;
|
2018-03-29 04:32:31 +08:00
|
|
|
}
|
|
|
|
if (!sk_X509_POLICY_DATA_push(tree->extra_data, data)) {
|
|
|
|
X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto node_error;
|
2018-03-29 04:32:31 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
parent->nchild++;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
node_error:
|
|
|
|
policy_node_free(node);
|
2016-02-01 10:14:51 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2004-03-23 22:14:35 +08:00
|
|
|
|
|
|
|
void policy_node_free(X509_POLICY_NODE *node)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
OPENSSL_free(node);
|
|
|
|
}
|
2004-03-23 22:14:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* See if a policy node matches a policy OID. If mapping enabled look through
|
2008-08-12 18:32:56 +08:00
|
|
|
* expected policy set otherwise just valid policy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int policy_node_match(const X509_POLICY_LEVEL *lvl,
|
2015-01-22 11:40:55 +08:00
|
|
|
const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ASN1_OBJECT *policy_oid;
|
|
|
|
const X509_POLICY_DATA *x = node->data;
|
|
|
|
|
|
|
|
if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP)
|
|
|
|
|| !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) {
|
|
|
|
if (!OBJ_cmp(x->valid_policy, oid))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) {
|
|
|
|
policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i);
|
|
|
|
if (!OBJ_cmp(policy_oid, oid))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|