xref: /openbsd-src/lib/libcrypto/x509/x509_akey.c (revision 2d7706badf6975d236773a4ee6404c41f5cbd116)
1*2d7706baStb /* $OpenBSD: x509_akey.c,v 1.3 2024/08/31 10:03:03 tb Exp $ */
2e500e238Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3e500e238Sjsing  * project 1999.
4e500e238Sjsing  */
5e500e238Sjsing /* ====================================================================
6e500e238Sjsing  * Copyright (c) 1999 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 #include <stdio.h>
60e500e238Sjsing #include <string.h>
61e500e238Sjsing 
62e500e238Sjsing #include <openssl/asn1.h>
63e500e238Sjsing #include <openssl/asn1t.h>
64e500e238Sjsing #include <openssl/conf.h>
65e500e238Sjsing #include <openssl/err.h>
66e500e238Sjsing #include <openssl/x509v3.h>
67e500e238Sjsing 
68*2d7706baStb #include "x509_local.h"
69*2d7706baStb 
70e500e238Sjsing static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
71e500e238Sjsing     AUTHORITY_KEYID *akeyid, STACK_OF(CONF_VALUE) *extlist);
72e500e238Sjsing static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
73e500e238Sjsing     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *values);
74e500e238Sjsing 
758b5faa71Stb static const X509V3_EXT_METHOD x509v3_ext_authority_key_identifier = {
76e500e238Sjsing 	.ext_nid = NID_authority_key_identifier,
77e500e238Sjsing 	.ext_flags = X509V3_EXT_MULTILINE,
78e500e238Sjsing 	.it = &AUTHORITY_KEYID_it,
79e500e238Sjsing 	.ext_new = NULL,
80e500e238Sjsing 	.ext_free = NULL,
81e500e238Sjsing 	.d2i = NULL,
82e500e238Sjsing 	.i2d = NULL,
83e500e238Sjsing 	.i2s = NULL,
84e500e238Sjsing 	.s2i = NULL,
85e500e238Sjsing 	.i2v = (X509V3_EXT_I2V)i2v_AUTHORITY_KEYID,
86e500e238Sjsing 	.v2i = (X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
87e500e238Sjsing 	.i2r = NULL,
88e500e238Sjsing 	.r2i = NULL,
89e500e238Sjsing 	.usr_data = NULL,
90e500e238Sjsing };
91e500e238Sjsing 
928b5faa71Stb const X509V3_EXT_METHOD *
938b5faa71Stb x509v3_ext_method_authority_key_identifier(void)
948b5faa71Stb {
958b5faa71Stb 	return &x509v3_ext_authority_key_identifier;
968b5faa71Stb }
978b5faa71Stb 
98e500e238Sjsing static STACK_OF(CONF_VALUE) *
99e500e238Sjsing i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, AUTHORITY_KEYID *akeyid,
100e500e238Sjsing     STACK_OF(CONF_VALUE) *extlist)
101e500e238Sjsing {
102e500e238Sjsing 	STACK_OF(CONF_VALUE) *free_extlist = NULL;
103e500e238Sjsing 	char *tmpstr = NULL;
104e500e238Sjsing 
105e500e238Sjsing 	if (extlist == NULL) {
106e500e238Sjsing 		if ((free_extlist = extlist = sk_CONF_VALUE_new_null()) == NULL)
107e500e238Sjsing 			return NULL;
108e500e238Sjsing 	}
109e500e238Sjsing 
110e500e238Sjsing 	if (akeyid->keyid != NULL) {
111e500e238Sjsing 		if ((tmpstr = hex_to_string(akeyid->keyid->data,
112e500e238Sjsing 		    akeyid->keyid->length)) == NULL)
113e500e238Sjsing 			goto err;
114e500e238Sjsing 		if (!X509V3_add_value("keyid", tmpstr, &extlist))
115e500e238Sjsing 			goto err;
116e500e238Sjsing 		free(tmpstr);
117e500e238Sjsing 		tmpstr = NULL;
118e500e238Sjsing 	}
119e500e238Sjsing 
120e500e238Sjsing 	if (akeyid->issuer != NULL) {
121e500e238Sjsing 		if ((extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer,
122e500e238Sjsing 		    extlist)) == NULL)
123e500e238Sjsing 			goto err;
124e500e238Sjsing 	}
125e500e238Sjsing 
126e500e238Sjsing 	if (akeyid->serial != NULL) {
127e500e238Sjsing 		if ((tmpstr = hex_to_string(akeyid->serial->data,
128e500e238Sjsing 		    akeyid->serial->length)) == NULL)
129e500e238Sjsing 			goto err;
130e500e238Sjsing 		if (!X509V3_add_value("serial", tmpstr, &extlist))
131e500e238Sjsing 			goto err;
132e500e238Sjsing 		free(tmpstr);
133e500e238Sjsing 		tmpstr = NULL;
134e500e238Sjsing 	}
135e500e238Sjsing 
136e500e238Sjsing 	if (sk_CONF_VALUE_num(extlist) <= 0)
137e500e238Sjsing 		goto err;
138e500e238Sjsing 
139e500e238Sjsing 	return extlist;
140e500e238Sjsing 
141e500e238Sjsing  err:
142e500e238Sjsing 	free(tmpstr);
143e500e238Sjsing 	sk_CONF_VALUE_pop_free(free_extlist, X509V3_conf_free);
144e500e238Sjsing 
145e500e238Sjsing 	return NULL;
146e500e238Sjsing }
147e500e238Sjsing 
148e500e238Sjsing /*
149e500e238Sjsing  * Currently two options:
150e500e238Sjsing  * keyid: use the issuers subject keyid, the value 'always' means its is
151e500e238Sjsing  * an error if the issuer certificate doesn't have a key id.
152e500e238Sjsing  * issuer: use the issuers cert issuer and serial number. The default is
153e500e238Sjsing  * to only use this if keyid is not present. With the option 'always'
154e500e238Sjsing  * this is always included.
155e500e238Sjsing  */
156e500e238Sjsing static AUTHORITY_KEYID *
157e500e238Sjsing v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
158e500e238Sjsing     STACK_OF(CONF_VALUE) *values)
159e500e238Sjsing {
160e500e238Sjsing 	char keyid = 0, issuer = 0;
161e500e238Sjsing 	int i;
162e500e238Sjsing 	CONF_VALUE *cnf;
163e500e238Sjsing 	ASN1_OCTET_STRING *ikeyid = NULL;
164e500e238Sjsing 	X509_NAME *isname = NULL;
165e500e238Sjsing 	STACK_OF(GENERAL_NAME) *gens = NULL;
166e500e238Sjsing 	GENERAL_NAME *gen = NULL;
167e500e238Sjsing 	ASN1_INTEGER *serial = NULL;
168e500e238Sjsing 	X509_EXTENSION *ext;
169e500e238Sjsing 	X509 *cert;
170e500e238Sjsing 	AUTHORITY_KEYID *akeyid = NULL;
171e500e238Sjsing 
172e500e238Sjsing 	for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
173e500e238Sjsing 		cnf = sk_CONF_VALUE_value(values, i);
174e500e238Sjsing 		if (!strcmp(cnf->name, "keyid")) {
175e500e238Sjsing 			keyid = 1;
176e500e238Sjsing 			if (cnf->value && !strcmp(cnf->value, "always"))
177e500e238Sjsing 				keyid = 2;
178e500e238Sjsing 		} else if (!strcmp(cnf->name, "issuer")) {
179e500e238Sjsing 			issuer = 1;
180e500e238Sjsing 			if (cnf->value && !strcmp(cnf->value, "always"))
181e500e238Sjsing 				issuer = 2;
182e500e238Sjsing 		} else {
183e500e238Sjsing 			X509V3error(X509V3_R_UNKNOWN_OPTION);
184e500e238Sjsing 			ERR_asprintf_error_data("name=%s", cnf->name);
185e500e238Sjsing 			return NULL;
186e500e238Sjsing 		}
187e500e238Sjsing 	}
188e500e238Sjsing 
189e500e238Sjsing 	if (!ctx || !ctx->issuer_cert) {
190e500e238Sjsing 		if (ctx && (ctx->flags == CTX_TEST))
191e500e238Sjsing 			return AUTHORITY_KEYID_new();
192e500e238Sjsing 		X509V3error(X509V3_R_NO_ISSUER_CERTIFICATE);
193e500e238Sjsing 		return NULL;
194e500e238Sjsing 	}
195e500e238Sjsing 
196e500e238Sjsing 	cert = ctx->issuer_cert;
197e500e238Sjsing 
198e500e238Sjsing 	if (keyid) {
199e500e238Sjsing 		i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
200e500e238Sjsing 		if ((i >= 0)  && (ext = X509_get_ext(cert, i)))
201e500e238Sjsing 			ikeyid = X509V3_EXT_d2i(ext);
202e500e238Sjsing 		if (keyid == 2 && !ikeyid) {
203e500e238Sjsing 			X509V3error(X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
204e500e238Sjsing 			return NULL;
205e500e238Sjsing 		}
206e500e238Sjsing 	}
207e500e238Sjsing 
208e500e238Sjsing 	if ((issuer && !ikeyid) || (issuer == 2)) {
209e500e238Sjsing 		isname = X509_NAME_dup(X509_get_issuer_name(cert));
210e500e238Sjsing 		serial = ASN1_INTEGER_dup(X509_get_serialNumber(cert));
211e500e238Sjsing 		if (!isname || !serial) {
212e500e238Sjsing 			X509V3error(X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
213e500e238Sjsing 			goto err;
214e500e238Sjsing 		}
215e500e238Sjsing 	}
216e500e238Sjsing 
217e500e238Sjsing 	if (!(akeyid = AUTHORITY_KEYID_new()))
218e500e238Sjsing 		goto err;
219e500e238Sjsing 
220e500e238Sjsing 	if (isname) {
221e500e238Sjsing 		if (!(gens = sk_GENERAL_NAME_new_null()) ||
222e500e238Sjsing 		    !(gen = GENERAL_NAME_new()) ||
223e500e238Sjsing 		    !sk_GENERAL_NAME_push(gens, gen)) {
224e500e238Sjsing 			X509V3error(ERR_R_MALLOC_FAILURE);
225e500e238Sjsing 			goto err;
226e500e238Sjsing 		}
227e500e238Sjsing 		gen->type = GEN_DIRNAME;
228e500e238Sjsing 		gen->d.dirn = isname;
229e500e238Sjsing 	}
230e500e238Sjsing 
231e500e238Sjsing 	akeyid->issuer = gens;
232e500e238Sjsing 	akeyid->serial = serial;
233e500e238Sjsing 	akeyid->keyid = ikeyid;
234e500e238Sjsing 
235e500e238Sjsing 	return akeyid;
236e500e238Sjsing 
237e500e238Sjsing  err:
238e500e238Sjsing 	AUTHORITY_KEYID_free(akeyid);
239e500e238Sjsing 	GENERAL_NAME_free(gen);
240e500e238Sjsing 	sk_GENERAL_NAME_free(gens);
241e500e238Sjsing 	X509_NAME_free(isname);
242e500e238Sjsing 	ASN1_INTEGER_free(serial);
243e500e238Sjsing 	ASN1_OCTET_STRING_free(ikeyid);
244e500e238Sjsing 	return NULL;
245e500e238Sjsing }
246