genpkey.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* apps/genpkey.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 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. static int init_keygen_file(BIO *err, EVP_PKEY_CTX **pctx,
  65. const char *file, ENGINE *e);
  66. static int init_gen_str(BIO *err, EVP_PKEY_CTX **pctx,
  67. const char *algname, ENGINE *e, int do_param);
  68. static int genpkey_cb(EVP_PKEY_CTX *ctx);
  69. #define PROG genpkey_main
  70. int MAIN(int, char **);
  71. int MAIN(int argc, char **argv)
  72. {
  73. ENGINE *e = NULL;
  74. char **args, *outfile = NULL;
  75. char *passarg = NULL;
  76. BIO *in = NULL, *out = NULL;
  77. const EVP_CIPHER *cipher = NULL;
  78. int outformat;
  79. int text = 0;
  80. EVP_PKEY *pkey=NULL;
  81. EVP_PKEY_CTX *ctx = NULL;
  82. char *pass = NULL;
  83. int badarg = 0;
  84. int ret = 1;
  85. int do_param = 0;
  86. if (bio_err == NULL)
  87. bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
  88. if (!load_config(bio_err, NULL))
  89. goto end;
  90. outformat=FORMAT_PEM;
  91. ERR_load_crypto_strings();
  92. OpenSSL_add_all_algorithms();
  93. args = argv + 1;
  94. while (!badarg && *args && *args[0] == '-')
  95. {
  96. if (!strcmp(*args,"-outform"))
  97. {
  98. if (args[1])
  99. {
  100. args++;
  101. outformat=str2fmt(*args);
  102. }
  103. else badarg = 1;
  104. }
  105. else if (!strcmp(*args,"-pass"))
  106. {
  107. if (!args[1]) goto bad;
  108. passarg= *(++args);
  109. }
  110. #ifndef OPENSSL_NO_ENGINE
  111. else if (strcmp(*args,"-engine") == 0)
  112. {
  113. if (!args[1])
  114. goto bad;
  115. e = setup_engine(bio_err, *(++args), 0);
  116. }
  117. #endif
  118. else if (!strcmp (*args, "-paramfile"))
  119. {
  120. if (!args[1])
  121. goto bad;
  122. args++;
  123. if (do_param == 1)
  124. goto bad;
  125. if (!init_keygen_file(bio_err, &ctx, *args, e))
  126. goto end;
  127. }
  128. else if (!strcmp (*args, "-out"))
  129. {
  130. if (args[1])
  131. {
  132. args++;
  133. outfile = *args;
  134. }
  135. else badarg = 1;
  136. }
  137. else if (strcmp(*args,"-algorithm") == 0)
  138. {
  139. if (!args[1])
  140. goto bad;
  141. if (!init_gen_str(bio_err, &ctx, *(++args),e, do_param))
  142. goto end;
  143. }
  144. else if (strcmp(*args,"-pkeyopt") == 0)
  145. {
  146. if (!args[1])
  147. goto bad;
  148. if (!ctx)
  149. {
  150. BIO_puts(bio_err, "No keytype specified\n");
  151. goto bad;
  152. }
  153. else if (pkey_ctrl_string(ctx, *(++args)) <= 0)
  154. {
  155. BIO_puts(bio_err, "parameter setting error\n");
  156. ERR_print_errors(bio_err);
  157. goto end;
  158. }
  159. }
  160. else if (strcmp(*args,"-genparam") == 0)
  161. {
  162. if (ctx)
  163. goto bad;
  164. do_param = 1;
  165. }
  166. else if (strcmp(*args,"-text") == 0)
  167. text=1;
  168. else
  169. {
  170. cipher = EVP_get_cipherbyname(*args + 1);
  171. if (!cipher)
  172. {
  173. BIO_printf(bio_err, "Unknown cipher %s\n",
  174. *args + 1);
  175. badarg = 1;
  176. }
  177. if (do_param == 1)
  178. badarg = 1;
  179. }
  180. args++;
  181. }
  182. if (!ctx)
  183. badarg = 1;
  184. if (badarg)
  185. {
  186. bad:
  187. BIO_printf(bio_err, "Usage genpkey [options]\n");
  188. BIO_printf(bio_err, "where options are\n");
  189. BIO_printf(bio_err, "-paramfile file parameter file\n");
  190. BIO_printf(bio_err, "-pass arg output file pass phrase source\n");
  191. BIO_printf(bio_err, "-outform X output format (DER or PEM)\n");
  192. BIO_printf(bio_err, "-out file output file\n");
  193. #ifndef OPENSSL_NO_ENGINE
  194. BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
  195. #endif
  196. return 1;
  197. }
  198. if (!app_passwd(bio_err, passarg, NULL, &pass, NULL))
  199. {
  200. BIO_puts(bio_err, "Error getting password\n");
  201. goto end;
  202. }
  203. if (outfile)
  204. {
  205. if (!(out = BIO_new_file (outfile, "wb")))
  206. {
  207. BIO_printf(bio_err,
  208. "Can't open output file %s\n", outfile);
  209. goto end;
  210. }
  211. }
  212. else
  213. {
  214. out = BIO_new_fp (stdout, BIO_NOCLOSE);
  215. #ifdef OPENSSL_SYS_VMS
  216. {
  217. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  218. out = BIO_push(tmpbio, out);
  219. }
  220. #endif
  221. }
  222. EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
  223. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  224. if (do_param)
  225. {
  226. if (EVP_PKEY_paramgen(ctx, &pkey) <= 0)
  227. {
  228. BIO_puts(bio_err, "Error generating parameters\n");
  229. ERR_print_errors(bio_err);
  230. goto end;
  231. }
  232. }
  233. else
  234. {
  235. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  236. {
  237. BIO_puts(bio_err, "Error generating key\n");
  238. ERR_print_errors(bio_err);
  239. goto end;
  240. }
  241. }
  242. if (do_param)
  243. PEM_write_bio_Parameters(out, pkey);
  244. else if (outformat == FORMAT_PEM)
  245. PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0,
  246. NULL, pass);
  247. else if (outformat == FORMAT_ASN1)
  248. i2d_PrivateKey_bio(out, pkey);
  249. else
  250. {
  251. BIO_printf(bio_err, "Bad format specified for key\n");
  252. goto end;
  253. }
  254. if (text)
  255. {
  256. if (do_param)
  257. EVP_PKEY_print_params(out, pkey, 0, NULL);
  258. else
  259. EVP_PKEY_print_private(out, pkey, 0, NULL);
  260. }
  261. ret = 0;
  262. end:
  263. if (pkey)
  264. EVP_PKEY_free(pkey);
  265. if (ctx)
  266. EVP_PKEY_CTX_free(ctx);
  267. if (out)
  268. BIO_free_all(out);
  269. BIO_free(in);
  270. if (pass)
  271. OPENSSL_free(pass);
  272. return ret;
  273. }
  274. static int init_keygen_file(BIO *err, EVP_PKEY_CTX **pctx,
  275. const char *file, ENGINE *e)
  276. {
  277. BIO *pbio;
  278. EVP_PKEY *pkey = NULL;
  279. EVP_PKEY_CTX *ctx = NULL;
  280. if (*pctx)
  281. {
  282. BIO_puts(err, "Parameters already set!\n");
  283. return 0;
  284. }
  285. pbio = BIO_new_file(file, "r");
  286. if (!pbio)
  287. {
  288. BIO_printf(err, "Can't open parameter file %s\n", file);
  289. return 0;
  290. }
  291. pkey = PEM_read_bio_Parameters(pbio, NULL);
  292. BIO_free(pbio);
  293. if (!pkey)
  294. {
  295. BIO_printf(bio_err, "Error reading parameter file %s\n", file);
  296. return 0;
  297. }
  298. ctx = EVP_PKEY_CTX_new(pkey, e);
  299. if (!ctx)
  300. goto err;
  301. if (EVP_PKEY_keygen_init(ctx) <= 0)
  302. goto err;
  303. EVP_PKEY_free(pkey);
  304. *pctx = ctx;
  305. return 1;
  306. err:
  307. BIO_puts(err, "Error initializing context\n");
  308. ERR_print_errors(err);
  309. if (ctx)
  310. EVP_PKEY_CTX_free(ctx);
  311. if (pkey)
  312. EVP_PKEY_free(pkey);
  313. return 0;
  314. }
  315. static int init_gen_str(BIO *err, EVP_PKEY_CTX **pctx,
  316. const char *algname, ENGINE *e, int do_param)
  317. {
  318. EVP_PKEY_CTX *ctx = NULL;
  319. const EVP_PKEY_ASN1_METHOD *ameth;
  320. int pkey_id;
  321. if (*pctx)
  322. {
  323. BIO_puts(err, "Algorithm already set!\n");
  324. return 0;
  325. }
  326. ameth = EVP_PKEY_asn1_find_str(algname, -1);
  327. if (!ameth)
  328. {
  329. BIO_printf(bio_err, "Algorithm %s not found\n", algname);
  330. return 0;
  331. }
  332. EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
  333. ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
  334. if (!ctx)
  335. goto err;
  336. if (do_param)
  337. {
  338. if (EVP_PKEY_paramgen_init(ctx) <= 0)
  339. goto err;
  340. }
  341. else
  342. {
  343. if (EVP_PKEY_keygen_init(ctx) <= 0)
  344. goto err;
  345. }
  346. *pctx = ctx;
  347. return 1;
  348. err:
  349. BIO_printf(err, "Error initializing %s context\n", algname);
  350. ERR_print_errors(err);
  351. if (ctx)
  352. EVP_PKEY_CTX_free(ctx);
  353. return 0;
  354. }
  355. static int genpkey_cb(EVP_PKEY_CTX *ctx)
  356. {
  357. char c='*';
  358. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  359. int p;
  360. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  361. if (p == 0) c='.';
  362. if (p == 1) c='+';
  363. if (p == 2) c='*';
  364. if (p == 3) c='\n';
  365. BIO_write(b,&c,1);
  366. (void)BIO_flush(b);
  367. #ifdef LINT
  368. p=n;
  369. #endif
  370. return 1;
  371. }