X509: add more error codes on malloc or sk_TYP_push failure
[openssl.git] / crypto / x509v3 / pcy_node.c
1 /*
2  * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/asn1.h>
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
13 #include <openssl/err.h>
14
15 #include "pcy_int.h"
16
17 static int node_cmp(const X509_POLICY_NODE *const *a,
18                     const X509_POLICY_NODE *const *b)
19 {
20     return OBJ_cmp((*a)->data->valid_policy, (*b)->data->valid_policy);
21 }
22
23 STACK_OF(X509_POLICY_NODE) *policy_node_cmp_new(void)
24 {
25     return sk_X509_POLICY_NODE_new(node_cmp);
26 }
27
28 X509_POLICY_NODE *tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes,
29                                const ASN1_OBJECT *id)
30 {
31     X509_POLICY_DATA n;
32     X509_POLICY_NODE l;
33     int idx;
34
35     n.valid_policy = (ASN1_OBJECT *)id;
36     l.data = &n;
37
38     idx = sk_X509_POLICY_NODE_find(nodes, &l);
39     if (idx == -1)
40         return NULL;
41
42     return sk_X509_POLICY_NODE_value(nodes, idx);
43
44 }
45
46 X509_POLICY_NODE *level_find_node(const X509_POLICY_LEVEL *level,
47                                   const X509_POLICY_NODE *parent,
48                                   const ASN1_OBJECT *id)
49 {
50     X509_POLICY_NODE *node;
51     int i;
52     for (i = 0; i < sk_X509_POLICY_NODE_num(level->nodes); i++) {
53         node = sk_X509_POLICY_NODE_value(level->nodes, i);
54         if (node->parent == parent) {
55             if (!OBJ_cmp(node->data->valid_policy, id))
56                 return node;
57         }
58     }
59     return NULL;
60 }
61
62 X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
63                                  X509_POLICY_DATA *data,
64                                  X509_POLICY_NODE *parent,
65                                  X509_POLICY_TREE *tree)
66 {
67     X509_POLICY_NODE *node;
68
69     node = OPENSSL_zalloc(sizeof(*node));
70     if (node == NULL) {
71         X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
72         return NULL;
73     }
74     node->data = data;
75     node->parent = parent;
76     if (level) {
77         if (OBJ_obj2nid(data->valid_policy) == NID_any_policy) {
78             if (level->anyPolicy)
79                 goto node_error;
80             level->anyPolicy = node;
81         } else {
82
83             if (level->nodes == NULL)
84                 level->nodes = policy_node_cmp_new();
85             if (level->nodes == NULL) {
86                 X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
87                 goto node_error;
88             }
89             if (!sk_X509_POLICY_NODE_push(level->nodes, node)) {
90                 X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
91                 goto node_error;
92             }
93         }
94     }
95
96     if (tree) {
97         if (tree->extra_data == NULL)
98             tree->extra_data = sk_X509_POLICY_DATA_new_null();
99         if (tree->extra_data == NULL){
100             X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
101             goto node_error;
102         }
103         if (!sk_X509_POLICY_DATA_push(tree->extra_data, data)) {
104             X509V3err(X509V3_F_LEVEL_ADD_NODE, ERR_R_MALLOC_FAILURE);
105             goto node_error;
106         }
107     }
108
109     if (parent)
110         parent->nchild++;
111
112     return node;
113
114  node_error:
115     policy_node_free(node);
116     return NULL;
117 }
118
119 void policy_node_free(X509_POLICY_NODE *node)
120 {
121     OPENSSL_free(node);
122 }
123
124 /*
125  * See if a policy node matches a policy OID. If mapping enabled look through
126  * expected policy set otherwise just valid policy.
127  */
128
129 int policy_node_match(const X509_POLICY_LEVEL *lvl,
130                       const X509_POLICY_NODE *node, const ASN1_OBJECT *oid)
131 {
132     int i;
133     ASN1_OBJECT *policy_oid;
134     const X509_POLICY_DATA *x = node->data;
135
136     if ((lvl->flags & X509_V_FLAG_INHIBIT_MAP)
137         || !(x->flags & POLICY_DATA_FLAG_MAP_MASK)) {
138         if (!OBJ_cmp(x->valid_policy, oid))
139             return 1;
140         return 0;
141     }
142
143     for (i = 0; i < sk_ASN1_OBJECT_num(x->expected_policy_set); i++) {
144         policy_oid = sk_ASN1_OBJECT_value(x->expected_policy_set, i);
145         if (!OBJ_cmp(policy_oid, oid))
146             return 1;
147     }
148     return 0;
149
150 }