pkcs12.c 28 KB

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