genpkey.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2006-2018 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. }
  118. }
  119. argc = opt_num_rest();
  120. if (argc != 0)
  121. goto opthelp;
  122. private = do_param ? 0 : 1;
  123. if (ctx == NULL)
  124. goto opthelp;
  125. if (!app_passwd(passarg, NULL, &pass, NULL)) {
  126. BIO_puts(bio_err, "Error getting password\n");
  127. goto end;
  128. }
  129. out = bio_open_owner(outfile, outformat, private);
  130. if (out == NULL)
  131. goto end;
  132. EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
  133. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  134. if (do_param) {
  135. if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
  136. BIO_puts(bio_err, "Error generating parameters\n");
  137. ERR_print_errors(bio_err);
  138. goto end;
  139. }
  140. } else {
  141. if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
  142. BIO_puts(bio_err, "Error generating key\n");
  143. ERR_print_errors(bio_err);
  144. goto end;
  145. }
  146. }
  147. if (do_param) {
  148. rv = PEM_write_bio_Parameters(out, pkey);
  149. } else if (outformat == FORMAT_PEM) {
  150. assert(private);
  151. rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
  152. } else if (outformat == FORMAT_ASN1) {
  153. assert(private);
  154. rv = i2d_PrivateKey_bio(out, pkey);
  155. } else {
  156. BIO_printf(bio_err, "Bad format specified for key\n");
  157. goto end;
  158. }
  159. if (rv <= 0) {
  160. BIO_puts(bio_err, "Error writing key\n");
  161. ERR_print_errors(bio_err);
  162. }
  163. if (text) {
  164. if (do_param)
  165. rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
  166. else
  167. rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
  168. if (rv <= 0) {
  169. BIO_puts(bio_err, "Error printing key\n");
  170. ERR_print_errors(bio_err);
  171. }
  172. }
  173. ret = 0;
  174. end:
  175. EVP_PKEY_free(pkey);
  176. EVP_PKEY_CTX_free(ctx);
  177. BIO_free_all(out);
  178. BIO_free(in);
  179. release_engine(e);
  180. OPENSSL_free(pass);
  181. return ret;
  182. }
  183. static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e)
  184. {
  185. BIO *pbio;
  186. EVP_PKEY *pkey = NULL;
  187. EVP_PKEY_CTX *ctx = NULL;
  188. if (*pctx) {
  189. BIO_puts(bio_err, "Parameters already set!\n");
  190. return 0;
  191. }
  192. pbio = BIO_new_file(file, "r");
  193. if (!pbio) {
  194. BIO_printf(bio_err, "Can't open parameter file %s\n", file);
  195. return 0;
  196. }
  197. pkey = PEM_read_bio_Parameters(pbio, NULL);
  198. BIO_free(pbio);
  199. if (!pkey) {
  200. BIO_printf(bio_err, "Error reading parameter file %s\n", file);
  201. return 0;
  202. }
  203. ctx = EVP_PKEY_CTX_new(pkey, e);
  204. if (ctx == NULL)
  205. goto err;
  206. if (EVP_PKEY_keygen_init(ctx) <= 0)
  207. goto err;
  208. EVP_PKEY_free(pkey);
  209. *pctx = ctx;
  210. return 1;
  211. err:
  212. BIO_puts(bio_err, "Error initializing context\n");
  213. ERR_print_errors(bio_err);
  214. EVP_PKEY_CTX_free(ctx);
  215. EVP_PKEY_free(pkey);
  216. return 0;
  217. }
  218. int init_gen_str(EVP_PKEY_CTX **pctx,
  219. const char *algname, ENGINE *e, int do_param)
  220. {
  221. EVP_PKEY_CTX *ctx = NULL;
  222. const EVP_PKEY_ASN1_METHOD *ameth;
  223. ENGINE *tmpeng = NULL;
  224. int pkey_id;
  225. if (*pctx) {
  226. BIO_puts(bio_err, "Algorithm already set!\n");
  227. return 0;
  228. }
  229. ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
  230. #ifndef OPENSSL_NO_ENGINE
  231. if (!ameth && e)
  232. ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
  233. #endif
  234. if (!ameth) {
  235. BIO_printf(bio_err, "Algorithm %s not found\n", algname);
  236. return 0;
  237. }
  238. ERR_clear_error();
  239. EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
  240. #ifndef OPENSSL_NO_ENGINE
  241. ENGINE_finish(tmpeng);
  242. #endif
  243. ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
  244. if (!ctx)
  245. goto err;
  246. if (do_param) {
  247. if (EVP_PKEY_paramgen_init(ctx) <= 0)
  248. goto err;
  249. } else {
  250. if (EVP_PKEY_keygen_init(ctx) <= 0)
  251. goto err;
  252. }
  253. *pctx = ctx;
  254. return 1;
  255. err:
  256. BIO_printf(bio_err, "Error initializing %s context\n", algname);
  257. ERR_print_errors(bio_err);
  258. EVP_PKEY_CTX_free(ctx);
  259. return 0;
  260. }
  261. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  262. {
  263. char c = '*';
  264. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  265. int p;
  266. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  267. if (p == 0)
  268. c = '.';
  269. if (p == 1)
  270. c = '+';
  271. if (p == 2)
  272. c = '*';
  273. if (p == 3)
  274. c = '\n';
  275. BIO_write(b, &c, 1);
  276. (void)BIO_flush(b);
  277. return 1;
  278. }