pkcs12.c 26 KB

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