pkcs12.c 28 KB

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