| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-09-07 16:59:15 +08:00
										 |  |  |  * Copyright 2004-2023 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  | STACK_OF(X509_POLICY_NODE) *ossl_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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  | X509_POLICY_NODE *ossl_policy_tree_find_sk(STACK_OF(X509_POLICY_NODE) *nodes, | 
					
						
							|  |  |  |                                            const ASN1_OBJECT *id) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  | X509_POLICY_NODE *ossl_policy_level_find_node(const X509_POLICY_LEVEL *level, | 
					
						
							|  |  |  |                                               const X509_POLICY_NODE *parent, | 
					
						
							|  |  |  |                                               const ASN1_OBJECT *id) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  | X509_POLICY_NODE *ossl_policy_level_add_node(X509_POLICY_LEVEL *level, | 
					
						
							|  |  |  |                                              X509_POLICY_DATA *data, | 
					
						
							|  |  |  |                                              X509_POLICY_NODE *parent, | 
					
						
							| 
									
										
										
										
											2023-03-08 12:28:20 +08:00
										 |  |  |                                              X509_POLICY_TREE *tree, | 
					
						
							|  |  |  |                                              int extra_data) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     X509_POLICY_NODE *node; | 
					
						
							| 
									
										
										
										
											2015-09-03 21:15:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:28:20 +08:00
										 |  |  |     /* Verify that the tree isn't too large.  This mitigates CVE-2023-0464 */ | 
					
						
							|  |  |  |     if (tree->node_maximum > 0 && tree->node_count >= tree->node_maximum) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-03 21:15:26 +08:00
										 |  |  |     node = OPENSSL_zalloc(sizeof(*node)); | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |     if (node == NULL) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     node->data = data; | 
					
						
							|  |  |  |     node->parent = parent; | 
					
						
							| 
									
										
										
										
											2023-03-08 12:28:20 +08:00
										 |  |  |     if (level != NULL) { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  |                 level->nodes = ossl_policy_node_cmp_new(); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |             if (level->nodes == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |                 ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB); | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |                 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:28:20 +08:00
										 |  |  |     if (extra_data) { | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2021-10-26 15:16:18 +08:00
										 |  |  |         if (tree->extra_data == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |             ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); | 
					
						
							| 
									
										
										
										
											2023-05-24 18:22:25 +08:00
										 |  |  |             goto extra_data_error; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (!sk_X509_POLICY_DATA_push(tree->extra_data, data)) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |             ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); | 
					
						
							| 
									
										
										
										
											2023-05-24 18:22:25 +08:00
										 |  |  |             goto extra_data_error; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:28:20 +08:00
										 |  |  |     tree->node_count++; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |     if (parent) | 
					
						
							|  |  |  |         parent->nchild++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return node; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-24 18:22:25 +08:00
										 |  |  |  extra_data_error: | 
					
						
							|  |  |  |     if (level != NULL) { | 
					
						
							|  |  |  |         if (level->anyPolicy == node) | 
					
						
							|  |  |  |             level->anyPolicy = NULL; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             (void) sk_X509_POLICY_NODE_pop(level->nodes); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |  node_error: | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  |     ossl_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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  | void ossl_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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 11:23:45 +08:00
										 |  |  | int ossl_policy_node_match(const X509_POLICY_LEVEL *lvl, | 
					
						
							|  |  |  |                            const X509_POLICY_NODE *node, const ASN1_OBJECT *oid) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |