dhparam.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #ifndef OPENSSL_NO_DEPRECATED_3_0
  10. /* We need to use some deprecated APIs */
  11. # define OPENSSL_SUPPRESS_DEPRECATED
  12. #endif
  13. #include <openssl/opensslconf.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #include "apps.h"
  19. #include "progs.h"
  20. #include <openssl/bio.h>
  21. #include <openssl/err.h>
  22. #include <openssl/bn.h>
  23. #include <openssl/dh.h>
  24. #include <openssl/x509.h>
  25. #include <openssl/pem.h>
  26. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  27. # include <openssl/dsa.h>
  28. #endif
  29. #define DEFBITS 2048
  30. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  31. static int dh_cb(int p, int n, BN_GENCB *cb);
  32. #endif
  33. static int gendh_cb(EVP_PKEY_CTX *ctx);
  34. typedef enum OPTION_choice {
  35. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  36. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
  37. OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
  38. OPT_DSAPARAM, OPT_C, OPT_2, OPT_3, OPT_5,
  39. OPT_R_ENUM, OPT_PROV_ENUM
  40. } OPTION_CHOICE;
  41. const OPTIONS dhparam_options[] = {
  42. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
  43. OPT_SECTION("General"),
  44. {"help", OPT_HELP, '-', "Display this summary"},
  45. {"check", OPT_CHECK, '-', "Check the DH parameters"},
  46. #ifndef OPENSSL_NO_DSA
  47. {"dsaparam", OPT_DSAPARAM, '-',
  48. "Read or generate DSA parameters, convert to DH"},
  49. #endif
  50. #ifndef OPENSSL_NO_ENGINE
  51. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  52. #endif
  53. OPT_SECTION("Input"),
  54. {"in", OPT_IN, '<', "Input file"},
  55. {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
  56. OPT_SECTION("Output"),
  57. {"out", OPT_OUT, '>', "Output file"},
  58. {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
  59. {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
  60. {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
  61. {"C", OPT_C, '-', "Print C code"},
  62. {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
  63. {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
  64. {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
  65. OPT_R_OPTIONS,
  66. OPT_PROV_OPTIONS,
  67. OPT_PARAMETERS(),
  68. {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
  69. {NULL}
  70. };
  71. int dhparam_main(int argc, char **argv)
  72. {
  73. BIO *in = NULL, *out = NULL;
  74. DH *dh = NULL, *alloc_dh = NULL;
  75. EVP_PKEY *pkey = NULL;
  76. EVP_PKEY_CTX *ctx = NULL;
  77. char *infile = NULL, *outfile = NULL, *prog;
  78. ENGINE *e = NULL;
  79. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  80. int dsaparam = 0;
  81. #endif
  82. int i, text = 0, C = 0, ret = 1, num = 0, g = 0;
  83. int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
  84. OPTION_CHOICE o;
  85. prog = opt_init(argc, argv, dhparam_options);
  86. while ((o = opt_next()) != OPT_EOF) {
  87. switch (o) {
  88. case OPT_EOF:
  89. case OPT_ERR:
  90. opthelp:
  91. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  92. goto end;
  93. case OPT_HELP:
  94. opt_help(dhparam_options);
  95. ret = 0;
  96. goto end;
  97. case OPT_INFORM:
  98. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  99. goto opthelp;
  100. break;
  101. case OPT_OUTFORM:
  102. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  103. goto opthelp;
  104. break;
  105. case OPT_IN:
  106. infile = opt_arg();
  107. break;
  108. case OPT_OUT:
  109. outfile = opt_arg();
  110. break;
  111. case OPT_ENGINE:
  112. e = setup_engine(opt_arg(), 0);
  113. break;
  114. case OPT_CHECK:
  115. check = 1;
  116. break;
  117. case OPT_TEXT:
  118. text = 1;
  119. break;
  120. case OPT_DSAPARAM:
  121. #ifndef OPENSSL_NO_DSA
  122. # ifdef OPENSSL_NO_DEPRECATED_3_0
  123. BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
  124. # else
  125. dsaparam = 1;
  126. # endif
  127. #endif
  128. break;
  129. case OPT_C:
  130. C = 1;
  131. break;
  132. case OPT_2:
  133. g = 2;
  134. break;
  135. case OPT_3:
  136. g = 3;
  137. break;
  138. case OPT_5:
  139. g = 5;
  140. break;
  141. case OPT_NOOUT:
  142. noout = 1;
  143. break;
  144. case OPT_R_CASES:
  145. if (!opt_rand(o))
  146. goto end;
  147. break;
  148. case OPT_PROV_CASES:
  149. if (!opt_provider(o))
  150. goto end;
  151. break;
  152. }
  153. }
  154. argc = opt_num_rest();
  155. argv = opt_rest();
  156. if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
  157. goto end;
  158. if (g && !num)
  159. num = DEFBITS;
  160. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  161. if (dsaparam && g) {
  162. BIO_printf(bio_err,
  163. "Error, generator may not be chosen for DSA parameters\n");
  164. goto end;
  165. }
  166. #endif
  167. out = bio_open_default(outfile, 'w', outformat);
  168. if (out == NULL)
  169. goto end;
  170. /* DH parameters */
  171. if (num && !g)
  172. g = 2;
  173. if (num) {
  174. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  175. if (dsaparam) {
  176. DSA *dsa = DSA_new();
  177. BN_GENCB *cb = BN_GENCB_new();
  178. if (cb == NULL)
  179. goto end;
  180. BN_GENCB_set(cb, dh_cb, bio_err);
  181. BIO_printf(bio_err,
  182. "Generating DSA parameters, %d bit long prime\n", num);
  183. if (dsa == NULL
  184. || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
  185. cb)) {
  186. DSA_free(dsa);
  187. BN_GENCB_free(cb);
  188. BIO_printf(bio_err, "Error, unable to generate DSA parameters\n");
  189. goto end;
  190. }
  191. dh = alloc_dh = DSA_dup_DH(dsa);
  192. DSA_free(dsa);
  193. BN_GENCB_free(cb);
  194. if (dh == NULL)
  195. goto end;
  196. } else
  197. #endif
  198. {
  199. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
  200. if (ctx == NULL) {
  201. BIO_printf(bio_err,
  202. "Error, DH key generation context allocation failed\n");
  203. goto end;
  204. }
  205. EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
  206. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  207. BIO_printf(bio_err,
  208. "Generating DH parameters, %d bit long safe prime, generator %d\n",
  209. num, g);
  210. BIO_printf(bio_err, "This is going to take a long time\n");
  211. if (!EVP_PKEY_paramgen_init(ctx)) {
  212. BIO_printf(bio_err,
  213. "Error, unable to initialise DH param generation\n");
  214. goto end;
  215. }
  216. if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
  217. BIO_printf(bio_err, "Error, unable to set DH prime length\n");
  218. goto end;
  219. }
  220. if (!EVP_PKEY_paramgen(ctx, &pkey)) {
  221. BIO_printf(bio_err, "Error, DH generation failed\n");
  222. goto end;
  223. }
  224. dh = EVP_PKEY_get0_DH(pkey);
  225. }
  226. } else {
  227. in = bio_open_default(infile, 'r', informat);
  228. if (in == NULL)
  229. goto end;
  230. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  231. if (dsaparam) {
  232. DSA *dsa;
  233. if (informat == FORMAT_ASN1)
  234. dsa = d2i_DSAparams_bio(in, NULL);
  235. else /* informat == FORMAT_PEM */
  236. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  237. if (dsa == NULL) {
  238. BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
  239. goto end;
  240. }
  241. dh = alloc_dh = DSA_dup_DH(dsa);
  242. DSA_free(dsa);
  243. if (dh == NULL)
  244. goto end;
  245. } else
  246. #endif
  247. {
  248. if (informat == FORMAT_ASN1) {
  249. /*
  250. * We have no PEM header to determine what type of DH params it
  251. * is. We'll just try both.
  252. */
  253. dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
  254. /* BIO_reset() returns 0 for success for file BIOs only!!! */
  255. if (dh == NULL && BIO_reset(in) == 0)
  256. dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
  257. } else {
  258. /* informat == FORMAT_PEM */
  259. dh = alloc_dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
  260. }
  261. if (dh == NULL) {
  262. BIO_printf(bio_err, "Error, unable to load DH parameters\n");
  263. goto end;
  264. }
  265. }
  266. /* dh != NULL */
  267. }
  268. if (text)
  269. EVP_PKEY_print_params(out, pkey, 4, NULL);
  270. if (check) {
  271. if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
  272. BIO_printf(bio_err, "Error, invalid parameters generated\n");
  273. goto end;
  274. }
  275. BIO_printf(bio_err, "DH parameters appear to be ok.\n");
  276. if (num != 0) {
  277. /*
  278. * We have generated parameters but DH_check() indicates they are
  279. * invalid! This should never happen!
  280. */
  281. BIO_printf(bio_err, "Error, invalid parameters generated\n");
  282. goto end;
  283. }
  284. }
  285. if (C) {
  286. unsigned char *data;
  287. int len, bits;
  288. const BIGNUM *pbn, *gbn;
  289. dh = EVP_PKEY_get0_DH(pkey);
  290. len = EVP_PKEY_size(pkey);
  291. bits = EVP_PKEY_size(pkey);
  292. DH_get0_pqg(dh, &pbn, NULL, &gbn);
  293. data = app_malloc(len, "print a BN");
  294. BIO_printf(out, "static DH *get_dh%d(void)\n{\n", bits);
  295. print_bignum_var(out, pbn, "dhp", bits, data);
  296. print_bignum_var(out, gbn, "dhg", bits, data);
  297. BIO_printf(out, " DH *dh = DH_new();\n"
  298. " BIGNUM *p, *g;\n"
  299. "\n"
  300. " if (dh == NULL)\n"
  301. " return NULL;\n");
  302. BIO_printf(out, " p = BN_bin2bn(dhp_%d, sizeof(dhp_%d), NULL);\n",
  303. bits, bits);
  304. BIO_printf(out, " g = BN_bin2bn(dhg_%d, sizeof(dhg_%d), NULL);\n",
  305. bits, bits);
  306. BIO_printf(out, " if (p == NULL || g == NULL\n"
  307. " || !DH_set0_pqg(dh, p, NULL, g)) {\n"
  308. " DH_free(dh);\n"
  309. " BN_free(p);\n"
  310. " BN_free(g);\n"
  311. " return NULL;\n"
  312. " }\n");
  313. if (DH_get_length(dh) > 0)
  314. BIO_printf(out,
  315. " if (!DH_set_length(dh, %ld)) {\n"
  316. " DH_free(dh);\n"
  317. " return NULL;\n"
  318. " }\n", DH_get_length(dh));
  319. BIO_printf(out, " return dh;\n}\n");
  320. OPENSSL_free(data);
  321. }
  322. if (!noout) {
  323. const BIGNUM *q;
  324. DH_get0_pqg(dh, NULL, &q, NULL);
  325. if (outformat == FORMAT_ASN1) {
  326. if (q != NULL)
  327. i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
  328. else
  329. i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
  330. } else if (q != NULL) {
  331. i = PEM_write_bio_DHxparams(out, dh);
  332. } else {
  333. i = PEM_write_bio_DHparams(out, dh);
  334. }
  335. if (!i) {
  336. BIO_printf(bio_err, "Error, unable to write DH parameters\n");
  337. goto end;
  338. }
  339. }
  340. ret = 0;
  341. end:
  342. if (ret != 0)
  343. ERR_print_errors(bio_err);
  344. DH_free(alloc_dh);
  345. BIO_free(in);
  346. BIO_free_all(out);
  347. EVP_PKEY_free(pkey);
  348. EVP_PKEY_CTX_free(ctx);
  349. release_engine(e);
  350. return ret;
  351. }
  352. static int common_dh_cb(int p, BIO *b)
  353. {
  354. static const char symbols[] = ".+*\n";
  355. char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  356. BIO_write(b, &c, 1);
  357. (void)BIO_flush(b);
  358. return 1;
  359. }
  360. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  361. static int dh_cb(int p, int n, BN_GENCB *cb)
  362. {
  363. return common_dh_cb(p, BN_GENCB_get_arg(cb));
  364. }
  365. #endif
  366. static int gendh_cb(EVP_PKEY_CTX *ctx)
  367. {
  368. return common_dh_cb(EVP_PKEY_CTX_get_keygen_info(ctx, 0),
  369. EVP_PKEY_CTX_get_app_data(ctx));
  370. }