dh.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* apps/dh.c */
  2. /* obsoleted by dhparam.c */
  3. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  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. #ifndef OPENSSL_NO_DH
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <time.h>
  63. #include <string.h>
  64. #include "apps.h"
  65. #include <openssl/bio.h>
  66. #include <openssl/err.h>
  67. #include <openssl/bn.h>
  68. #include <openssl/dh.h>
  69. #include <openssl/x509.h>
  70. #include <openssl/pem.h>
  71. #include <openssl/engine.h>
  72. #undef PROG
  73. #define PROG dh_main
  74. /* -inform arg - input format - default PEM (DER or PEM)
  75. * -outform arg - output format - default PEM
  76. * -in arg - input file - default stdin
  77. * -out arg - output file - default stdout
  78. * -check - check the parameters are ok
  79. * -noout
  80. * -text
  81. * -C
  82. */
  83. int MAIN(int, char **);
  84. int MAIN(int argc, char **argv)
  85. {
  86. ENGINE *e = NULL;
  87. DH *dh=NULL;
  88. int i,badops=0,text=0;
  89. BIO *in=NULL,*out=NULL;
  90. int informat,outformat,check=0,noout=0,C=0,ret=1;
  91. char *infile,*outfile,*prog,*engine;
  92. apps_startup();
  93. if (bio_err == NULL)
  94. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  95. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  96. engine=NULL;
  97. infile=NULL;
  98. outfile=NULL;
  99. informat=FORMAT_PEM;
  100. outformat=FORMAT_PEM;
  101. prog=argv[0];
  102. argc--;
  103. argv++;
  104. while (argc >= 1)
  105. {
  106. if (strcmp(*argv,"-inform") == 0)
  107. {
  108. if (--argc < 1) goto bad;
  109. informat=str2fmt(*(++argv));
  110. }
  111. else if (strcmp(*argv,"-outform") == 0)
  112. {
  113. if (--argc < 1) goto bad;
  114. outformat=str2fmt(*(++argv));
  115. }
  116. else if (strcmp(*argv,"-in") == 0)
  117. {
  118. if (--argc < 1) goto bad;
  119. infile= *(++argv);
  120. }
  121. else if (strcmp(*argv,"-out") == 0)
  122. {
  123. if (--argc < 1) goto bad;
  124. outfile= *(++argv);
  125. }
  126. else if (strcmp(*argv,"-engine") == 0)
  127. {
  128. if (--argc < 1) goto bad;
  129. engine= *(++argv);
  130. }
  131. else if (strcmp(*argv,"-check") == 0)
  132. check=1;
  133. else if (strcmp(*argv,"-text") == 0)
  134. text=1;
  135. else if (strcmp(*argv,"-C") == 0)
  136. C=1;
  137. else if (strcmp(*argv,"-noout") == 0)
  138. noout=1;
  139. else
  140. {
  141. BIO_printf(bio_err,"unknown option %s\n",*argv);
  142. badops=1;
  143. break;
  144. }
  145. argc--;
  146. argv++;
  147. }
  148. if (badops)
  149. {
  150. bad:
  151. BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
  152. BIO_printf(bio_err,"where options are\n");
  153. BIO_printf(bio_err," -inform arg input format - one of DER PEM\n");
  154. BIO_printf(bio_err," -outform arg output format - one of DER PEM\n");
  155. BIO_printf(bio_err," -in arg input file\n");
  156. BIO_printf(bio_err," -out arg output file\n");
  157. BIO_printf(bio_err," -check check the DH parameters\n");
  158. BIO_printf(bio_err," -text print a text form of the DH parameters\n");
  159. BIO_printf(bio_err," -C Output C code\n");
  160. BIO_printf(bio_err," -noout no output\n");
  161. BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
  162. goto end;
  163. }
  164. ERR_load_crypto_strings();
  165. if (engine != NULL)
  166. {
  167. if((e = ENGINE_by_id(engine)) == NULL)
  168. {
  169. BIO_printf(bio_err,"invalid engine \"%s\"\n",
  170. engine);
  171. goto end;
  172. }
  173. if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  174. {
  175. BIO_printf(bio_err,"can't use that engine\n");
  176. goto end;
  177. }
  178. BIO_printf(bio_err,"engine \"%s\" set.\n", engine);
  179. /* Free our "structural" reference. */
  180. ENGINE_free(e);
  181. }
  182. in=BIO_new(BIO_s_file());
  183. out=BIO_new(BIO_s_file());
  184. if ((in == NULL) || (out == NULL))
  185. {
  186. ERR_print_errors(bio_err);
  187. goto end;
  188. }
  189. if (infile == NULL)
  190. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  191. else
  192. {
  193. if (BIO_read_filename(in,infile) <= 0)
  194. {
  195. perror(infile);
  196. goto end;
  197. }
  198. }
  199. if (outfile == NULL)
  200. {
  201. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  202. #ifdef OPENSSL_SYS_VMS
  203. {
  204. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  205. out = BIO_push(tmpbio, out);
  206. }
  207. #endif
  208. }
  209. else
  210. {
  211. if (BIO_write_filename(out,outfile) <= 0)
  212. {
  213. perror(outfile);
  214. goto end;
  215. }
  216. }
  217. if (informat == FORMAT_ASN1)
  218. dh=d2i_DHparams_bio(in,NULL);
  219. else if (informat == FORMAT_PEM)
  220. dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL);
  221. else
  222. {
  223. BIO_printf(bio_err,"bad input format specified\n");
  224. goto end;
  225. }
  226. if (dh == NULL)
  227. {
  228. BIO_printf(bio_err,"unable to load DH parameters\n");
  229. ERR_print_errors(bio_err);
  230. goto end;
  231. }
  232. if (text)
  233. {
  234. DHparams_print(out,dh);
  235. #ifdef undef
  236. printf("p=");
  237. BN_print(stdout,dh->p);
  238. printf("\ng=");
  239. BN_print(stdout,dh->g);
  240. printf("\n");
  241. if (dh->length != 0)
  242. printf("recommended private length=%ld\n",dh->length);
  243. #endif
  244. }
  245. if (check)
  246. {
  247. if (!DH_check(dh,&i))
  248. {
  249. ERR_print_errors(bio_err);
  250. goto end;
  251. }
  252. if (i & DH_CHECK_P_NOT_PRIME)
  253. printf("p value is not prime\n");
  254. if (i & DH_CHECK_P_NOT_SAFE_PRIME)
  255. printf("p value is not a safe prime\n");
  256. if (i & DH_UNABLE_TO_CHECK_GENERATOR)
  257. printf("unable to check the generator value\n");
  258. if (i & DH_NOT_SUITABLE_GENERATOR)
  259. printf("the g value is not a generator\n");
  260. if (i == 0)
  261. printf("DH parameters appear to be ok.\n");
  262. }
  263. if (C)
  264. {
  265. unsigned char *data;
  266. int len,l,bits;
  267. len=BN_num_bytes(dh->p);
  268. bits=BN_num_bits(dh->p);
  269. data=(unsigned char *)OPENSSL_malloc(len);
  270. if (data == NULL)
  271. {
  272. perror("OPENSSL_malloc");
  273. goto end;
  274. }
  275. l=BN_bn2bin(dh->p,data);
  276. printf("static unsigned char dh%d_p[]={",bits);
  277. for (i=0; i<l; i++)
  278. {
  279. if ((i%12) == 0) printf("\n\t");
  280. printf("0x%02X,",data[i]);
  281. }
  282. printf("\n\t};\n");
  283. l=BN_bn2bin(dh->g,data);
  284. printf("static unsigned char dh%d_g[]={",bits);
  285. for (i=0; i<l; i++)
  286. {
  287. if ((i%12) == 0) printf("\n\t");
  288. printf("0x%02X,",data[i]);
  289. }
  290. printf("\n\t};\n\n");
  291. printf("DH *get_dh%d()\n\t{\n",bits);
  292. printf("\tDH *dh;\n\n");
  293. printf("\tif ((dh=DH_new()) == NULL) return(NULL);\n");
  294. printf("\tdh->p=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n",
  295. bits,bits);
  296. printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n",
  297. bits,bits);
  298. printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n");
  299. printf("\t\treturn(NULL);\n");
  300. printf("\treturn(dh);\n\t}\n");
  301. OPENSSL_free(data);
  302. }
  303. if (!noout)
  304. {
  305. if (outformat == FORMAT_ASN1)
  306. i=i2d_DHparams_bio(out,dh);
  307. else if (outformat == FORMAT_PEM)
  308. i=PEM_write_bio_DHparams(out,dh);
  309. else {
  310. BIO_printf(bio_err,"bad output format specified for outfile\n");
  311. goto end;
  312. }
  313. if (!i)
  314. {
  315. BIO_printf(bio_err,"unable to write DH parameters\n");
  316. ERR_print_errors(bio_err);
  317. goto end;
  318. }
  319. }
  320. ret=0;
  321. end:
  322. if (in != NULL) BIO_free(in);
  323. if (out != NULL) BIO_free_all(out);
  324. if (dh != NULL) DH_free(dh);
  325. EXIT(ret);
  326. }
  327. #endif