pkcs8.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/pem.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/pkcs12.h>
  18. typedef enum OPTION_choice {
  19. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  20. OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
  21. OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT,
  22. #ifndef OPENSSL_NO_SCRYPT
  23. OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
  24. #endif
  25. OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT,
  26. OPT_TRADITIONAL,
  27. OPT_R_ENUM, OPT_PROV_ENUM
  28. } OPTION_CHOICE;
  29. const OPTIONS pkcs8_options[] = {
  30. OPT_SECTION("General"),
  31. {"help", OPT_HELP, '-', "Display this summary"},
  32. #ifndef OPENSSL_NO_ENGINE
  33. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  34. #endif
  35. {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
  36. {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
  37. {"v2prf", OPT_V2PRF, 's', "Set the PRF algorithm to use with PKCS#5 v2.0"},
  38. OPT_SECTION("Input"),
  39. {"in", OPT_IN, '<', "Input file"},
  40. {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
  41. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  42. {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"},
  43. OPT_SECTION("Output"),
  44. {"out", OPT_OUT, '>', "Output file"},
  45. {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
  46. {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"},
  47. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  48. {"traditional", OPT_TRADITIONAL, '-', "use traditional format private key"},
  49. {"iter", OPT_ITER, 'p', "Specify the iteration count"},
  50. {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
  51. #ifndef OPENSSL_NO_SCRYPT
  52. OPT_SECTION("Scrypt"),
  53. {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
  54. {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
  55. {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
  56. {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
  57. #endif
  58. OPT_R_OPTIONS,
  59. OPT_PROV_OPTIONS,
  60. {NULL}
  61. };
  62. int pkcs8_main(int argc, char **argv)
  63. {
  64. BIO *in = NULL, *out = NULL;
  65. ENGINE *e = NULL;
  66. EVP_PKEY *pkey = NULL;
  67. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  68. X509_SIG *p8 = NULL;
  69. const EVP_CIPHER *cipher = NULL;
  70. char *infile = NULL, *outfile = NULL;
  71. char *passinarg = NULL, *passoutarg = NULL, *prog;
  72. #ifndef OPENSSL_NO_UI_CONSOLE
  73. char pass[APP_PASS_LEN];
  74. #endif
  75. char *passin = NULL, *passout = NULL, *p8pass = NULL;
  76. OPTION_CHOICE o;
  77. int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER;
  78. int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
  79. int private = 0, traditional = 0;
  80. #ifndef OPENSSL_NO_SCRYPT
  81. long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
  82. #endif
  83. prog = opt_init(argc, argv, pkcs8_options);
  84. while ((o = opt_next()) != OPT_EOF) {
  85. switch (o) {
  86. case OPT_EOF:
  87. case OPT_ERR:
  88. opthelp:
  89. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  90. goto end;
  91. case OPT_HELP:
  92. opt_help(pkcs8_options);
  93. ret = 0;
  94. goto end;
  95. case OPT_INFORM:
  96. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  97. goto opthelp;
  98. break;
  99. case OPT_IN:
  100. infile = opt_arg();
  101. break;
  102. case OPT_OUTFORM:
  103. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  104. goto opthelp;
  105. break;
  106. case OPT_OUT:
  107. outfile = opt_arg();
  108. break;
  109. case OPT_TOPK8:
  110. topk8 = 1;
  111. break;
  112. case OPT_NOITER:
  113. iter = 1;
  114. break;
  115. case OPT_NOCRYPT:
  116. nocrypt = 1;
  117. break;
  118. case OPT_R_CASES:
  119. if (!opt_rand(o))
  120. goto end;
  121. break;
  122. case OPT_PROV_CASES:
  123. if (!opt_provider(o))
  124. goto end;
  125. break;
  126. case OPT_TRADITIONAL:
  127. traditional = 1;
  128. break;
  129. case OPT_V2:
  130. if (!opt_cipher(opt_arg(), &cipher))
  131. goto opthelp;
  132. break;
  133. case OPT_V1:
  134. pbe_nid = OBJ_txt2nid(opt_arg());
  135. if (pbe_nid == NID_undef) {
  136. BIO_printf(bio_err,
  137. "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
  138. goto opthelp;
  139. }
  140. break;
  141. case OPT_V2PRF:
  142. pbe_nid = OBJ_txt2nid(opt_arg());
  143. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
  144. BIO_printf(bio_err,
  145. "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
  146. goto opthelp;
  147. }
  148. if (cipher == NULL)
  149. cipher = EVP_aes_256_cbc();
  150. break;
  151. case OPT_ITER:
  152. if (!opt_int(opt_arg(), &iter))
  153. goto opthelp;
  154. break;
  155. case OPT_PASSIN:
  156. passinarg = opt_arg();
  157. break;
  158. case OPT_PASSOUT:
  159. passoutarg = opt_arg();
  160. break;
  161. case OPT_ENGINE:
  162. e = setup_engine(opt_arg(), 0);
  163. break;
  164. #ifndef OPENSSL_NO_SCRYPT
  165. case OPT_SCRYPT:
  166. scrypt_N = 16384;
  167. scrypt_r = 8;
  168. scrypt_p = 1;
  169. if (cipher == NULL)
  170. cipher = EVP_aes_256_cbc();
  171. break;
  172. case OPT_SCRYPT_N:
  173. if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
  174. goto opthelp;
  175. break;
  176. case OPT_SCRYPT_R:
  177. if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
  178. goto opthelp;
  179. break;
  180. case OPT_SCRYPT_P:
  181. if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
  182. goto opthelp;
  183. break;
  184. #endif
  185. }
  186. }
  187. /* No extra arguments. */
  188. argc = opt_num_rest();
  189. if (argc != 0)
  190. goto opthelp;
  191. private = 1;
  192. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  193. BIO_printf(bio_err, "Error getting passwords\n");
  194. goto end;
  195. }
  196. if ((pbe_nid == -1) && cipher == NULL)
  197. cipher = EVP_aes_256_cbc();
  198. in = bio_open_default(infile, 'r', informat);
  199. if (in == NULL)
  200. goto end;
  201. out = bio_open_owner(outfile, outformat, private);
  202. if (out == NULL)
  203. goto end;
  204. if (topk8) {
  205. pkey = load_key(infile, informat, 1, passin, e, "key");
  206. if (pkey == NULL)
  207. goto end;
  208. if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
  209. BIO_printf(bio_err, "Error converting key\n");
  210. ERR_print_errors(bio_err);
  211. goto end;
  212. }
  213. if (nocrypt) {
  214. assert(private);
  215. if (outformat == FORMAT_PEM) {
  216. PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
  217. } else if (outformat == FORMAT_ASN1) {
  218. i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
  219. } else {
  220. BIO_printf(bio_err, "Bad format specified for key\n");
  221. goto end;
  222. }
  223. } else {
  224. X509_ALGOR *pbe;
  225. if (cipher) {
  226. #ifndef OPENSSL_NO_SCRYPT
  227. if (scrypt_N && scrypt_r && scrypt_p)
  228. pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
  229. scrypt_N, scrypt_r, scrypt_p);
  230. else
  231. #endif
  232. pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
  233. pbe_nid);
  234. } else {
  235. pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
  236. }
  237. if (pbe == NULL) {
  238. BIO_printf(bio_err, "Error setting PBE algorithm\n");
  239. ERR_print_errors(bio_err);
  240. goto end;
  241. }
  242. if (passout != NULL) {
  243. p8pass = passout;
  244. } else if (1) {
  245. /* To avoid bit rot */
  246. #ifndef OPENSSL_NO_UI_CONSOLE
  247. p8pass = pass;
  248. if (EVP_read_pw_string
  249. (pass, sizeof(pass), "Enter Encryption Password:", 1)) {
  250. X509_ALGOR_free(pbe);
  251. goto end;
  252. }
  253. } else {
  254. #endif
  255. BIO_printf(bio_err, "Password required\n");
  256. goto end;
  257. }
  258. p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
  259. if (p8 == NULL) {
  260. X509_ALGOR_free(pbe);
  261. BIO_printf(bio_err, "Error encrypting key\n");
  262. ERR_print_errors(bio_err);
  263. goto end;
  264. }
  265. assert(private);
  266. if (outformat == FORMAT_PEM)
  267. PEM_write_bio_PKCS8(out, p8);
  268. else if (outformat == FORMAT_ASN1)
  269. i2d_PKCS8_bio(out, p8);
  270. else {
  271. BIO_printf(bio_err, "Bad format specified for key\n");
  272. goto end;
  273. }
  274. }
  275. ret = 0;
  276. goto end;
  277. }
  278. if (nocrypt) {
  279. if (informat == FORMAT_PEM) {
  280. p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
  281. } else if (informat == FORMAT_ASN1) {
  282. p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
  283. } else {
  284. BIO_printf(bio_err, "Bad format specified for key\n");
  285. goto end;
  286. }
  287. } else {
  288. if (informat == FORMAT_PEM) {
  289. p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
  290. } else if (informat == FORMAT_ASN1) {
  291. p8 = d2i_PKCS8_bio(in, NULL);
  292. } else {
  293. BIO_printf(bio_err, "Bad format specified for key\n");
  294. goto end;
  295. }
  296. if (p8 == NULL) {
  297. BIO_printf(bio_err, "Error reading key\n");
  298. ERR_print_errors(bio_err);
  299. goto end;
  300. }
  301. if (passin != NULL) {
  302. p8pass = passin;
  303. } else if (1) {
  304. #ifndef OPENSSL_NO_UI_CONSOLE
  305. p8pass = pass;
  306. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Password:", 0)) {
  307. BIO_printf(bio_err, "Can't read Password\n");
  308. goto end;
  309. }
  310. } else {
  311. #endif
  312. BIO_printf(bio_err, "Password required\n");
  313. goto end;
  314. }
  315. p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
  316. }
  317. if (p8inf == NULL) {
  318. BIO_printf(bio_err, "Error decrypting key\n");
  319. ERR_print_errors(bio_err);
  320. goto end;
  321. }
  322. if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
  323. BIO_printf(bio_err, "Error converting key\n");
  324. ERR_print_errors(bio_err);
  325. goto end;
  326. }
  327. assert(private);
  328. if (outformat == FORMAT_PEM) {
  329. if (traditional)
  330. PEM_write_bio_PrivateKey_traditional(out, pkey, NULL, NULL, 0,
  331. NULL, passout);
  332. else
  333. PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
  334. } else if (outformat == FORMAT_ASN1) {
  335. i2d_PrivateKey_bio(out, pkey);
  336. } else {
  337. BIO_printf(bio_err, "Bad format specified for key\n");
  338. goto end;
  339. }
  340. ret = 0;
  341. end:
  342. X509_SIG_free(p8);
  343. PKCS8_PRIV_KEY_INFO_free(p8inf);
  344. EVP_PKEY_free(pkey);
  345. release_engine(e);
  346. BIO_free_all(out);
  347. BIO_free(in);
  348. OPENSSL_free(passin);
  349. OPENSSL_free(passout);
  350. return ret;
  351. }