1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | #include <config.h> |
22 | |
23 | #ifdef ENABLE_PKI1 |
24 | |
25 | #include <stdio.h> |
26 | #include <string.h> |
27 | #include <stdlib.h> |
28 | #include <unistd.h> |
29 | #include <gnutls/gnutls.h> |
30 | #include <read-file.h> |
31 | #include <certtool-common.h> |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | extern FILE *outfile; |
38 | extern FILE *infile; |
39 | |
40 | static void |
41 | print_dh_info (gnutls_datum_t * p, gnutls_datum_t * g, unsigned int q_bits) |
42 | { |
43 | int i; |
44 | |
45 | fprintf (outfile, "\nGenerator (%d bits): ", g->size*8); |
46 | |
47 | for (i = 0; i < g->size; i++) |
48 | { |
49 | if (i != 0 && i % 12 == 0) |
50 | fprintf (outfile, "\n\t"); |
51 | else if (i != 0 && i != g->size) |
52 | fprintf (outfile, ":"); |
53 | |
54 | fprintf (outfile, "%.2x", g->data[i]); |
55 | } |
56 | |
57 | fprintf (outfile, "\n\n"); |
58 | |
59 | |
60 | |
61 | fprintf (outfile, "Prime (%d bits):", p->size*8); |
62 | |
63 | for (i = 0; i < p->size; i++) |
64 | { |
65 | if (i % 12 == 0) |
66 | fprintf (outfile, "\n\t"); |
67 | else if (i != 0 && i != p->size) |
68 | fprintf (outfile, ":"); |
69 | fprintf (outfile, "%.2x", p->data[i]); |
70 | } |
71 | |
72 | if (q_bits > 0) |
73 | fprintf (outfile, "\n\nRecommended key length: %d bits\n", q_bits); |
74 | fprintf (outfile, "\n"); |
75 | |
76 | } |
77 | |
78 | void dh_info (common_info_st * ci) |
79 | { |
80 | gnutls_datum_t params; |
81 | size_t size; |
82 | int ret; |
83 | gnutls_dh_params_t dh_params; |
84 | gnutls_datum_t p, g; |
85 | unsigned int q_bits = 0; |
86 | |
87 | if (gnutls_dh_params_init (&dh_params) < 0) |
88 | { |
89 | fprintf (stderrstderr, "Error in dh parameter initialization\n"); |
90 | exit (1); |
91 | } |
92 | |
93 | params.data = fread_file_gnutls_fread_file (infile, &size); |
94 | params.size = size; |
95 | |
96 | ret = |
97 | gnutls_dh_params_import_pkcs3 (dh_params, ¶ms, GNUTLS_X509_FMT_PEM); |
98 | if (ret < 0) |
99 | { |
100 | fprintf (stderrstderr, "Error parsing dh params: %s\n", gnutls_strerror (ret)); |
101 | exit (1); |
102 | } |
103 | |
104 | ret = gnutls_dh_params_export_raw (dh_params, &p, &g, &q_bits); |
105 | if (ret < 0) |
106 | { |
107 | fprintf (stderrstderr, "Error exporting parameters: %s\n", |
108 | gnutls_strerror (ret)); |
109 | exit (1); |
110 | } |
111 | |
112 | print_dh_info (&p, &g, q_bits); |
113 | |
114 | { |
115 | size_t len = buffer_size; |
116 | |
117 | ret = gnutls_dh_params_export_pkcs3 (dh_params, GNUTLS_X509_FMT_PEM, |
118 | buffer, &len); |
119 | |
120 | if (ret == 0) |
121 | { |
122 | fprintf (outfile, "\n%s", buffer); |
123 | } |
124 | else |
125 | { |
126 | fprintf (stderrstderr, "Error: %s\n", gnutls_strerror (ret)); |
127 | } |
128 | |
129 | } |
130 | |
131 | gnutls_dh_params_deinit(dh_params); |
132 | } |
133 | |
134 | |
135 | |
136 | int |
137 | generate_prime (int how, common_info_st * info) |
138 | { |
139 | int ret; |
140 | gnutls_dh_params_t dh_params; |
141 | gnutls_datum_t p, g; |
142 | int bits = get_bits (GNUTLS_PK_DH, info->bits, info->sec_param); |
143 | unsigned int q_bits = 0; |
144 | |
145 | gnutls_dh_params_init (&dh_params); |
146 | |
147 | if (how != 0) |
148 | { |
149 | fprintf (stderrstderr, "Generating DH parameters (%d bits)...\n", bits); |
150 | fprintf (stderrstderr, "(might take long time)\n"); |
151 | } |
152 | else |
153 | fprintf (stderrstderr, "Retrieving DH parameters...\n"); |
154 | |
155 | if (how != 0) |
156 | { |
157 | ret = gnutls_dh_params_generate2 (dh_params, bits); |
158 | if (ret < 0) |
159 | { |
160 | fprintf (stderrstderr, "Error generating parameters: %s\n", |
161 | gnutls_strerror (ret)); |
162 | exit (1); |
163 | } |
164 | |
165 | ret = gnutls_dh_params_export_raw (dh_params, &p, &g, &q_bits); |
166 | if (ret < 0) |
167 | { |
168 | fprintf (stderrstderr, "Error exporting parameters: %s\n", |
169 | gnutls_strerror (ret)); |
170 | exit (1); |
171 | } |
172 | } |
173 | else |
174 | { |
175 | #ifdef ENABLE_SRP1 |
176 | if (bits <= 1024) |
177 | { |
178 | p = gnutls_srp_1024_group_prime; |
179 | g = gnutls_srp_1024_group_generator; |
180 | bits = 1024; |
181 | } |
182 | else if (bits <= 1536) |
183 | { |
184 | p = gnutls_srp_1536_group_prime; |
185 | g = gnutls_srp_1536_group_generator; |
186 | bits = 1536; |
187 | } |
188 | else if (bits <= 2048) |
189 | { |
190 | p = gnutls_srp_2048_group_prime; |
191 | g = gnutls_srp_2048_group_generator; |
192 | bits = 2048; |
193 | } |
194 | else if (bits <= 3072) |
195 | { |
196 | p = gnutls_srp_3072_group_prime; |
197 | g = gnutls_srp_3072_group_generator; |
198 | bits = 3072; |
| Value stored to 'bits' is never read |
199 | } |
200 | else |
201 | { |
202 | p = gnutls_srp_4096_group_prime; |
203 | g = gnutls_srp_4096_group_generator; |
204 | bits = 4096; |
205 | } |
206 | |
207 | ret = gnutls_dh_params_import_raw (dh_params, &p, &g); |
208 | if (ret < 0) |
209 | { |
210 | fprintf (stderrstderr, "Error exporting parameters: %s\n", |
211 | gnutls_strerror (ret)); |
212 | exit (1); |
213 | } |
214 | #else |
215 | fprintf (stderrstderr, "Parameters unavailable as SRP is disabled.\n"); |
216 | exit (1); |
217 | #endif |
218 | } |
219 | |
220 | print_dh_info (&p, &g, q_bits); |
221 | |
222 | { |
223 | size_t len = buffer_size; |
224 | |
225 | ret = gnutls_dh_params_export_pkcs3 (dh_params, GNUTLS_X509_FMT_PEM, |
226 | buffer, &len); |
227 | |
228 | if (ret == 0) |
229 | { |
230 | fprintf (outfile, "\n%s", buffer); |
231 | } |
232 | else |
233 | { |
234 | fprintf (stderrstderr, "Error: %s\n", gnutls_strerror (ret)); |
235 | } |
236 | |
237 | } |
238 | |
239 | gnutls_dh_params_deinit(dh_params); |
240 | |
241 | return 0; |
242 | } |
243 | |
244 | #endif |