xref: /openbsd-src/lib/libcrypto/pkcs12/p12_crt.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /* $OpenBSD: p12_crt.c,v 1.23 2023/02/16 08:38:17 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2002 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 
61 #include <openssl/err.h>
62 #include <openssl/pkcs12.h>
63 
64 #include "pkcs12_local.h"
65 
66 static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
67     PKCS12_SAFEBAG *bag);
68 
69 static int
70 copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
71 {
72 	int idx;
73 	X509_ATTRIBUTE *attr;
74 
75 	idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
76 	if (idx < 0)
77 		return 1;
78 	attr = EVP_PKEY_get_attr(pkey, idx);
79 	if (!X509at_add1_attr(&bag->attrib, attr))
80 		return 0;
81 	return 1;
82 }
83 
84 PKCS12 *
85 PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert,
86     STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter,
87     int keytype)
88 {
89 	PKCS12 *p12 = NULL;
90 	STACK_OF(PKCS7) *safes = NULL;
91 	STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
92 	PKCS12_SAFEBAG *bag = NULL;
93 	int i;
94 	unsigned char keyid[EVP_MAX_MD_SIZE];
95 	unsigned int keyidlen = 0;
96 
97 	/* Set defaults */
98 	if (!nid_cert) {
99 		nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
100 	}
101 	if (!nid_key)
102 		nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
103 	if (!iter)
104 		iter = PKCS12_DEFAULT_ITER;
105 	if (!mac_iter)
106 		mac_iter = 1;
107 
108 	if (!pkey && !cert && !ca) {
109 		PKCS12error(PKCS12_R_INVALID_NULL_ARGUMENT);
110 		return NULL;
111 	}
112 
113 	if (pkey && cert) {
114 		if (!X509_check_private_key(cert, pkey))
115 			return NULL;
116 		if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen))
117 			return NULL;
118 	}
119 
120 	if (cert) {
121 		bag = PKCS12_add_cert(&bags, cert);
122 		if (name && !PKCS12_add_friendlyname(bag, name, -1))
123 			goto err;
124 		if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
125 			goto err;
126 	}
127 
128 	/* Add all other certificates */
129 	for (i = 0; i < sk_X509_num(ca); i++) {
130 		if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
131 			goto err;
132 	}
133 
134 	if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
135 		goto err;
136 
137 	sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
138 	bags = NULL;
139 
140 	if (pkey) {
141 		bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
142 
143 		if (!bag)
144 			goto err;
145 
146 		if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
147 			goto err;
148 		if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
149 			goto err;
150 
151 		if (name && !PKCS12_add_friendlyname(bag, name, -1))
152 			goto err;
153 		if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
154 			goto err;
155 	}
156 
157 	if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
158 		goto err;
159 
160 	sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
161 	bags = NULL;
162 
163 	p12 = PKCS12_add_safes(safes, 0);
164 
165 	if (!p12)
166 		goto err;
167 
168 	sk_PKCS7_pop_free(safes, PKCS7_free);
169 
170 	safes = NULL;
171 
172 	if ((mac_iter != -1) &&
173 	    !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
174 		goto err;
175 
176 	return p12;
177 
178 err:
179 	if (p12)
180 		PKCS12_free(p12);
181 	if (safes)
182 		sk_PKCS7_pop_free(safes, PKCS7_free);
183 	if (bags)
184 		sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
185 	return NULL;
186 }
187 LCRYPTO_ALIAS(PKCS12_create);
188 
189 PKCS12_SAFEBAG *
190 PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
191 {
192 	PKCS12_SAFEBAG *bag = NULL;
193 	char *name;
194 	int namelen = -1;
195 	unsigned char *keyid;
196 	int keyidlen = -1;
197 
198 	/* Add user certificate */
199 	if (!(bag = PKCS12_x5092certbag(cert)))
200 		goto err;
201 
202 	/* Use friendlyName and localKeyID in certificate.
203 	 * (if present)
204 	 */
205 	name = (char *)X509_alias_get0(cert, &namelen);
206 	if (name && !PKCS12_add_friendlyname(bag, name, namelen))
207 		goto err;
208 
209 	keyid = X509_keyid_get0(cert, &keyidlen);
210 
211 	if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
212 		goto err;
213 
214 	if (!pkcs12_add_bag(pbags, bag))
215 		goto err;
216 
217 	return bag;
218 
219 err:
220 	if (bag)
221 		PKCS12_SAFEBAG_free(bag);
222 
223 	return NULL;
224 }
225 LCRYPTO_ALIAS(PKCS12_add_cert);
226 
227 PKCS12_SAFEBAG *
228 PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key, int key_usage,
229     int iter, int nid_key, const char *pass)
230 {
231 	PKCS12_SAFEBAG *bag = NULL;
232 	PKCS8_PRIV_KEY_INFO *p8 = NULL;
233 
234 	/* Make a PKCS#8 structure */
235 	if (!(p8 = EVP_PKEY2PKCS8(key)))
236 		goto err;
237 	if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
238 		goto err;
239 	if (nid_key != -1) {
240 		bag = PKCS12_SAFEBAG_create_pkcs8_encrypt(nid_key, pass, -1,
241 		    NULL, 0, iter, p8);
242 		PKCS8_PRIV_KEY_INFO_free(p8);
243 		p8 = NULL;
244 	} else {
245 		bag = PKCS12_SAFEBAG_create0_p8inf(p8);
246 		if (bag != NULL)
247 			p8 = NULL;
248 	}
249 
250 	if (!bag)
251 		goto err;
252 
253 	if (!pkcs12_add_bag(pbags, bag))
254 		goto err;
255 
256 	return bag;
257 
258 err:
259 	if (bag)
260 		PKCS12_SAFEBAG_free(bag);
261 	if (p8)
262 		PKCS8_PRIV_KEY_INFO_free(p8);
263 
264 	return NULL;
265 }
266 LCRYPTO_ALIAS(PKCS12_add_key);
267 
268 int
269 PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
270     int nid_safe, int iter, const char *pass)
271 {
272 	PKCS7 *p7 = NULL;
273 	int free_safes = 0;
274 
275 	if (!*psafes) {
276 		*psafes = sk_PKCS7_new_null();
277 		if (!*psafes)
278 			return 0;
279 		free_safes = 1;
280 	} else
281 		free_safes = 0;
282 
283 	if (nid_safe == 0)
284 		nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
285 
286 	if (nid_safe == -1)
287 		p7 = PKCS12_pack_p7data(bags);
288 	else
289 		p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
290 		    iter, bags);
291 	if (!p7)
292 		goto err;
293 
294 	if (!sk_PKCS7_push(*psafes, p7))
295 		goto err;
296 
297 	return 1;
298 
299 err:
300 	if (free_safes) {
301 		sk_PKCS7_free(*psafes);
302 		*psafes = NULL;
303 	}
304 
305 	if (p7)
306 		PKCS7_free(p7);
307 
308 	return 0;
309 }
310 LCRYPTO_ALIAS(PKCS12_add_safe);
311 
312 static int
313 pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
314 {
315 	int free_bags;
316 
317 	if (!pbags)
318 		return 1;
319 	if (!*pbags) {
320 		*pbags = sk_PKCS12_SAFEBAG_new_null();
321 		if (!*pbags)
322 			return 0;
323 		free_bags = 1;
324 	} else
325 		free_bags = 0;
326 
327 	if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
328 		if (free_bags) {
329 			sk_PKCS12_SAFEBAG_free(*pbags);
330 			*pbags = NULL;
331 		}
332 		return 0;
333 	}
334 
335 	return 1;
336 }
337 
338 PKCS12 *
339 PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
340 {
341 	PKCS12 *p12;
342 
343 	if (nid_p7 <= 0)
344 		nid_p7 = NID_pkcs7_data;
345 	p12 = PKCS12_init(nid_p7);
346 
347 	if (!p12)
348 		return NULL;
349 
350 	if (!PKCS12_pack_authsafes(p12, safes)) {
351 		PKCS12_free(p12);
352 		return NULL;
353 	}
354 
355 	return p12;
356 }
357 LCRYPTO_ALIAS(PKCS12_add_safes);
358