2
0

pkcs12.c 26 KB

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