pkcs8.c 12 KB

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