pkcs8.c 12 KB

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