2
0

pkcs12.c 36 KB

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