提交 8623f693 编写于 作者: D Dr. Stephen Henson

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.
上级 11af1a27
...@@ -4,6 +4,11 @@ ...@@ -4,6 +4,11 @@
Changes between 0.9.3a and 0.9.4 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. *) New function "CRYPTO_num_locks" that returns CRYPTO_NUM_LOCKS.
Whoever hopes to achieve shared-library compatibility across versions Whoever hopes to achieve shared-library compatibility across versions
must use this, not the compile-time macro. must use this, not the compile-time macro.
......
...@@ -323,9 +323,9 @@ void BN_init(BIGNUM *); ...@@ -323,9 +323,9 @@ void BN_init(BIGNUM *);
void BN_clear_free(BIGNUM *a); void BN_clear_free(BIGNUM *a);
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b); BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
BIGNUM *BN_bin2bn(const unsigned char *s,int len,BIGNUM *ret); 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); 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_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
int BN_usub(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); int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
...@@ -375,10 +375,10 @@ BIGNUM *BN_dup(const BIGNUM *a); ...@@ -375,10 +375,10 @@ BIGNUM *BN_dup(const BIGNUM *a);
int BN_ucmp(const BIGNUM *a, const BIGNUM *b); int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
int BN_set_bit(BIGNUM *a, int n); int BN_set_bit(BIGNUM *a, int n);
int BN_clear_bit(BIGNUM *a, int n); int BN_clear_bit(BIGNUM *a, int n);
char * BN_bn2hex(BIGNUM *a); char * BN_bn2hex(const BIGNUM *a);
char * BN_bn2dec(BIGNUM *a); char * BN_bn2dec(const BIGNUM *a);
int BN_hex2bn(BIGNUM **a,char *str); int BN_hex2bn(BIGNUM **a, const char *str);
int BN_dec2bn(BIGNUM **a,char *str); int BN_dec2bn(BIGNUM **a, const char *str);
int BN_gcd(BIGNUM *r,BIGNUM *in_a,BIGNUM *in_b,BN_CTX *ctx); 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_mod_inverse(BIGNUM *ret,BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int strong,BIGNUM *add, BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int strong,BIGNUM *add,
......
...@@ -629,7 +629,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) ...@@ -629,7 +629,7 @@ BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
} }
/* ignore negative */ /* ignore negative */
int BN_bn2bin(BIGNUM *a, unsigned char *to) int BN_bn2bin(const BIGNUM *a, unsigned char *to)
{ {
int n,i; int n,i;
BN_ULONG l; BN_ULONG l;
......
...@@ -60,7 +60,7 @@ ...@@ -60,7 +60,7 @@
#include "cryptlib.h" #include "cryptlib.h"
#include "bn_lcl.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 bits;
int num=0; int num=0;
......
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
static const char *Hex="0123456789ABCDEF"; static const char *Hex="0123456789ABCDEF";
/* Must 'Free' the returned data */ /* Must 'Free' the returned data */
char *BN_bn2hex(BIGNUM *a) char *BN_bn2hex(const BIGNUM *a)
{ {
int i,j,v,z=0; int i,j,v,z=0;
char *buf; char *buf;
...@@ -100,7 +100,7 @@ err: ...@@ -100,7 +100,7 @@ err:
} }
/* Must 'Free' the returned data */ /* Must 'Free' the returned data */
char *BN_bn2dec(BIGNUM *a) char *BN_bn2dec(const BIGNUM *a)
{ {
int i=0,num; int i=0,num;
char *buf=NULL; char *buf=NULL;
...@@ -154,7 +154,7 @@ err: ...@@ -154,7 +154,7 @@ err:
return(buf); return(buf);
} }
int BN_hex2bn(BIGNUM **bn, char *a) int BN_hex2bn(BIGNUM **bn, const char *a)
{ {
BIGNUM *ret=NULL; BIGNUM *ret=NULL;
BN_ULONG l=0; BN_ULONG l=0;
...@@ -220,7 +220,7 @@ err: ...@@ -220,7 +220,7 @@ err:
return(0); return(0);
} }
int BN_dec2bn(BIGNUM **bn, char *a) int BN_dec2bn(BIGNUM **bn, const char *a)
{ {
BIGNUM *ret=NULL; BIGNUM *ret=NULL;
BN_ULONG l=0; BN_ULONG l=0;
......
...@@ -85,5 +85,7 @@ conf.o: ../../include/openssl/e_os.h ../../include/openssl/e_os2.h ...@@ -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/err.h ../../include/openssl/lhash.h
conf.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h conf.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
conf.o: ../../include/openssl/stack.h ../cryptlib.h conf_lcl.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/bio.h ../../include/openssl/conf.h
conf_err.o: ../../include/openssl/lhash.h ../../include/openssl/stack.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
...@@ -82,10 +82,48 @@ static CONF_VALUE *get_section(LHASH *conf,char *section); ...@@ -82,10 +82,48 @@ static CONF_VALUE *get_section(LHASH *conf,char *section);
const char *CONF_version="CONF" OPENSSL_VERSION_PTEXT; 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; 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 #define BUFSIZE 512
char btmp[16]; char btmp[16];
int bufnum=0,i,ii; int bufnum=0,i,ii;
...@@ -101,28 +139,14 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) ...@@ -101,28 +139,14 @@ LHASH *CONF_load(LHASH *h, char *file, long *line)
if ((buff=BUF_MEM_new()) == NULL) if ((buff=BUF_MEM_new()) == NULL)
{ {
CONFerr(CONF_F_CONF_LOAD,ERR_R_BUF_LIB); CONFerr(CONF_F_CONF_LOAD_BIO,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);
goto err; goto err;
} }
section=(char *)Malloc(10); section=(char *)Malloc(10);
if (section == NULL) if (section == NULL)
{ {
CONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE); CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
goto err; goto err;
} }
strcpy(section,"default"); strcpy(section,"default");
...@@ -131,7 +155,7 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) ...@@ -131,7 +155,7 @@ LHASH *CONF_load(LHASH *h, char *file, long *line)
{ {
if ((ret=lh_new(hash,cmp)) == NULL) 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; goto err;
} }
} }
...@@ -141,7 +165,8 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) ...@@ -141,7 +165,8 @@ LHASH *CONF_load(LHASH *h, char *file, long *line)
sv=new_section(ret,section); sv=new_section(ret,section);
if (sv == NULL) 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; goto err;
} }
section_sk=(STACK *)sv->value; section_sk=(STACK *)sv->value;
...@@ -152,12 +177,12 @@ LHASH *CONF_load(LHASH *h, char *file, long *line) ...@@ -152,12 +177,12 @@ LHASH *CONF_load(LHASH *h, char *file, long *line)
again=0; again=0;
if (!BUF_MEM_grow(buff,bufnum+BUFSIZE)) 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; goto err;
} }
p= &(buff->data[bufnum]); p= &(buff->data[bufnum]);
*p='\0'; *p='\0';
fgets(p,BUFSIZE-1,in); BIO_gets(in, p, BUFSIZE-1);
p[BUFSIZE-1]='\0'; p[BUFSIZE-1]='\0';
ii=i=strlen(p); ii=i=strlen(p);
if (i == 0) break; if (i == 0) break;
...@@ -222,7 +247,8 @@ again: ...@@ -222,7 +247,8 @@ again:
ss=p; ss=p;
goto again; 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; goto err;
} }
*end='\0'; *end='\0';
...@@ -231,7 +257,8 @@ again: ...@@ -231,7 +257,8 @@ again:
sv=new_section(ret,section); sv=new_section(ret,section);
if (sv == NULL) 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; goto err;
} }
section_sk=(STACK *)sv->value; section_sk=(STACK *)sv->value;
...@@ -253,7 +280,8 @@ again: ...@@ -253,7 +280,8 @@ again:
p=eat_ws(end); p=eat_ws(end);
if (*p != '=') 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; goto err;
} }
*end='\0'; *end='\0';
...@@ -269,7 +297,8 @@ again: ...@@ -269,7 +297,8 @@ again:
if ((v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))) == NULL) 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; goto err;
} }
if (psection == NULL) psection=section; if (psection == NULL) psection=section;
...@@ -277,7 +306,8 @@ again: ...@@ -277,7 +306,8 @@ again:
v->value=NULL; v->value=NULL;
if (v->name == 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; goto err;
} }
strcpy(v->name,pname); strcpy(v->name,pname);
...@@ -290,7 +320,8 @@ again: ...@@ -290,7 +320,8 @@ again:
tv=new_section(ret,psection); tv=new_section(ret,psection);
if (tv == NULL) 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; goto err;
} }
ts=(STACK *)tv->value; ts=(STACK *)tv->value;
...@@ -303,7 +334,8 @@ again: ...@@ -303,7 +334,8 @@ again:
v->section=tv->section; v->section=tv->section;
if (!sk_push(ts,(char *)v)) 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; goto err;
} }
vv=(CONF_VALUE *)lh_insert(ret,(char *)v); vv=(CONF_VALUE *)lh_insert(ret,(char *)v);
...@@ -319,7 +351,6 @@ again: ...@@ -319,7 +351,6 @@ again:
} }
if (buff != NULL) BUF_MEM_free(buff); if (buff != NULL) BUF_MEM_free(buff);
if (section != NULL) Free(section); if (section != NULL) Free(section);
if (in != NULL) fclose(in);
return(ret); return(ret);
err: err:
if (buff != NULL) BUF_MEM_free(buff); if (buff != NULL) BUF_MEM_free(buff);
...@@ -327,7 +358,6 @@ err: ...@@ -327,7 +358,6 @@ err:
if (line != NULL) *line=eline; if (line != NULL) *line=eline;
sprintf(btmp,"%ld",eline); sprintf(btmp,"%ld",eline);
ERR_add_error_data(2,"line ",btmp); ERR_add_error_data(2,"line ",btmp);
if (in != NULL) fclose(in);
if ((h != ret) && (ret != NULL)) CONF_free(ret); if ((h != ret) && (ret != NULL)) CONF_free(ret);
if (v != NULL) if (v != NULL)
{ {
...@@ -337,7 +367,7 @@ err: ...@@ -337,7 +367,7 @@ err:
} }
return(NULL); return(NULL);
} }
char *CONF_get_string(LHASH *conf, char *section, char *name) char *CONF_get_string(LHASH *conf, char *section, char *name)
{ {
CONF_VALUE *v,vv; CONF_VALUE *v,vv;
......
...@@ -63,8 +63,9 @@ ...@@ -63,8 +63,9 @@
extern "C" { extern "C" {
#endif #endif
#include <openssl/stack.h> #include <openssl/bio.h>
#include <openssl/lhash.h> #include <openssl/lhash.h>
#include <openssl/stack.h>
typedef struct typedef struct
{ {
...@@ -74,7 +75,9 @@ typedef struct ...@@ -74,7 +75,9 @@ typedef struct
} CONF_VALUE; } 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); STACK *CONF_get_section(LHASH *conf,char *section);
char *CONF_get_string(LHASH *conf,char *group,char *name); char *CONF_get_string(LHASH *conf,char *group,char *name);
long CONF_get_number(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 ); ...@@ -90,6 +93,8 @@ void ERR_load_CONF_strings(void );
/* Function codes. */ /* Function codes. */
#define CONF_F_CONF_LOAD 100 #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 #define CONF_F_STR_COPY 101
/* Reason codes. */ /* Reason codes. */
......
...@@ -66,6 +66,8 @@ ...@@ -66,6 +66,8 @@
static ERR_STRING_DATA CONF_str_functs[]= static ERR_STRING_DATA CONF_str_functs[]=
{ {
{ERR_PACK(0,CONF_F_CONF_LOAD,0), "CONF_load"}, {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"}, {ERR_PACK(0,CONF_F_STR_COPY,0), "STR_COPY"},
{0,NULL} {0,NULL}
}; };
......
...@@ -1777,3 +1777,5 @@ BIO_ctrl_wpending 1801 ...@@ -1777,3 +1777,5 @@ BIO_ctrl_wpending 1801
BIO_new_bio_pair 1802 BIO_new_bio_pair 1802
BIO_ctrl_get_write_guarantee 1803 BIO_ctrl_get_write_guarantee 1803
CRYPTO_num_locks 1804 CRYPTO_num_locks 1804
CONF_load_bio 1805
CONF_load_fp 1806
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册