pkcs12.c 34 KB

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