pkcs8.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright 1999-2021 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_COMMON,
  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. EVP_CIPHER *cipher = NULL;
  70. char *infile = NULL, *outfile = NULL, *ciphername = 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_UNDEF, 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. ciphername = opt_arg();
  131. break;
  132. case OPT_V1:
  133. pbe_nid = OBJ_txt2nid(opt_arg());
  134. if (pbe_nid == NID_undef) {
  135. BIO_printf(bio_err,
  136. "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
  137. goto opthelp;
  138. }
  139. break;
  140. case OPT_V2PRF:
  141. pbe_nid = OBJ_txt2nid(opt_arg());
  142. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
  143. BIO_printf(bio_err,
  144. "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
  145. goto opthelp;
  146. }
  147. if (cipher == NULL)
  148. cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
  149. break;
  150. case OPT_ITER:
  151. iter = opt_int_arg();
  152. break;
  153. case OPT_PASSIN:
  154. passinarg = opt_arg();
  155. break;
  156. case OPT_PASSOUT:
  157. passoutarg = opt_arg();
  158. break;
  159. case OPT_ENGINE:
  160. e = setup_engine(opt_arg(), 0);
  161. break;
  162. #ifndef OPENSSL_NO_SCRYPT
  163. case OPT_SCRYPT:
  164. scrypt_N = 16384;
  165. scrypt_r = 8;
  166. scrypt_p = 1;
  167. if (cipher == NULL)
  168. cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
  169. break;
  170. case OPT_SCRYPT_N:
  171. if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
  172. goto opthelp;
  173. break;
  174. case OPT_SCRYPT_R:
  175. if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
  176. goto opthelp;
  177. break;
  178. case OPT_SCRYPT_P:
  179. if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
  180. goto opthelp;
  181. break;
  182. #endif
  183. }
  184. }
  185. /* No extra arguments. */
  186. if (!opt_check_rest_arg(NULL))
  187. goto opthelp;
  188. private = 1;
  189. if (!app_RAND_load())
  190. goto end;
  191. if (ciphername != NULL) {
  192. if (!opt_cipher(ciphername, &cipher))
  193. goto opthelp;
  194. }
  195. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  196. BIO_printf(bio_err, "Error getting passwords\n");
  197. goto end;
  198. }
  199. if ((pbe_nid == -1) && cipher == NULL)
  200. cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
  201. in = bio_open_default(infile, 'r',
  202. informat == FORMAT_UNDEF ? FORMAT_PEM : informat);
  203. if (in == NULL)
  204. goto end;
  205. out = bio_open_owner(outfile, outformat, private);
  206. if (out == NULL)
  207. goto end;
  208. if (topk8) {
  209. pkey = load_key(infile, informat, 1, passin, e, "key");
  210. if (pkey == NULL)
  211. goto end;
  212. if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
  213. BIO_printf(bio_err, "Error converting key\n");
  214. ERR_print_errors(bio_err);
  215. goto end;
  216. }
  217. if (nocrypt) {
  218. assert(private);
  219. if (outformat == FORMAT_PEM) {
  220. PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
  221. } else if (outformat == FORMAT_ASN1) {
  222. i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
  223. } else {
  224. BIO_printf(bio_err, "Bad format specified for key\n");
  225. goto end;
  226. }
  227. } else {
  228. X509_ALGOR *pbe;
  229. if (cipher) {
  230. #ifndef OPENSSL_NO_SCRYPT
  231. if (scrypt_N && scrypt_r && scrypt_p)
  232. pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
  233. scrypt_N, scrypt_r, scrypt_p);
  234. else
  235. #endif
  236. pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
  237. pbe_nid);
  238. } else {
  239. pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
  240. }
  241. if (pbe == NULL) {
  242. BIO_printf(bio_err, "Error setting PBE algorithm\n");
  243. ERR_print_errors(bio_err);
  244. goto end;
  245. }
  246. if (passout != NULL) {
  247. p8pass = passout;
  248. } else if (1) {
  249. /* To avoid bit rot */
  250. #ifndef OPENSSL_NO_UI_CONSOLE
  251. p8pass = pass;
  252. if (EVP_read_pw_string
  253. (pass, sizeof(pass), "Enter Encryption Password:", 1)) {
  254. X509_ALGOR_free(pbe);
  255. goto end;
  256. }
  257. } else {
  258. #endif
  259. BIO_printf(bio_err, "Password required\n");
  260. goto end;
  261. }
  262. p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
  263. if (p8 == NULL) {
  264. X509_ALGOR_free(pbe);
  265. BIO_printf(bio_err, "Error encrypting key\n");
  266. ERR_print_errors(bio_err);
  267. goto end;
  268. }
  269. assert(private);
  270. if (outformat == FORMAT_PEM)
  271. PEM_write_bio_PKCS8(out, p8);
  272. else if (outformat == FORMAT_ASN1)
  273. i2d_PKCS8_bio(out, p8);
  274. else {
  275. BIO_printf(bio_err, "Bad format specified for key\n");
  276. goto end;
  277. }
  278. }
  279. ret = 0;
  280. goto end;
  281. }
  282. if (nocrypt) {
  283. if (informat == FORMAT_PEM || informat == FORMAT_UNDEF) {
  284. p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
  285. } else if (informat == FORMAT_ASN1) {
  286. p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
  287. } else {
  288. BIO_printf(bio_err, "Bad format specified for key\n");
  289. goto end;
  290. }
  291. } else {
  292. if (informat == FORMAT_PEM || informat == FORMAT_UNDEF) {
  293. p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
  294. } else if (informat == FORMAT_ASN1) {
  295. p8 = d2i_PKCS8_bio(in, NULL);
  296. } else {
  297. BIO_printf(bio_err, "Bad format specified for key\n");
  298. goto end;
  299. }
  300. if (p8 == NULL) {
  301. BIO_printf(bio_err, "Error reading key\n");
  302. ERR_print_errors(bio_err);
  303. goto end;
  304. }
  305. if (passin != NULL) {
  306. p8pass = passin;
  307. } else if (1) {
  308. #ifndef OPENSSL_NO_UI_CONSOLE
  309. p8pass = pass;
  310. if (EVP_read_pw_string(pass, sizeof(pass), "Enter Password:", 0)) {
  311. BIO_printf(bio_err, "Can't read Password\n");
  312. goto end;
  313. }
  314. } else {
  315. #endif
  316. BIO_printf(bio_err, "Password required\n");
  317. goto end;
  318. }
  319. p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
  320. }
  321. if (p8inf == NULL) {
  322. BIO_printf(bio_err, "Error decrypting key\n");
  323. ERR_print_errors(bio_err);
  324. goto end;
  325. }
  326. if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
  327. BIO_printf(bio_err, "Error converting key\n");
  328. ERR_print_errors(bio_err);
  329. goto end;
  330. }
  331. assert(private);
  332. if (outformat == FORMAT_PEM) {
  333. if (traditional)
  334. PEM_write_bio_PrivateKey_traditional(out, pkey, NULL, NULL, 0,
  335. NULL, passout);
  336. else
  337. PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
  338. } else if (outformat == FORMAT_ASN1) {
  339. i2d_PrivateKey_bio(out, pkey);
  340. } else {
  341. BIO_printf(bio_err, "Bad format specified for key\n");
  342. goto end;
  343. }
  344. ret = 0;
  345. end:
  346. X509_SIG_free(p8);
  347. PKCS8_PRIV_KEY_INFO_free(p8inf);
  348. EVP_PKEY_free(pkey);
  349. EVP_CIPHER_free(cipher);
  350. release_engine(e);
  351. BIO_free_all(out);
  352. BIO_free(in);
  353. OPENSSL_free(passin);
  354. OPENSSL_free(passout);
  355. return ret;
  356. }