mark all block comments that need format preserving so that
[openssl.git] / apps / dhparam.c
index ea15ef32368f66abcf45b6575727f47d9d9c83c5..7982222abcc8355ac24bd0d0a8a15523db6510e4 100644 (file)
  *
  */
 
+#include <openssl/opensslconf.h>       /* for OPENSSL_NO_DH */
 #ifndef OPENSSL_NO_DH
 #include <stdio.h>
 #include <stdlib.h>
 #undef PROG
 #define PROG   dhparam_main
 
-#define DEFBITS        512
+#define DEFBITS        2048
 
-/* -inform arg - input format - default PEM (DER or PEM)
+/*-
+ * -inform arg - input format - default PEM (DER or PEM)
  * -outform arg - output format - default PEM
  * -in arg     - input file - default stdin
  * -out arg    - output file - default stdout
  * -C
  */
 
-static void MS_CALLBACK dh_cb(int p, int n, void *arg);
+static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb);
 
 int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
        {
-       ENGINE *e = NULL;
        DH *dh=NULL;
        int i,badops=0,text=0;
 #ifndef OPENSSL_NO_DSA
@@ -157,7 +158,10 @@ int MAIN(int argc, char **argv)
        BIO *in=NULL,*out=NULL;
        int informat,outformat,check=0,noout=0,C=0,ret=1;
        char *infile,*outfile,*prog;
-       char *inrand=NULL,*engine=NULL;
+       char *inrand=NULL;
+#ifndef OPENSSL_NO_ENGINE
+       char *engine=NULL;
+#endif
        int num = 0, g = 0;
 
        apps_startup();
@@ -199,11 +203,13 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        outfile= *(++argv);
                        }
+#ifndef OPENSSL_NO_ENGINE
                else if (strcmp(*argv,"-engine") == 0)
                        {
                        if (--argc < 1) goto bad;
                        engine= *(++argv);
                        }
+#endif
                else if (strcmp(*argv,"-check") == 0)
                        check=1;
                else if (strcmp(*argv,"-text") == 0)
@@ -248,8 +254,10 @@ bad:
                BIO_printf(bio_err," -C            Output C code\n");
                BIO_printf(bio_err," -2            generate parameters using  2 as the generator value\n");
                BIO_printf(bio_err," -5            generate parameters using  5 as the generator value\n");
-               BIO_printf(bio_err," numbits       number of bits in to generate (default 512)\n");
+               BIO_printf(bio_err," numbits       number of bits in to generate (default 2048)\n");
+#ifndef OPENSSL_NO_ENGINE
                BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
+#endif
                BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
                BIO_printf(bio_err,"               - load the file (or the files in the directory) into\n");
                BIO_printf(bio_err,"               the random number generator\n");
@@ -259,7 +267,9 @@ bad:
 
        ERR_load_crypto_strings();
 
-        e = setup_engine(bio_err, engine, 0);
+#ifndef OPENSSL_NO_ENGINE
+        setup_engine(bio_err, engine, 0);
+#endif
 
        if (g && !num)
                num = DEFBITS;
@@ -283,6 +293,15 @@ bad:
 
        if(num) {
 
+               BN_GENCB *cb;
+               cb = BN_GENCB_new();
+               if(!cb)
+                       {
+                       ERR_print_errors(bio_err);
+                       goto end;
+                       }
+
+               BN_GENCB_set(cb, dh_cb, bio_err);
                if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
                        {
                        BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
@@ -294,12 +313,14 @@ bad:
 #ifndef OPENSSL_NO_DSA
                if (dsaparam)
                        {
-                       DSA *dsa;
+                       DSA *dsa = DSA_new();
                        
                        BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num);
-                       dsa = DSA_generate_parameters(num, NULL, 0, NULL, NULL, dh_cb, bio_err);
-                       if (dsa == NULL)
+                       if(!dsa || !DSA_generate_parameters_ex(dsa, num,
+                                               NULL, 0, NULL, NULL, cb))
                                {
+                               if(dsa) DSA_free(dsa);
+                               BN_GENCB_free(cb);
                                ERR_print_errors(bio_err);
                                goto end;
                                }
@@ -308,6 +329,7 @@ bad:
                        DSA_free(dsa);
                        if (dh == NULL)
                                {
+                               BN_GENCB_free(cb);
                                ERR_print_errors(bio_err);
                                goto end;
                                }
@@ -315,17 +337,18 @@ bad:
                else
 #endif
                        {
+                       dh = DH_new();
                        BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g);
                        BIO_printf(bio_err,"This is going to take a long time\n");
-                       dh=DH_generate_parameters(num,g,dh_cb,bio_err);
-                       
-                       if (dh == NULL)
+                       if(!dh || !DH_generate_parameters_ex(dh, num, g, cb))
                                {
+                               BN_GENCB_free(cb);
                                ERR_print_errors(bio_err);
                                goto end;
                                }
                        }
 
+               BN_GENCB_free(cb);
                app_RAND_write_file(NULL, bio_err);
        } else {
 
@@ -501,7 +524,12 @@ bad:
                if      (outformat == FORMAT_ASN1)
                        i=i2d_DHparams_bio(out,dh);
                else if (outformat == FORMAT_PEM)
-                       i=PEM_write_bio_DHparams(out,dh);
+                       {
+                       if (dh->q)
+                               i=PEM_write_bio_DHxparams(out,dh);
+                       else
+                               i=PEM_write_bio_DHparams(out,dh);
+                       }
                else    {
                        BIO_printf(bio_err,"bad output format specified for outfile\n");
                        goto end;
@@ -519,11 +547,11 @@ end:
        if (out != NULL) BIO_free_all(out);
        if (dh != NULL) DH_free(dh);
        apps_shutdown();
-       EXIT(ret);
+       OPENSSL_EXIT(ret);
        }
 
 /* dh_cb is identical to dsa_cb in apps/dsaparam.c */
-static void MS_CALLBACK dh_cb(int p, int n, void *arg)
+static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb)
        {
        char c='*';
 
@@ -531,11 +559,15 @@ static void MS_CALLBACK dh_cb(int p, int n, void *arg)
        if (p == 1) c='+';
        if (p == 2) c='*';
        if (p == 3) c='\n';
-       BIO_write((BIO *)arg,&c,1);
-       (void)BIO_flush((BIO *)arg);
-#ifdef LINT
-       p=n;
-#endif
+       BIO_write(BN_GENCB_get_arg(cb),&c,1);
+       (void)BIO_flush(BN_GENCB_get_arg(cb));
+       return 1;
        }
 
+#else /* !OPENSSL_NO_DH */
+
+# if PEDANTIC
+static void *dummy=&dummy;
+# endif
+
 #endif