pkcs12.c 34 KB

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