pkcs8.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. argc = opt_num_rest();
  188. if (argc != 0)
  189. goto opthelp;
  190. private = 1;
  191. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  192. BIO_printf(bio_err, "Error getting passwords\n");
  193. goto end;
  194. }
  195. if ((pbe_nid == -1) && cipher == NULL)
  196. cipher = EVP_aes_256_cbc();
  197. in = bio_open_default(infile, 'r', informat);
  198. if (in == NULL)
  199. goto end;
  200. out = bio_open_owner(outfile, outformat, private);
  201. if (out == NULL)
  202. goto end;
  203. if (topk8) {
  204. pkey = load_key(infile, informat, 1, passin, e, "key");
  205. if (pkey == NULL)
  206. goto end;
  207. if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
  208. BIO_printf(bio_err, "Error converting key\n");
  209. ERR_print_errors(bio_err);
  210. goto end;
  211. }
  212. if (nocrypt) {
  213. assert(private);
  214. if (outformat == FORMAT_PEM) {
  215. PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
  216. } else if (outformat == FORMAT_ASN1) {
  217. i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
  218. } else {
  219. BIO_printf(bio_err, "Bad format specified for key\n");
  220. goto end;
  221. }
  222. } else {
  223. X509_ALGOR *pbe;
  224. if (cipher) {
  225. #ifndef OPENSSL_NO_SCRYPT
  226. if (scrypt_N && scrypt_r && scrypt_p)
  227. pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
  228. scrypt_N, scrypt_r, scrypt_p);
  229. else
  230. #endif
  231. pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
  232. pbe_nid);
  233. } else {
  234. pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
  235. }
  236. if (pbe == NULL) {
  237. BIO_printf(bio_err, "Error setting PBE algorithm\n");
  238. ERR_print_errors(bio_err);
  239. goto end;
  240. }
  241. if (passout != NULL) {
  242. p8pass = passout;
  243. } else if (1) {
  244. /* To avoid bit rot */
  245. #ifndef OPENSSL_NO_UI_CONSOLE
  246. p8pass = pass;
  247. if (EVP_read_pw_string
  248. (pass, sizeof(pass), "Enter Encryption Password:", 1)) {
  249. X509_ALGOR_free(pbe);
  250. goto end;
  251. }
  252. } else {
  253. #endif
  254. BIO_printf(bio_err, "Password required\n");
  255. goto end;
  256. }
  257. p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
  258. if (p8 == NULL) {
  259. X509_ALGOR_free(pbe);
  260. BIO_printf(bio_err, "Error encrypting key\n");
  261. ERR_print_errors(bio_err);
  262. goto end;
  263. }
  264. assert(private);
  265. if (outformat == FORMAT_PEM)
  266. PEM_write_bio_PKCS8(out, p8);
  267. else if (outformat == FORMAT_ASN1)
  268. i2d_PKCS8_bio(out, p8);
  269. else {
  270. BIO_printf(bio_err, "Bad format specified for key\n");
  271. goto end;
  272. }
  273. }
  274. ret = 0;
  275. goto end;
  276. }
  277. if (nocrypt) {
  278. if (informat == FORMAT_PEM) {
  279. p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
  280. } else if (informat == FORMAT_ASN1) {
  281. p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
  282. } else {
  283. BIO_printf(bio_err, "Bad format specified for key\n");
  284. goto end;
  285. }
  286. } else {
  287. if (informat == FORMAT_PEM) {
  288. p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
  289. } else if (informat == FORMAT_ASN1) {
  290. p8 = d2i_PKCS8_bio(in, NULL);
  291. } else {
  292. BIO_printf(bio_err, "Bad format specified for key\n");
  293. goto end;
  294. }
  295. if (p8 == NULL) {
  296. BIO_printf(bio_err, "Error reading key\n");
  297. ERR_print_errors(bio_err);
  298. goto end;
  299. }
  300. if (passin != NULL) {
  301. p8pass = passin;
  302. } else if (1) {
  303. #ifndef OPENSSL_NO_UI_CONSOLE
  304. p8pass = pass;
  305. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Password:", 0)) {
  306. BIO_printf(bio_err, "Can't read Password\n");
  307. goto end;
  308. }
  309. } else {
  310. #endif
  311. BIO_printf(bio_err, "Password required\n");
  312. goto end;
  313. }
  314. p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
  315. }
  316. if (p8inf == NULL) {
  317. BIO_printf(bio_err, "Error decrypting key\n");
  318. ERR_print_errors(bio_err);
  319. goto end;
  320. }
  321. if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
  322. BIO_printf(bio_err, "Error converting key\n");
  323. ERR_print_errors(bio_err);
  324. goto end;
  325. }
  326. assert(private);
  327. if (outformat == FORMAT_PEM) {
  328. if (traditional)
  329. PEM_write_bio_PrivateKey_traditional(out, pkey, NULL, NULL, 0,
  330. NULL, passout);
  331. else
  332. PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
  333. } else if (outformat == FORMAT_ASN1) {
  334. i2d_PrivateKey_bio(out, pkey);
  335. } else {
  336. BIO_printf(bio_err, "Bad format specified for key\n");
  337. goto end;
  338. }
  339. ret = 0;
  340. end:
  341. X509_SIG_free(p8);
  342. PKCS8_PRIV_KEY_INFO_free(p8inf);
  343. EVP_PKEY_free(pkey);
  344. release_engine(e);
  345. BIO_free_all(out);
  346. BIO_free(in);
  347. OPENSSL_free(passin);
  348. OPENSSL_free(passout);
  349. return ret;
  350. }