From b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 Mon Sep 17 00:00:00 2001 From: beck <> Date: Wed, 15 May 2002 02:29:21 +0000 Subject: OpenSSL 0.9.7 stable 2002 05 08 merge --- src/lib/libcrypto/dh/Makefile.ssl | 109 ++++++++++++++------------- src/lib/libcrypto/dh/dh.h | 60 +++++++-------- src/lib/libcrypto/dh/dh_asn1.c | 87 ++++++++++++++++++++++ src/lib/libcrypto/dh/dh_check.c | 2 +- src/lib/libcrypto/dh/dh_err.c | 9 ++- src/lib/libcrypto/dh/dh_gen.c | 32 ++++++-- src/lib/libcrypto/dh/dh_key.c | 59 ++++++++------- src/lib/libcrypto/dh/dh_lib.c | 150 +++++++++++++++++++------------------- src/lib/libcrypto/dh/dhtest.c | 30 ++++++-- 9 files changed, 330 insertions(+), 208 deletions(-) create mode 100644 src/lib/libcrypto/dh/dh_asn1.c (limited to 'src/lib/libcrypto/dh') diff --git a/src/lib/libcrypto/dh/Makefile.ssl b/src/lib/libcrypto/dh/Makefile.ssl index bf4b47ca9a..209e2bf39c 100644 --- a/src/lib/libcrypto/dh/Makefile.ssl +++ b/src/lib/libcrypto/dh/Makefile.ssl @@ -5,13 +5,14 @@ DIR= dh TOP= ../.. CC= cc -INCLUDES= -I.. -I../../include +INCLUDES= -I.. -I$(TOP) -I../../include CFLAG=-g INSTALL_PREFIX= OPENSSLDIR= /usr/local/ssl INSTALLTOP=/usr/local/ssl MAKE= make -f Makefile.ssl -MAKEDEPEND= $(TOP)/util/domd $(TOP) +MAKEDEPPROG= makedepend +MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) MAKEFILE= Makefile.ssl AR= ar r @@ -22,8 +23,8 @@ TEST= dhtest.c APPS= LIB=$(TOP)/libcrypto.a -LIBSRC= dh_gen.c dh_key.c dh_lib.c dh_check.c dh_err.c -LIBOBJ= dh_gen.o dh_key.o dh_lib.o dh_check.o dh_err.o +LIBSRC= dh_asn1.c dh_gen.c dh_key.c dh_lib.c dh_check.c dh_err.c +LIBOBJ= dh_asn1.o dh_gen.o dh_key.o dh_lib.o dh_check.o dh_err.o SRC= $(LIBSRC) @@ -39,8 +40,7 @@ all: lib lib: $(LIBOBJ) $(AR) $(LIB) $(LIBOBJ) - @echo You may get an error following this line. Please ignore. - - $(RANLIB) $(LIB) + $(RANLIB) $(LIB) || echo Never mind. @touch lib files: @@ -79,61 +79,58 @@ clean: # DO NOT DELETE THIS LINE -- make depend depends on it. -dh_check.o: ../../include/openssl/bio.h ../../include/openssl/bn.h +dh_asn1.o: ../../e_os.h ../../include/openssl/asn1.h +dh_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h +dh_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h +dh_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h +dh_asn1.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h +dh_asn1.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h +dh_asn1.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h +dh_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h +dh_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h +dh_asn1.o: ../../include/openssl/symhacks.h ../cryptlib.h dh_asn1.c +dh_check.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h dh_check.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h -dh_check.o: ../../include/openssl/dh.h ../../include/openssl/e_os.h -dh_check.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h -dh_check.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h -dh_check.o: ../../include/openssl/opensslv.h ../../include/openssl/safestack.h -dh_check.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h -dh_check.o: ../cryptlib.h +dh_check.o: ../../include/openssl/dh.h ../../include/openssl/e_os2.h +dh_check.o: ../../include/openssl/err.h ../../include/openssl/lhash.h +dh_check.o: ../../include/openssl/opensslconf.h +dh_check.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h +dh_check.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h +dh_check.o: ../../include/openssl/symhacks.h ../cryptlib.h dh_check.c dh_err.o: ../../include/openssl/bio.h ../../include/openssl/bn.h dh_err.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h -dh_err.o: ../../include/openssl/err.h ../../include/openssl/lhash.h -dh_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h +dh_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h +dh_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h +dh_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h dh_err.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h -dh_err.o: ../../include/openssl/symhacks.h -dh_gen.o: ../../include/openssl/bio.h ../../include/openssl/bn.h +dh_err.o: ../../include/openssl/symhacks.h dh_err.c +dh_gen.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h dh_gen.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h -dh_gen.o: ../../include/openssl/dh.h ../../include/openssl/e_os.h -dh_gen.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h -dh_gen.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h -dh_gen.o: ../../include/openssl/opensslv.h ../../include/openssl/safestack.h +dh_gen.o: ../../include/openssl/dh.h ../../include/openssl/e_os2.h +dh_gen.o: ../../include/openssl/err.h ../../include/openssl/lhash.h +dh_gen.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h +dh_gen.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h dh_gen.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h -dh_gen.o: ../cryptlib.h -dh_key.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h -dh_key.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h -dh_key.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h -dh_key.o: ../../include/openssl/crypto.h ../../include/openssl/des.h -dh_key.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h -dh_key.o: ../../include/openssl/e_os.h ../../include/openssl/e_os2.h +dh_gen.o: ../cryptlib.h dh_gen.c +dh_key.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h +dh_key.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h +dh_key.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h +dh_key.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h dh_key.o: ../../include/openssl/engine.h ../../include/openssl/err.h -dh_key.o: ../../include/openssl/evp.h ../../include/openssl/idea.h -dh_key.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h -dh_key.o: ../../include/openssl/md4.h ../../include/openssl/md5.h -dh_key.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h -dh_key.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h -dh_key.o: ../../include/openssl/opensslv.h ../../include/openssl/rand.h -dh_key.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h -dh_key.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h -dh_key.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h -dh_key.o: ../../include/openssl/sha.h ../../include/openssl/stack.h -dh_key.o: ../../include/openssl/symhacks.h ../cryptlib.h -dh_lib.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h -dh_lib.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h -dh_lib.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h -dh_lib.o: ../../include/openssl/crypto.h ../../include/openssl/des.h -dh_lib.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h -dh_lib.o: ../../include/openssl/e_os.h ../../include/openssl/e_os2.h +dh_key.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h +dh_key.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h +dh_key.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h +dh_key.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h +dh_key.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h +dh_key.o: ../cryptlib.h dh_key.c +dh_lib.o: ../../e_os.h ../../include/openssl/asn1.h ../../include/openssl/bio.h +dh_lib.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h +dh_lib.o: ../../include/openssl/crypto.h ../../include/openssl/dh.h +dh_lib.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h dh_lib.o: ../../include/openssl/engine.h ../../include/openssl/err.h -dh_lib.o: ../../include/openssl/evp.h ../../include/openssl/idea.h -dh_lib.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h -dh_lib.o: ../../include/openssl/md4.h ../../include/openssl/md5.h -dh_lib.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h -dh_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h -dh_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/rand.h -dh_lib.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h -dh_lib.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h -dh_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h -dh_lib.o: ../../include/openssl/sha.h ../../include/openssl/stack.h -dh_lib.o: ../../include/openssl/symhacks.h ../cryptlib.h +dh_lib.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h +dh_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h +dh_lib.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h +dh_lib.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h +dh_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h +dh_lib.o: ../cryptlib.h dh_lib.c diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h index 7a8d9f88c2..d51dc130f4 100644 --- a/src/lib/libcrypto/dh/dh.h +++ b/src/lib/libcrypto/dh/dh.h @@ -59,15 +59,16 @@ #ifndef HEADER_DH_H #define HEADER_DH_H -#ifdef NO_DH +#ifdef OPENSSL_NO_DH #error DH is disabled. #endif -#ifndef NO_BIO +#ifndef OPENSSL_NO_BIO #include #endif #include #include +#include #define DH_FLAG_CACHE_MONT_P 0x01 @@ -81,9 +82,9 @@ typedef struct dh_method { const char *name; /* Methods here */ int (*generate_key)(DH *dh); - int (*compute_key)(unsigned char *key,BIGNUM *pub_key,DH *dh); - int (*bn_mod_exp)(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p, - const BIGNUM *m, BN_CTX *ctx, + int (*compute_key)(unsigned char *key,const BIGNUM *pub_key,DH *dh); + int (*bn_mod_exp)(const DH *dh, BIGNUM *r, const BIGNUM *a, + const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); /* Can be null */ int (*init)(DH *dh); @@ -115,11 +116,8 @@ struct dh_st int references; CRYPTO_EX_DATA ex_data; -#if 0 - DH_METHOD *meth; -#else - struct engine_st *engine; -#endif + const DH_METHOD *meth; + ENGINE *engine; }; #define DH_GENERATOR_2 2 @@ -152,46 +150,42 @@ struct dh_st (unsigned char *)(x)) #endif -DH_METHOD *DH_OpenSSL(void); +const DH_METHOD *DH_OpenSSL(void); -void DH_set_default_openssl_method(DH_METHOD *meth); -DH_METHOD *DH_get_default_openssl_method(void); -#if 0 -DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth); -DH *DH_new_method(DH_METHOD *meth); -#else -int DH_set_method(DH *dh, struct engine_st *engine); -DH *DH_new_method(struct engine_st *engine); -#endif +void DH_set_default_method(const DH_METHOD *meth); +const DH_METHOD *DH_get_default_method(void); +int DH_set_method(DH *dh, const DH_METHOD *meth); +DH *DH_new_method(ENGINE *engine); DH * DH_new(void); void DH_free(DH *dh); -int DH_size(DH *dh); +int DH_up_ref(DH *dh); +int DH_size(const DH *dh); int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); int DH_set_ex_data(DH *d, int idx, void *arg); void *DH_get_ex_data(DH *d, int idx); DH * DH_generate_parameters(int prime_len,int generator, void (*callback)(int,int,void *),void *cb_arg); -int DH_check(DH *dh,int *codes); +int DH_check(const DH *dh,int *codes); int DH_generate_key(DH *dh); -int DH_compute_key(unsigned char *key,BIGNUM *pub_key,DH *dh); -DH * d2i_DHparams(DH **a,unsigned char **pp, long length); -int i2d_DHparams(DH *a,unsigned char **pp); -#ifndef NO_FP_API -int DHparams_print_fp(FILE *fp, DH *x); +int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); +DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); +int i2d_DHparams(const DH *a,unsigned char **pp); +#ifndef OPENSSL_NO_FP_API +int DHparams_print_fp(FILE *fp, const DH *x); #endif -#ifndef NO_BIO -int DHparams_print(BIO *bp, DH *x); +#ifndef OPENSSL_NO_BIO +int DHparams_print(BIO *bp, const DH *x); #else -int DHparams_print(char *bp, DH *x); +int DHparams_print(char *bp, const DH *x); #endif -void ERR_load_DH_strings(void ); /* BEGIN ERROR CODES */ /* The following lines are auto generated by the script mkerr.pl. Any changes * made after this point may be overwritten when the script is next run. */ +void ERR_load_DH_strings(void); /* Error codes for the DH functions. */ @@ -201,13 +195,13 @@ void ERR_load_DH_strings(void ); #define DH_F_DH_COMPUTE_KEY 102 #define DH_F_DH_GENERATE_KEY 103 #define DH_F_DH_GENERATE_PARAMETERS 104 -#define DH_F_DH_NEW 105 +#define DH_F_DH_NEW_METHOD 105 /* Reason codes. */ +#define DH_R_BAD_GENERATOR 101 #define DH_R_NO_PRIVATE_VALUE 100 #ifdef __cplusplus } #endif #endif - diff --git a/src/lib/libcrypto/dh/dh_asn1.c b/src/lib/libcrypto/dh/dh_asn1.c new file mode 100644 index 0000000000..769b5b68c5 --- /dev/null +++ b/src/lib/libcrypto/dh/dh_asn1.c @@ -0,0 +1,87 @@ +/* dh_asn1.c */ +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL + * project 2000. + */ +/* ==================================================================== + * Copyright (c) 2000 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * licensing@OpenSSL.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + +#include +#include "cryptlib.h" +#include +#include +#include +#include + +/* Override the default free and new methods */ +static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) +{ + if(operation == ASN1_OP_NEW_PRE) { + *pval = (ASN1_VALUE *)DH_new(); + if(*pval) return 2; + return 0; + } else if(operation == ASN1_OP_FREE_PRE) { + DH_free((DH *)*pval); + *pval = NULL; + return 2; + } + return 1; +} + +ASN1_SEQUENCE_cb(DHparams, dh_cb) = { + ASN1_SIMPLE(DH, p, BIGNUM), + ASN1_SIMPLE(DH, g, BIGNUM), + ASN1_OPT(DH, length, ZLONG), +} ASN1_SEQUENCE_END_cb(DH, DHparams) + +IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DH, DHparams, DHparams) diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c index 7e5cfd8bfc..f0373f7d68 100644 --- a/src/lib/libcrypto/dh/dh_check.c +++ b/src/lib/libcrypto/dh/dh_check.c @@ -70,7 +70,7 @@ * should hold. */ -int DH_check(DH *dh, int *ret) +int DH_check(const DH *dh, int *ret) { int ok=0; BN_CTX *ctx=NULL; diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c index ff2d1684c2..d837950aec 100644 --- a/src/lib/libcrypto/dh/dh_err.c +++ b/src/lib/libcrypto/dh/dh_err.c @@ -1,6 +1,6 @@ /* crypto/dh/dh_err.c */ /* ==================================================================== - * Copyright (c) 1999 The OpenSSL Project. All rights reserved. + * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -63,7 +63,7 @@ #include /* BEGIN ERROR CODES */ -#ifndef NO_ERR +#ifndef OPENSSL_NO_ERR static ERR_STRING_DATA DH_str_functs[]= { {ERR_PACK(0,DH_F_DHPARAMS_PRINT,0), "DHparams_print"}, @@ -71,12 +71,13 @@ static ERR_STRING_DATA DH_str_functs[]= {ERR_PACK(0,DH_F_DH_COMPUTE_KEY,0), "DH_compute_key"}, {ERR_PACK(0,DH_F_DH_GENERATE_KEY,0), "DH_generate_key"}, {ERR_PACK(0,DH_F_DH_GENERATE_PARAMETERS,0), "DH_generate_parameters"}, -{ERR_PACK(0,DH_F_DH_NEW,0), "DH_new"}, +{ERR_PACK(0,DH_F_DH_NEW_METHOD,0), "DH_new_method"}, {0,NULL} }; static ERR_STRING_DATA DH_str_reasons[]= { +{DH_R_BAD_GENERATOR ,"bad generator"}, {DH_R_NO_PRIVATE_VALUE ,"no private value"}, {0,NULL} }; @@ -90,7 +91,7 @@ void ERR_load_DH_strings(void) if (init) { init=0; -#ifndef NO_ERR +#ifndef OPENSSL_NO_ERR ERR_load_strings(ERR_LIB_DH,DH_str_functs); ERR_load_strings(ERR_LIB_DH,DH_str_reasons); #endif diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c index 7a6a38fbb4..06f78b35ab 100644 --- a/src/lib/libcrypto/dh/dh_gen.c +++ b/src/lib/libcrypto/dh/dh_gen.c @@ -82,7 +82,10 @@ * Since DH should be using a safe prime (both p and q are prime), * this generator function can take a very very long time to run. */ - +/* Actually there is no reason to insist that 'generator' be a generator. + * It's just as OK (and in some sense better) to use a generator of the + * order-q subgroup. + */ DH *DH_generate_parameters(int prime_len, int generator, void (*callback)(int,int,void *), void *cb_arg) { @@ -100,30 +103,43 @@ DH *DH_generate_parameters(int prime_len, int generator, t2 = BN_CTX_get(ctx); if (t1 == NULL || t2 == NULL) goto err; + if (generator <= 1) + { + DHerr(DH_F_DH_GENERATE_PARAMETERS, DH_R_BAD_GENERATOR); + goto err; + } if (generator == DH_GENERATOR_2) { - BN_set_word(t1,24); - BN_set_word(t2,11); + if (!BN_set_word(t1,24)) goto err; + if (!BN_set_word(t2,11)) goto err; g=2; } -#ifdef undef /* does not work for safe primes */ +#if 0 /* does not work for safe primes */ else if (generator == DH_GENERATOR_3) { - BN_set_word(t1,12); - BN_set_word(t2,5); + if (!BN_set_word(t1,12)) goto err; + if (!BN_set_word(t2,5)) goto err; g=3; } #endif else if (generator == DH_GENERATOR_5) { - BN_set_word(t1,10); - BN_set_word(t2,3); + if (!BN_set_word(t1,10)) goto err; + if (!BN_set_word(t2,3)) goto err; /* BN_set_word(t3,7); just have to miss * out on these ones :-( */ g=5; } else + { + /* in the general case, don't worry if 'generator' is a + * generator or not: since we are using safe primes, + * it will generate either an order-q or an order-2q group, + * which both is OK */ + if (!BN_set_word(t1,2)) goto err; + if (!BN_set_word(t2,1)) goto err; g=generator; + } p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg); if (p == NULL) goto err; diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c index 22b087b778..1a0efca2c4 100644 --- a/src/lib/libcrypto/dh/dh_key.c +++ b/src/lib/libcrypto/dh/dh_key.c @@ -64,8 +64,9 @@ #include static int generate_key(DH *dh); -static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh); -static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p, +static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); +static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, + const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); static int dh_init(DH *dh); @@ -73,12 +74,12 @@ static int dh_finish(DH *dh); int DH_generate_key(DH *dh) { - return ENGINE_get_DH(dh->engine)->generate_key(dh); + return dh->meth->generate_key(dh); } -int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) +int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) { - return ENGINE_get_DH(dh->engine)->compute_key(key, pub_key, dh); + return dh->meth->compute_key(key, pub_key, dh); } static DH_METHOD dh_ossl = { @@ -92,7 +93,7 @@ dh_finish, NULL }; -DH_METHOD *DH_OpenSSL(void) +const DH_METHOD *DH_OpenSSL(void) { return &dh_ossl; } @@ -100,19 +101,20 @@ DH_METHOD *DH_OpenSSL(void) static int generate_key(DH *dh) { int ok=0; - BN_CTX ctx; + int generate_new_key=0; + unsigned l; + BN_CTX *ctx; BN_MONT_CTX *mont; BIGNUM *pub_key=NULL,*priv_key=NULL; - BN_CTX_init(&ctx); + ctx = BN_CTX_new(); + if (ctx == NULL) goto err; if (dh->priv_key == NULL) { priv_key=BN_new(); if (priv_key == NULL) goto err; - do - if (!BN_rand_range(priv_key, dh->p)) goto err; - while (BN_is_zero(priv_key)); + generate_new_key=1; } else priv_key=dh->priv_key; @@ -129,12 +131,16 @@ static int generate_key(DH *dh) { if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL) if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p, - dh->p,&ctx)) goto err; + dh->p,ctx)) goto err; } mont=(BN_MONT_CTX *)dh->method_mont_p; - if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, pub_key, dh->g, - priv_key,dh->p,&ctx,mont)) + if (generate_new_key) + { + l = dh->length ? dh->length : BN_num_bits(dh->p)-1; /* secret exponent length */ + if (!BN_rand(priv_key, l, 0, 0)) goto err; + } + if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, priv_key,dh->p,ctx,mont)) goto err; dh->pub_key=pub_key; @@ -146,20 +152,21 @@ err: if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key); if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key); - BN_CTX_free(&ctx); + BN_CTX_free(ctx); return(ok); } -static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) +static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) { - BN_CTX ctx; + BN_CTX *ctx; BN_MONT_CTX *mont; BIGNUM *tmp; int ret= -1; - BN_CTX_init(&ctx); - BN_CTX_start(&ctx); - tmp = BN_CTX_get(&ctx); + ctx = BN_CTX_new(); + if (ctx == NULL) goto err; + BN_CTX_start(ctx); + tmp = BN_CTX_get(ctx); if (dh->priv_key == NULL) { @@ -170,12 +177,11 @@ static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) { if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL) if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p, - dh->p,&ctx)) goto err; + dh->p,ctx)) goto err; } mont=(BN_MONT_CTX *)dh->method_mont_p; - if (!ENGINE_get_DH(dh->engine)->bn_mod_exp(dh, tmp, pub_key, - dh->priv_key,dh->p,&ctx,mont)) + if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) { DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); goto err; @@ -183,12 +189,13 @@ static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) ret=BN_bn2bin(tmp,key); err: - BN_CTX_end(&ctx); - BN_CTX_free(&ctx); + BN_CTX_end(ctx); + BN_CTX_free(ctx); return(ret); } -static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p, +static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, + const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c index 96f118c153..ba5fd41057 100644 --- a/src/lib/libcrypto/dh/dh_lib.c +++ b/src/lib/libcrypto/dh/dh_lib.c @@ -64,95 +64,78 @@ const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; -static DH_METHOD *default_DH_method; -static int dh_meth_num = 0; -static STACK_OF(CRYPTO_EX_DATA_FUNCS) *dh_meth = NULL; - -void DH_set_default_openssl_method(DH_METHOD *meth) -{ - ENGINE *e; - /* We'll need to notify the "openssl" ENGINE of this - * change too. We won't bother locking things down at - * our end as there was never any locking in these - * functions! */ - if(default_DH_method != meth) - { - default_DH_method = meth; - e = ENGINE_by_id("openssl"); - if(e) - { - ENGINE_set_DH(e, meth); - ENGINE_free(e); - } - } -} +static const DH_METHOD *default_DH_method = NULL; + +void DH_set_default_method(const DH_METHOD *meth) + { + default_DH_method = meth; + } -DH_METHOD *DH_get_default_openssl_method(void) -{ - if(!default_DH_method) default_DH_method = DH_OpenSSL(); +const DH_METHOD *DH_get_default_method(void) + { + if(!default_DH_method) + default_DH_method = DH_OpenSSL(); return default_DH_method; -} + } -#if 0 -DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth) -{ - DH_METHOD *mtmp; +int DH_set_method(DH *dh, const DH_METHOD *meth) + { + /* NB: The caller is specifically setting a method, so it's not up to us + * to deal with which ENGINE it comes from. */ + const DH_METHOD *mtmp; mtmp = dh->meth; if (mtmp->finish) mtmp->finish(dh); + if (dh->engine) + { + ENGINE_finish(dh->engine); + dh->engine = NULL; + } dh->meth = meth; if (meth->init) meth->init(dh); - return mtmp; -} -#else -int DH_set_method(DH *dh, ENGINE *engine) -{ - ENGINE *mtmp; - DH_METHOD *meth; - mtmp = dh->engine; - meth = ENGINE_get_DH(mtmp); - if (!ENGINE_init(engine)) - return 0; - if (meth->finish) meth->finish(dh); - dh->engine= engine; - meth = ENGINE_get_DH(engine); - if (meth->init) meth->init(dh); - /* SHOULD ERROR CHECK THIS!!! */ - ENGINE_finish(mtmp); - return 1; -} -#endif + return 1; + } DH *DH_new(void) -{ + { return DH_new_method(NULL); -} + } -#if 0 -DH *DH_new_method(DH_METHOD *meth) -#else DH *DH_new_method(ENGINE *engine) -#endif { - DH_METHOD *meth; DH *ret; - ret=(DH *)OPENSSL_malloc(sizeof(DH)); + ret=(DH *)OPENSSL_malloc(sizeof(DH)); if (ret == NULL) { - DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE); + DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE); return(NULL); } - if(engine) + + ret->meth = DH_get_default_method(); + if (engine) + { + if (!ENGINE_init(engine)) + { + DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); + OPENSSL_free(ret); + return NULL; + } ret->engine = engine; + } else + ret->engine = ENGINE_get_default_DH(); + if(ret->engine) { - if((ret->engine=ENGINE_get_default_DH()) == NULL) + ret->meth = ENGINE_get_DH(ret->engine); + if(!ret->meth) { + DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB); + ENGINE_finish(ret->engine); OPENSSL_free(ret); return NULL; } } - meth = ENGINE_get_DH(ret->engine); + ret->pad=0; ret->version=0; ret->p=NULL; @@ -167,11 +150,13 @@ DH *DH_new_method(ENGINE *engine) ret->counter = NULL; ret->method_mont_p=NULL; ret->references = 1; - ret->flags=meth->flags; - CRYPTO_new_ex_data(dh_meth,ret,&ret->ex_data); - if ((meth->init != NULL) && !meth->init(ret)) + ret->flags=ret->meth->flags; + CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); + if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { - CRYPTO_free_ex_data(dh_meth,ret,&ret->ex_data); + if (ret->engine) + ENGINE_finish(ret->engine); + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); OPENSSL_free(ret); ret=NULL; } @@ -180,7 +165,6 @@ DH *DH_new_method(ENGINE *engine) void DH_free(DH *r) { - DH_METHOD *meth; int i; if(r == NULL) return; i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); @@ -196,11 +180,12 @@ void DH_free(DH *r) } #endif - meth = ENGINE_get_DH(r->engine); - if(meth->finish) meth->finish(r); - ENGINE_finish(r->engine); + if (r->meth->finish) + r->meth->finish(r); + if (r->engine) + ENGINE_finish(r->engine); - CRYPTO_free_ex_data(dh_meth, r, &r->ex_data); + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); if (r->p != NULL) BN_clear_free(r->p); if (r->g != NULL) BN_clear_free(r->g); @@ -213,12 +198,27 @@ void DH_free(DH *r) OPENSSL_free(r); } +int DH_up_ref(DH *r) + { + int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH); +#ifdef REF_PRINT + REF_PRINT("DH",r); +#endif +#ifdef REF_CHECK + if (i < 2) + { + fprintf(stderr, "DH_up, bad reference count\n"); + abort(); + } +#endif + return ((i > 1) ? 1 : 0); + } + int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) { - dh_meth_num++; - return(CRYPTO_get_ex_new_index(dh_meth_num-1, - &dh_meth,argl,argp,new_func,dup_func,free_func)); + return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, + new_func, dup_func, free_func); } int DH_set_ex_data(DH *d, int idx, void *arg) @@ -231,7 +231,7 @@ void *DH_get_ex_data(DH *d, int idx) return(CRYPTO_get_ex_data(&d->ex_data,idx)); } -int DH_size(DH *dh) +int DH_size(const DH *dh) { return(BN_num_bytes(dh->p)); } diff --git a/src/lib/libcrypto/dh/dhtest.c b/src/lib/libcrypto/dh/dhtest.c index f0151253d7..34894ced73 100644 --- a/src/lib/libcrypto/dh/dhtest.c +++ b/src/lib/libcrypto/dh/dhtest.c @@ -59,15 +59,16 @@ #include #include #include -#ifdef WINDOWS +#ifdef OPENSSL_SYS_WINDOWS #include "../bio/bss_file.c" #endif #include #include #include #include +#include -#ifdef NO_DH +#ifdef OPENSSL_NO_DH int main(int argc, char *argv[]) { printf("No DH support\n"); @@ -76,14 +77,14 @@ int main(int argc, char *argv[]) #else #include -#ifdef WIN16 +#ifdef OPENSSL_SYS_WIN16 #define MS_CALLBACK _far _loadds #else #define MS_CALLBACK #endif static void MS_CALLBACK cb(int p, int n, void *arg); -#ifdef NO_STDIO +#ifdef OPENSSL_NO_STDIO #define APPS_WIN16 #include "bss_file.c" #endif @@ -99,7 +100,11 @@ int main(int argc, char *argv[]) int i,alen,blen,aout,bout,ret=1; BIO *out; -#ifdef WIN32 + CRYPTO_malloc_debug_init(); + CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); + CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); + +#ifdef OPENSSL_SYS_WIN32 CRYPTO_malloc_init(); #endif @@ -112,6 +117,16 @@ int main(int argc, char *argv[]) a=DH_generate_parameters(64,DH_GENERATOR_5,cb,out); if (a == NULL) goto err; + if (!DH_check(a, &i)) goto err; + if (i & DH_CHECK_P_NOT_PRIME) + BIO_puts(out, "p value is not prime\n"); + if (i & DH_CHECK_P_NOT_SAFE_PRIME) + BIO_puts(out, "p value is not a safe prime\n"); + if (i & DH_UNABLE_TO_CHECK_GENERATOR) + BIO_puts(out, "unable to check the generator value\n"); + if (i & DH_NOT_SUITABLE_GENERATOR) + BIO_puts(out, "the g value is not a generator\n"); + BIO_puts(out,"\np ="); BN_print(out,a->p); BIO_puts(out,"\ng ="); @@ -170,11 +185,16 @@ int main(int argc, char *argv[]) else ret=0; err: + ERR_print_errors_fp(stderr); + if (abuf != NULL) OPENSSL_free(abuf); if (bbuf != NULL) OPENSSL_free(bbuf); if(b != NULL) DH_free(b); if(a != NULL) DH_free(a); BIO_free(out); + CRYPTO_cleanup_all_ex_data(); + ERR_remove_state(0); + CRYPTO_mem_leaks_fp(stderr); exit(ret); return(ret); } -- cgit v1.2.3-55-g6feb