2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 * 2010 Free Software Foundation, Inc.
5 * This file is part of GnuTLS.
7 * GnuTLS is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuTLS is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
30 #include <gnutls/gnutls.h>
31 #include <certtool-common.h>
33 /* Generates Diffie-Hellman parameters (a prime and a generator
34 * of the group). Exports them in PKCS #3 format. Used by certtool.
40 static int cparams = 0;
42 /* If how is zero then the included parameters are used.
45 generate_prime (int how)
49 gnutls_dh_params_t dh_params;
51 int bits = get_bits (GNUTLS_PK_DH);
53 gnutls_dh_params_init (&dh_params);
55 fprintf (stderr, "Generating DH parameters...");
59 ret = gnutls_dh_params_generate2 (dh_params, bits);
62 fprintf (stderr, "Error generating parameters: %s\n",
63 gnutls_strerror (ret));
67 ret = gnutls_dh_params_export_raw (dh_params, &p, &g, NULL);
70 fprintf (stderr, "Error exporting parameters: %s\n",
71 gnutls_strerror (ret));
80 p = gnutls_srp_1024_group_prime;
81 g = gnutls_srp_1024_group_generator;
83 else if (bits <= 1536)
85 p = gnutls_srp_1536_group_prime;
86 g = gnutls_srp_1536_group_generator;
90 p = gnutls_srp_2048_group_prime;
91 g = gnutls_srp_2048_group_generator;
94 ret = gnutls_dh_params_import_raw (dh_params, &p, &g);
97 fprintf (stderr, "Error exporting parameters: %s\n",
98 gnutls_strerror (ret));
102 fprintf (stderr, "Parameters unavailable as SRP disabled.\n");
109 fprintf (outfile, "/* generator */\n");
110 fprintf (outfile, "\nconst uint8 g[%d] = { ", g.size);
112 for (i = 0; i < g.size; i++)
115 fprintf (outfile, "\n\t");
116 fprintf (outfile, "0x%.2x", g.data[i]);
118 fprintf (outfile, ", ");
121 fprintf (outfile, "\n};\n\n");
125 fprintf (outfile, "\nGenerator: ");
127 for (i = 0; i < g.size; i++)
129 if (i != 0 && i % 12 == 0)
130 fprintf (outfile, "\n\t");
131 else if (i != 0 && i != g.size)
132 fprintf (outfile, ":");
134 fprintf (outfile, "%.2x", g.data[i]);
137 fprintf (outfile, "\n\n");
144 fprintf (outfile, "/* prime - %d bits */\n", p.size * 8);
145 fprintf (outfile, "\nconst uint8 prime[%d] = { ", p.size);
147 for (i = 0; i < p.size; i++)
150 fprintf (outfile, "\n\t");
151 fprintf (outfile, "0x%.2x", p.data[i]);
153 fprintf (outfile, ", ");
156 fprintf (outfile, "\n};\n");
160 fprintf (outfile, "Prime: ");
162 for (i = 0; i < p.size; i++)
164 if (i != 0 && i % 12 == 0)
165 fprintf (outfile, "\n\t");
166 else if (i != 0 && i != p.size)
167 fprintf (outfile, ":");
168 fprintf (outfile, "%.2x", p.data[i]);
171 fprintf (outfile, "\n\n");
176 { /* generate a PKCS#3 structure */
178 size_t len = buffer_size;
180 ret = gnutls_dh_params_export_pkcs3 (dh_params, GNUTLS_X509_FMT_PEM,
185 fprintf (outfile, "\n%s", buffer);
189 fprintf (stderr, "Error: %s\n", gnutls_strerror (ret));