genpkey.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2006
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include "apps.h"
  61. #include <openssl/pem.h>
  62. #include <openssl/err.h>
  63. #include <openssl/evp.h>
  64. #ifndef OPENSSL_NO_ENGINE
  65. # include <openssl/engine.h>
  66. #endif
  67. static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e);
  68. static int genpkey_cb(EVP_PKEY_CTX *ctx);
  69. typedef enum OPTION_choice {
  70. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  71. OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
  72. OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER
  73. } OPTION_CHOICE;
  74. OPTIONS genpkey_options[] = {
  75. {"help", OPT_HELP, '-', "Display this summary"},
  76. {"out", OPT_OUT, '>', "Output file"},
  77. {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
  78. {"pass", OPT_PASS, 's', "Output file pass phrase source"},
  79. {"paramfile", OPT_PARAMFILE, '<', "Parameters file"},
  80. {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"},
  81. {"pkeyopt", OPT_PKEYOPT, 's',
  82. "Set the public key algorithm option as opt:value"},
  83. {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
  84. {"text", OPT_TEXT, '-', "Print the in text"},
  85. {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
  86. #ifndef OPENSSL_NO_ENGINE
  87. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  88. #endif
  89. /* This is deliberately last. */
  90. {OPT_HELP_STR, 1, 1,
  91. "Order of options may be important! See the documentation.\n"},
  92. {NULL}
  93. };
  94. int genpkey_main(int argc, char **argv)
  95. {
  96. BIO *in = NULL, *out = NULL;
  97. ENGINE *e = NULL;
  98. EVP_PKEY *pkey = NULL;
  99. EVP_PKEY_CTX *ctx = NULL;
  100. char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
  101. const EVP_CIPHER *cipher = NULL;
  102. OPTION_CHOICE o;
  103. int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
  104. int private = 0;
  105. prog = opt_init(argc, argv, genpkey_options);
  106. while ((o = opt_next()) != OPT_EOF) {
  107. switch (o) {
  108. case OPT_EOF:
  109. case OPT_ERR:
  110. opthelp:
  111. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  112. goto end;
  113. case OPT_HELP:
  114. ret = 0;
  115. opt_help(genpkey_options);
  116. goto end;
  117. case OPT_OUTFORM:
  118. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  119. goto opthelp;
  120. break;
  121. case OPT_OUT:
  122. outfile = opt_arg();
  123. break;
  124. case OPT_PASS:
  125. passarg = opt_arg();
  126. break;
  127. case OPT_ENGINE:
  128. e = setup_engine(opt_arg(), 0);
  129. break;
  130. case OPT_PARAMFILE:
  131. if (do_param == 1)
  132. goto opthelp;
  133. if (!init_keygen_file(&ctx, opt_arg(), e))
  134. goto end;
  135. break;
  136. case OPT_ALGORITHM:
  137. if (!init_gen_str(&ctx, opt_arg(), e, do_param))
  138. goto end;
  139. break;
  140. case OPT_PKEYOPT:
  141. if (ctx == NULL) {
  142. BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
  143. goto opthelp;
  144. }
  145. if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
  146. BIO_printf(bio_err,
  147. "%s: Error setting %s parameter:\n",
  148. prog, opt_arg());
  149. ERR_print_errors(bio_err);
  150. goto end;
  151. }
  152. break;
  153. case OPT_GENPARAM:
  154. if (ctx != NULL)
  155. goto opthelp;
  156. do_param = 1;
  157. break;
  158. case OPT_TEXT:
  159. text = 1;
  160. break;
  161. case OPT_CIPHER:
  162. if (!opt_cipher(opt_unknown(), &cipher)
  163. || do_param == 1)
  164. goto opthelp;
  165. }
  166. }
  167. argc = opt_num_rest();
  168. argv = opt_rest();
  169. private = do_param ? 0 : 1;
  170. if (ctx == NULL)
  171. goto opthelp;
  172. if (!app_passwd(passarg, NULL, &pass, NULL)) {
  173. BIO_puts(bio_err, "Error getting password\n");
  174. goto end;
  175. }
  176. out = bio_open_owner(outfile, outformat, private);
  177. if (out == NULL)
  178. goto end;
  179. EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
  180. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  181. if (do_param) {
  182. if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
  183. BIO_puts(bio_err, "Error generating parameters\n");
  184. ERR_print_errors(bio_err);
  185. goto end;
  186. }
  187. } else {
  188. if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
  189. BIO_puts(bio_err, "Error generating key\n");
  190. ERR_print_errors(bio_err);
  191. goto end;
  192. }
  193. }
  194. if (do_param)
  195. rv = PEM_write_bio_Parameters(out, pkey);
  196. else if (outformat == FORMAT_PEM) {
  197. assert(private);
  198. rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
  199. } else if (outformat == FORMAT_ASN1) {
  200. assert(private);
  201. rv = i2d_PrivateKey_bio(out, pkey);
  202. } else {
  203. BIO_printf(bio_err, "Bad format specified for key\n");
  204. goto end;
  205. }
  206. if (rv <= 0) {
  207. BIO_puts(bio_err, "Error writing key\n");
  208. ERR_print_errors(bio_err);
  209. }
  210. if (text) {
  211. if (do_param)
  212. rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
  213. else
  214. rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
  215. if (rv <= 0) {
  216. BIO_puts(bio_err, "Error printing key\n");
  217. ERR_print_errors(bio_err);
  218. }
  219. }
  220. ret = 0;
  221. end:
  222. EVP_PKEY_free(pkey);
  223. EVP_PKEY_CTX_free(ctx);
  224. BIO_free_all(out);
  225. BIO_free(in);
  226. OPENSSL_free(pass);
  227. return ret;
  228. }
  229. static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e)
  230. {
  231. BIO *pbio;
  232. EVP_PKEY *pkey = NULL;
  233. EVP_PKEY_CTX *ctx = NULL;
  234. if (*pctx) {
  235. BIO_puts(bio_err, "Parameters already set!\n");
  236. return 0;
  237. }
  238. pbio = BIO_new_file(file, "r");
  239. if (!pbio) {
  240. BIO_printf(bio_err, "Can't open parameter file %s\n", file);
  241. return 0;
  242. }
  243. pkey = PEM_read_bio_Parameters(pbio, NULL);
  244. BIO_free(pbio);
  245. if (!pkey) {
  246. BIO_printf(bio_err, "Error reading parameter file %s\n", file);
  247. return 0;
  248. }
  249. ctx = EVP_PKEY_CTX_new(pkey, e);
  250. if (ctx == NULL)
  251. goto err;
  252. if (EVP_PKEY_keygen_init(ctx) <= 0)
  253. goto err;
  254. EVP_PKEY_free(pkey);
  255. *pctx = ctx;
  256. return 1;
  257. err:
  258. BIO_puts(bio_err, "Error initializing context\n");
  259. ERR_print_errors(bio_err);
  260. EVP_PKEY_CTX_free(ctx);
  261. EVP_PKEY_free(pkey);
  262. return 0;
  263. }
  264. int init_gen_str(EVP_PKEY_CTX **pctx,
  265. const char *algname, ENGINE *e, int do_param)
  266. {
  267. EVP_PKEY_CTX *ctx = NULL;
  268. const EVP_PKEY_ASN1_METHOD *ameth;
  269. ENGINE *tmpeng = NULL;
  270. int pkey_id;
  271. if (*pctx) {
  272. BIO_puts(bio_err, "Algorithm already set!\n");
  273. return 0;
  274. }
  275. ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
  276. #ifndef OPENSSL_NO_ENGINE
  277. if (!ameth && e)
  278. ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
  279. #endif
  280. if (!ameth) {
  281. BIO_printf(bio_err, "Algorithm %s not found\n", algname);
  282. return 0;
  283. }
  284. ERR_clear_error();
  285. EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
  286. #ifndef OPENSSL_NO_ENGINE
  287. if (tmpeng)
  288. ENGINE_finish(tmpeng);
  289. #endif
  290. ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
  291. if (!ctx)
  292. goto err;
  293. if (do_param) {
  294. if (EVP_PKEY_paramgen_init(ctx) <= 0)
  295. goto err;
  296. } else {
  297. if (EVP_PKEY_keygen_init(ctx) <= 0)
  298. goto err;
  299. }
  300. *pctx = ctx;
  301. return 1;
  302. err:
  303. BIO_printf(bio_err, "Error initializing %s context\n", algname);
  304. ERR_print_errors(bio_err);
  305. EVP_PKEY_CTX_free(ctx);
  306. return 0;
  307. }
  308. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  309. {
  310. char c = '*';
  311. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  312. int p;
  313. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  314. if (p == 0)
  315. c = '.';
  316. if (p == 1)
  317. c = '+';
  318. if (p == 2)
  319. c = '*';
  320. if (p == 3)
  321. c = '\n';
  322. BIO_write(b, &c, 1);
  323. (void)BIO_flush(b);
  324. return 1;
  325. }