From 8623f693d9a5c74f76311aebd29767e89467ecea Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Sun, 20 Jun 1999 17:36:11 +0000 Subject: [PATCH] New functions CONF_load_bio() and CONF_load_fp() to load a configuration file from a bio or fp. Added some more constification to the BN library. --- CHANGES | 5 +++ crypto/bn/bn.h | 12 ++--- crypto/bn/bn_lib.c | 2 +- crypto/bn/bn_mpi.c | 2 +- crypto/bn/bn_print.c | 8 ++-- crypto/conf/Makefile.ssl | 6 ++- crypto/conf/conf.c | 94 ++++++++++++++++++++++++++-------------- crypto/conf/conf.h | 9 +++- crypto/conf/conf_err.c | 2 + util/libeay.num | 2 + 10 files changed, 94 insertions(+), 48 deletions(-) diff --git a/CHANGES b/CHANGES index 0902e037ce..e5571b134f 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,11 @@ Changes between 0.9.3a and 0.9.4 + *) New functions CONF_load_bio() and CONF_load_fp() to allow a config + file to be loaded from a BIO or FILE pointer. The BIO version will + for example allow memory BIOs to contain config info. + [Steve Henson] + *) New function "CRYPTO_num_locks" that returns CRYPTO_NUM_LOCKS. Whoever hopes to achieve shared-library compatibility across versions must use this, not the compile-time macro. diff --git a/crypto/bn/bn.h b/crypto/bn/bn.h index ce5a4746e4..80421a24cc 100644 --- a/crypto/bn/bn.h +++ b/crypto/bn/bn.h @@ -323,9 +323,9 @@ void BN_init(BIGNUM *); void BN_clear_free(BIGNUM *a); BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b); BIGNUM *BN_bin2bn(const unsigned char *s,int len,BIGNUM *ret); -int BN_bn2bin(BIGNUM *a, unsigned char *to); +int BN_bn2bin(const BIGNUM *a, unsigned char *to); BIGNUM *BN_mpi2bn(unsigned char *s,int len,BIGNUM *ret); -int BN_bn2mpi(BIGNUM *a, unsigned char *to); +int BN_bn2mpi(const BIGNUM *a, unsigned char *to); int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); @@ -375,10 +375,10 @@ BIGNUM *BN_dup(const BIGNUM *a); int BN_ucmp(const BIGNUM *a, const BIGNUM *b); int BN_set_bit(BIGNUM *a, int n); int BN_clear_bit(BIGNUM *a, int n); -char * BN_bn2hex(BIGNUM *a); -char * BN_bn2dec(BIGNUM *a); -int BN_hex2bn(BIGNUM **a,char *str); -int BN_dec2bn(BIGNUM **a,char *str); +char * BN_bn2hex(const BIGNUM *a); +char * BN_bn2dec(const BIGNUM *a); +int BN_hex2bn(BIGNUM **a, const char *str); +int BN_dec2bn(BIGNUM **a, const char *str); int BN_gcd(BIGNUM *r,BIGNUM *in_a,BIGNUM *in_b,BN_CTX *ctx); BIGNUM *BN_mod_inverse(BIGNUM *ret,BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int strong,BIGNUM *add, diff --git a/crypto/bn/bn_lib.c b/crypto/bn/bn_lib.c index ee194c55b2..9c2c9d3238 100644 --- a/crypto/bn/bn_lib.c +++ b/crypto/bn/bn_lib.c @@ -629,7 +629,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) } /* ignore negative */ -int BN_bn2bin(BIGNUM *a, unsigned char *to) +int BN_bn2bin(const BIGNUM *a, unsigned char *to) { int n,i; BN_ULONG l; diff --git a/crypto/bn/bn_mpi.c b/crypto/bn/bn_mpi.c index 5d786d7b15..80e1dca6b7 100644 --- a/crypto/bn/bn_mpi.c +++ b/crypto/bn/bn_mpi.c @@ -60,7 +60,7 @@ #include "cryptlib.h" #include "bn_lcl.h" -int BN_bn2mpi(BIGNUM *a, unsigned char *d) +int BN_bn2mpi(const BIGNUM *a, unsigned char *d) { int bits; int num=0; diff --git a/crypto/bn/bn_print.c b/crypto/bn/bn_print.c index 91418a2351..2f5ab2617b 100644 --- a/crypto/bn/bn_print.c +++ b/crypto/bn/bn_print.c @@ -65,7 +65,7 @@ static const char *Hex="0123456789ABCDEF"; /* Must 'Free' the returned data */ -char *BN_bn2hex(BIGNUM *a) +char *BN_bn2hex(const BIGNUM *a) { int i,j,v,z=0; char *buf; @@ -100,7 +100,7 @@ err: } /* Must 'Free' the returned data */ -char *BN_bn2dec(BIGNUM *a) +char *BN_bn2dec(const BIGNUM *a) { int i=0,num; char *buf=NULL; @@ -154,7 +154,7 @@ err: return(buf); } -int BN_hex2bn(BIGNUM **bn, char *a) +int BN_hex2bn(BIGNUM **bn, const char *a) { BIGNUM *ret=NULL; BN_ULONG l=0; @@ -220,7 +220,7 @@ err: return(0); } -int BN_dec2bn(BIGNUM **bn, char *a) +int BN_dec2bn(BIGNUM **bn, const char *a) { BIGNUM *ret=NULL; BN_ULONG l=0; diff --git a/crypto/conf/Makefile.ssl b/crypto/conf/Makefile.ssl index c686b345a5..ab2e148be1 100644 --- a/crypto/conf/Makefile.ssl +++ b/crypto/conf/Makefile.ssl @@ -85,5 +85,7 @@ conf.o: ../../include/openssl/e_os.h ../../include/openssl/e_os2.h conf.o: ../../include/openssl/err.h ../../include/openssl/lhash.h conf.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h conf.o: ../../include/openssl/stack.h ../cryptlib.h conf_lcl.h -conf_err.o: ../../include/openssl/conf.h ../../include/openssl/err.h -conf_err.o: ../../include/openssl/lhash.h ../../include/openssl/stack.h +conf_err.o: ../../include/openssl/bio.h ../../include/openssl/conf.h +conf_err.o: ../../include/openssl/crypto.h ../../include/openssl/err.h +conf_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslv.h +conf_err.o: ../../include/openssl/stack.h diff --git a/crypto/conf/conf.c b/crypto/conf/conf.c index a55ecbb658..3a055fac3c 100644 --- a/crypto/conf/conf.c +++ b/crypto/conf/conf.c @@ -82,10 +82,48 @@ static CONF_VALUE *get_section(LHASH *conf,char *section); const char *CONF_version="CONF" OPENSSL_VERSION_PTEXT; -LHASH *CONF_load(LHASH *h, char *file, long *line) + +LHASH *CONF_load(LHASH *h, const char *file, long *line) { - LHASH *ret=NULL; + LHASH *ltmp; FILE *in=NULL; + +#ifdef VMS + in=fopen(file,"r"); +#else + in=fopen(file,"rb"); +#endif + if (in == NULL) + { + SYSerr(SYS_F_FOPEN,get_last_sys_error()); + ERR_set_error_data(BUF_strdup(file), + ERR_TXT_MALLOCED|ERR_TXT_STRING); + CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB); + return NULL; + } + + ltmp = CONF_load_fp(h, in, line); + fclose(in); + + return ltmp; +} + +LHASH *CONF_load_fp(LHASH *h, FILE *in, long *line) +{ + BIO *btmp; + LHASH *ltmp; + if(!(btmp = BIO_new_fp(in, BIO_NOCLOSE))) { + CONFerr(CONF_F_CONF_LOAD_FP,ERR_R_BUF_LIB); + return NULL; + } + ltmp = CONF_load_bio(h, btmp, line); + BIO_free(btmp); + return ltmp; +} + +LHASH *CONF_load_bio(LHASH *h, BIO *in, long *line) + { + LHASH *ret=NULL; #define BUFSIZE 512 char btmp[16]; int bufnum=0,i,ii; @@ -101,28 +139,14 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) if ((buff=BUF_MEM_new()) == NULL) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_BUF_LIB); - goto err; - } - -#ifdef VMS - in=fopen(file,"r"); -#else - in=fopen(file,"rb"); -#endif - if (in == NULL) - { - SYSerr(SYS_F_FOPEN,get_last_sys_error()); - ERR_set_error_data(BUF_strdup(file), - ERR_TXT_MALLOCED|ERR_TXT_STRING); - CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB); + CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB); goto err; } section=(char *)Malloc(10); if (section == NULL) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE); + CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE); goto err; } strcpy(section,"default"); @@ -131,7 +155,7 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) { if ((ret=lh_new(hash,cmp)) == NULL) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE); + CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE); goto err; } } @@ -141,7 +165,8 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) sv=new_section(ret,section); if (sv == NULL) { - CONFerr(CONF_F_CONF_LOAD,CONF_R_UNABLE_TO_CREATE_NEW_SECTION); + CONFerr(CONF_F_CONF_LOAD_BIO, + CONF_R_UNABLE_TO_CREATE_NEW_SECTION); goto err; } section_sk=(STACK *)sv->value; @@ -152,12 +177,12 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) again=0; if (!BUF_MEM_grow(buff,bufnum+BUFSIZE)) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_BUF_LIB); + CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB); goto err; } p= &(buff->data[bufnum]); *p='\0'; - fgets(p,BUFSIZE-1,in); + BIO_gets(in, p, BUFSIZE-1); p[BUFSIZE-1]='\0'; ii=i=strlen(p); if (i == 0) break; @@ -222,7 +247,8 @@ again: ss=p; goto again; } - CONFerr(CONF_F_CONF_LOAD,CONF_R_MISSING_CLOSE_SQUARE_BRACKET); + CONFerr(CONF_F_CONF_LOAD_BIO, + CONF_R_MISSING_CLOSE_SQUARE_BRACKET); goto err; } *end='\0'; @@ -231,7 +257,8 @@ again: sv=new_section(ret,section); if (sv == NULL) { - CONFerr(CONF_F_CONF_LOAD,CONF_R_UNABLE_TO_CREATE_NEW_SECTION); + CONFerr(CONF_F_CONF_LOAD_BIO, + CONF_R_UNABLE_TO_CREATE_NEW_SECTION); goto err; } section_sk=(STACK *)sv->value; @@ -253,7 +280,8 @@ again: p=eat_ws(end); if (*p != '=') { - CONFerr(CONF_F_CONF_LOAD,CONF_R_MISSING_EQUAL_SIGN); + CONFerr(CONF_F_CONF_LOAD_BIO, + CONF_R_MISSING_EQUAL_SIGN); goto err; } *end='\0'; @@ -269,7 +297,8 @@ again: if ((v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))) == NULL) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE); + CONFerr(CONF_F_CONF_LOAD_BIO, + ERR_R_MALLOC_FAILURE); goto err; } if (psection == NULL) psection=section; @@ -277,7 +306,8 @@ again: v->value=NULL; if (v->name == NULL) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE); + CONFerr(CONF_F_CONF_LOAD_BIO, + ERR_R_MALLOC_FAILURE); goto err; } strcpy(v->name,pname); @@ -290,7 +320,8 @@ again: tv=new_section(ret,psection); if (tv == NULL) { - CONFerr(CONF_F_CONF_LOAD,CONF_R_UNABLE_TO_CREATE_NEW_SECTION); + CONFerr(CONF_F_CONF_LOAD_BIO, + CONF_R_UNABLE_TO_CREATE_NEW_SECTION); goto err; } ts=(STACK *)tv->value; @@ -303,7 +334,8 @@ again: v->section=tv->section; if (!sk_push(ts,(char *)v)) { - CONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE); + CONFerr(CONF_F_CONF_LOAD_BIO, + ERR_R_MALLOC_FAILURE); goto err; } vv=(CONF_VALUE *)lh_insert(ret,(char *)v); @@ -319,7 +351,6 @@ again: } if (buff != NULL) BUF_MEM_free(buff); if (section != NULL) Free(section); - if (in != NULL) fclose(in); return(ret); err: if (buff != NULL) BUF_MEM_free(buff); @@ -327,7 +358,6 @@ err: if (line != NULL) *line=eline; sprintf(btmp,"%ld",eline); ERR_add_error_data(2,"line ",btmp); - if (in != NULL) fclose(in); if ((h != ret) && (ret != NULL)) CONF_free(ret); if (v != NULL) { @@ -337,7 +367,7 @@ err: } return(NULL); } - + char *CONF_get_string(LHASH *conf, char *section, char *name) { CONF_VALUE *v,vv; diff --git a/crypto/conf/conf.h b/crypto/conf/conf.h index 2401518a12..feb65f2a7d 100644 --- a/crypto/conf/conf.h +++ b/crypto/conf/conf.h @@ -63,8 +63,9 @@ extern "C" { #endif -#include +#include #include +#include typedef struct { @@ -74,7 +75,9 @@ typedef struct } CONF_VALUE; -LHASH *CONF_load(LHASH *conf,char *file,long *eline); +LHASH *CONF_load(LHASH *conf,const char *file,long *eline); +LHASH *CONF_load_fp(LHASH *conf, FILE *fp,long *eline); +LHASH *CONF_load_bio(LHASH *conf, BIO *bp,long *eline); STACK *CONF_get_section(LHASH *conf,char *section); char *CONF_get_string(LHASH *conf,char *group,char *name); long CONF_get_number(LHASH *conf,char *group,char *name); @@ -90,6 +93,8 @@ void ERR_load_CONF_strings(void ); /* Function codes. */ #define CONF_F_CONF_LOAD 100 +#define CONF_F_CONF_LOAD_BIO 102 +#define CONF_F_CONF_LOAD_FP 103 #define CONF_F_STR_COPY 101 /* Reason codes. */ diff --git a/crypto/conf/conf_err.c b/crypto/conf/conf_err.c index 340e42945c..eb4b3cfc70 100644 --- a/crypto/conf/conf_err.c +++ b/crypto/conf/conf_err.c @@ -66,6 +66,8 @@ static ERR_STRING_DATA CONF_str_functs[]= { {ERR_PACK(0,CONF_F_CONF_LOAD,0), "CONF_load"}, +{ERR_PACK(0,CONF_F_CONF_LOAD_BIO,0), "CONF_load_bio"}, +{ERR_PACK(0,CONF_F_CONF_LOAD_FP,0), "CONF_load_fp"}, {ERR_PACK(0,CONF_F_STR_COPY,0), "STR_COPY"}, {0,NULL} }; diff --git a/util/libeay.num b/util/libeay.num index 56bff6575b..3236c3ad4d 100755 --- a/util/libeay.num +++ b/util/libeay.num @@ -1777,3 +1777,5 @@ BIO_ctrl_wpending 1801 BIO_new_bio_pair 1802 BIO_ctrl_get_write_guarantee 1803 CRYPTO_num_locks 1804 +CONF_load_bio 1805 +CONF_load_fp 1806 -- 2.25.1