Annotation of src/usr.bin/openssl/pkcs12.c, Revision 1.12
1.12 ! inoguchi 1: /* $OpenBSD: pkcs12.c,v 1.11 2019/07/23 10:18:32 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:
750: if (key)
751: EVP_PKEY_free(key);
752: if (certs)
753: sk_X509_pop_free(certs, X509_free);
754: if (ucert)
755: X509_free(ucert);
756:
757: goto end;
758:
759: }
760: if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
761: ERR_print_errors(bio_err);
762: goto end;
763: }
1.11 inoguchi 764: if (!pkcs12_config.noprompt && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:", 0)) {
1.1 jsing 765: BIO_printf(bio_err, "Can't read Password\n");
766: goto end;
767: }
768:
1.11 inoguchi 769: if (!pkcs12_config.twopass)
1.1 jsing 770: strlcpy(macpass, pass, sizeof macpass);
771:
1.11 inoguchi 772: if ((pkcs12_config.options & INFO) && p12->mac)
1.1 jsing 773: BIO_printf(bio_err, "MAC Iteration %ld\n", p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
1.11 inoguchi 774: if (pkcs12_config.macver) {
1.1 jsing 775: /* If we enter empty password try no password first */
776: if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
777: /* If mac and crypto pass the same set it to NULL too */
1.11 inoguchi 778: if (!pkcs12_config.twopass)
1.1 jsing 779: cpass = NULL;
780: } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
781: BIO_printf(bio_err, "Mac verify error: invalid password?\n");
782: ERR_print_errors(bio_err);
783: goto end;
784: }
785: BIO_printf(bio_err, "MAC verified OK\n");
786: }
1.11 inoguchi 787: if (!dump_certs_keys_p12(out, p12, cpass, -1, pkcs12_config.options, passout)) {
1.1 jsing 788: BIO_printf(bio_err, "Error outputting keys and certificates\n");
789: ERR_print_errors(bio_err);
790: goto end;
791: }
792: ret = 0;
1.10 jsing 793: end:
1.1 jsing 794: if (p12)
795: PKCS12_free(p12);
796: BIO_free(in);
797: BIO_free_all(out);
1.11 inoguchi 798: if (pkcs12_config.canames)
799: sk_OPENSSL_STRING_free(pkcs12_config.canames);
1.1 jsing 800: free(passin);
801: free(passout);
802:
803: return (ret);
804: }
805:
806: int
807: dump_certs_keys_p12(BIO * out, PKCS12 * p12, char *pass,
808: int passlen, int options, char *pempass)
809: {
810: STACK_OF(PKCS7) * asafes = NULL;
811: STACK_OF(PKCS12_SAFEBAG) * bags;
812: int i, bagnid;
813: int ret = 0;
814: PKCS7 *p7;
815:
816: if (!(asafes = PKCS12_unpack_authsafes(p12)))
817: return 0;
818: for (i = 0; i < sk_PKCS7_num(asafes); i++) {
819: p7 = sk_PKCS7_value(asafes, i);
820: bagnid = OBJ_obj2nid(p7->type);
821: if (bagnid == NID_pkcs7_data) {
822: bags = PKCS12_unpack_p7data(p7);
823: if (options & INFO)
824: BIO_printf(bio_err, "PKCS7 Data\n");
825: } else if (bagnid == NID_pkcs7_encrypted) {
826: if (options & INFO) {
827: BIO_printf(bio_err, "PKCS7 Encrypted data: ");
828: alg_print(bio_err,
829: p7->d.encrypted->enc_data->algorithm);
830: }
831: bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
832: } else
833: continue;
834: if (!bags)
835: goto err;
836: if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
837: options, pempass)) {
838: sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
839: goto err;
840: }
841: sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
842: bags = NULL;
843: }
844: ret = 1;
845:
1.10 jsing 846: err:
1.1 jsing 847:
848: if (asafes)
849: sk_PKCS7_pop_free(asafes, PKCS7_free);
850: return ret;
851: }
852:
853: int
854: dump_certs_pkeys_bags(BIO * out, STACK_OF(PKCS12_SAFEBAG) * bags,
855: char *pass, int passlen, int options, char *pempass)
856: {
857: int i;
858: for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
859: if (!dump_certs_pkeys_bag(out,
860: sk_PKCS12_SAFEBAG_value(bags, i),
861: pass, passlen,
862: options, pempass))
863: return 0;
864: }
865: return 1;
866: }
867:
868: int
869: dump_certs_pkeys_bag(BIO * out, PKCS12_SAFEBAG * bag, char *pass,
870: int passlen, int options, char *pempass)
871: {
872: EVP_PKEY *pkey;
873: PKCS8_PRIV_KEY_INFO *p8;
874: X509 *x509;
875:
1.8 jsing 876: switch (OBJ_obj2nid(bag->type)) {
1.1 jsing 877: case NID_keyBag:
878: if (options & INFO)
879: BIO_printf(bio_err, "Key bag\n");
880: if (options & NOKEYS)
881: return 1;
882: print_attribs(out, bag->attrib, "Bag Attributes");
883: p8 = bag->value.keybag;
884: if (!(pkey = EVP_PKCS82PKEY(p8)))
885: return 0;
886: print_attribs(out, p8->attributes, "Key Attributes");
1.11 inoguchi 887: PEM_write_bio_PrivateKey(out, pkey, pkcs12_config.enc, NULL, 0, NULL, pempass);
1.1 jsing 888: EVP_PKEY_free(pkey);
889: break;
890:
891: case NID_pkcs8ShroudedKeyBag:
892: if (options & INFO) {
893: BIO_printf(bio_err, "Shrouded Keybag: ");
894: alg_print(bio_err, bag->value.shkeybag->algor);
895: }
896: if (options & NOKEYS)
897: return 1;
898: print_attribs(out, bag->attrib, "Bag Attributes");
899: if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
900: return 0;
901: if (!(pkey = EVP_PKCS82PKEY(p8))) {
902: PKCS8_PRIV_KEY_INFO_free(p8);
903: return 0;
904: }
905: print_attribs(out, p8->attributes, "Key Attributes");
906: PKCS8_PRIV_KEY_INFO_free(p8);
1.11 inoguchi 907: PEM_write_bio_PrivateKey(out, pkey, pkcs12_config.enc, NULL, 0, NULL, pempass);
1.1 jsing 908: EVP_PKEY_free(pkey);
909: break;
910:
911: case NID_certBag:
912: if (options & INFO)
913: BIO_printf(bio_err, "Certificate bag\n");
914: if (options & NOCERTS)
915: return 1;
916: if (PKCS12_get_attr(bag, NID_localKeyID)) {
917: if (options & CACERTS)
918: return 1;
919: } else if (options & CLCERTS)
920: return 1;
921: print_attribs(out, bag->attrib, "Bag Attributes");
1.8 jsing 922: if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
1.1 jsing 923: return 1;
924: if (!(x509 = PKCS12_certbag2x509(bag)))
925: return 0;
926: dump_cert_text(out, x509);
927: PEM_write_bio_X509(out, x509);
928: X509_free(x509);
929: break;
930:
931: case NID_safeContentsBag:
932: if (options & INFO)
933: BIO_printf(bio_err, "Safe Contents bag\n");
934: print_attribs(out, bag->attrib, "Bag Attributes");
935: return dump_certs_pkeys_bags(out, bag->value.safes, pass,
936: passlen, options, pempass);
937:
938: default:
939: BIO_printf(bio_err, "Warning unsupported bag type: ");
940: i2a_ASN1_OBJECT(bio_err, bag->type);
941: BIO_printf(bio_err, "\n");
942: return 1;
943: break;
944: }
945: return 1;
946: }
947:
948: /* Given a single certificate return a verified chain or NULL if error */
949:
950: /* Hope this is OK .... */
951:
952: int
953: get_cert_chain(X509 * cert, X509_STORE * store, STACK_OF(X509) ** chain)
954: {
955: X509_STORE_CTX store_ctx;
956: STACK_OF(X509) * chn;
957: int i = 0;
958:
959: /*
960: * FIXME: Should really check the return status of
961: * X509_STORE_CTX_init for an error, but how that fits into the
962: * return value of this function is less obvious.
963: */
964: X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
965: if (X509_verify_cert(&store_ctx) <= 0) {
966: i = X509_STORE_CTX_get_error(&store_ctx);
967: if (i == 0)
968: /*
969: * avoid returning 0 if X509_verify_cert() did not
970: * set an appropriate error value in the context
971: */
972: i = -1;
973: chn = NULL;
974: goto err;
975: } else
976: chn = X509_STORE_CTX_get1_chain(&store_ctx);
1.10 jsing 977: err:
1.1 jsing 978: X509_STORE_CTX_cleanup(&store_ctx);
979: *chain = chn;
980:
981: return i;
982: }
983:
984: int
985: alg_print(BIO * x, X509_ALGOR * alg)
986: {
987: PBEPARAM *pbe;
988: const unsigned char *p;
989: p = alg->parameter->value.sequence->data;
990: pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
991: if (!pbe)
992: return 1;
993: BIO_printf(bio_err, "%s, Iteration %ld\n",
994: OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
995: ASN1_INTEGER_get(pbe->iter));
996: PBEPARAM_free(pbe);
997: return 1;
998: }
999:
1000: /* Load all certificates from a given file */
1001:
1002: int
1003: cert_load(BIO * in, STACK_OF(X509) * sk)
1004: {
1005: int ret;
1006: X509 *cert;
1007: ret = 0;
1008: while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
1009: ret = 1;
1010: sk_X509_push(sk, cert);
1011: }
1012: if (ret)
1013: ERR_clear_error();
1014: return ret;
1015: }
1016:
1017: /* Generalised attribute print: handle PKCS#8 and bag attributes */
1018:
1019: int
1020: print_attribs(BIO * out, STACK_OF(X509_ATTRIBUTE) * attrlst, const char *name)
1021: {
1022: X509_ATTRIBUTE *attr;
1023: ASN1_TYPE *av;
1024: char *value;
1025: int i, attr_nid;
1026: if (!attrlst) {
1027: BIO_printf(out, "%s: <No Attributes>\n", name);
1028: return 1;
1029: }
1030: if (!sk_X509_ATTRIBUTE_num(attrlst)) {
1031: BIO_printf(out, "%s: <Empty Attributes>\n", name);
1032: return 1;
1033: }
1034: BIO_printf(out, "%s\n", name);
1035: for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
1036: attr = sk_X509_ATTRIBUTE_value(attrlst, i);
1037: attr_nid = OBJ_obj2nid(attr->object);
1038: BIO_printf(out, " ");
1039: if (attr_nid == NID_undef) {
1040: i2a_ASN1_OBJECT(out, attr->object);
1041: BIO_printf(out, ": ");
1042: } else
1043: BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
1044:
1045: if (sk_ASN1_TYPE_num(attr->value.set)) {
1046: av = sk_ASN1_TYPE_value(attr->value.set, 0);
1047: switch (av->type) {
1048: case V_ASN1_BMPSTRING:
1049: value = OPENSSL_uni2asc(av->value.bmpstring->data,
1050: av->value.bmpstring->length);
1051: BIO_printf(out, "%s\n", value);
1052: free(value);
1053: break;
1054:
1055: case V_ASN1_OCTET_STRING:
1056: hex_prin(out, av->value.octet_string->data,
1057: av->value.octet_string->length);
1058: BIO_printf(out, "\n");
1059: break;
1060:
1061: case V_ASN1_BIT_STRING:
1062: hex_prin(out, av->value.bit_string->data,
1063: av->value.bit_string->length);
1064: BIO_printf(out, "\n");
1065: break;
1066:
1067: default:
1068: BIO_printf(out, "<Unsupported tag %d>\n", av->type);
1069: break;
1070: }
1071: } else
1072: BIO_printf(out, "<No Values>\n");
1073: }
1074: return 1;
1075: }
1076:
1077: void
1078: hex_prin(BIO * out, unsigned char *buf, int len)
1079: {
1080: int i;
1081: for (i = 0; i < len; i++)
1082: BIO_printf(out, "%02X ", buf[i]);
1083: }
1084:
1085: static int
1086: set_pbe(BIO * err, int *ppbe, const char *str)
1087: {
1088: if (!str)
1089: return 0;
1090: if (!strcmp(str, "NONE")) {
1091: *ppbe = -1;
1092: return 1;
1093: }
1094: *ppbe = OBJ_txt2nid(str);
1095: if (*ppbe == NID_undef) {
1096: BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
1097: return 0;
1098: }
1099: return 1;
1100: }
1101:
1102: #endif