]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* apps/dh.c */ |
41918458 | 2 | /* obsoleted by dhparam.c */ |
58964a49 | 3 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
4 | * All rights reserved. |
5 | * | |
6 | * This package is an SSL implementation written | |
7 | * by Eric Young (eay@cryptsoft.com). | |
8 | * The implementation was written so as to conform with Netscapes SSL. | |
9 | * | |
10 | * This library is free for commercial and non-commercial use as long as | |
11 | * the following conditions are aheared to. The following conditions | |
12 | * apply to all code found in this distribution, be it the RC4, RSA, | |
13 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
14 | * included with this distribution is covered by the same copyright terms | |
15 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
16 | * | |
17 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
18 | * the code are not to be removed. | |
19 | * If this package is used in a product, Eric Young should be given attribution | |
20 | * as the author of the parts of the library used. | |
21 | * This can be in the form of a textual message at program startup or | |
22 | * in documentation (online or textual) provided with the package. | |
23 | * | |
24 | * Redistribution and use in source and binary forms, with or without | |
25 | * modification, are permitted provided that the following conditions | |
26 | * are met: | |
27 | * 1. Redistributions of source code must retain the copyright | |
28 | * notice, this list of conditions and the following disclaimer. | |
29 | * 2. Redistributions in binary form must reproduce the above copyright | |
30 | * notice, this list of conditions and the following disclaimer in the | |
31 | * documentation and/or other materials provided with the distribution. | |
32 | * 3. All advertising materials mentioning features or use of this software | |
33 | * must display the following acknowledgement: | |
34 | * "This product includes cryptographic software written by | |
35 | * Eric Young (eay@cryptsoft.com)" | |
36 | * The word 'cryptographic' can be left out if the rouines from the library | |
37 | * being used are not cryptographic related :-). | |
38 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
39 | * the apps directory (application code) you must include an acknowledgement: | |
40 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * The licence and distribution terms for any publically available version or | |
55 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
56 | * copied and put under another distribution licence | |
57 | * [including the GNU Public Licence.] | |
58 | */ | |
59 | ||
3eeaab4b | 60 | #include <openssl/opensslconf.h> /* for OPENSSL_NO_DH */ |
cf1b7d96 | 61 | #ifndef OPENSSL_NO_DH |
d02b48c6 RE |
62 | #include <stdio.h> |
63 | #include <stdlib.h> | |
64 | #include <time.h> | |
65 | #include <string.h> | |
66 | #include "apps.h" | |
ec577822 BM |
67 | #include <openssl/bio.h> |
68 | #include <openssl/err.h> | |
69 | #include <openssl/bn.h> | |
70 | #include <openssl/dh.h> | |
71 | #include <openssl/x509.h> | |
72 | #include <openssl/pem.h> | |
d02b48c6 RE |
73 | |
74 | #undef PROG | |
75 | #define PROG dh_main | |
76 | ||
6977c7e2 TH |
77 | /*- |
78 | * -inform arg - input format - default PEM (DER or PEM) | |
d02b48c6 RE |
79 | * -outform arg - output format - default PEM |
80 | * -in arg - input file - default stdin | |
81 | * -out arg - output file - default stdout | |
82 | * -check - check the parameters are ok | |
83 | * -noout | |
84 | * -text | |
85 | * -C | |
86 | */ | |
87 | ||
667ac4ec RE |
88 | int MAIN(int, char **); |
89 | ||
6b691a5c | 90 | int MAIN(int argc, char **argv) |
d02b48c6 RE |
91 | { |
92 | DH *dh=NULL; | |
93 | int i,badops=0,text=0; | |
94 | BIO *in=NULL,*out=NULL; | |
95 | int informat,outformat,check=0,noout=0,C=0,ret=1; | |
0b13e9f0 RL |
96 | char *infile,*outfile,*prog; |
97 | #ifndef OPENSSL_NO_ENGINE | |
98 | char *engine; | |
99 | #endif | |
d02b48c6 RE |
100 | |
101 | apps_startup(); | |
102 | ||
103 | if (bio_err == NULL) | |
104 | if ((bio_err=BIO_new(BIO_s_file())) != NULL) | |
58964a49 | 105 | BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); |
d02b48c6 | 106 | |
3647bee2 DSH |
107 | if (!load_config(bio_err, NULL)) |
108 | goto end; | |
109 | ||
0b13e9f0 | 110 | #ifndef OPENSSL_NO_ENGINE |
5270e702 | 111 | engine=NULL; |
0b13e9f0 | 112 | #endif |
d02b48c6 RE |
113 | infile=NULL; |
114 | outfile=NULL; | |
115 | informat=FORMAT_PEM; | |
116 | outformat=FORMAT_PEM; | |
117 | ||
118 | prog=argv[0]; | |
119 | argc--; | |
120 | argv++; | |
121 | while (argc >= 1) | |
122 | { | |
123 | if (strcmp(*argv,"-inform") == 0) | |
124 | { | |
125 | if (--argc < 1) goto bad; | |
126 | informat=str2fmt(*(++argv)); | |
127 | } | |
128 | else if (strcmp(*argv,"-outform") == 0) | |
129 | { | |
130 | if (--argc < 1) goto bad; | |
131 | outformat=str2fmt(*(++argv)); | |
132 | } | |
133 | else if (strcmp(*argv,"-in") == 0) | |
134 | { | |
135 | if (--argc < 1) goto bad; | |
136 | infile= *(++argv); | |
137 | } | |
138 | else if (strcmp(*argv,"-out") == 0) | |
139 | { | |
140 | if (--argc < 1) goto bad; | |
141 | outfile= *(++argv); | |
142 | } | |
0b13e9f0 | 143 | #ifndef OPENSSL_NO_ENGINE |
5270e702 RL |
144 | else if (strcmp(*argv,"-engine") == 0) |
145 | { | |
146 | if (--argc < 1) goto bad; | |
147 | engine= *(++argv); | |
148 | } | |
0b13e9f0 | 149 | #endif |
d02b48c6 RE |
150 | else if (strcmp(*argv,"-check") == 0) |
151 | check=1; | |
152 | else if (strcmp(*argv,"-text") == 0) | |
153 | text=1; | |
154 | else if (strcmp(*argv,"-C") == 0) | |
155 | C=1; | |
156 | else if (strcmp(*argv,"-noout") == 0) | |
157 | noout=1; | |
158 | else | |
159 | { | |
160 | BIO_printf(bio_err,"unknown option %s\n",*argv); | |
161 | badops=1; | |
162 | break; | |
163 | } | |
164 | argc--; | |
165 | argv++; | |
166 | } | |
167 | ||
168 | if (badops) | |
169 | { | |
170 | bad: | |
171 | BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog); | |
172 | BIO_printf(bio_err,"where options are\n"); | |
ef7eaa4c DSH |
173 | BIO_printf(bio_err," -inform arg input format - one of DER PEM\n"); |
174 | BIO_printf(bio_err," -outform arg output format - one of DER PEM\n"); | |
9fe84296 | 175 | BIO_printf(bio_err," -in arg input file\n"); |
d02b48c6 RE |
176 | BIO_printf(bio_err," -out arg output file\n"); |
177 | BIO_printf(bio_err," -check check the DH parameters\n"); | |
d1f4c83c | 178 | BIO_printf(bio_err," -text print a text form of the DH parameters\n"); |
d02b48c6 RE |
179 | BIO_printf(bio_err," -C Output C code\n"); |
180 | BIO_printf(bio_err," -noout no output\n"); | |
0b13e9f0 | 181 | #ifndef OPENSSL_NO_ENGINE |
5270e702 | 182 | BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); |
0b13e9f0 | 183 | #endif |
d02b48c6 RE |
184 | goto end; |
185 | } | |
186 | ||
187 | ERR_load_crypto_strings(); | |
188 | ||
0b13e9f0 | 189 | #ifndef OPENSSL_NO_ENGINE |
e9735943 | 190 | setup_engine(bio_err, engine, 0); |
0b13e9f0 | 191 | #endif |
5270e702 | 192 | |
d02b48c6 RE |
193 | in=BIO_new(BIO_s_file()); |
194 | out=BIO_new(BIO_s_file()); | |
195 | if ((in == NULL) || (out == NULL)) | |
196 | { | |
197 | ERR_print_errors(bio_err); | |
198 | goto end; | |
199 | } | |
200 | ||
201 | if (infile == NULL) | |
202 | BIO_set_fp(in,stdin,BIO_NOCLOSE); | |
203 | else | |
204 | { | |
205 | if (BIO_read_filename(in,infile) <= 0) | |
206 | { | |
207 | perror(infile); | |
208 | goto end; | |
209 | } | |
210 | } | |
211 | if (outfile == NULL) | |
645749ef | 212 | { |
d02b48c6 | 213 | BIO_set_fp(out,stdout,BIO_NOCLOSE); |
bc36ee62 | 214 | #ifdef OPENSSL_SYS_VMS |
645749ef RL |
215 | { |
216 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); | |
217 | out = BIO_push(tmpbio, out); | |
218 | } | |
219 | #endif | |
220 | } | |
d02b48c6 RE |
221 | else |
222 | { | |
223 | if (BIO_write_filename(out,outfile) <= 0) | |
224 | { | |
225 | perror(outfile); | |
226 | goto end; | |
227 | } | |
228 | } | |
229 | ||
230 | if (informat == FORMAT_ASN1) | |
231 | dh=d2i_DHparams_bio(in,NULL); | |
232 | else if (informat == FORMAT_PEM) | |
74678cc2 | 233 | dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL); |
d02b48c6 RE |
234 | else |
235 | { | |
236 | BIO_printf(bio_err,"bad input format specified\n"); | |
237 | goto end; | |
238 | } | |
239 | if (dh == NULL) | |
240 | { | |
241 | BIO_printf(bio_err,"unable to load DH parameters\n"); | |
242 | ERR_print_errors(bio_err); | |
243 | goto end; | |
244 | } | |
245 | ||
246 | ||
247 | ||
248 | if (text) | |
249 | { | |
250 | DHparams_print(out,dh); | |
251 | #ifdef undef | |
252 | printf("p="); | |
253 | BN_print(stdout,dh->p); | |
254 | printf("\ng="); | |
255 | BN_print(stdout,dh->g); | |
256 | printf("\n"); | |
257 | if (dh->length != 0) | |
657e60fa | 258 | printf("recommended private length=%ld\n",dh->length); |
d02b48c6 RE |
259 | #endif |
260 | } | |
261 | ||
262 | if (check) | |
263 | { | |
264 | if (!DH_check(dh,&i)) | |
265 | { | |
266 | ERR_print_errors(bio_err); | |
267 | goto end; | |
268 | } | |
269 | if (i & DH_CHECK_P_NOT_PRIME) | |
270 | printf("p value is not prime\n"); | |
41918458 BM |
271 | if (i & DH_CHECK_P_NOT_SAFE_PRIME) |
272 | printf("p value is not a safe prime\n"); | |
d02b48c6 RE |
273 | if (i & DH_UNABLE_TO_CHECK_GENERATOR) |
274 | printf("unable to check the generator value\n"); | |
275 | if (i & DH_NOT_SUITABLE_GENERATOR) | |
276 | printf("the g value is not a generator\n"); | |
277 | if (i == 0) | |
278 | printf("DH parameters appear to be ok.\n"); | |
279 | } | |
280 | if (C) | |
281 | { | |
282 | unsigned char *data; | |
283 | int len,l,bits; | |
284 | ||
285 | len=BN_num_bytes(dh->p); | |
286 | bits=BN_num_bits(dh->p); | |
26a3a48d | 287 | data=(unsigned char *)OPENSSL_malloc(len); |
d02b48c6 RE |
288 | if (data == NULL) |
289 | { | |
26a3a48d | 290 | perror("OPENSSL_malloc"); |
d02b48c6 RE |
291 | goto end; |
292 | } | |
293 | l=BN_bn2bin(dh->p,data); | |
294 | printf("static unsigned char dh%d_p[]={",bits); | |
295 | for (i=0; i<l; i++) | |
296 | { | |
297 | if ((i%12) == 0) printf("\n\t"); | |
298 | printf("0x%02X,",data[i]); | |
299 | } | |
300 | printf("\n\t};\n"); | |
301 | ||
302 | l=BN_bn2bin(dh->g,data); | |
303 | printf("static unsigned char dh%d_g[]={",bits); | |
304 | for (i=0; i<l; i++) | |
305 | { | |
306 | if ((i%12) == 0) printf("\n\t"); | |
307 | printf("0x%02X,",data[i]); | |
308 | } | |
309 | printf("\n\t};\n\n"); | |
310 | ||
311 | printf("DH *get_dh%d()\n\t{\n",bits); | |
312 | printf("\tDH *dh;\n\n"); | |
313 | printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n"); | |
314 | printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n", | |
315 | bits,bits); | |
316 | printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n", | |
317 | bits,bits); | |
318 | printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n"); | |
319 | printf("\t\treturn(NULL);\n"); | |
320 | printf("\treturn(dh);\n\t}\n"); | |
26a3a48d | 321 | OPENSSL_free(data); |
d02b48c6 RE |
322 | } |
323 | ||
324 | ||
325 | if (!noout) | |
326 | { | |
327 | if (outformat == FORMAT_ASN1) | |
328 | i=i2d_DHparams_bio(out,dh); | |
329 | else if (outformat == FORMAT_PEM) | |
330 | i=PEM_write_bio_DHparams(out,dh); | |
331 | else { | |
332 | BIO_printf(bio_err,"bad output format specified for outfile\n"); | |
333 | goto end; | |
334 | } | |
335 | if (!i) | |
336 | { | |
657e60fa | 337 | BIO_printf(bio_err,"unable to write DH parameters\n"); |
d02b48c6 RE |
338 | ERR_print_errors(bio_err); |
339 | goto end; | |
340 | } | |
341 | } | |
342 | ret=0; | |
343 | end: | |
344 | if (in != NULL) BIO_free(in); | |
645749ef | 345 | if (out != NULL) BIO_free_all(out); |
d02b48c6 | 346 | if (dh != NULL) DH_free(dh); |
c04f8cf4 | 347 | apps_shutdown(); |
1c3e4a36 | 348 | OPENSSL_EXIT(ret); |
d02b48c6 | 349 | } |
7134507d DSH |
350 | #else /* !OPENSSL_NO_DH */ |
351 | ||
352 | # if PEDANTIC | |
353 | static void *dummy=&dummy; | |
354 | # endif | |
355 | ||
f5d7a031 | 356 | #endif |