Annotation of src/usr.bin/openssl/pkcs12.c, Revision 1.13
1.13 ! inoguchi 1: /* $OpenBSD: pkcs12.c,v 1.12 2019/07/24 13:49:24 inoguchi Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project.
4: */
5: /* ====================================================================
6: * Copyright (c) 1999-2006 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 <openssl/opensslconf.h>
60:
61: #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
62:
63: #include <stdio.h>
64: #include <stdlib.h>
65: #include <string.h>
66:
67: #include "apps.h"
68:
69: #include <openssl/crypto.h>
70: #include <openssl/err.h>
71: #include <openssl/pem.h>
72: #include <openssl/pkcs12.h>
73:
74: #define NOKEYS 0x1
75: #define NOCERTS 0x2
76: #define INFO 0x4
77: #define CLCERTS 0x8
78: #define CACERTS 0x10
79:
80: int get_cert_chain(X509 * cert, X509_STORE * store, STACK_OF(X509) ** chain);
81: int dump_certs_keys_p12(BIO * out, PKCS12 * p12, char *pass, int passlen,
82: int options, char *pempass);
83: int dump_certs_pkeys_bags(BIO * out, STACK_OF(PKCS12_SAFEBAG) * bags, char *pass,
84: int passlen, int options, char *pempass);
85: int dump_certs_pkeys_bag(BIO * out, PKCS12_SAFEBAG * bags, char *pass, int passlen,
86: int options, char *pempass);
87: int print_attribs(BIO * out, STACK_OF(X509_ATTRIBUTE) * attrlst, const char *name);
88: void hex_prin(BIO * out, unsigned char *buf, int len);
89: int alg_print(BIO * x, X509_ALGOR * alg);
90: int cert_load(BIO * in, STACK_OF(X509) * sk);
91: static int set_pbe(BIO * err, int *ppbe, const char *str);
92:
1.11 inoguchi 93: static struct {
94: int add_lmk;
95: char *CAfile;
96: STACK_OF(OPENSSL_STRING) *canames;
97: char *CApath;
98: int cert_pbe;
99: char *certfile;
100: int chain;
101: char *csp_name;
102: const EVP_CIPHER *enc;
103: int export_cert;
104: int key_pbe;
105: char *keyname;
106: int keytype;
107: char *infile;
108: int iter;
109: char *macalg;
110: int maciter;
111: int macver;
112: char *name;
113: int noprompt;
114: int options;
115: char *outfile;
116: char *passarg;
117: char *passargin;
118: char *passargout;
119: int twopass;
120: } pkcs12_config;
121:
1.12 inoguchi 122: static int
123: pkcs12_opt_canames(char *arg)
124: {
125: if (pkcs12_config.canames == NULL &&
126: (pkcs12_config.canames = sk_OPENSSL_STRING_new_null()) == NULL)
127: return (1);
128:
129: if (!sk_OPENSSL_STRING_push(pkcs12_config.canames, arg))
130: return (1);
131:
132: return (0);
133: }
134:
135: static int
136: pkcs12_opt_cert_pbe(char *arg)
137: {
138: return (!set_pbe(bio_err, &pkcs12_config.cert_pbe, arg));
139: }
140:
141: static int
142: pkcs12_opt_key_pbe(char *arg)
143: {
144: return (!set_pbe(bio_err, &pkcs12_config.key_pbe, arg));
145: }
146:
147: static int
148: pkcs12_opt_passarg(char *arg)
149: {
150: pkcs12_config.passarg = arg;
151: pkcs12_config.noprompt = 1;
152: return (0);
153: }
154:
155: static const EVP_CIPHER *get_cipher_by_name(char *name)
156: {
157: if (name == NULL || strcmp(name, "") == 0)
158: return (NULL);
159: #ifndef OPENSSL_NO_AES
160: else if (strcmp(name, "aes128") == 0)
161: return EVP_aes_128_cbc();
162: else if (strcmp(name, "aes192") == 0)
163: return EVP_aes_192_cbc();
164: else if (strcmp(name, "aes256") == 0)
165: return EVP_aes_256_cbc();
166: #endif
167: #ifndef OPENSSL_NO_CAMELLIA
168: else if (strcmp(name, "camellia128") == 0)
169: return EVP_camellia_128_cbc();
170: else if (strcmp(name, "camellia192") == 0)
171: return EVP_camellia_192_cbc();
172: else if (strcmp(name, "camellia256") == 0)
173: return EVP_camellia_256_cbc();
174: #endif
175: #ifndef OPENSSL_NO_DES
176: else if (strcmp(name, "des") == 0)
177: return EVP_des_cbc();
178: else if (strcmp(name, "des3") == 0)
179: return EVP_des_ede3_cbc();
180: #endif
181: #ifndef OPENSSL_NO_IDEA
182: else if (strcmp(name, "idea") == 0)
183: return EVP_idea_cbc();
184: #endif
185: else
186: return (NULL);
187: }
188:
189: static int
190: pkcs12_opt_enc(int argc, char **argv, int *argsused)
191: {
192: char *name = argv[0];
193:
194: if (*name++ != '-')
195: return (1);
196:
197: if (strcmp(name, "nodes") == 0)
198: pkcs12_config.enc = NULL;
199: else if ((pkcs12_config.enc = get_cipher_by_name(name)) == NULL)
200: return (1);
201:
202: *argsused = 1;
203: return (0);
204: }
205:
206: static const struct option pkcs12_options[] = {
207: #ifndef OPENSSL_NO_AES
208: {
209: .name = "aes128",
210: .desc = "Encrypt PEM output with CBC AES",
211: .type = OPTION_ARGV_FUNC,
212: .opt.argvfunc = pkcs12_opt_enc,
213: },
214: {
215: .name = "aes192",
216: .desc = "Encrypt PEM output with CBC AES",
217: .type = OPTION_ARGV_FUNC,
218: .opt.argvfunc = pkcs12_opt_enc,
219: },
220: {
221: .name = "aes256",
222: .desc = "Encrypt PEM output with CBC AES",
223: .type = OPTION_ARGV_FUNC,
224: .opt.argvfunc = pkcs12_opt_enc,
225: },
226: #endif
227: #ifndef OPENSSL_NO_CAMELLIA
228: {
229: .name = "camellia128",
230: .desc = "Encrypt PEM output with CBC Camellia",
231: .type = OPTION_ARGV_FUNC,
232: .opt.argvfunc = pkcs12_opt_enc,
233: },
234: {
235: .name = "camellia192",
236: .desc = "Encrypt PEM output with CBC Camellia",
237: .type = OPTION_ARGV_FUNC,
238: .opt.argvfunc = pkcs12_opt_enc,
239: },
240: {
241: .name = "camellia256",
242: .desc = "Encrypt PEM output with CBC Camellia",
243: .type = OPTION_ARGV_FUNC,
244: .opt.argvfunc = pkcs12_opt_enc,
245: },
246: #endif
247: {
248: .name = "des",
249: .desc = "Encrypt private keys with DES",
250: .type = OPTION_ARGV_FUNC,
251: .opt.argvfunc = pkcs12_opt_enc,
252: },
253: {
254: .name = "des3",
255: .desc = "Encrypt private keys with triple DES (default)",
256: .type = OPTION_ARGV_FUNC,
257: .opt.argvfunc = pkcs12_opt_enc,
258: },
259: #ifndef OPENSSL_NO_IDEA
260: {
261: .name = "idea",
262: .desc = "Encrypt private keys with IDEA",
263: .type = OPTION_ARGV_FUNC,
264: .opt.argvfunc = pkcs12_opt_enc,
265: },
266: #endif
267: {
268: .name = "cacerts",
269: .desc = "Only output CA certificates",
270: .type = OPTION_VALUE_OR,
271: .opt.value = &pkcs12_config.options,
272: .value = CACERTS,
273: },
274: {
275: .name = "CAfile",
276: .argname = "file",
277: .desc = "PEM format file of CA certificates",
278: .type = OPTION_ARG,
279: .opt.arg = &pkcs12_config.CAfile,
280: },
281: {
282: .name = "caname",
283: .argname = "name",
284: .desc = "Use name as CA friendly name (can be used more than once)",
285: .type = OPTION_ARG_FUNC,
286: .opt.argfunc = pkcs12_opt_canames,
287: },
288: {
289: .name = "CApath",
290: .argname = "directory",
291: .desc = "PEM format directory of CA certificates",
292: .type = OPTION_ARG,
293: .opt.arg = &pkcs12_config.CApath,
294: },
295: {
296: .name = "certfile",
297: .argname = "file",
298: .desc = "Add all certs in file",
299: .type = OPTION_ARG,
300: .opt.arg = &pkcs12_config.certfile,
301: },
302: {
303: .name = "certpbe",
304: .argname = "alg",
305: .desc = "Specify certificate PBE algorithm (default RC2-40)",
306: .type = OPTION_ARG_FUNC,
307: .opt.argfunc = pkcs12_opt_cert_pbe,
308: },
309: {
310: .name = "chain",
311: .desc = "Add certificate chain",
312: .type = OPTION_FLAG,
313: .opt.flag = &pkcs12_config.chain,
314: },
315: {
316: .name = "clcerts",
317: .desc = "Only output client certificates",
318: .type = OPTION_VALUE_OR,
319: .opt.value = &pkcs12_config.options,
320: .value = CLCERTS,
321: },
322: {
323: .name = "CSP",
324: .argname = "name",
325: .desc = "Microsoft CSP name",
326: .type = OPTION_ARG,
327: .opt.arg = &pkcs12_config.csp_name,
328: },
329: {
330: .name = "descert",
331: .desc = "Encrypt PKCS#12 certificates with triple DES (default RC2-40)",
332: .type = OPTION_VALUE,
333: .opt.value = &pkcs12_config.cert_pbe,
334: .value = NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
335: },
336: {
337: .name = "export",
338: .desc = "Output PKCS#12 file",
339: .type = OPTION_FLAG,
340: .opt.flag = &pkcs12_config.export_cert,
341: },
342: {
343: .name = "in",
344: .argname = "file",
345: .desc = "Input filename",
346: .type = OPTION_ARG,
347: .opt.arg = &pkcs12_config.infile,
348: },
349: {
350: .name = "info",
351: .desc = "Give info about PKCS#12 structure",
352: .type = OPTION_VALUE_OR,
353: .opt.value = &pkcs12_config.options,
354: .value = INFO,
355: },
356: {
357: .name = "inkey",
358: .argname = "file",
359: .desc = "Private key if not infile",
360: .type = OPTION_ARG,
361: .opt.arg = &pkcs12_config.keyname,
362: },
363: {
364: .name = "keyex",
365: .desc = "Set MS key exchange type",
366: .type = OPTION_VALUE,
367: .opt.value = &pkcs12_config.keytype,
368: .value = KEY_EX,
369: },
370: {
371: .name = "keypbe",
372: .argname = "alg",
373: .desc = "Specify private key PBE algorithm (default 3DES)",
374: .type = OPTION_ARG_FUNC,
375: .opt.argfunc = pkcs12_opt_key_pbe,
376: },
377: {
378: .name = "keysig",
379: .desc = "Set MS key signature type",
380: .type = OPTION_VALUE,
381: .opt.value = &pkcs12_config.keytype,
382: .value = KEY_SIG,
383: },
384: {
385: .name = "LMK",
386: .desc = "Add local machine keyset attribute to private key",
387: .type = OPTION_FLAG,
388: .opt.flag = &pkcs12_config.add_lmk,
389: },
390: {
391: .name = "macalg",
392: .argname = "alg",
393: .desc = "Digest algorithm used in MAC (default SHA1)",
394: .type = OPTION_ARG,
395: .opt.arg = &pkcs12_config.macalg,
396: },
397: {
398: .name = "maciter",
399: .desc = "Use MAC iteration",
400: .type = OPTION_VALUE,
401: .opt.value = &pkcs12_config.maciter,
402: .value = PKCS12_DEFAULT_ITER,
403: },
404: {
405: .name = "name",
406: .argname = "name",
407: .desc = "Use name as friendly name",
408: .type = OPTION_ARG,
409: .opt.arg = &pkcs12_config.name,
410: },
411: {
412: .name = "nocerts",
413: .desc = "Don't output certificates",
414: .type = OPTION_VALUE_OR,
415: .opt.value = &pkcs12_config.options,
416: .value = NOCERTS,
417: },
418: {
419: .name = "nodes",
420: .desc = "Don't encrypt private keys",
421: .type = OPTION_ARGV_FUNC,
422: .opt.argvfunc = pkcs12_opt_enc,
423: },
424: {
425: .name = "noiter",
426: .desc = "Don't use encryption iteration",
427: .type = OPTION_VALUE,
428: .opt.value = &pkcs12_config.iter,
429: .value = 1,
430: },
431: {
432: .name = "nokeys",
433: .desc = "Don't output private keys",
434: .type = OPTION_VALUE_OR,
435: .opt.value = &pkcs12_config.options,
436: .value = NOKEYS,
437: },
438: {
439: .name = "nomac",
440: .desc = "Don't generate MAC",
441: .type = OPTION_VALUE,
442: .opt.value = &pkcs12_config.maciter,
443: .value = -1,
444: },
445: {
446: .name = "nomaciter",
447: .desc = "Don't use MAC iteration",
448: .type = OPTION_VALUE,
449: .opt.value = &pkcs12_config.maciter,
450: .value = 1,
451: },
452: {
453: .name = "nomacver",
454: .desc = "Don't verify MAC",
455: .type = OPTION_VALUE,
456: .opt.value = &pkcs12_config.macver,
457: .value = 0,
458: },
459: {
460: .name = "noout",
461: .desc = "Don't output anything, just verify",
462: .type = OPTION_VALUE_OR,
463: .opt.value = &pkcs12_config.options,
464: .value = (NOKEYS | NOCERTS),
465: },
466: {
467: .name = "out",
468: .argname = "file",
469: .desc = "Output filename",
470: .type = OPTION_ARG,
471: .opt.arg = &pkcs12_config.outfile,
472: },
473: {
474: .name = "passin",
475: .argname = "arg",
476: .desc = "Input file passphrase source",
477: .type = OPTION_ARG,
478: .opt.arg = &pkcs12_config.passargin,
479: },
480: {
481: .name = "passout",
482: .argname = "arg",
483: .desc = "Output file passphrase source",
484: .type = OPTION_ARG,
485: .opt.arg = &pkcs12_config.passargout,
486: },
487: {
488: .name = "password",
489: .argname = "arg",
490: .desc = "Set import/export password source",
491: .type = OPTION_ARG_FUNC,
492: .opt.argfunc = pkcs12_opt_passarg,
493: },
494: {
495: .name = "twopass",
496: .desc = "Separate MAC, encryption passwords",
497: .type = OPTION_FLAG,
498: .opt.flag = &pkcs12_config.twopass,
499: },
500: { NULL },
501: };
502:
503: static void
504: pkcs12_usage(void)
505: {
506: fprintf(stderr, "usage: pkcs12 [-aes128 | -aes192 | -aes256 |");
507: fprintf(stderr, " -camellia128 |\n");
508: fprintf(stderr, " -camellia192 | -camellia256 | -des | -des3 |");
509: fprintf(stderr, " -idea]\n");
510: fprintf(stderr, " [-cacerts] [-CAfile file] [-caname name]\n");
511: fprintf(stderr, " [-CApath directory] [-certfile file]");
512: fprintf(stderr, " [-certpbe alg]\n");
513: fprintf(stderr, " [-chain] [-clcerts] [-CSP name] [-descert]");
514: fprintf(stderr, " [-export]\n");
515: fprintf(stderr, " [-in file] [-info] [-inkey file] [-keyex]");
516: fprintf(stderr, " [-keypbe alg]\n");
517: fprintf(stderr, " [-keysig] [-LMK] [-macalg alg] [-maciter]");
518: fprintf(stderr, " [-name name]\n");
519: fprintf(stderr, " [-nocerts] [-nodes] [-noiter] [-nokeys]");
520: fprintf(stderr, " [-nomac]\n");
521: fprintf(stderr, " [-nomaciter] [-nomacver] [-noout] [-out file]\n");
522: fprintf(stderr, " [-passin arg] [-passout arg] [-password arg]");
523: fprintf(stderr, " [-twopass]\n\n");
524: options_usage(pkcs12_options);
525: fprintf(stderr, "\n");
526: }
527:
1.1 jsing 528: int
529: pkcs12_main(int argc, char **argv)
530: {
531: BIO *in = NULL, *out = NULL;
532: PKCS12 *p12 = NULL;
533: char pass[50], macpass[50];
534: int ret = 1;
535: char *cpass = NULL, *mpass = NULL;
536: char *passin = NULL, *passout = NULL;
1.5 doug 537:
538: if (single_execution) {
1.9 deraadt 539: if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
1.5 doug 540: perror("pledge");
1.7 doug 541: exit(1);
542: }
1.5 doug 543: }
1.1 jsing 544:
1.11 inoguchi 545: memset(&pkcs12_config, 0, sizeof(pkcs12_config));
546: pkcs12_config.cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
547: pkcs12_config.enc = EVP_des_ede3_cbc();
548: pkcs12_config.iter = PKCS12_DEFAULT_ITER;
549: pkcs12_config.key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
550: pkcs12_config.maciter = PKCS12_DEFAULT_ITER;
551: pkcs12_config.macver = 1;
1.1 jsing 552:
1.12 inoguchi 553: if (options_parse(argc, argv, pkcs12_options, NULL, NULL) != 0) {
554: pkcs12_usage();
1.1 jsing 555: goto end;
556: }
557:
1.11 inoguchi 558: if (pkcs12_config.passarg) {
559: if (pkcs12_config.export_cert)
560: pkcs12_config.passargout = pkcs12_config.passarg;
1.1 jsing 561: else
1.11 inoguchi 562: pkcs12_config.passargin = pkcs12_config.passarg;
1.1 jsing 563: }
1.11 inoguchi 564: if (!app_passwd(bio_err, pkcs12_config.passargin, pkcs12_config.passargout, &passin, &passout)) {
1.1 jsing 565: BIO_printf(bio_err, "Error getting passwords\n");
566: goto end;
567: }
568: if (!cpass) {
1.11 inoguchi 569: if (pkcs12_config.export_cert)
1.1 jsing 570: cpass = passout;
571: else
572: cpass = passin;
573: }
574: if (cpass) {
575: mpass = cpass;
1.11 inoguchi 576: pkcs12_config.noprompt = 1;
1.1 jsing 577: } else {
578: cpass = pass;
579: mpass = macpass;
580: }
581:
1.11 inoguchi 582: if (!pkcs12_config.infile)
1.1 jsing 583: in = BIO_new_fp(stdin, BIO_NOCLOSE);
584: else
1.11 inoguchi 585: in = BIO_new_file(pkcs12_config.infile, "rb");
1.1 jsing 586: if (!in) {
587: BIO_printf(bio_err, "Error opening input file %s\n",
1.11 inoguchi 588: pkcs12_config.infile ? pkcs12_config.infile : "<stdin>");
589: perror(pkcs12_config.infile);
1.1 jsing 590: goto end;
591: }
592:
1.11 inoguchi 593: if (!pkcs12_config.outfile) {
1.1 jsing 594: out = BIO_new_fp(stdout, BIO_NOCLOSE);
595: } else
1.11 inoguchi 596: out = BIO_new_file(pkcs12_config.outfile, "wb");
1.1 jsing 597: if (!out) {
598: BIO_printf(bio_err, "Error opening output file %s\n",
1.11 inoguchi 599: pkcs12_config.outfile ? pkcs12_config.outfile : "<stdout>");
600: perror(pkcs12_config.outfile);
1.1 jsing 601: goto end;
602: }
1.11 inoguchi 603: if (pkcs12_config.twopass) {
604: if (EVP_read_pw_string(macpass, sizeof macpass, "Enter MAC Password:", pkcs12_config.export_cert)) {
1.1 jsing 605: BIO_printf(bio_err, "Can't read Password\n");
606: goto end;
607: }
608: }
1.11 inoguchi 609: if (pkcs12_config.export_cert) {
1.1 jsing 610: EVP_PKEY *key = NULL;
611: X509 *ucert = NULL, *x = NULL;
612: STACK_OF(X509) * certs = NULL;
613: const EVP_MD *macmd = NULL;
614: unsigned char *catmp = NULL;
615: int i;
616:
1.11 inoguchi 617: if ((pkcs12_config.options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
1.1 jsing 618: BIO_printf(bio_err, "Nothing to do!\n");
619: goto export_end;
620: }
1.11 inoguchi 621: if (pkcs12_config.options & NOCERTS)
622: pkcs12_config.chain = 0;
1.1 jsing 623:
1.11 inoguchi 624: if (!(pkcs12_config.options & NOKEYS)) {
625: key = load_key(bio_err, pkcs12_config.keyname ? pkcs12_config.keyname : pkcs12_config.infile,
1.4 bcook 626: FORMAT_PEM, 1, passin, "private key");
1.1 jsing 627: if (!key)
628: goto export_end;
629: }
630:
631: /* Load in all certs in input file */
1.11 inoguchi 632: if (!(pkcs12_config.options & NOCERTS)) {
633: certs = load_certs(bio_err, pkcs12_config.infile, FORMAT_PEM, NULL,
1.1 jsing 634: "certificates");
635: if (!certs)
636: goto export_end;
637:
638: if (key) {
639: /* Look for matching private key */
640: for (i = 0; i < sk_X509_num(certs); i++) {
641: x = sk_X509_value(certs, i);
642: if (X509_check_private_key(x, key)) {
643: ucert = x;
644: /* Zero keyid and alias */
645: X509_keyid_set1(ucert, NULL, 0);
646: X509_alias_set1(ucert, NULL, 0);
647: /* Remove from list */
648: (void) sk_X509_delete(certs, i);
649: break;
650: }
651: }
652: if (!ucert) {
653: BIO_printf(bio_err, "No certificate matches private key\n");
654: goto export_end;
655: }
656: }
657: }
658:
659: /* Add any more certificates asked for */
1.11 inoguchi 660: if (pkcs12_config.certfile) {
1.1 jsing 661: STACK_OF(X509) * morecerts = NULL;
1.11 inoguchi 662: if (!(morecerts = load_certs(bio_err, pkcs12_config.certfile, FORMAT_PEM,
1.4 bcook 663: NULL, "certificates from certfile")))
1.1 jsing 664: goto export_end;
665: while (sk_X509_num(morecerts) > 0)
666: sk_X509_push(certs, sk_X509_shift(morecerts));
667: sk_X509_free(morecerts);
668: }
669:
670:
671: /* If chaining get chain from user cert */
1.11 inoguchi 672: if (pkcs12_config.chain) {
1.1 jsing 673: int vret;
674: STACK_OF(X509) * chain2;
675: X509_STORE *store = X509_STORE_new();
676: if (!store) {
677: BIO_printf(bio_err, "Memory allocation error\n");
678: goto export_end;
679: }
1.11 inoguchi 680: if (!X509_STORE_load_locations(store, pkcs12_config.CAfile, pkcs12_config.CApath))
1.1 jsing 681: X509_STORE_set_default_paths(store);
682:
683: vret = get_cert_chain(ucert, store, &chain2);
684: X509_STORE_free(store);
685:
686: if (!vret) {
687: /* Exclude verified certificate */
688: for (i = 1; i < sk_X509_num(chain2); i++)
689: sk_X509_push(certs, sk_X509_value(chain2, i));
690: /* Free first certificate */
691: X509_free(sk_X509_value(chain2, 0));
692: sk_X509_free(chain2);
693: } else {
694: if (vret >= 0)
695: BIO_printf(bio_err, "Error %s getting chain.\n",
696: X509_verify_cert_error_string(vret));
697: else
698: ERR_print_errors(bio_err);
699: goto export_end;
700: }
701: }
702: /* Add any CA names */
703:
1.11 inoguchi 704: for (i = 0; i < sk_OPENSSL_STRING_num(pkcs12_config.canames); i++) {
705: catmp = (unsigned char *) sk_OPENSSL_STRING_value(pkcs12_config.canames, i);
1.1 jsing 706: X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
707: }
708:
1.11 inoguchi 709: if (pkcs12_config.csp_name && key)
1.1 jsing 710: EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
1.11 inoguchi 711: MBSTRING_ASC, (unsigned char *) pkcs12_config.csp_name, -1);
1.1 jsing 712:
1.11 inoguchi 713: if (pkcs12_config.add_lmk && key)
1.1 jsing 714: EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
715:
716:
1.11 inoguchi 717: if (!pkcs12_config.noprompt &&
1.1 jsing 718: EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:", 1)) {
719: BIO_printf(bio_err, "Can't read Password\n");
720: goto export_end;
721: }
1.11 inoguchi 722: if (!pkcs12_config.twopass)
1.1 jsing 723: strlcpy(macpass, pass, sizeof macpass);
724:
725:
1.11 inoguchi 726: p12 = PKCS12_create(cpass, pkcs12_config.name, key, ucert, certs,
727: pkcs12_config.key_pbe, pkcs12_config.cert_pbe, pkcs12_config.iter, -1, pkcs12_config.keytype);
1.1 jsing 728:
729: if (!p12) {
730: ERR_print_errors(bio_err);
731: goto export_end;
732: }
1.11 inoguchi 733: if (pkcs12_config.macalg) {
734: macmd = EVP_get_digestbyname(pkcs12_config.macalg);
1.1 jsing 735: if (!macmd) {
736: BIO_printf(bio_err, "Unknown digest algorithm %s\n",
1.11 inoguchi 737: pkcs12_config.macalg);
1.1 jsing 738: }
739: }
1.11 inoguchi 740: if (pkcs12_config.maciter != -1)
741: PKCS12_set_mac(p12, mpass, -1, NULL, 0, pkcs12_config.maciter, macmd);
1.1 jsing 742:
743:
744: i2d_PKCS12_bio(out, p12);
745:
746: ret = 0;
747:
748: export_end:
749:
1.13 ! inoguchi 750: EVP_PKEY_free(key);
! 751: sk_X509_pop_free(certs, X509_free);
! 752: X509_free(ucert);
1.1 jsing 753:
754: goto end;
755:
756: }
757: if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
758: ERR_print_errors(bio_err);
759: goto end;
760: }
1.11 inoguchi 761: if (!pkcs12_config.noprompt && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:", 0)) {
1.1 jsing 762: BIO_printf(bio_err, "Can't read Password\n");
763: goto end;
764: }
765:
1.11 inoguchi 766: if (!pkcs12_config.twopass)
1.1 jsing 767: strlcpy(macpass, pass, sizeof macpass);
768:
1.11 inoguchi 769: if ((pkcs12_config.options & INFO) && p12->mac)
1.1 jsing 770: BIO_printf(bio_err, "MAC Iteration %ld\n", p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
1.11 inoguchi 771: if (pkcs12_config.macver) {
1.1 jsing 772: /* If we enter empty password try no password first */
773: if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
774: /* If mac and crypto pass the same set it to NULL too */
1.11 inoguchi 775: if (!pkcs12_config.twopass)
1.1 jsing 776: cpass = NULL;
777: } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
778: BIO_printf(bio_err, "Mac verify error: invalid password?\n");
779: ERR_print_errors(bio_err);
780: goto end;
781: }
782: BIO_printf(bio_err, "MAC verified OK\n");
783: }
1.11 inoguchi 784: if (!dump_certs_keys_p12(out, p12, cpass, -1, pkcs12_config.options, passout)) {
1.1 jsing 785: BIO_printf(bio_err, "Error outputting keys and certificates\n");
786: ERR_print_errors(bio_err);
787: goto end;
788: }
789: ret = 0;
1.10 jsing 790: end:
1.13 ! inoguchi 791: PKCS12_free(p12);
1.1 jsing 792: BIO_free(in);
793: BIO_free_all(out);
1.13 ! inoguchi 794: sk_OPENSSL_STRING_free(pkcs12_config.canames);
1.1 jsing 795: free(passin);
796: free(passout);
797:
798: return (ret);
799: }
800:
801: int
802: dump_certs_keys_p12(BIO * out, PKCS12 * p12, char *pass,
803: int passlen, int options, char *pempass)
804: {
805: STACK_OF(PKCS7) * asafes = NULL;
806: STACK_OF(PKCS12_SAFEBAG) * bags;
807: int i, bagnid;
808: int ret = 0;
809: PKCS7 *p7;
810:
811: if (!(asafes = PKCS12_unpack_authsafes(p12)))
812: return 0;
813: for (i = 0; i < sk_PKCS7_num(asafes); i++) {
814: p7 = sk_PKCS7_value(asafes, i);
815: bagnid = OBJ_obj2nid(p7->type);
816: if (bagnid == NID_pkcs7_data) {
817: bags = PKCS12_unpack_p7data(p7);
818: if (options & INFO)
819: BIO_printf(bio_err, "PKCS7 Data\n");
820: } else if (bagnid == NID_pkcs7_encrypted) {
821: if (options & INFO) {
822: BIO_printf(bio_err, "PKCS7 Encrypted data: ");
823: alg_print(bio_err,
824: p7->d.encrypted->enc_data->algorithm);
825: }
826: bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
827: } else
828: continue;
829: if (!bags)
830: goto err;
831: if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
832: options, pempass)) {
833: sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
834: goto err;
835: }
836: sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
837: bags = NULL;
838: }
839: ret = 1;
840:
1.10 jsing 841: err:
1.13 ! inoguchi 842: sk_PKCS7_pop_free(asafes, PKCS7_free);
1.1 jsing 843: return ret;
844: }
845:
846: int
847: dump_certs_pkeys_bags(BIO * out, STACK_OF(PKCS12_SAFEBAG) * bags,
848: char *pass, int passlen, int options, char *pempass)
849: {
850: int i;
851: for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
852: if (!dump_certs_pkeys_bag(out,
853: sk_PKCS12_SAFEBAG_value(bags, i),
854: pass, passlen,
855: options, pempass))
856: return 0;
857: }
858: return 1;
859: }
860:
861: int
862: dump_certs_pkeys_bag(BIO * out, PKCS12_SAFEBAG * bag, char *pass,
863: int passlen, int options, char *pempass)
864: {
865: EVP_PKEY *pkey;
866: PKCS8_PRIV_KEY_INFO *p8;
867: X509 *x509;
868:
1.8 jsing 869: switch (OBJ_obj2nid(bag->type)) {
1.1 jsing 870: case NID_keyBag:
871: if (options & INFO)
872: BIO_printf(bio_err, "Key bag\n");
873: if (options & NOKEYS)
874: return 1;
875: print_attribs(out, bag->attrib, "Bag Attributes");
876: p8 = bag->value.keybag;
877: if (!(pkey = EVP_PKCS82PKEY(p8)))
878: return 0;
879: print_attribs(out, p8->attributes, "Key Attributes");
1.11 inoguchi 880: PEM_write_bio_PrivateKey(out, pkey, pkcs12_config.enc, NULL, 0, NULL, pempass);
1.1 jsing 881: EVP_PKEY_free(pkey);
882: break;
883:
884: case NID_pkcs8ShroudedKeyBag:
885: if (options & INFO) {
886: BIO_printf(bio_err, "Shrouded Keybag: ");
887: alg_print(bio_err, bag->value.shkeybag->algor);
888: }
889: if (options & NOKEYS)
890: return 1;
891: print_attribs(out, bag->attrib, "Bag Attributes");
892: if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
893: return 0;
894: if (!(pkey = EVP_PKCS82PKEY(p8))) {
895: PKCS8_PRIV_KEY_INFO_free(p8);
896: return 0;
897: }
898: print_attribs(out, p8->attributes, "Key Attributes");
899: PKCS8_PRIV_KEY_INFO_free(p8);
1.11 inoguchi 900: PEM_write_bio_PrivateKey(out, pkey, pkcs12_config.enc, NULL, 0, NULL, pempass);
1.1 jsing 901: EVP_PKEY_free(pkey);
902: break;
903:
904: case NID_certBag:
905: if (options & INFO)
906: BIO_printf(bio_err, "Certificate bag\n");
907: if (options & NOCERTS)
908: return 1;
909: if (PKCS12_get_attr(bag, NID_localKeyID)) {
910: if (options & CACERTS)
911: return 1;
912: } else if (options & CLCERTS)
913: return 1;
914: print_attribs(out, bag->attrib, "Bag Attributes");
1.8 jsing 915: if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
1.1 jsing 916: return 1;
917: if (!(x509 = PKCS12_certbag2x509(bag)))
918: return 0;
919: dump_cert_text(out, x509);
920: PEM_write_bio_X509(out, x509);
921: X509_free(x509);
922: break;
923:
924: case NID_safeContentsBag:
925: if (options & INFO)
926: BIO_printf(bio_err, "Safe Contents bag\n");
927: print_attribs(out, bag->attrib, "Bag Attributes");
928: return dump_certs_pkeys_bags(out, bag->value.safes, pass,
929: passlen, options, pempass);
930:
931: default:
932: BIO_printf(bio_err, "Warning unsupported bag type: ");
933: i2a_ASN1_OBJECT(bio_err, bag->type);
934: BIO_printf(bio_err, "\n");
935: return 1;
936: break;
937: }
938: return 1;
939: }
940:
941: /* Given a single certificate return a verified chain or NULL if error */
942:
943: /* Hope this is OK .... */
944:
945: int
946: get_cert_chain(X509 * cert, X509_STORE * store, STACK_OF(X509) ** chain)
947: {
948: X509_STORE_CTX store_ctx;
949: STACK_OF(X509) * chn;
950: int i = 0;
951:
952: /*
953: * FIXME: Should really check the return status of
954: * X509_STORE_CTX_init for an error, but how that fits into the
955: * return value of this function is less obvious.
956: */
957: X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
958: if (X509_verify_cert(&store_ctx) <= 0) {
959: i = X509_STORE_CTX_get_error(&store_ctx);
960: if (i == 0)
961: /*
962: * avoid returning 0 if X509_verify_cert() did not
963: * set an appropriate error value in the context
964: */
965: i = -1;
966: chn = NULL;
967: goto err;
968: } else
969: chn = X509_STORE_CTX_get1_chain(&store_ctx);
1.10 jsing 970: err:
1.1 jsing 971: X509_STORE_CTX_cleanup(&store_ctx);
972: *chain = chn;
973:
974: return i;
975: }
976:
977: int
978: alg_print(BIO * x, X509_ALGOR * alg)
979: {
980: PBEPARAM *pbe;
981: const unsigned char *p;
982: p = alg->parameter->value.sequence->data;
983: pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
984: if (!pbe)
985: return 1;
986: BIO_printf(bio_err, "%s, Iteration %ld\n",
987: OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
988: ASN1_INTEGER_get(pbe->iter));
989: PBEPARAM_free(pbe);
990: return 1;
991: }
992:
993: /* Load all certificates from a given file */
994:
995: int
996: cert_load(BIO * in, STACK_OF(X509) * sk)
997: {
998: int ret;
999: X509 *cert;
1000: ret = 0;
1001: while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1002: ret = 1;
1003: sk_X509_push(sk, cert);
1004: }
1005: if (ret)
1006: ERR_clear_error();
1007: return ret;
1008: }
1009:
1010: /* Generalised attribute print: handle PKCS#8 and bag attributes */
1011:
1012: int
1013: print_attribs(BIO * out, STACK_OF(X509_ATTRIBUTE) * attrlst, const char *name)
1014: {
1015: X509_ATTRIBUTE *attr;
1016: ASN1_TYPE *av;
1017: char *value;
1018: int i, attr_nid;
1019: if (!attrlst) {
1020: BIO_printf(out, "%s: <No Attributes>\n", name);
1021: return 1;
1022: }
1023: if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1024: BIO_printf(out, "%s: <Empty Attributes>\n", name);
1025: return 1;
1026: }
1027: BIO_printf(out, "%s\n", name);
1028: for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1029: attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1030: attr_nid = OBJ_obj2nid(attr->object);
1031: BIO_printf(out, " ");
1032: if (attr_nid == NID_undef) {
1033: i2a_ASN1_OBJECT(out, attr->object);
1034: BIO_printf(out, ": ");
1035: } else
1036: BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1037:
1038: if (sk_ASN1_TYPE_num(attr->value.set)) {
1039: av = sk_ASN1_TYPE_value(attr->value.set, 0);
1040: switch (av->type) {
1041: case V_ASN1_BMPSTRING:
1042: value = OPENSSL_uni2asc(av->value.bmpstring->data,
1043: av->value.bmpstring->length);
1044: BIO_printf(out, "%s\n", value);
1045: free(value);
1046: break;
1047:
1048: case V_ASN1_OCTET_STRING:
1049: hex_prin(out, av->value.octet_string->data,
1050: av->value.octet_string->length);
1051: BIO_printf(out, "\n");
1052: break;
1053:
1054: case V_ASN1_BIT_STRING:
1055: hex_prin(out, av->value.bit_string->data,
1056: av->value.bit_string->length);
1057: BIO_printf(out, "\n");
1058: break;
1059:
1060: default:
1061: BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1062: break;
1063: }
1064: } else
1065: BIO_printf(out, "<No Values>\n");
1066: }
1067: return 1;
1068: }
1069:
1070: void
1071: hex_prin(BIO * out, unsigned char *buf, int len)
1072: {
1073: int i;
1074: for (i = 0; i < len; i++)
1075: BIO_printf(out, "%02X ", buf[i]);
1076: }
1077:
1078: static int
1079: set_pbe(BIO * err, int *ppbe, const char *str)
1080: {
1081: if (!str)
1082: return 0;
1083: if (!strcmp(str, "NONE")) {
1084: *ppbe = -1;
1085: return 1;
1086: }
1087: *ppbe = OBJ_txt2nid(str);
1088: if (*ppbe == NID_undef) {
1089: BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
1090: return 0;
1091: }
1092: return 1;
1093: }
1094:
1095: #endif