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