genpkey.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. 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. if (rv <= 0) {
  167. BIO_puts(bio_err, "Error writing key\n");
  168. ERR_print_errors(bio_err);
  169. }
  170. if (text) {
  171. if (do_param)
  172. rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
  173. else
  174. rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
  175. if (rv <= 0) {
  176. BIO_puts(bio_err, "Error printing key\n");
  177. ERR_print_errors(bio_err);
  178. }
  179. }
  180. ret = 0;
  181. end:
  182. EVP_PKEY_free(pkey);
  183. EVP_PKEY_CTX_free(ctx);
  184. BIO_free_all(out);
  185. BIO_free(in);
  186. release_engine(e);
  187. OPENSSL_free(pass);
  188. return ret;
  189. }
  190. static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e)
  191. {
  192. BIO *pbio;
  193. EVP_PKEY *pkey = NULL;
  194. EVP_PKEY_CTX *ctx = NULL;
  195. if (*pctx) {
  196. BIO_puts(bio_err, "Parameters already set!\n");
  197. return 0;
  198. }
  199. pbio = BIO_new_file(file, "r");
  200. if (!pbio) {
  201. BIO_printf(bio_err, "Can't open parameter file %s\n", file);
  202. return 0;
  203. }
  204. pkey = PEM_read_bio_Parameters(pbio, NULL);
  205. BIO_free(pbio);
  206. if (!pkey) {
  207. BIO_printf(bio_err, "Error reading parameter file %s\n", file);
  208. return 0;
  209. }
  210. ctx = EVP_PKEY_CTX_new(pkey, e);
  211. if (ctx == NULL)
  212. goto err;
  213. if (EVP_PKEY_keygen_init(ctx) <= 0)
  214. goto err;
  215. EVP_PKEY_free(pkey);
  216. *pctx = ctx;
  217. return 1;
  218. err:
  219. BIO_puts(bio_err, "Error initializing context\n");
  220. ERR_print_errors(bio_err);
  221. EVP_PKEY_CTX_free(ctx);
  222. EVP_PKEY_free(pkey);
  223. return 0;
  224. }
  225. int init_gen_str(EVP_PKEY_CTX **pctx,
  226. const char *algname, ENGINE *e, int do_param)
  227. {
  228. EVP_PKEY_CTX *ctx = NULL;
  229. const EVP_PKEY_ASN1_METHOD *ameth;
  230. ENGINE *tmpeng = NULL;
  231. int pkey_id;
  232. if (*pctx) {
  233. BIO_puts(bio_err, "Algorithm already set!\n");
  234. return 0;
  235. }
  236. ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
  237. #ifndef OPENSSL_NO_ENGINE
  238. if (!ameth && e)
  239. ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
  240. #endif
  241. if (!ameth) {
  242. BIO_printf(bio_err, "Algorithm %s not found\n", algname);
  243. return 0;
  244. }
  245. ERR_clear_error();
  246. EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
  247. #ifndef OPENSSL_NO_ENGINE
  248. ENGINE_finish(tmpeng);
  249. #endif
  250. ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
  251. if (!ctx)
  252. goto err;
  253. if (do_param) {
  254. if (EVP_PKEY_paramgen_init(ctx) <= 0)
  255. goto err;
  256. } else {
  257. if (EVP_PKEY_keygen_init(ctx) <= 0)
  258. goto err;
  259. }
  260. *pctx = ctx;
  261. return 1;
  262. err:
  263. BIO_printf(bio_err, "Error initializing %s context\n", algname);
  264. ERR_print_errors(bio_err);
  265. EVP_PKEY_CTX_free(ctx);
  266. return 0;
  267. }
  268. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  269. {
  270. char c = '*';
  271. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  272. int p;
  273. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  274. if (p == 0)
  275. c = '.';
  276. if (p == 1)
  277. c = '+';
  278. if (p == 2)
  279. c = '*';
  280. if (p == 3)
  281. c = '\n';
  282. BIO_write(b, &c, 1);
  283. (void)BIO_flush(b);
  284. return 1;
  285. }