sl@0: /* apps/dhparam.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include /* for OPENSSL_NO_DH */ sl@0: #ifndef OPENSSL_NO_DH sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifndef OPENSSL_NO_DSA sl@0: #include sl@0: #endif sl@0: sl@0: #undef PROG sl@0: #define PROG dhparam_main sl@0: sl@0: #define DEFBITS 512 sl@0: sl@0: /* -inform arg - input format - default PEM (DER or PEM) sl@0: * -outform arg - output format - default PEM sl@0: * -in arg - input file - default stdin sl@0: * -out arg - output file - default stdout sl@0: * -dsaparam - read or generate DSA parameters, convert to DH sl@0: * -check - check the parameters are ok sl@0: * -noout sl@0: * -text sl@0: * -C sl@0: */ sl@0: sl@0: static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb); sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: ENGINE *e = NULL; sl@0: #endif sl@0: DH *dh=NULL; sl@0: int i,badops=0,text=0; sl@0: #ifndef OPENSSL_NO_DSA sl@0: int dsaparam=0; sl@0: #endif sl@0: BIO *in=NULL,*out=NULL; sl@0: int informat,outformat,check=0,noout=0,C=0,ret=1; sl@0: char *infile,*outfile,*prog; sl@0: char *inrand=NULL; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine=NULL; sl@0: #endif sl@0: int num = 0, g = 0; sl@0: sl@0: apps_startup(); sl@0: sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: sl@0: infile=NULL; sl@0: outfile=NULL; sl@0: informat=FORMAT_PEM; sl@0: outformat=FORMAT_PEM; sl@0: sl@0: prog=argv[0]; sl@0: argc--; sl@0: argv++; sl@0: while (argc >= 1) sl@0: { sl@0: if (strcmp(*argv,"-inform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: informat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-outform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: outformat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-in") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: infile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-out") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: outfile= *(++argv); sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: else if (strcmp(*argv,"-engine") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: engine= *(++argv); sl@0: } sl@0: #endif sl@0: else if (strcmp(*argv,"-check") == 0) sl@0: check=1; sl@0: else if (strcmp(*argv,"-text") == 0) sl@0: text=1; sl@0: #ifndef OPENSSL_NO_DSA sl@0: else if (strcmp(*argv,"-dsaparam") == 0) sl@0: dsaparam=1; sl@0: #endif sl@0: else if (strcmp(*argv,"-C") == 0) sl@0: C=1; sl@0: else if (strcmp(*argv,"-noout") == 0) sl@0: noout=1; sl@0: else if (strcmp(*argv,"-2") == 0) sl@0: g=2; sl@0: else if (strcmp(*argv,"-5") == 0) sl@0: g=5; sl@0: else if (strcmp(*argv,"-rand") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: inrand= *(++argv); sl@0: } sl@0: else if (((sscanf(*argv,"%d",&num) == 0) || (num <= 0))) sl@0: goto bad; sl@0: argv++; sl@0: argc--; sl@0: } sl@0: sl@0: if (badops) sl@0: { sl@0: bad: sl@0: BIO_printf(bio_err,"%s [options] [numbits]\n",prog); sl@0: BIO_printf(bio_err,"where options are\n"); sl@0: BIO_printf(bio_err," -inform arg input format - one of DER PEM\n"); sl@0: BIO_printf(bio_err," -outform arg output format - one of DER PEM\n"); sl@0: BIO_printf(bio_err," -in arg input file\n"); sl@0: BIO_printf(bio_err," -out arg output file\n"); sl@0: #ifndef OPENSSL_NO_DSA sl@0: BIO_printf(bio_err," -dsaparam read or generate DSA parameters, convert to DH\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -check check the DH parameters\n"); sl@0: BIO_printf(bio_err," -text print a text form of the DH parameters\n"); sl@0: BIO_printf(bio_err," -C Output C code\n"); sl@0: BIO_printf(bio_err," -2 generate parameters using 2 as the generator value\n"); sl@0: BIO_printf(bio_err," -5 generate parameters using 5 as the generator value\n"); sl@0: BIO_printf(bio_err," numbits number of bits in to generate (default 512)\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); sl@0: #endif sl@0: BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); sl@0: BIO_printf(bio_err," - load the file (or the files in the directory) into\n"); sl@0: BIO_printf(bio_err," the random number generator\n"); sl@0: BIO_printf(bio_err," -noout no output\n"); sl@0: goto end; sl@0: } sl@0: sl@0: ERR_load_crypto_strings(); sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine, 0); sl@0: #endif sl@0: sl@0: if (g && !num) sl@0: num = DEFBITS; sl@0: sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (dsaparam) sl@0: { sl@0: if (g) sl@0: { sl@0: BIO_printf(bio_err, "generator may not be chosen for DSA parameters\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: /* DH parameters */ sl@0: if (num && !g) sl@0: g = 2; sl@0: } sl@0: sl@0: if(num) { sl@0: sl@0: BN_GENCB cb; sl@0: BN_GENCB_set(&cb, dh_cb, bio_err); sl@0: if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL) sl@0: { sl@0: BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); sl@0: } sl@0: if (inrand != NULL) sl@0: BIO_printf(bio_err,"%ld semi-random bytes loaded\n", sl@0: app_RAND_load_files(inrand)); sl@0: sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (dsaparam) sl@0: { sl@0: DSA *dsa = DSA_new(); sl@0: sl@0: BIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\n",num); sl@0: if(!dsa || !DSA_generate_parameters_ex(dsa, num, sl@0: NULL, 0, NULL, NULL, &cb)) sl@0: { sl@0: if(dsa) DSA_free(dsa); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: dh = DSA_dup_DH(dsa); sl@0: DSA_free(dsa); sl@0: if (dh == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: dh = DH_new(); sl@0: BIO_printf(bio_err,"Generating DH parameters, %d bit long safe prime, generator %d\n",num,g); sl@0: BIO_printf(bio_err,"This is going to take a long time\n"); sl@0: if(!dh || !DH_generate_parameters_ex(dh, num, g, &cb)) sl@0: { sl@0: if(dh) DH_free(dh); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: app_RAND_write_file(NULL, bio_err); sl@0: } else { sl@0: sl@0: in=BIO_new(BIO_s_file()); sl@0: if (in == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: if (infile == NULL) sl@0: BIO_set_fp(in,stdin,BIO_NOCLOSE); sl@0: sl@0: else sl@0: { sl@0: if (BIO_read_filename(in,infile) <= 0) sl@0: { sl@0: perror(infile); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (informat != FORMAT_ASN1 && informat != FORMAT_PEM) sl@0: { sl@0: BIO_printf(bio_err,"bad input format specified\n"); sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (dsaparam) sl@0: { sl@0: DSA *dsa; sl@0: sl@0: if (informat == FORMAT_ASN1) sl@0: dsa=d2i_DSAparams_bio(in,NULL); sl@0: else /* informat == FORMAT_PEM */ sl@0: dsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL); sl@0: sl@0: if (dsa == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to load DSA parameters\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: dh = DSA_dup_DH(dsa); sl@0: DSA_free(dsa); sl@0: if (dh == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: if (informat == FORMAT_ASN1) sl@0: dh=d2i_DHparams_bio(in,NULL); sl@0: else /* informat == FORMAT_PEM */ sl@0: dh=PEM_read_bio_DHparams(in,NULL,NULL,NULL); sl@0: sl@0: if (dh == NULL) sl@0: { sl@0: BIO_printf(bio_err,"unable to load DH parameters\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: /* dh != NULL */ sl@0: } sl@0: sl@0: out=BIO_new(BIO_s_file()); sl@0: if (out == NULL) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: if (outfile == NULL) sl@0: { sl@0: BIO_set_fp(out,stdout,BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: } sl@0: else sl@0: { sl@0: if (BIO_write_filename(out,outfile) <= 0) sl@0: { sl@0: perror(outfile); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: sl@0: if (text) sl@0: { sl@0: DHparams_print(out,dh); sl@0: } sl@0: sl@0: if (check) sl@0: { sl@0: if (!DH_check(dh,&i)) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if (i & DH_CHECK_P_NOT_PRIME) sl@0: printf("p value is not prime\n"); sl@0: if (i & DH_CHECK_P_NOT_SAFE_PRIME) sl@0: printf("p value is not a safe prime\n"); sl@0: if (i & DH_UNABLE_TO_CHECK_GENERATOR) sl@0: printf("unable to check the generator value\n"); sl@0: if (i & DH_NOT_SUITABLE_GENERATOR) sl@0: printf("the g value is not a generator\n"); sl@0: if (i == 0) sl@0: printf("DH parameters appear to be ok.\n"); sl@0: sl@0: } sl@0: if (C) sl@0: { sl@0: unsigned char *data; sl@0: int len,l,bits; sl@0: sl@0: len=BN_num_bytes(dh->p); sl@0: bits=BN_num_bits(dh->p); sl@0: data=(unsigned char *)OPENSSL_malloc(len); sl@0: if (data == NULL) sl@0: { sl@0: perror("OPENSSL_malloc"); sl@0: goto end; sl@0: } sl@0: printf("#ifndef HEADER_DH_H\n" sl@0: "#include \n" sl@0: "#endif\n"); sl@0: printf("DH *get_dh%d()\n\t{\n",bits); sl@0: sl@0: l=BN_bn2bin(dh->p,data); sl@0: printf("\tstatic unsigned char dh%d_p[]={",bits); sl@0: for (i=0; ig,data); sl@0: printf("\tstatic unsigned char dh%d_g[]={",bits); sl@0: for (i=0; ip=BN_bin2bn(dh%d_p,sizeof(dh%d_p),NULL);\n", sl@0: bits,bits); sl@0: printf("\tdh->g=BN_bin2bn(dh%d_g,sizeof(dh%d_g),NULL);\n", sl@0: bits,bits); sl@0: printf("\tif ((dh->p == NULL) || (dh->g == NULL))\n"); sl@0: printf("\t\t{ DH_free(dh); return(NULL); }\n"); sl@0: if (dh->length) sl@0: printf("\tdh->length = %ld;\n", dh->length); sl@0: printf("\treturn(dh);\n\t}\n"); sl@0: sl@0: OPENSSL_free(data); sl@0: } sl@0: sl@0: sl@0: if (!noout) sl@0: { sl@0: if (outformat == FORMAT_ASN1) sl@0: i=i2d_DHparams_bio(out,dh); sl@0: else if (outformat == FORMAT_PEM) sl@0: i=PEM_write_bio_DHparams(out,dh); sl@0: else { sl@0: BIO_printf(bio_err,"bad output format specified for outfile\n"); sl@0: goto end; sl@0: } sl@0: if (!i) sl@0: { sl@0: BIO_printf(bio_err,"unable to write DH parameters\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: ret=0; sl@0: end: sl@0: if (in != NULL) BIO_free(in); sl@0: if (out != NULL) BIO_free_all(out); sl@0: if (dh != NULL) DH_free(dh); sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: /* dh_cb is identical to dsa_cb in apps/dsaparam.c */ sl@0: static int MS_CALLBACK dh_cb(int p, int n, BN_GENCB *cb) sl@0: { sl@0: char c='*'; sl@0: sl@0: if (p == 0) c='.'; sl@0: if (p == 1) c='+'; sl@0: if (p == 2) c='*'; sl@0: if (p == 3) c='\n'; sl@0: BIO_write(cb->arg,&c,1); sl@0: (void)BIO_flush(cb->arg); sl@0: #ifdef LINT sl@0: p=n; sl@0: #endif sl@0: return 1; sl@0: } sl@0: sl@0: #endif