genpkey.c 9.0 KB

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