2
0

genpkey.c 9.3 KB

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