]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
9a48b07e | 3 | * project. |
ee0508d4 DSH |
4 | */ |
5 | /* ==================================================================== | |
f3dea9a5 | 6 | * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved. |
ee0508d4 DSH |
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 | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
ee0508d4 DSH |
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 | ||
6e04afb8 | 59 | #include <openssl/opensslconf.h> |
474e469b | 60 | #if !defined(OPENSSL_NO_DES) |
6e04afb8 | 61 | |
0f113f3e MC |
62 | # include <stdio.h> |
63 | # include <stdlib.h> | |
64 | # include <string.h> | |
65 | # include "apps.h" | |
66 | # include <openssl/crypto.h> | |
67 | # include <openssl/err.h> | |
68 | # include <openssl/pem.h> | |
69 | # include <openssl/pkcs12.h> | |
ee0508d4 | 70 | |
0f113f3e MC |
71 | # define NOKEYS 0x1 |
72 | # define NOCERTS 0x2 | |
73 | # define INFO 0x4 | |
74 | # define CLCERTS 0x8 | |
75 | # define CACERTS 0x10 | |
76 | ||
77 | int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain); | |
78 | int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen, | |
7e1b7485 | 79 | int options, char *pempass, const EVP_CIPHER *enc); |
0f113f3e | 80 | int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags, |
7e1b7485 RS |
81 | char *pass, int passlen, int options, char *pempass, |
82 | const EVP_CIPHER *enc); | |
0f113f3e | 83 | int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass, |
7e1b7485 RS |
84 | int passlen, int options, char *pempass, |
85 | const EVP_CIPHER *enc); | |
0f113f3e MC |
86 | int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst, |
87 | const char *name); | |
ee0508d4 | 88 | void hex_prin(BIO *out, unsigned char *buf, int len); |
ecf3a1fb | 89 | static int alg_print(X509_ALGOR *alg); |
84c15db5 | 90 | int cert_load(BIO *in, STACK_OF(X509) *sk); |
7e1b7485 RS |
91 | static int set_pbe(int *ppbe, const char *str); |
92 | ||
93 | typedef enum OPTION_choice { | |
94 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
95 | OPT_CIPHER, OPT_NOKEYS, OPT_KEYEX, OPT_KEYSIG, OPT_NOCERTS, OPT_CLCERTS, | |
96 | OPT_CACERTS, OPT_NOOUT, OPT_INFO, OPT_CHAIN, OPT_TWOPASS, OPT_NOMACVER, | |
97 | OPT_DESCERT, OPT_EXPORT, OPT_NOITER, OPT_MACITER, OPT_NOMACITER, | |
98 | OPT_NOMAC, OPT_LMK, OPT_NODES, OPT_MACALG, OPT_CERTPBE, OPT_KEYPBE, | |
99 | OPT_RAND, OPT_INKEY, OPT_CERTFILE, OPT_NAME, OPT_CSP, OPT_CANAME, | |
100 | OPT_IN, OPT_OUT, OPT_PASSIN, OPT_PASSOUT, OPT_PASSWORD, OPT_CAPATH, | |
2b6bcb70 | 101 | OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_ENGINE |
7e1b7485 RS |
102 | } OPTION_CHOICE; |
103 | ||
104 | OPTIONS pkcs12_options[] = { | |
105 | {"help", OPT_HELP, '-', "Display this summary"}, | |
106 | {"nokeys", OPT_NOKEYS, '-', "Don't output private keys"}, | |
107 | {"keyex", OPT_KEYEX, '-', "Set MS key exchange type"}, | |
108 | {"keysig", OPT_KEYSIG, '-', "Set MS key signature type"}, | |
109 | {"nocerts", OPT_NOCERTS, '-', "Don't output certificates"}, | |
110 | {"clcerts", OPT_CLCERTS, '-', "Only output client certificates"}, | |
111 | {"cacerts", OPT_CACERTS, '-', "Only output CA certificates"}, | |
112 | {"noout", OPT_NOOUT, '-', "Don't output anything, just verify"}, | |
113 | {"info", OPT_INFO, '-', "Print info about PKCS#12 structure"}, | |
114 | {"chain", OPT_CHAIN, '-', "Add certificate chain"}, | |
115 | {"twopass", OPT_TWOPASS, '-', "Separate MAC, encryption passwords"}, | |
116 | {"nomacver", OPT_NOMACVER, '-', "Don't verify MAC"}, | |
117 | # ifndef OPENSSL_NO_RC2 | |
118 | {"descert", OPT_DESCERT, '-', | |
119 | "Encrypt output with 3DES (default RC2-40)"}, | |
120 | {"certpbe", OPT_CERTPBE, 's', | |
121 | "Certificate PBE algorithm (default RC2-40)"}, | |
122 | # else | |
123 | {"descert", OPT_DESCERT, '-', "Encrypt output with 3DES (the default)"}, | |
124 | {"certpbe", OPT_CERTPBE, 's', "Certificate PBE algorithm (default 3DES)"}, | |
125 | # endif | |
126 | {"export", OPT_EXPORT, '-', "Output PKCS12 file"}, | |
127 | {"noiter", OPT_NOITER, '-', "Don't use encryption iteration"}, | |
128 | {"maciter", OPT_MACITER, '-', "Use MAC iteration"}, | |
129 | {"nomaciter", OPT_NOMACITER, '-', "Don't use MAC iteration"}, | |
130 | {"nomac", OPT_NOMAC, '-', "Don't generate MAC"}, | |
131 | {"LMK", OPT_LMK, '-', | |
132 | "Add local machine keyset attribute to private key"}, | |
133 | {"nodes", OPT_NODES, '-', "Don't encrypt private keys"}, | |
134 | {"macalg", OPT_MACALG, 's', | |
135 | "Digest algorithm used in MAC (default SHA1)"}, | |
136 | {"keypbe", OPT_KEYPBE, 's', "Private key PBE algorithm (default 3DES)"}, | |
137 | {"rand", OPT_RAND, 's', | |
138 | "Load the file(s) into the random number generator"}, | |
139 | {"inkey", OPT_INKEY, '<', "Private key if not infile"}, | |
140 | {"certfile", OPT_CERTFILE, '<', "Load certs from file"}, | |
141 | {"name", OPT_NAME, 's', "Use name as friendly name"}, | |
142 | {"CSP", OPT_CSP, 's', "Microsoft CSP name"}, | |
143 | {"caname", OPT_CANAME, 's', | |
144 | "Use name as CA friendly name (can be repeated)"}, | |
145 | {"in", OPT_IN, '<', "Input filename"}, | |
146 | {"out", OPT_OUT, '>', "Output filename"}, | |
147 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
148 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, | |
149 | {"password", OPT_PASSWORD, 's', "Set import/export password source"}, | |
150 | {"CApath", OPT_CAPATH, '/', "PEM-format directory of CA's"}, | |
151 | {"CAfile", OPT_CAFILE, '<', "PEM-format file of CA's"}, | |
2b6bcb70 MC |
152 | {"no-CAfile", OPT_NOCAFILE, '-', |
153 | "Do not load the default certificates file"}, | |
154 | {"no-CApath", OPT_NOCAPATH, '-', | |
155 | "Do not load certificates from the default certificates directory"}, | |
9c3bcfa0 | 156 | {"", OPT_CIPHER, '-', "Any supported cipher"}, |
7e1b7485 RS |
157 | # ifndef OPENSSL_NO_ENGINE |
158 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
159 | # endif | |
7e1b7485 RS |
160 | {NULL} |
161 | }; | |
667ac4ec | 162 | |
7e1b7485 | 163 | int pkcs12_main(int argc, char **argv) |
ee0508d4 | 164 | { |
7e1b7485 RS |
165 | char *infile = NULL, *outfile = NULL, *keyname = NULL, *certfile = NULL; |
166 | char *name = NULL, *csp_name = NULL; | |
65d3941f | 167 | char pass[2048], macpass[2048]; |
7e1b7485 RS |
168 | int export_cert = 0, options = 0, chain = 0, twopass = 0, keytype = 0; |
169 | int iter = PKCS12_DEFAULT_ITER, maciter = PKCS12_DEFAULT_ITER; | |
170 | # ifndef OPENSSL_NO_RC2 | |
ee0508d4 | 171 | int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC; |
7e1b7485 RS |
172 | # else |
173 | int cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; | |
174 | # endif | |
525f51f6 | 175 | int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; |
3b061a00 | 176 | int ret = 1, macver = 1, noprompt = 0, add_lmk = 0, private = 0; |
7e1b7485 RS |
177 | char *passinarg = NULL, *passoutarg = NULL, *passarg = NULL; |
178 | char *passin = NULL, *passout = NULL, *inrand = NULL, *macalg = NULL; | |
179 | char *cpass = NULL, *mpass = NULL, *CApath = NULL, *CAfile = NULL; | |
333b070e | 180 | char *prog; |
2b6bcb70 | 181 | int noCApath = 0, noCAfile = 0; |
7e1b7485 RS |
182 | ENGINE *e = NULL; |
183 | BIO *in = NULL, *out = NULL; | |
184 | PKCS12 *p12 = NULL; | |
c869da88 | 185 | STACK_OF(OPENSSL_STRING) *canames = NULL; |
7e1b7485 RS |
186 | const EVP_CIPHER *enc = EVP_des_ede3_cbc(); |
187 | OPTION_CHOICE o; | |
188 | ||
189 | prog = opt_init(argc, argv, pkcs12_options); | |
190 | while ((o = opt_next()) != OPT_EOF) { | |
191 | switch (o) { | |
192 | case OPT_EOF: | |
193 | case OPT_ERR: | |
194 | opthelp: | |
195 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
196 | goto end; | |
197 | case OPT_HELP: | |
198 | opt_help(pkcs12_options); | |
199 | ret = 0; | |
200 | goto end; | |
201 | case OPT_NOKEYS: | |
202 | options |= NOKEYS; | |
203 | break; | |
204 | case OPT_KEYEX: | |
205 | keytype = KEY_EX; | |
206 | break; | |
207 | case OPT_KEYSIG: | |
208 | keytype = KEY_SIG; | |
209 | break; | |
210 | case OPT_NOCERTS: | |
211 | options |= NOCERTS; | |
212 | break; | |
213 | case OPT_CLCERTS: | |
214 | options |= CLCERTS; | |
215 | break; | |
216 | case OPT_CACERTS: | |
217 | options |= CACERTS; | |
218 | break; | |
219 | case OPT_NOOUT: | |
220 | options |= (NOKEYS | NOCERTS); | |
221 | break; | |
222 | case OPT_INFO: | |
223 | options |= INFO; | |
224 | break; | |
225 | case OPT_CHAIN: | |
226 | chain = 1; | |
227 | break; | |
228 | case OPT_TWOPASS: | |
229 | twopass = 1; | |
230 | break; | |
231 | case OPT_NOMACVER: | |
232 | macver = 0; | |
233 | break; | |
234 | case OPT_DESCERT: | |
235 | cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; | |
236 | break; | |
237 | case OPT_EXPORT: | |
238 | export_cert = 1; | |
239 | break; | |
240 | case OPT_CIPHER: | |
241 | if (!opt_cipher(opt_unknown(), &enc)) | |
242 | goto opthelp; | |
243 | break; | |
244 | case OPT_NOITER: | |
245 | iter = 1; | |
246 | break; | |
247 | case OPT_MACITER: | |
248 | maciter = PKCS12_DEFAULT_ITER; | |
249 | break; | |
250 | case OPT_NOMACITER: | |
251 | maciter = 1; | |
252 | break; | |
253 | case OPT_NOMAC: | |
254 | maciter = -1; | |
255 | break; | |
256 | case OPT_MACALG: | |
257 | macalg = opt_arg(); | |
258 | break; | |
259 | case OPT_NODES: | |
260 | enc = NULL; | |
261 | break; | |
262 | case OPT_CERTPBE: | |
263 | if (!set_pbe(&cert_pbe, opt_arg())) | |
264 | goto opthelp; | |
265 | break; | |
266 | case OPT_KEYPBE: | |
267 | if (!set_pbe(&key_pbe, opt_arg())) | |
268 | goto opthelp; | |
269 | break; | |
270 | case OPT_RAND: | |
271 | inrand = opt_arg(); | |
272 | break; | |
273 | case OPT_INKEY: | |
274 | keyname = opt_arg(); | |
275 | break; | |
276 | case OPT_CERTFILE: | |
277 | certfile = opt_arg(); | |
278 | break; | |
279 | case OPT_NAME: | |
280 | name = opt_arg(); | |
281 | break; | |
282 | case OPT_LMK: | |
283 | add_lmk = 1; | |
284 | break; | |
285 | case OPT_CSP: | |
286 | csp_name = opt_arg(); | |
287 | break; | |
288 | case OPT_CANAME: | |
289 | if (canames == NULL | |
290 | && (canames = sk_OPENSSL_STRING_new_null()) == NULL) | |
291 | goto end; | |
292 | sk_OPENSSL_STRING_push(canames, opt_arg()); | |
293 | break; | |
294 | case OPT_IN: | |
295 | infile = opt_arg(); | |
296 | break; | |
297 | case OPT_OUT: | |
298 | outfile = opt_arg(); | |
299 | break; | |
300 | case OPT_PASSIN: | |
301 | passinarg = opt_arg(); | |
302 | break; | |
303 | case OPT_PASSOUT: | |
304 | passoutarg = opt_arg(); | |
305 | break; | |
306 | case OPT_PASSWORD: | |
307 | passarg = opt_arg(); | |
308 | break; | |
309 | case OPT_CAPATH: | |
310 | CApath = opt_arg(); | |
311 | break; | |
312 | case OPT_CAFILE: | |
313 | CAfile = opt_arg(); | |
314 | break; | |
2b6bcb70 MC |
315 | case OPT_NOCAPATH: |
316 | noCApath = 1; | |
317 | break; | |
318 | case OPT_NOCAFILE: | |
319 | noCAfile = 1; | |
320 | break; | |
7e1b7485 | 321 | case OPT_ENGINE: |
333b070e | 322 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
323 | break; |
324 | } | |
ee0508d4 | 325 | } |
7e1b7485 RS |
326 | argc = opt_num_rest(); |
327 | argv = opt_rest(); | |
3b061a00 | 328 | private = 1; |
ee0508d4 | 329 | |
0f113f3e MC |
330 | if (passarg) { |
331 | if (export_cert) | |
7e1b7485 | 332 | passoutarg = passarg; |
0f113f3e | 333 | else |
7e1b7485 | 334 | passinarg = passarg; |
a3fe382e DSH |
335 | } |
336 | ||
7e1b7485 | 337 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
338 | BIO_printf(bio_err, "Error getting passwords\n"); |
339 | goto end; | |
a3fe382e DSH |
340 | } |
341 | ||
0f113f3e MC |
342 | if (!cpass) { |
343 | if (export_cert) | |
344 | cpass = passout; | |
345 | else | |
346 | cpass = passin; | |
f07fb9b2 DSH |
347 | } |
348 | ||
0f113f3e MC |
349 | if (cpass) { |
350 | mpass = cpass; | |
351 | noprompt = 1; | |
f07fb9b2 | 352 | } else { |
0f113f3e MC |
353 | cpass = pass; |
354 | mpass = macpass; | |
e40b7abe DSH |
355 | } |
356 | ||
0f113f3e | 357 | if (export_cert || inrand) { |
7e1b7485 | 358 | app_RAND_load_file(NULL, (inrand != NULL)); |
d13e4eb0 | 359 | if (inrand != NULL) |
0f113f3e MC |
360 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", |
361 | app_RAND_load_files(inrand)); | |
d13e4eb0 | 362 | } |
a873356c | 363 | |
ee0508d4 | 364 | if (twopass) { |
0f113f3e MC |
365 | if (EVP_read_pw_string |
366 | (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) { | |
367 | BIO_printf(bio_err, "Can't read Password\n"); | |
368 | goto end; | |
369 | } | |
ee0508d4 DSH |
370 | } |
371 | ||
2d681b77 | 372 | if (export_cert) { |
0f113f3e MC |
373 | EVP_PKEY *key = NULL; |
374 | X509 *ucert = NULL, *x = NULL; | |
375 | STACK_OF(X509) *certs = NULL; | |
376 | const EVP_MD *macmd = NULL; | |
377 | unsigned char *catmp = NULL; | |
378 | int i; | |
379 | ||
380 | if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) { | |
381 | BIO_printf(bio_err, "Nothing to do!\n"); | |
382 | goto export_end; | |
383 | } | |
384 | ||
385 | if (options & NOCERTS) | |
386 | chain = 0; | |
387 | ||
0f113f3e | 388 | if (!(options & NOKEYS)) { |
7e1b7485 | 389 | key = load_key(keyname ? keyname : infile, |
0f113f3e MC |
390 | FORMAT_PEM, 1, passin, e, "private key"); |
391 | if (!key) | |
392 | goto export_end; | |
393 | } | |
0f113f3e MC |
394 | |
395 | /* Load in all certs in input file */ | |
396 | if (!(options & NOCERTS)) { | |
7e1b7485 | 397 | certs = load_certs(infile, FORMAT_PEM, NULL, e, |
0f113f3e MC |
398 | "certificates"); |
399 | if (!certs) | |
400 | goto export_end; | |
401 | ||
402 | if (key) { | |
403 | /* Look for matching private key */ | |
404 | for (i = 0; i < sk_X509_num(certs); i++) { | |
405 | x = sk_X509_value(certs, i); | |
406 | if (X509_check_private_key(x, key)) { | |
407 | ucert = x; | |
408 | /* Zero keyid and alias */ | |
409 | X509_keyid_set1(ucert, NULL, 0); | |
410 | X509_alias_set1(ucert, NULL, 0); | |
411 | /* Remove from list */ | |
412 | (void)sk_X509_delete(certs, i); | |
413 | break; | |
414 | } | |
415 | } | |
416 | if (!ucert) { | |
417 | BIO_printf(bio_err, | |
418 | "No certificate matches private key\n"); | |
419 | goto export_end; | |
420 | } | |
421 | } | |
422 | ||
423 | } | |
0f113f3e MC |
424 | |
425 | /* Add any more certificates asked for */ | |
426 | if (certfile) { | |
427 | STACK_OF(X509) *morecerts = NULL; | |
75ebbd9a RS |
428 | if ((morecerts = load_certs(certfile, FORMAT_PEM, NULL, e, |
429 | "certificates from certfile")) == NULL) | |
0f113f3e MC |
430 | goto export_end; |
431 | while (sk_X509_num(morecerts) > 0) | |
432 | sk_X509_push(certs, sk_X509_shift(morecerts)); | |
433 | sk_X509_free(morecerts); | |
434 | } | |
0f113f3e MC |
435 | |
436 | /* If chaining get chain from user cert */ | |
437 | if (chain) { | |
438 | int vret; | |
439 | STACK_OF(X509) *chain2; | |
7e1b7485 | 440 | X509_STORE *store; |
2b6bcb70 MC |
441 | if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) |
442 | == NULL) | |
0f113f3e | 443 | goto export_end; |
0f113f3e MC |
444 | |
445 | vret = get_cert_chain(ucert, store, &chain2); | |
446 | X509_STORE_free(store); | |
447 | ||
448 | if (!vret) { | |
449 | /* Exclude verified certificate */ | |
450 | for (i = 1; i < sk_X509_num(chain2); i++) | |
451 | sk_X509_push(certs, sk_X509_value(chain2, i)); | |
452 | /* Free first certificate */ | |
453 | X509_free(sk_X509_value(chain2, 0)); | |
454 | sk_X509_free(chain2); | |
455 | } else { | |
456 | if (vret >= 0) | |
457 | BIO_printf(bio_err, "Error %s getting chain.\n", | |
458 | X509_verify_cert_error_string(vret)); | |
459 | else | |
460 | ERR_print_errors(bio_err); | |
461 | goto export_end; | |
462 | } | |
463 | } | |
464 | ||
465 | /* Add any CA names */ | |
466 | ||
467 | for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) { | |
468 | catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i); | |
469 | X509_alias_set1(sk_X509_value(certs, i), catmp, -1); | |
470 | } | |
471 | ||
472 | if (csp_name && key) | |
473 | EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name, | |
474 | MBSTRING_ASC, (unsigned char *)csp_name, | |
475 | -1); | |
476 | ||
477 | if (add_lmk && key) | |
478 | EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1); | |
479 | ||
0f113f3e MC |
480 | if (!noprompt && |
481 | EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:", | |
482 | 1)) { | |
483 | BIO_printf(bio_err, "Can't read Password\n"); | |
484 | goto export_end; | |
485 | } | |
486 | if (!twopass) | |
7644a9ae | 487 | OPENSSL_strlcpy(macpass, pass, sizeof macpass); |
0f113f3e | 488 | |
0f113f3e MC |
489 | p12 = PKCS12_create(cpass, name, key, ucert, certs, |
490 | key_pbe, cert_pbe, iter, -1, keytype); | |
491 | ||
492 | if (!p12) { | |
493 | ERR_print_errors(bio_err); | |
494 | goto export_end; | |
495 | } | |
496 | ||
497 | if (macalg) { | |
7e1b7485 RS |
498 | if (!opt_md(macalg, &macmd)) |
499 | goto opthelp; | |
0f113f3e MC |
500 | } |
501 | ||
502 | if (maciter != -1) | |
503 | PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd); | |
504 | ||
3b061a00 | 505 | assert(private); |
bdd58d98 RL |
506 | |
507 | out = bio_open_owner(outfile, FORMAT_PKCS12, private); | |
508 | if (out == NULL) | |
509 | goto end; | |
510 | ||
0f113f3e MC |
511 | i2d_PKCS12_bio(out, p12); |
512 | ||
513 | ret = 0; | |
514 | ||
515 | export_end: | |
0f113f3e | 516 | |
c5ba2d99 | 517 | EVP_PKEY_free(key); |
222561fe RS |
518 | sk_X509_pop_free(certs, X509_free); |
519 | X509_free(ucert); | |
0f113f3e | 520 | |
0f113f3e | 521 | goto end; |
ee0508d4 | 522 | |
ee86c3f5 | 523 | } |
ee0508d4 | 524 | |
bdd58d98 RL |
525 | in = bio_open_default(infile, 'r', FORMAT_PKCS12); |
526 | if (in == NULL) | |
527 | goto end; | |
528 | out = bio_open_owner(outfile, FORMAT_PEM, private); | |
529 | if (out == NULL) | |
530 | goto end; | |
531 | ||
75ebbd9a | 532 | if ((p12 = d2i_PKCS12_bio(in, NULL)) == NULL) { |
0f113f3e MC |
533 | ERR_print_errors(bio_err); |
534 | goto end; | |
ee0508d4 | 535 | } |
7e1b7485 | 536 | |
0f113f3e MC |
537 | if (!noprompt |
538 | && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:", | |
539 | 0)) { | |
540 | BIO_printf(bio_err, "Can't read Password\n"); | |
541 | goto end; | |
ee0508d4 | 542 | } |
0f113f3e MC |
543 | |
544 | if (!twopass) | |
7644a9ae | 545 | OPENSSL_strlcpy(macpass, pass, sizeof macpass); |
0f113f3e MC |
546 | |
547 | if ((options & INFO) && p12->mac) | |
548 | BIO_printf(bio_err, "MAC Iteration %ld\n", | |
549 | p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1); | |
550 | if (macver) { | |
0f113f3e MC |
551 | /* If we enter empty password try no password first */ |
552 | if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) { | |
553 | /* If mac and crypto pass the same set it to NULL too */ | |
554 | if (!twopass) | |
555 | cpass = NULL; | |
556 | } else if (!PKCS12_verify_mac(p12, mpass, -1)) { | |
557 | BIO_printf(bio_err, "Mac verify error: invalid password?\n"); | |
558 | ERR_print_errors(bio_err); | |
559 | goto end; | |
560 | } | |
ee0508d4 | 561 | } |
7e1b7485 | 562 | |
3b061a00 | 563 | assert(private); |
7e1b7485 | 564 | if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout, enc)) { |
0f113f3e MC |
565 | BIO_printf(bio_err, "Error outputting keys and certificates\n"); |
566 | ERR_print_errors(bio_err); | |
567 | goto end; | |
ee0508d4 | 568 | } |
ee0508d4 | 569 | ret = 0; |
88364bc2 | 570 | end: |
e0e920b1 | 571 | PKCS12_free(p12); |
0f113f3e | 572 | if (export_cert || inrand) |
7e1b7485 | 573 | app_RAND_write_file(NULL); |
a873356c | 574 | BIO_free(in); |
645749ef | 575 | BIO_free_all(out); |
25aaa98a | 576 | sk_OPENSSL_STRING_free(canames); |
b548a1f1 RS |
577 | OPENSSL_free(passin); |
578 | OPENSSL_free(passout); | |
7e1b7485 | 579 | return (ret); |
ee0508d4 DSH |
580 | } |
581 | ||
0f113f3e | 582 | int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, |
7e1b7485 RS |
583 | int passlen, int options, char *pempass, |
584 | const EVP_CIPHER *enc) | |
ee0508d4 | 585 | { |
0f113f3e MC |
586 | STACK_OF(PKCS7) *asafes = NULL; |
587 | STACK_OF(PKCS12_SAFEBAG) *bags; | |
588 | int i, bagnid; | |
589 | int ret = 0; | |
590 | PKCS7 *p7; | |
591 | ||
75ebbd9a | 592 | if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) |
0f113f3e MC |
593 | return 0; |
594 | for (i = 0; i < sk_PKCS7_num(asafes); i++) { | |
595 | p7 = sk_PKCS7_value(asafes, i); | |
596 | bagnid = OBJ_obj2nid(p7->type); | |
597 | if (bagnid == NID_pkcs7_data) { | |
598 | bags = PKCS12_unpack_p7data(p7); | |
599 | if (options & INFO) | |
600 | BIO_printf(bio_err, "PKCS7 Data\n"); | |
601 | } else if (bagnid == NID_pkcs7_encrypted) { | |
602 | if (options & INFO) { | |
603 | BIO_printf(bio_err, "PKCS7 Encrypted data: "); | |
ecf3a1fb | 604 | alg_print(p7->d.encrypted->enc_data->algorithm); |
0f113f3e MC |
605 | } |
606 | bags = PKCS12_unpack_p7encdata(p7, pass, passlen); | |
607 | } else | |
608 | continue; | |
609 | if (!bags) | |
610 | goto err; | |
611 | if (!dump_certs_pkeys_bags(out, bags, pass, passlen, | |
7e1b7485 | 612 | options, pempass, enc)) { |
0f113f3e MC |
613 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); |
614 | goto err; | |
615 | } | |
616 | sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | |
617 | bags = NULL; | |
618 | } | |
619 | ret = 1; | |
620 | ||
621 | err: | |
e0e920b1 | 622 | sk_PKCS7_pop_free(asafes, PKCS7_free); |
0f113f3e | 623 | return ret; |
ee0508d4 DSH |
624 | } |
625 | ||
0f113f3e | 626 | int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags, |
7e1b7485 RS |
627 | char *pass, int passlen, int options, char *pempass, |
628 | const EVP_CIPHER *enc) | |
ee0508d4 | 629 | { |
0f113f3e MC |
630 | int i; |
631 | for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { | |
632 | if (!dump_certs_pkeys_bag(out, | |
633 | sk_PKCS12_SAFEBAG_value(bags, i), | |
7e1b7485 | 634 | pass, passlen, options, pempass, enc)) |
0f113f3e MC |
635 | return 0; |
636 | } | |
637 | return 1; | |
ee0508d4 DSH |
638 | } |
639 | ||
0f113f3e | 640 | int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass, |
7e1b7485 RS |
641 | int passlen, int options, char *pempass, |
642 | const EVP_CIPHER *enc) | |
ee0508d4 | 643 | { |
0f113f3e MC |
644 | EVP_PKEY *pkey; |
645 | PKCS8_PRIV_KEY_INFO *p8; | |
646 | X509 *x509; | |
647 | ||
648 | switch (M_PKCS12_bag_type(bag)) { | |
649 | case NID_keyBag: | |
650 | if (options & INFO) | |
651 | BIO_printf(bio_err, "Key bag\n"); | |
652 | if (options & NOKEYS) | |
653 | return 1; | |
654 | print_attribs(out, bag->attrib, "Bag Attributes"); | |
655 | p8 = bag->value.keybag; | |
75ebbd9a | 656 | if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) |
0f113f3e MC |
657 | return 0; |
658 | print_attribs(out, p8->attributes, "Key Attributes"); | |
659 | PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass); | |
660 | EVP_PKEY_free(pkey); | |
661 | break; | |
662 | ||
663 | case NID_pkcs8ShroudedKeyBag: | |
664 | if (options & INFO) { | |
665 | BIO_printf(bio_err, "Shrouded Keybag: "); | |
ecf3a1fb | 666 | alg_print(bag->value.shkeybag->algor); |
0f113f3e MC |
667 | } |
668 | if (options & NOKEYS) | |
669 | return 1; | |
670 | print_attribs(out, bag->attrib, "Bag Attributes"); | |
75ebbd9a | 671 | if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL) |
0f113f3e | 672 | return 0; |
75ebbd9a | 673 | if ((pkey = EVP_PKCS82PKEY(p8)) == NULL) { |
0f113f3e MC |
674 | PKCS8_PRIV_KEY_INFO_free(p8); |
675 | return 0; | |
676 | } | |
677 | print_attribs(out, p8->attributes, "Key Attributes"); | |
678 | PKCS8_PRIV_KEY_INFO_free(p8); | |
679 | PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass); | |
680 | EVP_PKEY_free(pkey); | |
681 | break; | |
682 | ||
683 | case NID_certBag: | |
684 | if (options & INFO) | |
685 | BIO_printf(bio_err, "Certificate bag\n"); | |
686 | if (options & NOCERTS) | |
687 | return 1; | |
688 | if (PKCS12_get_attr(bag, NID_localKeyID)) { | |
689 | if (options & CACERTS) | |
690 | return 1; | |
691 | } else if (options & CLCERTS) | |
692 | return 1; | |
693 | print_attribs(out, bag->attrib, "Bag Attributes"); | |
694 | if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate) | |
695 | return 1; | |
75ebbd9a | 696 | if ((x509 = PKCS12_certbag2x509(bag)) == NULL) |
0f113f3e MC |
697 | return 0; |
698 | dump_cert_text(out, x509); | |
699 | PEM_write_bio_X509(out, x509); | |
700 | X509_free(x509); | |
701 | break; | |
702 | ||
703 | case NID_safeContentsBag: | |
704 | if (options & INFO) | |
705 | BIO_printf(bio_err, "Safe Contents bag\n"); | |
706 | print_attribs(out, bag->attrib, "Bag Attributes"); | |
707 | return dump_certs_pkeys_bags(out, bag->value.safes, pass, | |
7e1b7485 | 708 | passlen, options, pempass, enc); |
0f113f3e MC |
709 | |
710 | default: | |
711 | BIO_printf(bio_err, "Warning unsupported bag type: "); | |
712 | i2a_ASN1_OBJECT(bio_err, bag->type); | |
713 | BIO_printf(bio_err, "\n"); | |
714 | return 1; | |
0f113f3e MC |
715 | } |
716 | return 1; | |
ee0508d4 DSH |
717 | } |
718 | ||
719 | /* Given a single certificate return a verified chain or NULL if error */ | |
720 | ||
721 | /* Hope this is OK .... */ | |
722 | ||
0f113f3e | 723 | int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain) |
ee0508d4 | 724 | { |
0f113f3e MC |
725 | X509_STORE_CTX store_ctx; |
726 | STACK_OF(X509) *chn; | |
727 | int i = 0; | |
728 | ||
729 | /* | |
730 | * FIXME: Should really check the return status of X509_STORE_CTX_init | |
731 | * for an error, but how that fits into the return value of this function | |
732 | * is less obvious. | |
733 | */ | |
734 | X509_STORE_CTX_init(&store_ctx, store, cert, NULL); | |
735 | if (X509_verify_cert(&store_ctx) <= 0) { | |
736 | i = X509_STORE_CTX_get_error(&store_ctx); | |
737 | if (i == 0) | |
738 | /* | |
739 | * avoid returning 0 if X509_verify_cert() did not set an | |
740 | * appropriate error value in the context | |
741 | */ | |
742 | i = -1; | |
743 | chn = NULL; | |
744 | goto err; | |
745 | } else | |
746 | chn = X509_STORE_CTX_get1_chain(&store_ctx); | |
747 | err: | |
748 | X509_STORE_CTX_cleanup(&store_ctx); | |
749 | *chain = chn; | |
750 | ||
751 | return i; | |
752 | } | |
753 | ||
ecf3a1fb | 754 | static int alg_print(X509_ALGOR *alg) |
ee0508d4 | 755 | { |
0f113f3e | 756 | PBEPARAM *pbe; |
ecf3a1fb RS |
757 | const unsigned char *p = alg->parameter->value.sequence->data; |
758 | ||
0f113f3e MC |
759 | pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length); |
760 | if (!pbe) | |
761 | return 1; | |
762 | BIO_printf(bio_err, "%s, Iteration %ld\n", | |
763 | OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)), | |
764 | ASN1_INTEGER_get(pbe->iter)); | |
765 | PBEPARAM_free(pbe); | |
766 | return 1; | |
ee0508d4 DSH |
767 | } |
768 | ||
769 | /* Load all certificates from a given file */ | |
770 | ||
84c15db5 | 771 | int cert_load(BIO *in, STACK_OF(X509) *sk) |
ee0508d4 | 772 | { |
0f113f3e MC |
773 | int ret; |
774 | X509 *cert; | |
775 | ret = 0; | |
0f113f3e | 776 | while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
0f113f3e MC |
777 | ret = 1; |
778 | sk_X509_push(sk, cert); | |
0f113f3e | 779 | } |
0f113f3e MC |
780 | if (ret) |
781 | ERR_clear_error(); | |
782 | return ret; | |
ee0508d4 DSH |
783 | } |
784 | ||
785 | /* Generalised attribute print: handle PKCS#8 and bag attributes */ | |
786 | ||
0f113f3e MC |
787 | int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst, |
788 | const char *name) | |
ee0508d4 | 789 | { |
0f113f3e MC |
790 | X509_ATTRIBUTE *attr; |
791 | ASN1_TYPE *av; | |
792 | char *value; | |
793 | int i, attr_nid; | |
794 | if (!attrlst) { | |
795 | BIO_printf(out, "%s: <No Attributes>\n", name); | |
796 | return 1; | |
797 | } | |
798 | if (!sk_X509_ATTRIBUTE_num(attrlst)) { | |
799 | BIO_printf(out, "%s: <Empty Attributes>\n", name); | |
800 | return 1; | |
801 | } | |
802 | BIO_printf(out, "%s\n", name); | |
803 | for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) { | |
9b0a4531 | 804 | ASN1_OBJECT *attr_obj; |
0f113f3e | 805 | attr = sk_X509_ATTRIBUTE_value(attrlst, i); |
9b0a4531 DSH |
806 | attr_obj = X509_ATTRIBUTE_get0_object(attr); |
807 | attr_nid = OBJ_obj2nid(attr_obj); | |
0f113f3e MC |
808 | BIO_printf(out, " "); |
809 | if (attr_nid == NID_undef) { | |
9b0a4531 | 810 | i2a_ASN1_OBJECT(out, attr_obj); |
0f113f3e MC |
811 | BIO_printf(out, ": "); |
812 | } else | |
813 | BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid)); | |
814 | ||
9b0a4531 DSH |
815 | if (X509_ATTRIBUTE_count(attr)) { |
816 | av = X509_ATTRIBUTE_get0_type(attr, 0); | |
0f113f3e MC |
817 | switch (av->type) { |
818 | case V_ASN1_BMPSTRING: | |
819 | value = OPENSSL_uni2asc(av->value.bmpstring->data, | |
820 | av->value.bmpstring->length); | |
821 | BIO_printf(out, "%s\n", value); | |
822 | OPENSSL_free(value); | |
823 | break; | |
824 | ||
825 | case V_ASN1_OCTET_STRING: | |
826 | hex_prin(out, av->value.octet_string->data, | |
827 | av->value.octet_string->length); | |
828 | BIO_printf(out, "\n"); | |
829 | break; | |
830 | ||
831 | case V_ASN1_BIT_STRING: | |
832 | hex_prin(out, av->value.bit_string->data, | |
833 | av->value.bit_string->length); | |
834 | BIO_printf(out, "\n"); | |
835 | break; | |
836 | ||
837 | default: | |
838 | BIO_printf(out, "<Unsupported tag %d>\n", av->type); | |
839 | break; | |
840 | } | |
841 | } else | |
842 | BIO_printf(out, "<No Values>\n"); | |
843 | } | |
844 | return 1; | |
ee0508d4 DSH |
845 | } |
846 | ||
6b691a5c | 847 | void hex_prin(BIO *out, unsigned char *buf, int len) |
ee0508d4 | 848 | { |
0f113f3e MC |
849 | int i; |
850 | for (i = 0; i < len; i++) | |
851 | BIO_printf(out, "%02X ", buf[i]); | |
ee0508d4 | 852 | } |
a8515441 | 853 | |
7e1b7485 | 854 | static int set_pbe(int *ppbe, const char *str) |
0f113f3e MC |
855 | { |
856 | if (!str) | |
857 | return 0; | |
86885c28 | 858 | if (strcmp(str, "NONE") == 0) { |
0f113f3e MC |
859 | *ppbe = -1; |
860 | return 1; | |
861 | } | |
862 | *ppbe = OBJ_txt2nid(str); | |
863 | if (*ppbe == NID_undef) { | |
864 | BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str); | |
865 | return 0; | |
866 | } | |
867 | return 1; | |
868 | } | |
869 | ||
a8515441 | 870 | #endif |