xref: /openbsd/lib/libcrypto/x509/x509_akey.c (revision 8b5faa71)
1 /* $OpenBSD: x509_akey.c,v 1.2 2024/07/13 15:08:58 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/conf.h>
65 #include <openssl/err.h>
66 #include <openssl/x509v3.h>
67 
68 static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
69     AUTHORITY_KEYID *akeyid, STACK_OF(CONF_VALUE) *extlist);
70 static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
71     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *values);
72 
73 static const X509V3_EXT_METHOD x509v3_ext_authority_key_identifier = {
74 	.ext_nid = NID_authority_key_identifier,
75 	.ext_flags = X509V3_EXT_MULTILINE,
76 	.it = &AUTHORITY_KEYID_it,
77 	.ext_new = NULL,
78 	.ext_free = NULL,
79 	.d2i = NULL,
80 	.i2d = NULL,
81 	.i2s = NULL,
82 	.s2i = NULL,
83 	.i2v = (X509V3_EXT_I2V)i2v_AUTHORITY_KEYID,
84 	.v2i = (X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
85 	.i2r = NULL,
86 	.r2i = NULL,
87 	.usr_data = NULL,
88 };
89 
90 const X509V3_EXT_METHOD *
x509v3_ext_method_authority_key_identifier(void)91 x509v3_ext_method_authority_key_identifier(void)
92 {
93 	return &x509v3_ext_authority_key_identifier;
94 }
95 
STACK_OF(CONF_VALUE)96 static STACK_OF(CONF_VALUE) *
97 i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, AUTHORITY_KEYID *akeyid,
98     STACK_OF(CONF_VALUE) *extlist)
99 {
100 	STACK_OF(CONF_VALUE) *free_extlist = NULL;
101 	char *tmpstr = NULL;
102 
103 	if (extlist == NULL) {
104 		if ((free_extlist = extlist = sk_CONF_VALUE_new_null()) == NULL)
105 			return NULL;
106 	}
107 
108 	if (akeyid->keyid != NULL) {
109 		if ((tmpstr = hex_to_string(akeyid->keyid->data,
110 		    akeyid->keyid->length)) == NULL)
111 			goto err;
112 		if (!X509V3_add_value("keyid", tmpstr, &extlist))
113 			goto err;
114 		free(tmpstr);
115 		tmpstr = NULL;
116 	}
117 
118 	if (akeyid->issuer != NULL) {
119 		if ((extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer,
120 		    extlist)) == NULL)
121 			goto err;
122 	}
123 
124 	if (akeyid->serial != NULL) {
125 		if ((tmpstr = hex_to_string(akeyid->serial->data,
126 		    akeyid->serial->length)) == NULL)
127 			goto err;
128 		if (!X509V3_add_value("serial", tmpstr, &extlist))
129 			goto err;
130 		free(tmpstr);
131 		tmpstr = NULL;
132 	}
133 
134 	if (sk_CONF_VALUE_num(extlist) <= 0)
135 		goto err;
136 
137 	return extlist;
138 
139  err:
140 	free(tmpstr);
141 	sk_CONF_VALUE_pop_free(free_extlist, X509V3_conf_free);
142 
143 	return NULL;
144 }
145 
146 /*
147  * Currently two options:
148  * keyid: use the issuers subject keyid, the value 'always' means its is
149  * an error if the issuer certificate doesn't have a key id.
150  * issuer: use the issuers cert issuer and serial number. The default is
151  * to only use this if keyid is not present. With the option 'always'
152  * this is always included.
153  */
154 static AUTHORITY_KEYID *
v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* values)155 v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
156     STACK_OF(CONF_VALUE) *values)
157 {
158 	char keyid = 0, issuer = 0;
159 	int i;
160 	CONF_VALUE *cnf;
161 	ASN1_OCTET_STRING *ikeyid = NULL;
162 	X509_NAME *isname = NULL;
163 	STACK_OF(GENERAL_NAME) *gens = NULL;
164 	GENERAL_NAME *gen = NULL;
165 	ASN1_INTEGER *serial = NULL;
166 	X509_EXTENSION *ext;
167 	X509 *cert;
168 	AUTHORITY_KEYID *akeyid = NULL;
169 
170 	for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
171 		cnf = sk_CONF_VALUE_value(values, i);
172 		if (!strcmp(cnf->name, "keyid")) {
173 			keyid = 1;
174 			if (cnf->value && !strcmp(cnf->value, "always"))
175 				keyid = 2;
176 		} else if (!strcmp(cnf->name, "issuer")) {
177 			issuer = 1;
178 			if (cnf->value && !strcmp(cnf->value, "always"))
179 				issuer = 2;
180 		} else {
181 			X509V3error(X509V3_R_UNKNOWN_OPTION);
182 			ERR_asprintf_error_data("name=%s", cnf->name);
183 			return NULL;
184 		}
185 	}
186 
187 	if (!ctx || !ctx->issuer_cert) {
188 		if (ctx && (ctx->flags == CTX_TEST))
189 			return AUTHORITY_KEYID_new();
190 		X509V3error(X509V3_R_NO_ISSUER_CERTIFICATE);
191 		return NULL;
192 	}
193 
194 	cert = ctx->issuer_cert;
195 
196 	if (keyid) {
197 		i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
198 		if ((i >= 0)  && (ext = X509_get_ext(cert, i)))
199 			ikeyid = X509V3_EXT_d2i(ext);
200 		if (keyid == 2 && !ikeyid) {
201 			X509V3error(X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
202 			return NULL;
203 		}
204 	}
205 
206 	if ((issuer && !ikeyid) || (issuer == 2)) {
207 		isname = X509_NAME_dup(X509_get_issuer_name(cert));
208 		serial = ASN1_INTEGER_dup(X509_get_serialNumber(cert));
209 		if (!isname || !serial) {
210 			X509V3error(X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
211 			goto err;
212 		}
213 	}
214 
215 	if (!(akeyid = AUTHORITY_KEYID_new()))
216 		goto err;
217 
218 	if (isname) {
219 		if (!(gens = sk_GENERAL_NAME_new_null()) ||
220 		    !(gen = GENERAL_NAME_new()) ||
221 		    !sk_GENERAL_NAME_push(gens, gen)) {
222 			X509V3error(ERR_R_MALLOC_FAILURE);
223 			goto err;
224 		}
225 		gen->type = GEN_DIRNAME;
226 		gen->d.dirn = isname;
227 	}
228 
229 	akeyid->issuer = gens;
230 	akeyid->serial = serial;
231 	akeyid->keyid = ikeyid;
232 
233 	return akeyid;
234 
235  err:
236 	AUTHORITY_KEYID_free(akeyid);
237 	GENERAL_NAME_free(gen);
238 	sk_GENERAL_NAME_free(gens);
239 	X509_NAME_free(isname);
240 	ASN1_INTEGER_free(serial);
241 	ASN1_OCTET_STRING_free(ikeyid);
242 	return NULL;
243 }
244