xref: /openbsd-src/lib/libcrypto/x509/x509_pmaps.c (revision 2d7706badf6975d236773a4ee6404c41f5cbd116)
1*2d7706baStb /* $OpenBSD: x509_pmaps.c,v 1.6 2024/08/31 10:03:03 tb Exp $ */
2e500e238Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3e500e238Sjsing  * project.
4e500e238Sjsing  */
5e500e238Sjsing /* ====================================================================
6e500e238Sjsing  * Copyright (c) 2003 The OpenSSL Project.  All rights reserved.
7e500e238Sjsing  *
8e500e238Sjsing  * Redistribution and use in source and binary forms, with or without
9e500e238Sjsing  * modification, are permitted provided that the following conditions
10e500e238Sjsing  * are met:
11e500e238Sjsing  *
12e500e238Sjsing  * 1. Redistributions of source code must retain the above copyright
13e500e238Sjsing  *    notice, this list of conditions and the following disclaimer.
14e500e238Sjsing  *
15e500e238Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
16e500e238Sjsing  *    notice, this list of conditions and the following disclaimer in
17e500e238Sjsing  *    the documentation and/or other materials provided with the
18e500e238Sjsing  *    distribution.
19e500e238Sjsing  *
20e500e238Sjsing  * 3. All advertising materials mentioning features or use of this
21e500e238Sjsing  *    software must display the following acknowledgment:
22e500e238Sjsing  *    "This product includes software developed by the OpenSSL Project
23e500e238Sjsing  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24e500e238Sjsing  *
25e500e238Sjsing  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26e500e238Sjsing  *    endorse or promote products derived from this software without
27e500e238Sjsing  *    prior written permission. For written permission, please contact
28e500e238Sjsing  *    licensing@OpenSSL.org.
29e500e238Sjsing  *
30e500e238Sjsing  * 5. Products derived from this software may not be called "OpenSSL"
31e500e238Sjsing  *    nor may "OpenSSL" appear in their names without prior written
32e500e238Sjsing  *    permission of the OpenSSL Project.
33e500e238Sjsing  *
34e500e238Sjsing  * 6. Redistributions of any form whatsoever must retain the following
35e500e238Sjsing  *    acknowledgment:
36e500e238Sjsing  *    "This product includes software developed by the OpenSSL Project
37e500e238Sjsing  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38e500e238Sjsing  *
39e500e238Sjsing  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40e500e238Sjsing  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41e500e238Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42e500e238Sjsing  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43e500e238Sjsing  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44e500e238Sjsing  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45e500e238Sjsing  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46e500e238Sjsing  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47e500e238Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48e500e238Sjsing  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49e500e238Sjsing  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50e500e238Sjsing  * OF THE POSSIBILITY OF SUCH DAMAGE.
51e500e238Sjsing  * ====================================================================
52e500e238Sjsing  *
53e500e238Sjsing  * This product includes cryptographic software written by Eric Young
54e500e238Sjsing  * (eay@cryptsoft.com).  This product includes software written by Tim
55e500e238Sjsing  * Hudson (tjh@cryptsoft.com).
56e500e238Sjsing  *
57e500e238Sjsing  */
58e500e238Sjsing 
59e500e238Sjsing 
60e500e238Sjsing #include <stdio.h>
61e500e238Sjsing 
62e500e238Sjsing #include <openssl/asn1t.h>
63e500e238Sjsing #include <openssl/conf.h>
64e500e238Sjsing #include <openssl/err.h>
65e500e238Sjsing #include <openssl/x509v3.h>
66e500e238Sjsing 
67*2d7706baStb #include "x509_local.h"
68*2d7706baStb 
69e500e238Sjsing static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
70e500e238Sjsing     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
71e500e238Sjsing static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(
72e500e238Sjsing     const X509V3_EXT_METHOD *method, void *pmps, STACK_OF(CONF_VALUE) *extlist);
73e500e238Sjsing 
748b5faa71Stb static const X509V3_EXT_METHOD x509v3_ext_policy_mappings = {
75e500e238Sjsing 	.ext_nid = NID_policy_mappings,
76e500e238Sjsing 	.ext_flags = 0,
77e500e238Sjsing 	.it = &POLICY_MAPPINGS_it,
78e500e238Sjsing 	.ext_new = NULL,
79e500e238Sjsing 	.ext_free = NULL,
80e500e238Sjsing 	.d2i = NULL,
81e500e238Sjsing 	.i2d = NULL,
82e500e238Sjsing 	.i2s = NULL,
83e500e238Sjsing 	.s2i = NULL,
84e500e238Sjsing 	.i2v = i2v_POLICY_MAPPINGS,
85e500e238Sjsing 	.v2i = v2i_POLICY_MAPPINGS,
86e500e238Sjsing 	.i2r = NULL,
87e500e238Sjsing 	.r2i = NULL,
88e500e238Sjsing 	.usr_data = NULL,
89e500e238Sjsing };
90e500e238Sjsing 
918b5faa71Stb const X509V3_EXT_METHOD *
928b5faa71Stb x509v3_ext_method_policy_mappings(void)
938b5faa71Stb {
948b5faa71Stb 	return &x509v3_ext_policy_mappings;
958b5faa71Stb }
968b5faa71Stb 
97e500e238Sjsing static const ASN1_TEMPLATE POLICY_MAPPING_seq_tt[] = {
98e500e238Sjsing 	{
99e500e238Sjsing 		.flags = 0,
100e500e238Sjsing 		.tag = 0,
101e500e238Sjsing 		.offset = offsetof(POLICY_MAPPING, issuerDomainPolicy),
102e500e238Sjsing 		.field_name = "issuerDomainPolicy",
103e500e238Sjsing 		.item = &ASN1_OBJECT_it,
104e500e238Sjsing 	},
105e500e238Sjsing 	{
106e500e238Sjsing 		.flags = 0,
107e500e238Sjsing 		.tag = 0,
108e500e238Sjsing 		.offset = offsetof(POLICY_MAPPING, subjectDomainPolicy),
109e500e238Sjsing 		.field_name = "subjectDomainPolicy",
110e500e238Sjsing 		.item = &ASN1_OBJECT_it,
111e500e238Sjsing 	},
112e500e238Sjsing };
113e500e238Sjsing 
114e500e238Sjsing const ASN1_ITEM POLICY_MAPPING_it = {
115e500e238Sjsing 	.itype = ASN1_ITYPE_SEQUENCE,
116e500e238Sjsing 	.utype = V_ASN1_SEQUENCE,
117e500e238Sjsing 	.templates = POLICY_MAPPING_seq_tt,
118e500e238Sjsing 	.tcount = sizeof(POLICY_MAPPING_seq_tt) / sizeof(ASN1_TEMPLATE),
119e500e238Sjsing 	.funcs = NULL,
120e500e238Sjsing 	.size = sizeof(POLICY_MAPPING),
121e500e238Sjsing 	.sname = "POLICY_MAPPING",
122e500e238Sjsing };
123c0ebdaf2Sbeck LCRYPTO_ALIAS(POLICY_MAPPING_it);
124e500e238Sjsing 
125e500e238Sjsing static const ASN1_TEMPLATE POLICY_MAPPINGS_item_tt = {
126e500e238Sjsing 	.flags = ASN1_TFLG_SEQUENCE_OF,
127e500e238Sjsing 	.tag = 0,
128e500e238Sjsing 	.offset = 0,
129e500e238Sjsing 	.field_name = "POLICY_MAPPINGS",
130e500e238Sjsing 	.item = &POLICY_MAPPING_it,
131e500e238Sjsing };
132e500e238Sjsing 
133e500e238Sjsing const ASN1_ITEM POLICY_MAPPINGS_it = {
134e500e238Sjsing 	.itype = ASN1_ITYPE_PRIMITIVE,
135e500e238Sjsing 	.utype = -1,
136e500e238Sjsing 	.templates = &POLICY_MAPPINGS_item_tt,
137e500e238Sjsing 	.tcount = 0,
138e500e238Sjsing 	.funcs = NULL,
139e500e238Sjsing 	.size = 0,
140e500e238Sjsing 	.sname = "POLICY_MAPPINGS",
141e500e238Sjsing };
142c0ebdaf2Sbeck LCRYPTO_ALIAS(POLICY_MAPPINGS_it);
143e500e238Sjsing 
144e500e238Sjsing 
145e500e238Sjsing POLICY_MAPPING *
146e500e238Sjsing POLICY_MAPPING_new(void)
147e500e238Sjsing {
148e500e238Sjsing 	return (POLICY_MAPPING*)ASN1_item_new(&POLICY_MAPPING_it);
149e500e238Sjsing }
150cedac418Stb LCRYPTO_ALIAS(POLICY_MAPPING_new);
151e500e238Sjsing 
152e500e238Sjsing void
153e500e238Sjsing POLICY_MAPPING_free(POLICY_MAPPING *a)
154e500e238Sjsing {
155e500e238Sjsing 	ASN1_item_free((ASN1_VALUE *)a, &POLICY_MAPPING_it);
156e500e238Sjsing }
157cedac418Stb LCRYPTO_ALIAS(POLICY_MAPPING_free);
158e500e238Sjsing 
159e500e238Sjsing static STACK_OF(CONF_VALUE) *
160e500e238Sjsing i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, void *a,
161e500e238Sjsing     STACK_OF(CONF_VALUE) *extlist)
162e500e238Sjsing {
163e500e238Sjsing 	STACK_OF(CONF_VALUE) *free_extlist = NULL;
164e500e238Sjsing 	POLICY_MAPPINGS *pmaps = a;
165e500e238Sjsing 	POLICY_MAPPING *pmap;
166e500e238Sjsing 	char issuer[80], subject[80];
167e500e238Sjsing 	int i;
168e500e238Sjsing 
169e500e238Sjsing 	if (extlist == NULL) {
170e500e238Sjsing 		if ((free_extlist = extlist = sk_CONF_VALUE_new_null()) == NULL)
171e500e238Sjsing 			return NULL;
172e500e238Sjsing 	}
173e500e238Sjsing 
174e500e238Sjsing 	for (i = 0; i < sk_POLICY_MAPPING_num(pmaps); i++) {
175e500e238Sjsing 		if ((pmap = sk_POLICY_MAPPING_value(pmaps, i)) == NULL)
176e500e238Sjsing 			goto err;
177e500e238Sjsing 		if (!i2t_ASN1_OBJECT(issuer, sizeof issuer,
178e500e238Sjsing 		    pmap->issuerDomainPolicy))
179e500e238Sjsing 			goto err;
180e500e238Sjsing 		if (!i2t_ASN1_OBJECT(subject, sizeof subject,
181e500e238Sjsing 		    pmap->subjectDomainPolicy))
182e500e238Sjsing 			goto err;
183e500e238Sjsing 		if (!X509V3_add_value(issuer, subject, &extlist))
184e500e238Sjsing 			goto err;
185e500e238Sjsing 	}
186e500e238Sjsing 
187e500e238Sjsing 	return extlist;
188e500e238Sjsing 
189e500e238Sjsing  err:
190e500e238Sjsing 	sk_CONF_VALUE_pop_free(free_extlist, X509V3_conf_free);
191e500e238Sjsing 
192e500e238Sjsing 	return NULL;
193e500e238Sjsing }
194e500e238Sjsing 
195e500e238Sjsing static void *
196e500e238Sjsing v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
197e500e238Sjsing     STACK_OF(CONF_VALUE) *nval)
198e500e238Sjsing {
199e500e238Sjsing 	POLICY_MAPPINGS *pmaps = NULL;
200e500e238Sjsing 	POLICY_MAPPING *pmap = NULL;
201e500e238Sjsing 	ASN1_OBJECT *obj1 = NULL, *obj2 = NULL;
202e500e238Sjsing 	CONF_VALUE *val;
203e500e238Sjsing 	int i, rc;
204e500e238Sjsing 
205e500e238Sjsing 	if (!(pmaps = sk_POLICY_MAPPING_new_null())) {
206e500e238Sjsing 		X509V3error(ERR_R_MALLOC_FAILURE);
207e500e238Sjsing 		return NULL;
208e500e238Sjsing 	}
209e500e238Sjsing 
210e500e238Sjsing 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
211e500e238Sjsing 		val = sk_CONF_VALUE_value(nval, i);
212e500e238Sjsing 		if (!val->value || !val->name) {
213e500e238Sjsing 			rc = X509V3_R_INVALID_OBJECT_IDENTIFIER;
214e500e238Sjsing 			goto err;
215e500e238Sjsing 		}
216e500e238Sjsing 		obj1 = OBJ_txt2obj(val->name, 0);
217e500e238Sjsing 		obj2 = OBJ_txt2obj(val->value, 0);
218e500e238Sjsing 		if (!obj1 || !obj2) {
219e500e238Sjsing 			rc = X509V3_R_INVALID_OBJECT_IDENTIFIER;
220e500e238Sjsing 			goto err;
221e500e238Sjsing 		}
222e500e238Sjsing 		pmap = POLICY_MAPPING_new();
223e500e238Sjsing 		if (!pmap) {
224e500e238Sjsing 	    		rc = ERR_R_MALLOC_FAILURE;
225e500e238Sjsing 			goto err;
226e500e238Sjsing 		}
227e500e238Sjsing 		pmap->issuerDomainPolicy = obj1;
228e500e238Sjsing 		pmap->subjectDomainPolicy = obj2;
229e500e238Sjsing 		obj1 = obj2 = NULL;
230e500e238Sjsing 		if (sk_POLICY_MAPPING_push(pmaps, pmap) == 0) {
231e500e238Sjsing 	    		rc = ERR_R_MALLOC_FAILURE;
232e500e238Sjsing 			goto err;
233e500e238Sjsing 		}
234e500e238Sjsing 		pmap = NULL;
235e500e238Sjsing 	}
236e500e238Sjsing 	return pmaps;
237e500e238Sjsing 
238e500e238Sjsing err:
239e500e238Sjsing 	sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free);
240e500e238Sjsing 	X509V3error(rc);
241e500e238Sjsing 	if (rc == X509V3_R_INVALID_OBJECT_IDENTIFIER)
242e500e238Sjsing 		X509V3_conf_err(val);
243e500e238Sjsing 	ASN1_OBJECT_free(obj1);
244e500e238Sjsing 	ASN1_OBJECT_free(obj2);
245e500e238Sjsing 	POLICY_MAPPING_free(pmap);
246e500e238Sjsing 	return NULL;
247e500e238Sjsing }
248