]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkcs12.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / apps / pkcs12.c
CommitLineData
ee0508d4 1/* pkcs12.c */
0f113f3e
MC
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
9a48b07e 4 * project.
ee0508d4
DSH
5 */
6/* ====================================================================
f3dea9a5 7 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
ee0508d4
DSH
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
ee0508d4
DSH
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
6e04afb8
NL
60#include <openssl/opensslconf.h>
61#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
62
0f113f3e
MC
63# include <stdio.h>
64# include <stdlib.h>
65# include <string.h>
66# include "apps.h"
67# include <openssl/crypto.h>
68# include <openssl/err.h>
69# include <openssl/pem.h>
70# include <openssl/pkcs12.h>
ee0508d4 71
0f113f3e 72# define PROG pkcs12_main
ee0508d4 73
13588350 74const EVP_CIPHER *enc;
ee0508d4 75
0f113f3e
MC
76# define NOKEYS 0x1
77# define NOCERTS 0x2
78# define INFO 0x4
79# define CLCERTS 0x8
80# define CACERTS 0x10
81
82int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain);
83int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
84 int options, char *pempass);
85int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
86 char *pass, int passlen, int options,
87 char *pempass);
88int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
89 int passlen, int options, char *pempass);
90int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
91 const char *name);
ee0508d4
DSH
92void hex_prin(BIO *out, unsigned char *buf, int len);
93int alg_print(BIO *x, X509_ALGOR *alg);
84c15db5 94int cert_load(BIO *in, STACK_OF(X509) *sk);
fbf66436 95static int set_pbe(BIO *err, int *ppbe, const char *str);
667ac4ec
RE
96
97int MAIN(int, char **);
98
6b691a5c 99int MAIN(int argc, char **argv)
ee0508d4 100{
5270e702 101 ENGINE *e = NULL;
0f113f3e
MC
102 char *infile = NULL, *outfile = NULL, *keyname = NULL;
103 char *certfile = NULL;
104 BIO *in = NULL, *out = NULL;
ee0508d4
DSH
105 char **args;
106 char *name = NULL;
f2a253e0 107 char *csp_name = NULL;
8528128b 108 int add_lmk = 0;
ee0508d4
DSH
109 PKCS12 *p12 = NULL;
110 char pass[50], macpass[50];
111 int export_cert = 0;
112 int options = 0;
113 int chain = 0;
114 int badarg = 0;
e84240d4 115 int iter = PKCS12_DEFAULT_ITER;
dad666fb 116 int maciter = PKCS12_DEFAULT_ITER;
ee0508d4
DSH
117 int twopass = 0;
118 int keytype = 0;
119 int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
525f51f6 120 int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
ee0508d4
DSH
121 int ret = 1;
122 int macver = 1;
e40b7abe 123 int noprompt = 0;
c869da88 124 STACK_OF(OPENSSL_STRING) *canames = NULL;
e40b7abe 125 char *cpass = NULL, *mpass = NULL;
a3fe382e 126 char *passargin = NULL, *passargout = NULL, *passarg = NULL;
f07fb9b2 127 char *passin = NULL, *passout = NULL;
f365611c 128 char *inrand = NULL;
121dd39f 129 char *macalg = NULL;
88364bc2 130 char *CApath = NULL, *CAfile = NULL;
0f113f3e
MC
131# ifndef OPENSSL_NO_ENGINE
132 char *engine = NULL;
133# endif
ee0508d4
DSH
134
135 apps_startup();
136
137 enc = EVP_des_ede3_cbc();
0f113f3e
MC
138 if (bio_err == NULL)
139 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
ee0508d4 140
0f113f3e
MC
141 if (!load_config(bio_err, NULL))
142 goto end;
3647bee2 143
ee0508d4
DSH
144 args = argv + 1;
145
ee0508d4 146 while (*args) {
0f113f3e
MC
147 if (*args[0] == '-') {
148 if (!strcmp(*args, "-nokeys"))
149 options |= NOKEYS;
150 else if (!strcmp(*args, "-keyex"))
151 keytype = KEY_EX;
152 else if (!strcmp(*args, "-keysig"))
153 keytype = KEY_SIG;
154 else if (!strcmp(*args, "-nocerts"))
155 options |= NOCERTS;
156 else if (!strcmp(*args, "-clcerts"))
157 options |= CLCERTS;
158 else if (!strcmp(*args, "-cacerts"))
159 options |= CACERTS;
160 else if (!strcmp(*args, "-noout"))
161 options |= (NOKEYS | NOCERTS);
162 else if (!strcmp(*args, "-info"))
163 options |= INFO;
164 else if (!strcmp(*args, "-chain"))
165 chain = 1;
166 else if (!strcmp(*args, "-twopass"))
167 twopass = 1;
168 else if (!strcmp(*args, "-nomacver"))
169 macver = 0;
170 else if (!strcmp(*args, "-descert"))
171 cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
172 else if (!strcmp(*args, "-export"))
173 export_cert = 1;
174 else if (!strcmp(*args, "-des"))
175 enc = EVP_des_cbc();
176 else if (!strcmp(*args, "-des3"))
177 enc = EVP_des_ede3_cbc();
178# ifndef OPENSSL_NO_IDEA
179 else if (!strcmp(*args, "-idea"))
180 enc = EVP_idea_cbc();
181# endif
182# ifndef OPENSSL_NO_SEED
183 else if (!strcmp(*args, "-seed"))
184 enc = EVP_seed_cbc();
185# endif
186# ifndef OPENSSL_NO_AES
187 else if (!strcmp(*args, "-aes128"))
188 enc = EVP_aes_128_cbc();
189 else if (!strcmp(*args, "-aes192"))
190 enc = EVP_aes_192_cbc();
191 else if (!strcmp(*args, "-aes256"))
192 enc = EVP_aes_256_cbc();
193# endif
194# ifndef OPENSSL_NO_CAMELLIA
195 else if (!strcmp(*args, "-camellia128"))
196 enc = EVP_camellia_128_cbc();
197 else if (!strcmp(*args, "-camellia192"))
198 enc = EVP_camellia_192_cbc();
199 else if (!strcmp(*args, "-camellia256"))
200 enc = EVP_camellia_256_cbc();
201# endif
202 else if (!strcmp(*args, "-noiter"))
203 iter = 1;
204 else if (!strcmp(*args, "-maciter"))
205 maciter = PKCS12_DEFAULT_ITER;
206 else if (!strcmp(*args, "-nomaciter"))
207 maciter = 1;
208 else if (!strcmp(*args, "-nomac"))
209 maciter = -1;
210 else if (!strcmp(*args, "-macalg"))
211 if (args[1]) {
212 args++;
213 macalg = *args;
214 } else
215 badarg = 1;
216 else if (!strcmp(*args, "-nodes"))
217 enc = NULL;
218 else if (!strcmp(*args, "-certpbe")) {
219 if (!set_pbe(bio_err, &cert_pbe, *++args))
220 badarg = 1;
221 } else if (!strcmp(*args, "-keypbe")) {
222 if (!set_pbe(bio_err, &key_pbe, *++args))
223 badarg = 1;
224 } else if (!strcmp(*args, "-rand")) {
225 if (args[1]) {
226 args++;
227 inrand = *args;
228 } else
229 badarg = 1;
230 } else if (!strcmp(*args, "-inkey")) {
231 if (args[1]) {
232 args++;
233 keyname = *args;
234 } else
235 badarg = 1;
236 } else if (!strcmp(*args, "-certfile")) {
237 if (args[1]) {
238 args++;
239 certfile = *args;
240 } else
241 badarg = 1;
242 } else if (!strcmp(*args, "-name")) {
243 if (args[1]) {
244 args++;
245 name = *args;
246 } else
247 badarg = 1;
248 } else if (!strcmp(*args, "-LMK"))
249 add_lmk = 1;
250 else if (!strcmp(*args, "-CSP")) {
251 if (args[1]) {
252 args++;
253 csp_name = *args;
254 } else
255 badarg = 1;
256 } else if (!strcmp(*args, "-caname")) {
257 if (args[1]) {
258 args++;
259 if (!canames)
260 canames = sk_OPENSSL_STRING_new_null();
261 sk_OPENSSL_STRING_push(canames, *args);
262 } else
263 badarg = 1;
264 } else if (!strcmp(*args, "-in")) {
265 if (args[1]) {
266 args++;
267 infile = *args;
268 } else
269 badarg = 1;
270 } else if (!strcmp(*args, "-out")) {
271 if (args[1]) {
272 args++;
273 outfile = *args;
274 } else
275 badarg = 1;
276 } else if (!strcmp(*args, "-passin")) {
277 if (args[1]) {
278 args++;
279 passargin = *args;
280 } else
281 badarg = 1;
282 } else if (!strcmp(*args, "-passout")) {
283 if (args[1]) {
284 args++;
285 passargout = *args;
286 } else
287 badarg = 1;
288 } else if (!strcmp(*args, "-password")) {
289 if (args[1]) {
290 args++;
291 passarg = *args;
292 noprompt = 1;
293 } else
294 badarg = 1;
295 } else if (!strcmp(*args, "-CApath")) {
296 if (args[1]) {
297 args++;
298 CApath = *args;
299 } else
300 badarg = 1;
301 } else if (!strcmp(*args, "-CAfile")) {
302 if (args[1]) {
303 args++;
304 CAfile = *args;
305 } else
306 badarg = 1;
307# ifndef OPENSSL_NO_ENGINE
308 } else if (!strcmp(*args, "-engine")) {
309 if (args[1]) {
310 args++;
311 engine = *args;
312 } else
313 badarg = 1;
314# endif
315 } else
316 badarg = 1;
317
318 } else
319 badarg = 1;
320 args++;
ee0508d4
DSH
321 }
322
323 if (badarg) {
0f113f3e
MC
324 BIO_printf(bio_err, "Usage: pkcs12 [options]\n");
325 BIO_printf(bio_err, "where options are\n");
326 BIO_printf(bio_err, "-export output PKCS12 file\n");
327 BIO_printf(bio_err, "-chain add certificate chain\n");
328 BIO_printf(bio_err, "-inkey file private key if not infile\n");
329 BIO_printf(bio_err, "-certfile f add all certs in f\n");
330 BIO_printf(bio_err, "-CApath arg - PEM format directory of CA's\n");
331 BIO_printf(bio_err, "-CAfile arg - PEM format file of CA's\n");
332 BIO_printf(bio_err, "-name \"name\" use name as friendly name\n");
333 BIO_printf(bio_err,
334 "-caname \"nm\" use nm as CA friendly name (can be used more than once).\n");
335 BIO_printf(bio_err, "-in infile input filename\n");
336 BIO_printf(bio_err, "-out outfile output filename\n");
337 BIO_printf(bio_err,
338 "-noout don't output anything, just verify.\n");
339 BIO_printf(bio_err, "-nomacver don't verify MAC.\n");
340 BIO_printf(bio_err, "-nocerts don't output certificates.\n");
341 BIO_printf(bio_err,
342 "-clcerts only output client certificates.\n");
343 BIO_printf(bio_err, "-cacerts only output CA certificates.\n");
344 BIO_printf(bio_err, "-nokeys don't output private keys.\n");
345 BIO_printf(bio_err,
346 "-info give info about PKCS#12 structure.\n");
347 BIO_printf(bio_err, "-des encrypt private keys with DES\n");
348 BIO_printf(bio_err,
349 "-des3 encrypt private keys with triple DES (default)\n");
350# ifndef OPENSSL_NO_IDEA
351 BIO_printf(bio_err, "-idea encrypt private keys with idea\n");
352# endif
353# ifndef OPENSSL_NO_SEED
354 BIO_printf(bio_err, "-seed encrypt private keys with seed\n");
355# endif
356# ifndef OPENSSL_NO_AES
357 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
358 BIO_printf(bio_err,
359 " encrypt PEM output with cbc aes\n");
360# endif
361# ifndef OPENSSL_NO_CAMELLIA
362 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
363 BIO_printf(bio_err,
364 " encrypt PEM output with cbc camellia\n");
365# endif
366 BIO_printf(bio_err, "-nodes don't encrypt private keys\n");
367 BIO_printf(bio_err, "-noiter don't use encryption iteration\n");
368 BIO_printf(bio_err, "-nomaciter don't use MAC iteration\n");
369 BIO_printf(bio_err, "-maciter use MAC iteration\n");
370 BIO_printf(bio_err, "-nomac don't generate MAC\n");
371 BIO_printf(bio_err,
372 "-twopass separate MAC, encryption passwords\n");
373 BIO_printf(bio_err,
374 "-descert encrypt PKCS#12 certificates with triple DES (default RC2-40)\n");
375 BIO_printf(bio_err,
376 "-certpbe alg specify certificate PBE algorithm (default RC2-40)\n");
377 BIO_printf(bio_err,
378 "-keypbe alg specify private key PBE algorithm (default 3DES)\n");
379 BIO_printf(bio_err,
380 "-macalg alg digest algorithm used in MAC (default SHA1)\n");
381 BIO_printf(bio_err, "-keyex set MS key exchange type\n");
382 BIO_printf(bio_err, "-keysig set MS key signature type\n");
383 BIO_printf(bio_err,
384 "-password p set import/export password source\n");
385 BIO_printf(bio_err, "-passin p input file pass phrase source\n");
386 BIO_printf(bio_err, "-passout p output file pass phrase source\n");
387# ifndef OPENSSL_NO_ENGINE
388 BIO_printf(bio_err,
389 "-engine e use engine e, possibly a hardware device.\n");
390# endif
391 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
392 LIST_SEPARATOR_CHAR);
393 BIO_printf(bio_err,
394 " load the file (or the files in the directory) into\n");
395 BIO_printf(bio_err, " the random number generator\n");
396 BIO_printf(bio_err, "-CSP name Microsoft CSP name\n");
397 BIO_printf(bio_err,
398 "-LMK Add local machine keyset attribute to private key\n");
399 goto end;
ee0508d4 400 }
0f113f3e 401# ifndef OPENSSL_NO_ENGINE
531d630b 402 e = setup_engine(bio_err, engine, 0);
0f113f3e 403# endif
5270e702 404
0f113f3e
MC
405 if (passarg) {
406 if (export_cert)
407 passargout = passarg;
408 else
409 passargin = passarg;
a3fe382e
DSH
410 }
411
0f113f3e
MC
412 if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
413 BIO_printf(bio_err, "Error getting passwords\n");
414 goto end;
a3fe382e
DSH
415 }
416
0f113f3e
MC
417 if (!cpass) {
418 if (export_cert)
419 cpass = passout;
420 else
421 cpass = passin;
f07fb9b2
DSH
422 }
423
0f113f3e
MC
424 if (cpass) {
425 mpass = cpass;
426 noprompt = 1;
f07fb9b2 427 } else {
0f113f3e
MC
428 cpass = pass;
429 mpass = macpass;
e40b7abe
DSH
430 }
431
0f113f3e
MC
432 if (export_cert || inrand) {
433 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
d13e4eb0 434 if (inrand != NULL)
0f113f3e
MC
435 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
436 app_RAND_load_files(inrand));
d13e4eb0 437 }
ee0508d4
DSH
438 ERR_load_crypto_strings();
439
0f113f3e 440# ifdef CRYPTO_MDEBUG
a873356c 441 CRYPTO_push_info("read files");
0f113f3e 442# endif
a873356c 443
0f113f3e
MC
444 if (!infile)
445 in = BIO_new_fp(stdin, BIO_NOCLOSE);
446 else
447 in = BIO_new_file(infile, "rb");
12ea4470 448 if (!in) {
0f113f3e
MC
449 BIO_printf(bio_err, "Error opening input file %s\n",
450 infile ? infile : "<stdin>");
451 perror(infile);
452 goto end;
453 }
454# ifdef CRYPTO_MDEBUG
a873356c
BM
455 CRYPTO_pop_info();
456 CRYPTO_push_info("write files");
0f113f3e 457# endif
a873356c 458
645749ef 459 if (!outfile) {
0f113f3e
MC
460 out = BIO_new_fp(stdout, BIO_NOCLOSE);
461# ifdef OPENSSL_SYS_VMS
462 {
463 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
464 out = BIO_push(tmpbio, out);
465 }
466# endif
467 } else
468 out = BIO_new_file(outfile, "wb");
12ea4470 469 if (!out) {
0f113f3e
MC
470 BIO_printf(bio_err, "Error opening output file %s\n",
471 outfile ? outfile : "<stdout>");
472 perror(outfile);
473 goto end;
ee0508d4
DSH
474 }
475 if (twopass) {
0f113f3e
MC
476# ifdef CRYPTO_MDEBUG
477 CRYPTO_push_info("read MAC password");
478# endif
479 if (EVP_read_pw_string
480 (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
481 BIO_printf(bio_err, "Can't read Password\n");
482 goto end;
483 }
484# ifdef CRYPTO_MDEBUG
485 CRYPTO_pop_info();
486# endif
ee0508d4
DSH
487 }
488
2d681b77 489 if (export_cert) {
0f113f3e
MC
490 EVP_PKEY *key = NULL;
491 X509 *ucert = NULL, *x = NULL;
492 STACK_OF(X509) *certs = NULL;
493 const EVP_MD *macmd = NULL;
494 unsigned char *catmp = NULL;
495 int i;
496
497 if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
498 BIO_printf(bio_err, "Nothing to do!\n");
499 goto export_end;
500 }
501
502 if (options & NOCERTS)
503 chain = 0;
504
505# ifdef CRYPTO_MDEBUG
506 CRYPTO_push_info("process -export_cert");
507 CRYPTO_push_info("reading private key");
508# endif
509 if (!(options & NOKEYS)) {
510 key = load_key(bio_err, keyname ? keyname : infile,
511 FORMAT_PEM, 1, passin, e, "private key");
512 if (!key)
513 goto export_end;
514 }
515# ifdef CRYPTO_MDEBUG
516 CRYPTO_pop_info();
517 CRYPTO_push_info("reading certs from input");
518# endif
519
520 /* Load in all certs in input file */
521 if (!(options & NOCERTS)) {
522 certs = load_certs(bio_err, infile, FORMAT_PEM, NULL, e,
523 "certificates");
524 if (!certs)
525 goto export_end;
526
527 if (key) {
528 /* Look for matching private key */
529 for (i = 0; i < sk_X509_num(certs); i++) {
530 x = sk_X509_value(certs, i);
531 if (X509_check_private_key(x, key)) {
532 ucert = x;
533 /* Zero keyid and alias */
534 X509_keyid_set1(ucert, NULL, 0);
535 X509_alias_set1(ucert, NULL, 0);
536 /* Remove from list */
537 (void)sk_X509_delete(certs, i);
538 break;
539 }
540 }
541 if (!ucert) {
542 BIO_printf(bio_err,
543 "No certificate matches private key\n");
544 goto export_end;
545 }
546 }
547
548 }
549# ifdef CRYPTO_MDEBUG
550 CRYPTO_pop_info();
551 CRYPTO_push_info("reading certs from input 2");
552# endif
553
554 /* Add any more certificates asked for */
555 if (certfile) {
556 STACK_OF(X509) *morecerts = NULL;
557 if (!(morecerts = load_certs(bio_err, certfile, FORMAT_PEM,
558 NULL, e,
559 "certificates from certfile")))
560 goto export_end;
561 while (sk_X509_num(morecerts) > 0)
562 sk_X509_push(certs, sk_X509_shift(morecerts));
563 sk_X509_free(morecerts);
564 }
565# ifdef CRYPTO_MDEBUG
566 CRYPTO_pop_info();
567 CRYPTO_push_info("reading certs from certfile");
568# endif
569
570# ifdef CRYPTO_MDEBUG
571 CRYPTO_pop_info();
572 CRYPTO_push_info("building chain");
573# endif
574
575 /* If chaining get chain from user cert */
576 if (chain) {
577 int vret;
578 STACK_OF(X509) *chain2;
579 X509_STORE *store = X509_STORE_new();
580 if (!store) {
581 BIO_printf(bio_err, "Memory allocation error\n");
582 goto export_end;
583 }
584 if (!X509_STORE_load_locations(store, CAfile, CApath))
585 X509_STORE_set_default_paths(store);
586
587 vret = get_cert_chain(ucert, store, &chain2);
588 X509_STORE_free(store);
589
590 if (!vret) {
591 /* Exclude verified certificate */
592 for (i = 1; i < sk_X509_num(chain2); i++)
593 sk_X509_push(certs, sk_X509_value(chain2, i));
594 /* Free first certificate */
595 X509_free(sk_X509_value(chain2, 0));
596 sk_X509_free(chain2);
597 } else {
598 if (vret >= 0)
599 BIO_printf(bio_err, "Error %s getting chain.\n",
600 X509_verify_cert_error_string(vret));
601 else
602 ERR_print_errors(bio_err);
603 goto export_end;
604 }
605 }
606
607 /* Add any CA names */
608
609 for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
610 catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
611 X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
612 }
613
614 if (csp_name && key)
615 EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
616 MBSTRING_ASC, (unsigned char *)csp_name,
617 -1);
618
619 if (add_lmk && key)
620 EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
621
622# ifdef CRYPTO_MDEBUG
623 CRYPTO_pop_info();
624 CRYPTO_push_info("reading password");
625# endif
626
627 if (!noprompt &&
628 EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
629 1)) {
630 BIO_printf(bio_err, "Can't read Password\n");
631 goto export_end;
632 }
633 if (!twopass)
634 BUF_strlcpy(macpass, pass, sizeof macpass);
635
636# ifdef CRYPTO_MDEBUG
637 CRYPTO_pop_info();
638 CRYPTO_push_info("creating PKCS#12 structure");
639# endif
640
641 p12 = PKCS12_create(cpass, name, key, ucert, certs,
642 key_pbe, cert_pbe, iter, -1, keytype);
643
644 if (!p12) {
645 ERR_print_errors(bio_err);
646 goto export_end;
647 }
648
649 if (macalg) {
650 macmd = EVP_get_digestbyname(macalg);
651 if (!macmd) {
652 BIO_printf(bio_err, "Unknown digest algorithm %s\n", macalg);
653 }
654 }
655
656 if (maciter != -1)
657 PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
658
659# ifdef CRYPTO_MDEBUG
660 CRYPTO_pop_info();
661 CRYPTO_push_info("writing pkcs12");
662# endif
663
664 i2d_PKCS12_bio(out, p12);
665
666 ret = 0;
667
668 export_end:
669# ifdef CRYPTO_MDEBUG
670 CRYPTO_pop_info();
671 CRYPTO_pop_info();
672 CRYPTO_push_info("process -export_cert: freeing");
673# endif
674
675 if (key)
676 EVP_PKEY_free(key);
677 if (certs)
678 sk_X509_pop_free(certs, X509_free);
679 if (ucert)
680 X509_free(ucert);
681
682# ifdef CRYPTO_MDEBUG
683 CRYPTO_pop_info();
684# endif
685 goto end;
ee0508d4 686
ee86c3f5 687 }
ee0508d4 688
0f113f3e
MC
689 if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
690 ERR_print_errors(bio_err);
691 goto end;
ee0508d4 692 }
0f113f3e 693# ifdef CRYPTO_MDEBUG
a873356c 694 CRYPTO_push_info("read import password");
0f113f3e
MC
695# endif
696 if (!noprompt
697 && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
698 0)) {
699 BIO_printf(bio_err, "Can't read Password\n");
700 goto end;
ee0508d4 701 }
0f113f3e 702# ifdef CRYPTO_MDEBUG
a873356c 703 CRYPTO_pop_info();
0f113f3e
MC
704# endif
705
706 if (!twopass)
707 BUF_strlcpy(macpass, pass, sizeof macpass);
708
709 if ((options & INFO) && p12->mac)
710 BIO_printf(bio_err, "MAC Iteration %ld\n",
711 p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
712 if (macver) {
713# ifdef CRYPTO_MDEBUG
714 CRYPTO_push_info("verify MAC");
715# endif
716 /* If we enter empty password try no password first */
717 if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
718 /* If mac and crypto pass the same set it to NULL too */
719 if (!twopass)
720 cpass = NULL;
721 } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
722 BIO_printf(bio_err, "Mac verify error: invalid password?\n");
723 ERR_print_errors(bio_err);
724 goto end;
725 }
726 BIO_printf(bio_err, "MAC verified OK\n");
727# ifdef CRYPTO_MDEBUG
728 CRYPTO_pop_info();
729# endif
ee0508d4 730 }
0f113f3e 731# ifdef CRYPTO_MDEBUG
a873356c 732 CRYPTO_push_info("output keys and certificates");
0f113f3e
MC
733# endif
734 if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout)) {
735 BIO_printf(bio_err, "Error outputting keys and certificates\n");
736 ERR_print_errors(bio_err);
737 goto end;
ee0508d4 738 }
0f113f3e 739# ifdef CRYPTO_MDEBUG
a873356c 740 CRYPTO_pop_info();
0f113f3e 741# endif
ee0508d4 742 ret = 0;
88364bc2 743 end:
0f113f3e
MC
744 if (p12)
745 PKCS12_free(p12);
746 if (export_cert || inrand)
747 app_RAND_write_file(NULL, bio_err);
748# ifdef CRYPTO_MDEBUG
a873356c 749 CRYPTO_remove_all_info();
0f113f3e 750# endif
a873356c 751 BIO_free(in);
645749ef 752 BIO_free_all(out);
0f113f3e
MC
753 if (canames)
754 sk_OPENSSL_STRING_free(canames);
755 if (passin)
756 OPENSSL_free(passin);
757 if (passout)
758 OPENSSL_free(passout);
5abc8ae6 759 apps_shutdown();
1c3e4a36 760 OPENSSL_EXIT(ret);
ee0508d4
DSH
761}
762
0f113f3e
MC
763int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
764 int passlen, int options, char *pempass)
ee0508d4 765{
0f113f3e
MC
766 STACK_OF(PKCS7) *asafes = NULL;
767 STACK_OF(PKCS12_SAFEBAG) *bags;
768 int i, bagnid;
769 int ret = 0;
770 PKCS7 *p7;
771
772 if (!(asafes = PKCS12_unpack_authsafes(p12)))
773 return 0;
774 for (i = 0; i < sk_PKCS7_num(asafes); i++) {
775 p7 = sk_PKCS7_value(asafes, i);
776 bagnid = OBJ_obj2nid(p7->type);
777 if (bagnid == NID_pkcs7_data) {
778 bags = PKCS12_unpack_p7data(p7);
779 if (options & INFO)
780 BIO_printf(bio_err, "PKCS7 Data\n");
781 } else if (bagnid == NID_pkcs7_encrypted) {
782 if (options & INFO) {
783 BIO_printf(bio_err, "PKCS7 Encrypted data: ");
784 alg_print(bio_err, p7->d.encrypted->enc_data->algorithm);
785 }
786 bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
787 } else
788 continue;
789 if (!bags)
790 goto err;
791 if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
792 options, pempass)) {
793 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
794 goto err;
795 }
796 sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
797 bags = NULL;
798 }
799 ret = 1;
800
801 err:
802
803 if (asafes)
804 sk_PKCS7_pop_free(asafes, PKCS7_free);
805 return ret;
ee0508d4
DSH
806}
807
0f113f3e
MC
808int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
809 char *pass, int passlen, int options, char *pempass)
ee0508d4 810{
0f113f3e
MC
811 int i;
812 for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
813 if (!dump_certs_pkeys_bag(out,
814 sk_PKCS12_SAFEBAG_value(bags, i),
815 pass, passlen, options, pempass))
816 return 0;
817 }
818 return 1;
ee0508d4
DSH
819}
820
0f113f3e
MC
821int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
822 int passlen, int options, char *pempass)
ee0508d4 823{
0f113f3e
MC
824 EVP_PKEY *pkey;
825 PKCS8_PRIV_KEY_INFO *p8;
826 X509 *x509;
827
828 switch (M_PKCS12_bag_type(bag)) {
829 case NID_keyBag:
830 if (options & INFO)
831 BIO_printf(bio_err, "Key bag\n");
832 if (options & NOKEYS)
833 return 1;
834 print_attribs(out, bag->attrib, "Bag Attributes");
835 p8 = bag->value.keybag;
836 if (!(pkey = EVP_PKCS82PKEY(p8)))
837 return 0;
838 print_attribs(out, p8->attributes, "Key Attributes");
839 PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
840 EVP_PKEY_free(pkey);
841 break;
842
843 case NID_pkcs8ShroudedKeyBag:
844 if (options & INFO) {
845 BIO_printf(bio_err, "Shrouded Keybag: ");
846 alg_print(bio_err, bag->value.shkeybag->algor);
847 }
848 if (options & NOKEYS)
849 return 1;
850 print_attribs(out, bag->attrib, "Bag Attributes");
851 if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
852 return 0;
853 if (!(pkey = EVP_PKCS82PKEY(p8))) {
854 PKCS8_PRIV_KEY_INFO_free(p8);
855 return 0;
856 }
857 print_attribs(out, p8->attributes, "Key Attributes");
858 PKCS8_PRIV_KEY_INFO_free(p8);
859 PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
860 EVP_PKEY_free(pkey);
861 break;
862
863 case NID_certBag:
864 if (options & INFO)
865 BIO_printf(bio_err, "Certificate bag\n");
866 if (options & NOCERTS)
867 return 1;
868 if (PKCS12_get_attr(bag, NID_localKeyID)) {
869 if (options & CACERTS)
870 return 1;
871 } else if (options & CLCERTS)
872 return 1;
873 print_attribs(out, bag->attrib, "Bag Attributes");
874 if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
875 return 1;
876 if (!(x509 = PKCS12_certbag2x509(bag)))
877 return 0;
878 dump_cert_text(out, x509);
879 PEM_write_bio_X509(out, x509);
880 X509_free(x509);
881 break;
882
883 case NID_safeContentsBag:
884 if (options & INFO)
885 BIO_printf(bio_err, "Safe Contents bag\n");
886 print_attribs(out, bag->attrib, "Bag Attributes");
887 return dump_certs_pkeys_bags(out, bag->value.safes, pass,
888 passlen, options, pempass);
889
890 default:
891 BIO_printf(bio_err, "Warning unsupported bag type: ");
892 i2a_ASN1_OBJECT(bio_err, bag->type);
893 BIO_printf(bio_err, "\n");
894 return 1;
895 break;
896 }
897 return 1;
ee0508d4
DSH
898}
899
900/* Given a single certificate return a verified chain or NULL if error */
901
902/* Hope this is OK .... */
903
0f113f3e 904int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain)
ee0508d4 905{
0f113f3e
MC
906 X509_STORE_CTX store_ctx;
907 STACK_OF(X509) *chn;
908 int i = 0;
909
910 /*
911 * FIXME: Should really check the return status of X509_STORE_CTX_init
912 * for an error, but how that fits into the return value of this function
913 * is less obvious.
914 */
915 X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
916 if (X509_verify_cert(&store_ctx) <= 0) {
917 i = X509_STORE_CTX_get_error(&store_ctx);
918 if (i == 0)
919 /*
920 * avoid returning 0 if X509_verify_cert() did not set an
921 * appropriate error value in the context
922 */
923 i = -1;
924 chn = NULL;
925 goto err;
926 } else
927 chn = X509_STORE_CTX_get1_chain(&store_ctx);
928 err:
929 X509_STORE_CTX_cleanup(&store_ctx);
930 *chain = chn;
931
932 return i;
933}
934
935int alg_print(BIO *x, X509_ALGOR *alg)
ee0508d4 936{
0f113f3e
MC
937 PBEPARAM *pbe;
938 const unsigned char *p;
939 p = alg->parameter->value.sequence->data;
940 pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
941 if (!pbe)
942 return 1;
943 BIO_printf(bio_err, "%s, Iteration %ld\n",
944 OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
945 ASN1_INTEGER_get(pbe->iter));
946 PBEPARAM_free(pbe);
947 return 1;
ee0508d4
DSH
948}
949
950/* Load all certificates from a given file */
951
84c15db5 952int cert_load(BIO *in, STACK_OF(X509) *sk)
ee0508d4 953{
0f113f3e
MC
954 int ret;
955 X509 *cert;
956 ret = 0;
957# ifdef CRYPTO_MDEBUG
958 CRYPTO_push_info("cert_load(): reading one cert");
959# endif
960 while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
961# ifdef CRYPTO_MDEBUG
962 CRYPTO_pop_info();
963# endif
964 ret = 1;
965 sk_X509_push(sk, cert);
966# ifdef CRYPTO_MDEBUG
967 CRYPTO_push_info("cert_load(): reading one cert");
968# endif
969 }
970# ifdef CRYPTO_MDEBUG
971 CRYPTO_pop_info();
972# endif
973 if (ret)
974 ERR_clear_error();
975 return ret;
ee0508d4
DSH
976}
977
978/* Generalised attribute print: handle PKCS#8 and bag attributes */
979
0f113f3e
MC
980int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
981 const char *name)
ee0508d4 982{
0f113f3e
MC
983 X509_ATTRIBUTE *attr;
984 ASN1_TYPE *av;
985 char *value;
986 int i, attr_nid;
987 if (!attrlst) {
988 BIO_printf(out, "%s: <No Attributes>\n", name);
989 return 1;
990 }
991 if (!sk_X509_ATTRIBUTE_num(attrlst)) {
992 BIO_printf(out, "%s: <Empty Attributes>\n", name);
993 return 1;
994 }
995 BIO_printf(out, "%s\n", name);
996 for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
997 attr = sk_X509_ATTRIBUTE_value(attrlst, i);
998 attr_nid = OBJ_obj2nid(attr->object);
999 BIO_printf(out, " ");
1000 if (attr_nid == NID_undef) {
1001 i2a_ASN1_OBJECT(out, attr->object);
1002 BIO_printf(out, ": ");
1003 } else
1004 BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1005
1006 if (sk_ASN1_TYPE_num(attr->value.set)) {
1007 av = sk_ASN1_TYPE_value(attr->value.set, 0);
1008 switch (av->type) {
1009 case V_ASN1_BMPSTRING:
1010 value = OPENSSL_uni2asc(av->value.bmpstring->data,
1011 av->value.bmpstring->length);
1012 BIO_printf(out, "%s\n", value);
1013 OPENSSL_free(value);
1014 break;
1015
1016 case V_ASN1_OCTET_STRING:
1017 hex_prin(out, av->value.octet_string->data,
1018 av->value.octet_string->length);
1019 BIO_printf(out, "\n");
1020 break;
1021
1022 case V_ASN1_BIT_STRING:
1023 hex_prin(out, av->value.bit_string->data,
1024 av->value.bit_string->length);
1025 BIO_printf(out, "\n");
1026 break;
1027
1028 default:
1029 BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1030 break;
1031 }
1032 } else
1033 BIO_printf(out, "<No Values>\n");
1034 }
1035 return 1;
ee0508d4
DSH
1036}
1037
6b691a5c 1038void hex_prin(BIO *out, unsigned char *buf, int len)
ee0508d4 1039{
0f113f3e
MC
1040 int i;
1041 for (i = 0; i < len; i++)
1042 BIO_printf(out, "%02X ", buf[i]);
ee0508d4 1043}
a8515441 1044
fbf66436 1045static int set_pbe(BIO *err, int *ppbe, const char *str)
0f113f3e
MC
1046{
1047 if (!str)
1048 return 0;
1049 if (!strcmp(str, "NONE")) {
1050 *ppbe = -1;
1051 return 1;
1052 }
1053 *ppbe = OBJ_txt2nid(str);
1054 if (*ppbe == NID_undef) {
1055 BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
1056 return 0;
1057 }
1058 return 1;
1059}
1060
a8515441 1061#endif