2
0

genpkey.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright 2006-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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "apps.h"
  14. #include "progs.h"
  15. #include <openssl/pem.h>
  16. #include <openssl/err.h>
  17. #include <openssl/evp.h>
  18. #ifndef OPENSSL_NO_ENGINE
  19. # include <openssl/engine.h>
  20. #endif
  21. static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
  22. OSSL_LIB_CTX *libctx, const char *propq);
  23. static int genpkey_cb(EVP_PKEY_CTX *ctx);
  24. typedef enum OPTION_choice {
  25. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  26. OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
  27. OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
  28. OPT_CONFIG,
  29. OPT_PROV_ENUM
  30. } OPTION_CHOICE;
  31. const OPTIONS genpkey_options[] = {
  32. OPT_SECTION("General"),
  33. {"help", OPT_HELP, '-', "Display this summary"},
  34. #ifndef OPENSSL_NO_ENGINE
  35. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  36. #endif
  37. {"paramfile", OPT_PARAMFILE, '<', "Parameters file"},
  38. {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"},
  39. {"pkeyopt", OPT_PKEYOPT, 's',
  40. "Set the public key algorithm option as opt:value"},
  41. OPT_CONFIG_OPTION,
  42. OPT_SECTION("Output"),
  43. {"out", OPT_OUT, '>', "Output file"},
  44. {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
  45. {"pass", OPT_PASS, 's', "Output file pass phrase source"},
  46. {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
  47. {"text", OPT_TEXT, '-', "Print the in text"},
  48. {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
  49. OPT_PROV_OPTIONS,
  50. /* This is deliberately last. */
  51. {OPT_HELP_STR, 1, 1,
  52. "Order of options may be important! See the documentation.\n"},
  53. {NULL}
  54. };
  55. int genpkey_main(int argc, char **argv)
  56. {
  57. CONF *conf = NULL;
  58. BIO *in = NULL, *out = NULL;
  59. ENGINE *e = NULL;
  60. EVP_PKEY *pkey = NULL;
  61. EVP_PKEY_CTX *ctx = NULL;
  62. char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
  63. const EVP_CIPHER *cipher = NULL;
  64. OPTION_CHOICE o;
  65. int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
  66. int private = 0;
  67. OSSL_LIB_CTX *libctx = app_get0_libctx();
  68. const char *propq = app_get0_propq();
  69. prog = opt_init(argc, argv, genpkey_options);
  70. while ((o = opt_next()) != OPT_EOF) {
  71. switch (o) {
  72. case OPT_EOF:
  73. case OPT_ERR:
  74. opthelp:
  75. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  76. goto end;
  77. case OPT_HELP:
  78. ret = 0;
  79. opt_help(genpkey_options);
  80. goto end;
  81. case OPT_OUTFORM:
  82. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  83. goto opthelp;
  84. break;
  85. case OPT_OUT:
  86. outfile = opt_arg();
  87. break;
  88. case OPT_PASS:
  89. passarg = opt_arg();
  90. break;
  91. case OPT_ENGINE:
  92. e = setup_engine(opt_arg(), 0);
  93. break;
  94. case OPT_PARAMFILE:
  95. if (do_param == 1)
  96. goto opthelp;
  97. if (!init_keygen_file(&ctx, opt_arg(), e, libctx, propq))
  98. goto end;
  99. break;
  100. case OPT_ALGORITHM:
  101. if (!init_gen_str(&ctx, opt_arg(), e, do_param, libctx, propq))
  102. goto end;
  103. break;
  104. case OPT_PKEYOPT:
  105. if (ctx == NULL) {
  106. BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
  107. goto opthelp;
  108. }
  109. if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
  110. BIO_printf(bio_err,
  111. "%s: Error setting %s parameter:\n",
  112. prog, opt_arg());
  113. ERR_print_errors(bio_err);
  114. goto end;
  115. }
  116. break;
  117. case OPT_GENPARAM:
  118. if (ctx != NULL) {
  119. BIO_printf(bio_err,
  120. "%s: '-genparam' option must be set before"
  121. " the '-algorithm' option.\n", prog);
  122. goto opthelp;
  123. }
  124. do_param = 1;
  125. break;
  126. case OPT_TEXT:
  127. text = 1;
  128. break;
  129. case OPT_CIPHER:
  130. if (!opt_cipher(opt_unknown(), &cipher)
  131. || do_param == 1)
  132. goto opthelp;
  133. if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE ||
  134. EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE ||
  135. EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE ||
  136. EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE) {
  137. BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
  138. goto end;
  139. }
  140. break;
  141. case OPT_CONFIG:
  142. conf = app_load_config_modules(opt_arg());
  143. if (conf == NULL)
  144. goto end;
  145. break;
  146. case OPT_PROV_CASES:
  147. if (!opt_provider(o))
  148. goto end;
  149. break;
  150. }
  151. }
  152. argc = opt_num_rest();
  153. if (argc != 0)
  154. goto opthelp;
  155. private = do_param ? 0 : 1;
  156. if (ctx == NULL)
  157. goto opthelp;
  158. if (!app_passwd(passarg, NULL, &pass, NULL)) {
  159. BIO_puts(bio_err, "Error getting password\n");
  160. goto end;
  161. }
  162. out = bio_open_owner(outfile, outformat, private);
  163. if (out == NULL)
  164. goto end;
  165. EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
  166. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  167. if (do_param) {
  168. if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
  169. BIO_puts(bio_err, "Error generating parameters\n");
  170. ERR_print_errors(bio_err);
  171. goto end;
  172. }
  173. } else {
  174. if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
  175. BIO_puts(bio_err, "Error generating key\n");
  176. ERR_print_errors(bio_err);
  177. goto end;
  178. }
  179. }
  180. if (do_param) {
  181. rv = PEM_write_bio_Parameters(out, pkey);
  182. } else if (outformat == FORMAT_PEM) {
  183. assert(private);
  184. rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
  185. } else if (outformat == FORMAT_ASN1) {
  186. assert(private);
  187. rv = i2d_PrivateKey_bio(out, pkey);
  188. } else {
  189. BIO_printf(bio_err, "Bad format specified for key\n");
  190. goto end;
  191. }
  192. ret = 0;
  193. if (rv <= 0) {
  194. BIO_puts(bio_err, "Error writing key\n");
  195. ERR_print_errors(bio_err);
  196. ret = 1;
  197. }
  198. if (text) {
  199. if (do_param)
  200. rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
  201. else
  202. rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
  203. if (rv <= 0) {
  204. BIO_puts(bio_err, "Error printing key\n");
  205. ERR_print_errors(bio_err);
  206. ret = 1;
  207. }
  208. }
  209. end:
  210. EVP_PKEY_free(pkey);
  211. EVP_PKEY_CTX_free(ctx);
  212. BIO_free_all(out);
  213. BIO_free(in);
  214. release_engine(e);
  215. OPENSSL_free(pass);
  216. NCONF_free(conf);
  217. return ret;
  218. }
  219. static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
  220. OSSL_LIB_CTX *libctx, const char *propq)
  221. {
  222. BIO *pbio;
  223. EVP_PKEY *pkey = NULL;
  224. EVP_PKEY_CTX *ctx = NULL;
  225. if (*pctx) {
  226. BIO_puts(bio_err, "Parameters already set!\n");
  227. return 0;
  228. }
  229. pbio = BIO_new_file(file, "r");
  230. if (pbio == NULL) {
  231. BIO_printf(bio_err, "Can't open parameter file %s\n", file);
  232. return 0;
  233. }
  234. pkey = PEM_read_bio_Parameters(pbio, NULL);
  235. BIO_free(pbio);
  236. if (pkey == NULL) {
  237. BIO_printf(bio_err, "Error reading parameter file %s\n", file);
  238. return 0;
  239. }
  240. if (e != NULL)
  241. ctx = EVP_PKEY_CTX_new(pkey, e);
  242. else
  243. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
  244. if (ctx == NULL)
  245. goto err;
  246. if (EVP_PKEY_keygen_init(ctx) <= 0)
  247. goto err;
  248. EVP_PKEY_free(pkey);
  249. *pctx = ctx;
  250. return 1;
  251. err:
  252. BIO_puts(bio_err, "Error initializing context\n");
  253. ERR_print_errors(bio_err);
  254. EVP_PKEY_CTX_free(ctx);
  255. EVP_PKEY_free(pkey);
  256. return 0;
  257. }
  258. int init_gen_str(EVP_PKEY_CTX **pctx,
  259. const char *algname, ENGINE *e, int do_param,
  260. OSSL_LIB_CTX *libctx, const char *propq)
  261. {
  262. EVP_PKEY_CTX *ctx = NULL;
  263. const EVP_PKEY_ASN1_METHOD *ameth;
  264. ENGINE *tmpeng = NULL;
  265. int pkey_id;
  266. if (*pctx) {
  267. BIO_puts(bio_err, "Algorithm already set!\n");
  268. return 0;
  269. }
  270. if (libctx == NULL || e != NULL) {
  271. ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
  272. #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  273. if (ameth == NULL && e != NULL)
  274. ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
  275. #endif
  276. if (ameth == NULL) {
  277. BIO_printf(bio_err, "Algorithm %s not found\n", algname);
  278. return 0;
  279. }
  280. ERR_clear_error();
  281. EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
  282. #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  283. ENGINE_finish(tmpeng);
  284. #endif
  285. ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
  286. } else {
  287. ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
  288. }
  289. if (!ctx)
  290. goto err;
  291. if (do_param) {
  292. if (EVP_PKEY_paramgen_init(ctx) <= 0)
  293. goto err;
  294. } else {
  295. if (EVP_PKEY_keygen_init(ctx) <= 0)
  296. goto err;
  297. }
  298. *pctx = ctx;
  299. return 1;
  300. err:
  301. BIO_printf(bio_err, "Error initializing %s context\n", algname);
  302. ERR_print_errors(bio_err);
  303. EVP_PKEY_CTX_free(ctx);
  304. return 0;
  305. }
  306. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  307. {
  308. char c = '*';
  309. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  310. int p;
  311. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  312. if (p == 0)
  313. c = '.';
  314. if (p == 1)
  315. c = '+';
  316. if (p == 2)
  317. c = '*';
  318. if (p == 3)
  319. c = '\n';
  320. BIO_write(b, &c, 1);
  321. (void)BIO_flush(b);
  322. return 1;
  323. }