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