genpkey.c 10 KB

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