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