2
0

genpkey.c 8.6 KB

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