pkcs8.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 1999-2004.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/pem.h>
  63. #include <openssl/err.h>
  64. #include <openssl/evp.h>
  65. #include <openssl/pkcs12.h>
  66. typedef enum OPTION_choice {
  67. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  68. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  69. OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT, OPT_NOOCT, OPT_NSDB, OPT_EMBED,
  70. #ifndef OPENSSL_NO_SCRYPT
  71. OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
  72. #endif
  73. OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT
  74. } OPTION_CHOICE;
  75. OPTIONS pkcs8_options[] = {
  76. {"help", OPT_HELP, '-', "Display this summary"},
  77. {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
  78. {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
  79. {"in", OPT_IN, '<', "Input file"},
  80. {"out", OPT_OUT, '>', "Output file"},
  81. {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"},
  82. {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
  83. {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"},
  84. {"nooct", OPT_NOOCT, '-', "Use (nonstandard) no octet format"},
  85. {"nsdb", OPT_NSDB, '-', "Use (nonstandard) DSA Netscape DB format"},
  86. {"embed", OPT_EMBED, '-',
  87. "Use (nonstandard) embedded DSA parameters format"},
  88. {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
  89. {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
  90. {"v2prf", OPT_V2PRF, 's'},
  91. {"iter", OPT_ITER, 'p', "Specify the iteration count"},
  92. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  93. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  94. #ifndef OPENSSL_NO_ENGINE
  95. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  96. #endif
  97. #ifndef OPENSSL_NO_SCRYPT
  98. {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
  99. {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
  100. {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
  101. {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
  102. #endif
  103. {NULL}
  104. };
  105. int pkcs8_main(int argc, char **argv)
  106. {
  107. BIO *in = NULL, *out = NULL;
  108. ENGINE *e = NULL;
  109. EVP_PKEY *pkey = NULL;
  110. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  111. X509_SIG *p8 = NULL;
  112. const EVP_CIPHER *cipher = NULL;
  113. char *infile = NULL, *outfile = NULL;
  114. char *passinarg = NULL, *passoutarg = NULL, *prog;
  115. char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL;
  116. OPTION_CHOICE o;
  117. int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER, p8_broken = PKCS8_OK;
  118. int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
  119. int private = 0;
  120. #ifndef OPENSSL_NO_SCRYPT
  121. long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
  122. #endif
  123. prog = opt_init(argc, argv, pkcs8_options);
  124. while ((o = opt_next()) != OPT_EOF) {
  125. switch (o) {
  126. case OPT_EOF:
  127. case OPT_ERR:
  128. opthelp:
  129. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  130. goto end;
  131. case OPT_HELP:
  132. opt_help(pkcs8_options);
  133. ret = 0;
  134. goto end;
  135. case OPT_INFORM:
  136. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  137. goto opthelp;
  138. break;
  139. case OPT_IN:
  140. infile = opt_arg();
  141. break;
  142. case OPT_OUTFORM:
  143. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  144. goto opthelp;
  145. break;
  146. case OPT_OUT:
  147. outfile = opt_arg();
  148. break;
  149. case OPT_TOPK8:
  150. topk8 = 1;
  151. break;
  152. case OPT_NOITER:
  153. iter = 1;
  154. break;
  155. case OPT_NOCRYPT:
  156. nocrypt = 1;
  157. break;
  158. case OPT_NOOCT:
  159. p8_broken = PKCS8_NO_OCTET;
  160. break;
  161. case OPT_NSDB:
  162. p8_broken = PKCS8_NS_DB;
  163. break;
  164. case OPT_EMBED:
  165. p8_broken = PKCS8_EMBEDDED_PARAM;
  166. break;
  167. case OPT_V2:
  168. if (!opt_cipher(opt_arg(), &cipher))
  169. goto opthelp;
  170. break;
  171. case OPT_V1:
  172. pbe_nid = OBJ_txt2nid(opt_arg());
  173. if (pbe_nid == NID_undef) {
  174. BIO_printf(bio_err,
  175. "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
  176. goto opthelp;
  177. }
  178. break;
  179. case OPT_V2PRF:
  180. pbe_nid = OBJ_txt2nid(opt_arg());
  181. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
  182. BIO_printf(bio_err,
  183. "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
  184. goto opthelp;
  185. }
  186. break;
  187. case OPT_ITER:
  188. if (!opt_int(opt_arg(), &iter))
  189. goto opthelp;
  190. break;
  191. case OPT_PASSIN:
  192. passinarg = opt_arg();
  193. break;
  194. case OPT_PASSOUT:
  195. passoutarg = opt_arg();
  196. break;
  197. case OPT_ENGINE:
  198. e = setup_engine(opt_arg(), 0);
  199. break;
  200. #ifndef OPENSSL_NO_SCRYPT
  201. case OPT_SCRYPT:
  202. scrypt_N = 1024;
  203. scrypt_r = 8;
  204. scrypt_p = 16;
  205. if (cipher == NULL)
  206. cipher = EVP_aes_256_cbc();
  207. break;
  208. case OPT_SCRYPT_N:
  209. if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
  210. goto opthelp;
  211. break;
  212. case OPT_SCRYPT_R:
  213. if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
  214. goto opthelp;
  215. break;
  216. case OPT_SCRYPT_P:
  217. if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
  218. goto opthelp;
  219. break;
  220. #endif
  221. }
  222. }
  223. argc = opt_num_rest();
  224. argv = opt_rest();
  225. private = 1;
  226. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  227. BIO_printf(bio_err, "Error getting passwords\n");
  228. goto end;
  229. }
  230. if ((pbe_nid == -1) && !cipher)
  231. pbe_nid = NID_pbeWithMD5AndDES_CBC;
  232. in = bio_open_default(infile, 'r', informat);
  233. if (in == NULL)
  234. goto end;
  235. out = bio_open_owner(outfile, outformat, private);
  236. if (out == NULL)
  237. goto end;
  238. if (topk8) {
  239. pkey = load_key(infile, informat, 1, passin, e, "key");
  240. if (!pkey)
  241. goto end;
  242. if ((p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken)) == NULL) {
  243. BIO_printf(bio_err, "Error converting key\n");
  244. ERR_print_errors(bio_err);
  245. goto end;
  246. }
  247. if (nocrypt) {
  248. assert(private);
  249. if (outformat == FORMAT_PEM)
  250. PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
  251. else if (outformat == FORMAT_ASN1)
  252. i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
  253. else {
  254. BIO_printf(bio_err, "Bad format specified for key\n");
  255. goto end;
  256. }
  257. } else {
  258. X509_ALGOR *pbe;
  259. if (cipher) {
  260. #ifndef OPENSSL_NO_SCRYPT
  261. if (scrypt_N && scrypt_r && scrypt_p)
  262. pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
  263. scrypt_N, scrypt_r, scrypt_p);
  264. else
  265. #endif
  266. pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
  267. pbe_nid);
  268. } else {
  269. pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
  270. }
  271. if (pbe == NULL) {
  272. BIO_printf(bio_err, "Error setting PBE algorithm\n");
  273. ERR_print_errors(bio_err);
  274. goto end;
  275. }
  276. if (passout)
  277. p8pass = passout;
  278. else {
  279. p8pass = pass;
  280. if (EVP_read_pw_string
  281. (pass, sizeof pass, "Enter Encryption Password:", 1)) {
  282. X509_ALGOR_free(pbe);
  283. goto end;
  284. }
  285. }
  286. app_RAND_load_file(NULL, 0);
  287. p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
  288. if (p8 == NULL) {
  289. X509_ALGOR_free(pbe);
  290. BIO_printf(bio_err, "Error encrypting key\n");
  291. ERR_print_errors(bio_err);
  292. goto end;
  293. }
  294. app_RAND_write_file(NULL);
  295. assert(private);
  296. if (outformat == FORMAT_PEM)
  297. PEM_write_bio_PKCS8(out, p8);
  298. else if (outformat == FORMAT_ASN1)
  299. i2d_PKCS8_bio(out, p8);
  300. else {
  301. BIO_printf(bio_err, "Bad format specified for key\n");
  302. goto end;
  303. }
  304. }
  305. ret = 0;
  306. goto end;
  307. }
  308. if (nocrypt) {
  309. if (informat == FORMAT_PEM)
  310. p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
  311. else if (informat == FORMAT_ASN1)
  312. p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
  313. else {
  314. BIO_printf(bio_err, "Bad format specified for key\n");
  315. goto end;
  316. }
  317. } else {
  318. if (informat == FORMAT_PEM)
  319. p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
  320. else if (informat == FORMAT_ASN1)
  321. p8 = d2i_PKCS8_bio(in, NULL);
  322. else {
  323. BIO_printf(bio_err, "Bad format specified for key\n");
  324. goto end;
  325. }
  326. if (!p8) {
  327. BIO_printf(bio_err, "Error reading key\n");
  328. ERR_print_errors(bio_err);
  329. goto end;
  330. }
  331. if (passin)
  332. p8pass = passin;
  333. else {
  334. p8pass = pass;
  335. EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
  336. }
  337. p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
  338. }
  339. if (!p8inf) {
  340. BIO_printf(bio_err, "Error decrypting key\n");
  341. ERR_print_errors(bio_err);
  342. goto end;
  343. }
  344. if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
  345. BIO_printf(bio_err, "Error converting key\n");
  346. ERR_print_errors(bio_err);
  347. goto end;
  348. }
  349. if (p8inf->broken) {
  350. BIO_printf(bio_err, "Warning: broken key encoding: ");
  351. switch (p8inf->broken) {
  352. case PKCS8_NO_OCTET:
  353. BIO_printf(bio_err, "No Octet String in PrivateKey\n");
  354. break;
  355. case PKCS8_EMBEDDED_PARAM:
  356. BIO_printf(bio_err, "DSA parameters included in PrivateKey\n");
  357. break;
  358. case PKCS8_NS_DB:
  359. BIO_printf(bio_err, "DSA public key include in PrivateKey\n");
  360. break;
  361. case PKCS8_NEG_PRIVKEY:
  362. BIO_printf(bio_err, "DSA private key value is negative\n");
  363. break;
  364. default:
  365. BIO_printf(bio_err, "Unknown broken type\n");
  366. break;
  367. }
  368. }
  369. assert(private);
  370. if (outformat == FORMAT_PEM)
  371. PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
  372. else if (outformat == FORMAT_ASN1)
  373. i2d_PrivateKey_bio(out, pkey);
  374. else {
  375. BIO_printf(bio_err, "Bad format specified for key\n");
  376. goto end;
  377. }
  378. ret = 0;
  379. end:
  380. X509_SIG_free(p8);
  381. PKCS8_PRIV_KEY_INFO_free(p8inf);
  382. EVP_PKEY_free(pkey);
  383. BIO_free_all(out);
  384. BIO_free(in);
  385. OPENSSL_free(passin);
  386. OPENSSL_free(passout);
  387. return ret;
  388. }