pkcs12.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /* pkcs12.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. #include <openssl/opensslconf.h>
  60. #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
  61. # include <stdio.h>
  62. # include <stdlib.h>
  63. # include <string.h>
  64. # include "apps.h"
  65. # include <openssl/crypto.h>
  66. # include <openssl/err.h>
  67. # include <openssl/pem.h>
  68. # include <openssl/pkcs12.h>
  69. # define PROG pkcs12_main
  70. const EVP_CIPHER *enc;
  71. # define NOKEYS 0x1
  72. # define NOCERTS 0x2
  73. # define INFO 0x4
  74. # define CLCERTS 0x8
  75. # define CACERTS 0x10
  76. int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain);
  77. int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass, int passlen,
  78. int options, char *pempass);
  79. int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
  80. char *pass, int passlen, int options,
  81. char *pempass);
  82. int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bags, char *pass,
  83. int passlen, int options, char *pempass);
  84. int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
  85. const char *name);
  86. void hex_prin(BIO *out, unsigned char *buf, int len);
  87. int alg_print(BIO *x, X509_ALGOR *alg);
  88. int cert_load(BIO *in, STACK_OF(X509) *sk);
  89. static int set_pbe(BIO *err, int *ppbe, const char *str);
  90. int MAIN(int, char **);
  91. int MAIN(int argc, char **argv)
  92. {
  93. ENGINE *e = NULL;
  94. char *infile = NULL, *outfile = NULL, *keyname = NULL;
  95. char *certfile = NULL;
  96. BIO *in = NULL, *out = NULL;
  97. char **args;
  98. char *name = NULL;
  99. char *csp_name = NULL;
  100. int add_lmk = 0;
  101. PKCS12 *p12 = NULL;
  102. char pass[50], macpass[50];
  103. int export_cert = 0;
  104. int options = 0;
  105. int chain = 0;
  106. int badarg = 0;
  107. int iter = PKCS12_DEFAULT_ITER;
  108. int maciter = PKCS12_DEFAULT_ITER;
  109. int twopass = 0;
  110. int keytype = 0;
  111. int cert_pbe;
  112. int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  113. int ret = 1;
  114. int macver = 1;
  115. int noprompt = 0;
  116. STACK_OF(OPENSSL_STRING) *canames = NULL;
  117. char *cpass = NULL, *mpass = NULL;
  118. char *passargin = NULL, *passargout = NULL, *passarg = NULL;
  119. char *passin = NULL, *passout = NULL;
  120. char *inrand = NULL;
  121. char *macalg = NULL;
  122. char *CApath = NULL, *CAfile = NULL;
  123. # ifndef OPENSSL_NO_ENGINE
  124. char *engine = NULL;
  125. # endif
  126. apps_startup();
  127. # ifdef OPENSSL_FIPS
  128. if (FIPS_mode())
  129. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  130. else
  131. # endif
  132. cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;
  133. enc = EVP_des_ede3_cbc();
  134. if (bio_err == NULL)
  135. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  136. if (!load_config(bio_err, NULL))
  137. goto end;
  138. args = argv + 1;
  139. while (*args) {
  140. if (*args[0] == '-') {
  141. if (!strcmp(*args, "-nokeys"))
  142. options |= NOKEYS;
  143. else if (!strcmp(*args, "-keyex"))
  144. keytype = KEY_EX;
  145. else if (!strcmp(*args, "-keysig"))
  146. keytype = KEY_SIG;
  147. else if (!strcmp(*args, "-nocerts"))
  148. options |= NOCERTS;
  149. else if (!strcmp(*args, "-clcerts"))
  150. options |= CLCERTS;
  151. else if (!strcmp(*args, "-cacerts"))
  152. options |= CACERTS;
  153. else if (!strcmp(*args, "-noout"))
  154. options |= (NOKEYS | NOCERTS);
  155. else if (!strcmp(*args, "-info"))
  156. options |= INFO;
  157. else if (!strcmp(*args, "-chain"))
  158. chain = 1;
  159. else if (!strcmp(*args, "-twopass"))
  160. twopass = 1;
  161. else if (!strcmp(*args, "-nomacver"))
  162. macver = 0;
  163. else if (!strcmp(*args, "-descert"))
  164. cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
  165. else if (!strcmp(*args, "-export"))
  166. export_cert = 1;
  167. else if (!strcmp(*args, "-des"))
  168. enc = EVP_des_cbc();
  169. else if (!strcmp(*args, "-des3"))
  170. enc = EVP_des_ede3_cbc();
  171. # ifndef OPENSSL_NO_IDEA
  172. else if (!strcmp(*args, "-idea"))
  173. enc = EVP_idea_cbc();
  174. # endif
  175. # ifndef OPENSSL_NO_SEED
  176. else if (!strcmp(*args, "-seed"))
  177. enc = EVP_seed_cbc();
  178. # endif
  179. # ifndef OPENSSL_NO_AES
  180. else if (!strcmp(*args, "-aes128"))
  181. enc = EVP_aes_128_cbc();
  182. else if (!strcmp(*args, "-aes192"))
  183. enc = EVP_aes_192_cbc();
  184. else if (!strcmp(*args, "-aes256"))
  185. enc = EVP_aes_256_cbc();
  186. # endif
  187. # ifndef OPENSSL_NO_CAMELLIA
  188. else if (!strcmp(*args, "-camellia128"))
  189. enc = EVP_camellia_128_cbc();
  190. else if (!strcmp(*args, "-camellia192"))
  191. enc = EVP_camellia_192_cbc();
  192. else if (!strcmp(*args, "-camellia256"))
  193. enc = EVP_camellia_256_cbc();
  194. # endif
  195. else if (!strcmp(*args, "-noiter"))
  196. iter = 1;
  197. else if (!strcmp(*args, "-maciter"))
  198. maciter = PKCS12_DEFAULT_ITER;
  199. else if (!strcmp(*args, "-nomaciter"))
  200. maciter = 1;
  201. else if (!strcmp(*args, "-nomac"))
  202. maciter = -1;
  203. else if (!strcmp(*args, "-macalg"))
  204. if (args[1]) {
  205. args++;
  206. macalg = *args;
  207. } else
  208. badarg = 1;
  209. else if (!strcmp(*args, "-nodes"))
  210. enc = NULL;
  211. else if (!strcmp(*args, "-certpbe")) {
  212. if (!set_pbe(bio_err, &cert_pbe, *++args))
  213. badarg = 1;
  214. } else if (!strcmp(*args, "-keypbe")) {
  215. if (!set_pbe(bio_err, &key_pbe, *++args))
  216. badarg = 1;
  217. } else if (!strcmp(*args, "-rand")) {
  218. if (args[1]) {
  219. args++;
  220. inrand = *args;
  221. } else
  222. badarg = 1;
  223. } else if (!strcmp(*args, "-inkey")) {
  224. if (args[1]) {
  225. args++;
  226. keyname = *args;
  227. } else
  228. badarg = 1;
  229. } else if (!strcmp(*args, "-certfile")) {
  230. if (args[1]) {
  231. args++;
  232. certfile = *args;
  233. } else
  234. badarg = 1;
  235. } else if (!strcmp(*args, "-name")) {
  236. if (args[1]) {
  237. args++;
  238. name = *args;
  239. } else
  240. badarg = 1;
  241. } else if (!strcmp(*args, "-LMK"))
  242. add_lmk = 1;
  243. else if (!strcmp(*args, "-CSP")) {
  244. if (args[1]) {
  245. args++;
  246. csp_name = *args;
  247. } else
  248. badarg = 1;
  249. } else if (!strcmp(*args, "-caname")) {
  250. if (args[1]) {
  251. args++;
  252. if (!canames)
  253. canames = sk_OPENSSL_STRING_new_null();
  254. sk_OPENSSL_STRING_push(canames, *args);
  255. } else
  256. badarg = 1;
  257. } else if (!strcmp(*args, "-in")) {
  258. if (args[1]) {
  259. args++;
  260. infile = *args;
  261. } else
  262. badarg = 1;
  263. } else if (!strcmp(*args, "-out")) {
  264. if (args[1]) {
  265. args++;
  266. outfile = *args;
  267. } else
  268. badarg = 1;
  269. } else if (!strcmp(*args, "-passin")) {
  270. if (args[1]) {
  271. args++;
  272. passargin = *args;
  273. } else
  274. badarg = 1;
  275. } else if (!strcmp(*args, "-passout")) {
  276. if (args[1]) {
  277. args++;
  278. passargout = *args;
  279. } else
  280. badarg = 1;
  281. } else if (!strcmp(*args, "-password")) {
  282. if (args[1]) {
  283. args++;
  284. passarg = *args;
  285. noprompt = 1;
  286. } else
  287. badarg = 1;
  288. } else if (!strcmp(*args, "-CApath")) {
  289. if (args[1]) {
  290. args++;
  291. CApath = *args;
  292. } else
  293. badarg = 1;
  294. } else if (!strcmp(*args, "-CAfile")) {
  295. if (args[1]) {
  296. args++;
  297. CAfile = *args;
  298. } else
  299. badarg = 1;
  300. # ifndef OPENSSL_NO_ENGINE
  301. } else if (!strcmp(*args, "-engine")) {
  302. if (args[1]) {
  303. args++;
  304. engine = *args;
  305. } else
  306. badarg = 1;
  307. # endif
  308. } else
  309. badarg = 1;
  310. } else
  311. badarg = 1;
  312. args++;
  313. }
  314. if (badarg) {
  315. BIO_printf(bio_err, "Usage: pkcs12 [options]\n");
  316. BIO_printf(bio_err, "where options are\n");
  317. BIO_printf(bio_err, "-export output PKCS12 file\n");
  318. BIO_printf(bio_err, "-chain add certificate chain\n");
  319. BIO_printf(bio_err, "-inkey file private key if not infile\n");
  320. BIO_printf(bio_err, "-certfile f add all certs in f\n");
  321. BIO_printf(bio_err, "-CApath arg - PEM format directory of CA's\n");
  322. BIO_printf(bio_err, "-CAfile arg - PEM format file of CA's\n");
  323. BIO_printf(bio_err, "-name \"name\" use name as friendly name\n");
  324. BIO_printf(bio_err,
  325. "-caname \"nm\" use nm as CA friendly name (can be used more than once).\n");
  326. BIO_printf(bio_err, "-in infile input filename\n");
  327. BIO_printf(bio_err, "-out outfile output filename\n");
  328. BIO_printf(bio_err,
  329. "-noout don't output anything, just verify.\n");
  330. BIO_printf(bio_err, "-nomacver don't verify MAC.\n");
  331. BIO_printf(bio_err, "-nocerts don't output certificates.\n");
  332. BIO_printf(bio_err,
  333. "-clcerts only output client certificates.\n");
  334. BIO_printf(bio_err, "-cacerts only output CA certificates.\n");
  335. BIO_printf(bio_err, "-nokeys don't output private keys.\n");
  336. BIO_printf(bio_err,
  337. "-info give info about PKCS#12 structure.\n");
  338. BIO_printf(bio_err, "-des encrypt private keys with DES\n");
  339. BIO_printf(bio_err,
  340. "-des3 encrypt private keys with triple DES (default)\n");
  341. # ifndef OPENSSL_NO_IDEA
  342. BIO_printf(bio_err, "-idea encrypt private keys with idea\n");
  343. # endif
  344. # ifndef OPENSSL_NO_SEED
  345. BIO_printf(bio_err, "-seed encrypt private keys with seed\n");
  346. # endif
  347. # ifndef OPENSSL_NO_AES
  348. BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
  349. BIO_printf(bio_err,
  350. " encrypt PEM output with cbc aes\n");
  351. # endif
  352. # ifndef OPENSSL_NO_CAMELLIA
  353. BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
  354. BIO_printf(bio_err,
  355. " encrypt PEM output with cbc camellia\n");
  356. # endif
  357. BIO_printf(bio_err, "-nodes don't encrypt private keys\n");
  358. BIO_printf(bio_err, "-noiter don't use encryption iteration\n");
  359. BIO_printf(bio_err, "-nomaciter don't use MAC iteration\n");
  360. BIO_printf(bio_err, "-maciter use MAC iteration\n");
  361. BIO_printf(bio_err, "-nomac don't generate MAC\n");
  362. BIO_printf(bio_err,
  363. "-twopass separate MAC, encryption passwords\n");
  364. BIO_printf(bio_err,
  365. "-descert encrypt PKCS#12 certificates with triple DES (default RC2-40)\n");
  366. BIO_printf(bio_err,
  367. "-certpbe alg specify certificate PBE algorithm (default RC2-40)\n");
  368. BIO_printf(bio_err,
  369. "-keypbe alg specify private key PBE algorithm (default 3DES)\n");
  370. BIO_printf(bio_err,
  371. "-macalg alg digest algorithm used in MAC (default SHA1)\n");
  372. BIO_printf(bio_err, "-keyex set MS key exchange type\n");
  373. BIO_printf(bio_err, "-keysig set MS key signature type\n");
  374. BIO_printf(bio_err,
  375. "-password p set import/export password source\n");
  376. BIO_printf(bio_err, "-passin p input file pass phrase source\n");
  377. BIO_printf(bio_err, "-passout p output file pass phrase source\n");
  378. # ifndef OPENSSL_NO_ENGINE
  379. BIO_printf(bio_err,
  380. "-engine e use engine e, possibly a hardware device.\n");
  381. # endif
  382. BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  383. LIST_SEPARATOR_CHAR);
  384. BIO_printf(bio_err,
  385. " load the file (or the files in the directory) into\n");
  386. BIO_printf(bio_err, " the random number generator\n");
  387. BIO_printf(bio_err, "-CSP name Microsoft CSP name\n");
  388. BIO_printf(bio_err,
  389. "-LMK Add local machine keyset attribute to private key\n");
  390. goto end;
  391. }
  392. # ifndef OPENSSL_NO_ENGINE
  393. e = setup_engine(bio_err, engine, 0);
  394. # endif
  395. if (passarg) {
  396. if (export_cert)
  397. passargout = passarg;
  398. else
  399. passargin = passarg;
  400. }
  401. if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
  402. BIO_printf(bio_err, "Error getting passwords\n");
  403. goto end;
  404. }
  405. if (!cpass) {
  406. if (export_cert)
  407. cpass = passout;
  408. else
  409. cpass = passin;
  410. }
  411. if (cpass) {
  412. mpass = cpass;
  413. noprompt = 1;
  414. } else {
  415. cpass = pass;
  416. mpass = macpass;
  417. }
  418. if (export_cert || inrand) {
  419. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  420. if (inrand != NULL)
  421. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  422. app_RAND_load_files(inrand));
  423. }
  424. ERR_load_crypto_strings();
  425. # ifdef CRYPTO_MDEBUG
  426. CRYPTO_push_info("read files");
  427. # endif
  428. if (!infile)
  429. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  430. else
  431. in = BIO_new_file(infile, "rb");
  432. if (!in) {
  433. BIO_printf(bio_err, "Error opening input file %s\n",
  434. infile ? infile : "<stdin>");
  435. perror(infile);
  436. goto end;
  437. }
  438. # ifdef CRYPTO_MDEBUG
  439. CRYPTO_pop_info();
  440. CRYPTO_push_info("write files");
  441. # endif
  442. if (!outfile) {
  443. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  444. # ifdef OPENSSL_SYS_VMS
  445. {
  446. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  447. out = BIO_push(tmpbio, out);
  448. }
  449. # endif
  450. } else
  451. out = BIO_new_file(outfile, "wb");
  452. if (!out) {
  453. BIO_printf(bio_err, "Error opening output file %s\n",
  454. outfile ? outfile : "<stdout>");
  455. perror(outfile);
  456. goto end;
  457. }
  458. if (twopass) {
  459. # ifdef CRYPTO_MDEBUG
  460. CRYPTO_push_info("read MAC password");
  461. # endif
  462. if (EVP_read_pw_string
  463. (macpass, sizeof macpass, "Enter MAC Password:", export_cert)) {
  464. BIO_printf(bio_err, "Can't read Password\n");
  465. goto end;
  466. }
  467. # ifdef CRYPTO_MDEBUG
  468. CRYPTO_pop_info();
  469. # endif
  470. }
  471. if (export_cert) {
  472. EVP_PKEY *key = NULL;
  473. X509 *ucert = NULL, *x = NULL;
  474. STACK_OF(X509) *certs = NULL;
  475. const EVP_MD *macmd = NULL;
  476. unsigned char *catmp = NULL;
  477. int i;
  478. if ((options & (NOCERTS | NOKEYS)) == (NOCERTS | NOKEYS)) {
  479. BIO_printf(bio_err, "Nothing to do!\n");
  480. goto export_end;
  481. }
  482. if (options & NOCERTS)
  483. chain = 0;
  484. # ifdef CRYPTO_MDEBUG
  485. CRYPTO_push_info("process -export_cert");
  486. CRYPTO_push_info("reading private key");
  487. # endif
  488. if (!(options & NOKEYS)) {
  489. key = load_key(bio_err, keyname ? keyname : infile,
  490. FORMAT_PEM, 1, passin, e, "private key");
  491. if (!key)
  492. goto export_end;
  493. }
  494. # ifdef CRYPTO_MDEBUG
  495. CRYPTO_pop_info();
  496. CRYPTO_push_info("reading certs from input");
  497. # endif
  498. /* Load in all certs in input file */
  499. if (!(options & NOCERTS)) {
  500. certs = load_certs(bio_err, infile, FORMAT_PEM, NULL, e,
  501. "certificates");
  502. if (!certs)
  503. goto export_end;
  504. if (key) {
  505. /* Look for matching private key */
  506. for (i = 0; i < sk_X509_num(certs); i++) {
  507. x = sk_X509_value(certs, i);
  508. if (X509_check_private_key(x, key)) {
  509. ucert = x;
  510. /* Zero keyid and alias */
  511. X509_keyid_set1(ucert, NULL, 0);
  512. X509_alias_set1(ucert, NULL, 0);
  513. /* Remove from list */
  514. (void)sk_X509_delete(certs, i);
  515. break;
  516. }
  517. }
  518. if (!ucert) {
  519. BIO_printf(bio_err,
  520. "No certificate matches private key\n");
  521. goto export_end;
  522. }
  523. }
  524. }
  525. # ifdef CRYPTO_MDEBUG
  526. CRYPTO_pop_info();
  527. CRYPTO_push_info("reading certs from input 2");
  528. # endif
  529. /* Add any more certificates asked for */
  530. if (certfile) {
  531. STACK_OF(X509) *morecerts = NULL;
  532. if (!(morecerts = load_certs(bio_err, certfile, FORMAT_PEM,
  533. NULL, e,
  534. "certificates from certfile")))
  535. goto export_end;
  536. while (sk_X509_num(morecerts) > 0)
  537. sk_X509_push(certs, sk_X509_shift(morecerts));
  538. sk_X509_free(morecerts);
  539. }
  540. # ifdef CRYPTO_MDEBUG
  541. CRYPTO_pop_info();
  542. CRYPTO_push_info("reading certs from certfile");
  543. # endif
  544. # ifdef CRYPTO_MDEBUG
  545. CRYPTO_pop_info();
  546. CRYPTO_push_info("building chain");
  547. # endif
  548. /* If chaining get chain from user cert */
  549. if (chain) {
  550. int vret;
  551. STACK_OF(X509) *chain2;
  552. X509_STORE *store = X509_STORE_new();
  553. if (!store) {
  554. BIO_printf(bio_err, "Memory allocation error\n");
  555. goto export_end;
  556. }
  557. if (!X509_STORE_load_locations(store, CAfile, CApath))
  558. X509_STORE_set_default_paths(store);
  559. vret = get_cert_chain(ucert, store, &chain2);
  560. X509_STORE_free(store);
  561. if (!vret) {
  562. /* Exclude verified certificate */
  563. for (i = 1; i < sk_X509_num(chain2); i++)
  564. sk_X509_push(certs, sk_X509_value(chain2, i));
  565. /* Free first certificate */
  566. X509_free(sk_X509_value(chain2, 0));
  567. sk_X509_free(chain2);
  568. } else {
  569. if (vret >= 0)
  570. BIO_printf(bio_err, "Error %s getting chain.\n",
  571. X509_verify_cert_error_string(vret));
  572. else
  573. ERR_print_errors(bio_err);
  574. goto export_end;
  575. }
  576. }
  577. /* Add any CA names */
  578. for (i = 0; i < sk_OPENSSL_STRING_num(canames); i++) {
  579. catmp = (unsigned char *)sk_OPENSSL_STRING_value(canames, i);
  580. X509_alias_set1(sk_X509_value(certs, i), catmp, -1);
  581. }
  582. if (csp_name && key)
  583. EVP_PKEY_add1_attr_by_NID(key, NID_ms_csp_name,
  584. MBSTRING_ASC, (unsigned char *)csp_name,
  585. -1);
  586. if (add_lmk && key)
  587. EVP_PKEY_add1_attr_by_NID(key, NID_LocalKeySet, 0, NULL, -1);
  588. # ifdef CRYPTO_MDEBUG
  589. CRYPTO_pop_info();
  590. CRYPTO_push_info("reading password");
  591. # endif
  592. if (!noprompt &&
  593. EVP_read_pw_string(pass, sizeof pass, "Enter Export Password:",
  594. 1)) {
  595. BIO_printf(bio_err, "Can't read Password\n");
  596. goto export_end;
  597. }
  598. if (!twopass)
  599. BUF_strlcpy(macpass, pass, sizeof macpass);
  600. # ifdef CRYPTO_MDEBUG
  601. CRYPTO_pop_info();
  602. CRYPTO_push_info("creating PKCS#12 structure");
  603. # endif
  604. p12 = PKCS12_create(cpass, name, key, ucert, certs,
  605. key_pbe, cert_pbe, iter, -1, keytype);
  606. if (!p12) {
  607. ERR_print_errors(bio_err);
  608. goto export_end;
  609. }
  610. if (macalg) {
  611. macmd = EVP_get_digestbyname(macalg);
  612. if (!macmd) {
  613. BIO_printf(bio_err, "Unknown digest algorithm %s\n", macalg);
  614. }
  615. }
  616. if (maciter != -1)
  617. PKCS12_set_mac(p12, mpass, -1, NULL, 0, maciter, macmd);
  618. # ifdef CRYPTO_MDEBUG
  619. CRYPTO_pop_info();
  620. CRYPTO_push_info("writing pkcs12");
  621. # endif
  622. i2d_PKCS12_bio(out, p12);
  623. ret = 0;
  624. export_end:
  625. # ifdef CRYPTO_MDEBUG
  626. CRYPTO_pop_info();
  627. CRYPTO_pop_info();
  628. CRYPTO_push_info("process -export_cert: freeing");
  629. # endif
  630. if (key)
  631. EVP_PKEY_free(key);
  632. if (certs)
  633. sk_X509_pop_free(certs, X509_free);
  634. if (ucert)
  635. X509_free(ucert);
  636. # ifdef CRYPTO_MDEBUG
  637. CRYPTO_pop_info();
  638. # endif
  639. goto end;
  640. }
  641. if (!(p12 = d2i_PKCS12_bio(in, NULL))) {
  642. ERR_print_errors(bio_err);
  643. goto end;
  644. }
  645. # ifdef CRYPTO_MDEBUG
  646. CRYPTO_push_info("read import password");
  647. # endif
  648. if (!noprompt
  649. && EVP_read_pw_string(pass, sizeof pass, "Enter Import Password:",
  650. 0)) {
  651. BIO_printf(bio_err, "Can't read Password\n");
  652. goto end;
  653. }
  654. # ifdef CRYPTO_MDEBUG
  655. CRYPTO_pop_info();
  656. # endif
  657. if (!twopass)
  658. BUF_strlcpy(macpass, pass, sizeof macpass);
  659. if ((options & INFO) && p12->mac)
  660. BIO_printf(bio_err, "MAC Iteration %ld\n",
  661. p12->mac->iter ? ASN1_INTEGER_get(p12->mac->iter) : 1);
  662. if (macver) {
  663. # ifdef CRYPTO_MDEBUG
  664. CRYPTO_push_info("verify MAC");
  665. # endif
  666. /* If we enter empty password try no password first */
  667. if (!mpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {
  668. /* If mac and crypto pass the same set it to NULL too */
  669. if (!twopass)
  670. cpass = NULL;
  671. } else if (!PKCS12_verify_mac(p12, mpass, -1)) {
  672. BIO_printf(bio_err, "Mac verify error: invalid password?\n");
  673. ERR_print_errors(bio_err);
  674. goto end;
  675. }
  676. BIO_printf(bio_err, "MAC verified OK\n");
  677. # ifdef CRYPTO_MDEBUG
  678. CRYPTO_pop_info();
  679. # endif
  680. }
  681. # ifdef CRYPTO_MDEBUG
  682. CRYPTO_push_info("output keys and certificates");
  683. # endif
  684. if (!dump_certs_keys_p12(out, p12, cpass, -1, options, passout)) {
  685. BIO_printf(bio_err, "Error outputting keys and certificates\n");
  686. ERR_print_errors(bio_err);
  687. goto end;
  688. }
  689. # ifdef CRYPTO_MDEBUG
  690. CRYPTO_pop_info();
  691. # endif
  692. ret = 0;
  693. end:
  694. if (p12)
  695. PKCS12_free(p12);
  696. if (export_cert || inrand)
  697. app_RAND_write_file(NULL, bio_err);
  698. # ifdef CRYPTO_MDEBUG
  699. CRYPTO_remove_all_info();
  700. # endif
  701. BIO_free(in);
  702. BIO_free_all(out);
  703. if (canames)
  704. sk_OPENSSL_STRING_free(canames);
  705. if (passin)
  706. OPENSSL_free(passin);
  707. if (passout)
  708. OPENSSL_free(passout);
  709. apps_shutdown();
  710. OPENSSL_EXIT(ret);
  711. }
  712. int dump_certs_keys_p12(BIO *out, PKCS12 *p12, char *pass,
  713. int passlen, int options, char *pempass)
  714. {
  715. STACK_OF(PKCS7) *asafes = NULL;
  716. STACK_OF(PKCS12_SAFEBAG) *bags;
  717. int i, bagnid;
  718. int ret = 0;
  719. PKCS7 *p7;
  720. if (!(asafes = PKCS12_unpack_authsafes(p12)))
  721. return 0;
  722. for (i = 0; i < sk_PKCS7_num(asafes); i++) {
  723. p7 = sk_PKCS7_value(asafes, i);
  724. bagnid = OBJ_obj2nid(p7->type);
  725. if (bagnid == NID_pkcs7_data) {
  726. bags = PKCS12_unpack_p7data(p7);
  727. if (options & INFO)
  728. BIO_printf(bio_err, "PKCS7 Data\n");
  729. } else if (bagnid == NID_pkcs7_encrypted) {
  730. if (options & INFO) {
  731. BIO_printf(bio_err, "PKCS7 Encrypted data: ");
  732. alg_print(bio_err, p7->d.encrypted->enc_data->algorithm);
  733. }
  734. bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
  735. } else
  736. continue;
  737. if (!bags)
  738. goto err;
  739. if (!dump_certs_pkeys_bags(out, bags, pass, passlen,
  740. options, pempass)) {
  741. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  742. goto err;
  743. }
  744. sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
  745. bags = NULL;
  746. }
  747. ret = 1;
  748. err:
  749. if (asafes)
  750. sk_PKCS7_pop_free(asafes, PKCS7_free);
  751. return ret;
  752. }
  753. int dump_certs_pkeys_bags(BIO *out, STACK_OF(PKCS12_SAFEBAG) *bags,
  754. char *pass, int passlen, int options, char *pempass)
  755. {
  756. int i;
  757. for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
  758. if (!dump_certs_pkeys_bag(out,
  759. sk_PKCS12_SAFEBAG_value(bags, i),
  760. pass, passlen, options, pempass))
  761. return 0;
  762. }
  763. return 1;
  764. }
  765. int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
  766. int passlen, int options, char *pempass)
  767. {
  768. EVP_PKEY *pkey;
  769. PKCS8_PRIV_KEY_INFO *p8;
  770. X509 *x509;
  771. switch (M_PKCS12_bag_type(bag)) {
  772. case NID_keyBag:
  773. if (options & INFO)
  774. BIO_printf(bio_err, "Key bag\n");
  775. if (options & NOKEYS)
  776. return 1;
  777. print_attribs(out, bag->attrib, "Bag Attributes");
  778. p8 = bag->value.keybag;
  779. if (!(pkey = EVP_PKCS82PKEY(p8)))
  780. return 0;
  781. print_attribs(out, p8->attributes, "Key Attributes");
  782. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  783. EVP_PKEY_free(pkey);
  784. break;
  785. case NID_pkcs8ShroudedKeyBag:
  786. if (options & INFO) {
  787. BIO_printf(bio_err, "Shrouded Keybag: ");
  788. alg_print(bio_err, bag->value.shkeybag->algor);
  789. }
  790. if (options & NOKEYS)
  791. return 1;
  792. print_attribs(out, bag->attrib, "Bag Attributes");
  793. if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
  794. return 0;
  795. if (!(pkey = EVP_PKCS82PKEY(p8))) {
  796. PKCS8_PRIV_KEY_INFO_free(p8);
  797. return 0;
  798. }
  799. print_attribs(out, p8->attributes, "Key Attributes");
  800. PKCS8_PRIV_KEY_INFO_free(p8);
  801. PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, pempass);
  802. EVP_PKEY_free(pkey);
  803. break;
  804. case NID_certBag:
  805. if (options & INFO)
  806. BIO_printf(bio_err, "Certificate bag\n");
  807. if (options & NOCERTS)
  808. return 1;
  809. if (PKCS12_get_attr(bag, NID_localKeyID)) {
  810. if (options & CACERTS)
  811. return 1;
  812. } else if (options & CLCERTS)
  813. return 1;
  814. print_attribs(out, bag->attrib, "Bag Attributes");
  815. if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
  816. return 1;
  817. if (!(x509 = PKCS12_certbag2x509(bag)))
  818. return 0;
  819. dump_cert_text(out, x509);
  820. PEM_write_bio_X509(out, x509);
  821. X509_free(x509);
  822. break;
  823. case NID_safeContentsBag:
  824. if (options & INFO)
  825. BIO_printf(bio_err, "Safe Contents bag\n");
  826. print_attribs(out, bag->attrib, "Bag Attributes");
  827. return dump_certs_pkeys_bags(out, bag->value.safes, pass,
  828. passlen, options, pempass);
  829. default:
  830. BIO_printf(bio_err, "Warning unsupported bag type: ");
  831. i2a_ASN1_OBJECT(bio_err, bag->type);
  832. BIO_printf(bio_err, "\n");
  833. return 1;
  834. break;
  835. }
  836. return 1;
  837. }
  838. /* Given a single certificate return a verified chain or NULL if error */
  839. /* Hope this is OK .... */
  840. int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain)
  841. {
  842. X509_STORE_CTX store_ctx;
  843. STACK_OF(X509) *chn;
  844. int i = 0;
  845. /*
  846. * FIXME: Should really check the return status of X509_STORE_CTX_init
  847. * for an error, but how that fits into the return value of this function
  848. * is less obvious.
  849. */
  850. X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
  851. if (X509_verify_cert(&store_ctx) <= 0) {
  852. i = X509_STORE_CTX_get_error(&store_ctx);
  853. if (i == 0)
  854. /*
  855. * avoid returning 0 if X509_verify_cert() did not set an
  856. * appropriate error value in the context
  857. */
  858. i = -1;
  859. chn = NULL;
  860. goto err;
  861. } else
  862. chn = X509_STORE_CTX_get1_chain(&store_ctx);
  863. err:
  864. X509_STORE_CTX_cleanup(&store_ctx);
  865. *chain = chn;
  866. return i;
  867. }
  868. int alg_print(BIO *x, X509_ALGOR *alg)
  869. {
  870. PBEPARAM *pbe;
  871. const unsigned char *p;
  872. p = alg->parameter->value.sequence->data;
  873. pbe = d2i_PBEPARAM(NULL, &p, alg->parameter->value.sequence->length);
  874. if (!pbe)
  875. return 1;
  876. BIO_printf(bio_err, "%s, Iteration %ld\n",
  877. OBJ_nid2ln(OBJ_obj2nid(alg->algorithm)),
  878. ASN1_INTEGER_get(pbe->iter));
  879. PBEPARAM_free(pbe);
  880. return 1;
  881. }
  882. /* Load all certificates from a given file */
  883. int cert_load(BIO *in, STACK_OF(X509) *sk)
  884. {
  885. int ret;
  886. X509 *cert;
  887. ret = 0;
  888. # ifdef CRYPTO_MDEBUG
  889. CRYPTO_push_info("cert_load(): reading one cert");
  890. # endif
  891. while ((cert = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
  892. # ifdef CRYPTO_MDEBUG
  893. CRYPTO_pop_info();
  894. # endif
  895. ret = 1;
  896. sk_X509_push(sk, cert);
  897. # ifdef CRYPTO_MDEBUG
  898. CRYPTO_push_info("cert_load(): reading one cert");
  899. # endif
  900. }
  901. # ifdef CRYPTO_MDEBUG
  902. CRYPTO_pop_info();
  903. # endif
  904. if (ret)
  905. ERR_clear_error();
  906. return ret;
  907. }
  908. /* Generalised attribute print: handle PKCS#8 and bag attributes */
  909. int print_attribs(BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst,
  910. const char *name)
  911. {
  912. X509_ATTRIBUTE *attr;
  913. ASN1_TYPE *av;
  914. char *value;
  915. int i, attr_nid;
  916. if (!attrlst) {
  917. BIO_printf(out, "%s: <No Attributes>\n", name);
  918. return 1;
  919. }
  920. if (!sk_X509_ATTRIBUTE_num(attrlst)) {
  921. BIO_printf(out, "%s: <Empty Attributes>\n", name);
  922. return 1;
  923. }
  924. BIO_printf(out, "%s\n", name);
  925. for (i = 0; i < sk_X509_ATTRIBUTE_num(attrlst); i++) {
  926. attr = sk_X509_ATTRIBUTE_value(attrlst, i);
  927. attr_nid = OBJ_obj2nid(attr->object);
  928. BIO_printf(out, " ");
  929. if (attr_nid == NID_undef) {
  930. i2a_ASN1_OBJECT(out, attr->object);
  931. BIO_printf(out, ": ");
  932. } else
  933. BIO_printf(out, "%s: ", OBJ_nid2ln(attr_nid));
  934. if (sk_ASN1_TYPE_num(attr->value.set)) {
  935. av = sk_ASN1_TYPE_value(attr->value.set, 0);
  936. switch (av->type) {
  937. case V_ASN1_BMPSTRING:
  938. value = OPENSSL_uni2asc(av->value.bmpstring->data,
  939. av->value.bmpstring->length);
  940. BIO_printf(out, "%s\n", value);
  941. OPENSSL_free(value);
  942. break;
  943. case V_ASN1_OCTET_STRING:
  944. hex_prin(out, av->value.octet_string->data,
  945. av->value.octet_string->length);
  946. BIO_printf(out, "\n");
  947. break;
  948. case V_ASN1_BIT_STRING:
  949. hex_prin(out, av->value.bit_string->data,
  950. av->value.bit_string->length);
  951. BIO_printf(out, "\n");
  952. break;
  953. default:
  954. BIO_printf(out, "<Unsupported tag %d>\n", av->type);
  955. break;
  956. }
  957. } else
  958. BIO_printf(out, "<No Values>\n");
  959. }
  960. return 1;
  961. }
  962. void hex_prin(BIO *out, unsigned char *buf, int len)
  963. {
  964. int i;
  965. for (i = 0; i < len; i++)
  966. BIO_printf(out, "%02X ", buf[i]);
  967. }
  968. static int set_pbe(BIO *err, int *ppbe, const char *str)
  969. {
  970. if (!str)
  971. return 0;
  972. if (!strcmp(str, "NONE")) {
  973. *ppbe = -1;
  974. return 1;
  975. }
  976. *ppbe = OBJ_txt2nid(str);
  977. if (*ppbe == NID_undef) {
  978. BIO_printf(bio_err, "Unknown PBE algorithm %s\n", str);
  979. return 0;
  980. }
  981. return 1;
  982. }
  983. #endif