genpkey.c 11 KB

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