genpkey.c 11 KB

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