- Moved file loading to load_file

This commit is contained in:
Paul Bakker 2009-04-19 18:44:26 +00:00
parent 592457c0ad
commit 2b245ebd9f
2 changed files with 35 additions and 38 deletions

View File

@ -12,6 +12,8 @@ PolarSSL ChangeLog
* Fixed minor memory leak in x509parse_crt() and added better * Fixed minor memory leak in x509parse_crt() and added better
handling of 'full' certificate chains (found by Mathias handling of 'full' certificate chains (found by Mathias
Olsson). Olsson).
* Centralized file opening and reading for x509 files into
load_file()
= Version 0.10.0 released on 2009-01-12 = Version 0.10.0 released on 2009-01-12
* Migrated XySSL to PolarSSL * Migrated XySSL to PolarSSL

View File

@ -1003,40 +1003,53 @@ int x509parse_crt( x509_cert *chain, unsigned char *buf, int buflen )
return( 0 ); return( 0 );
} }
/*
* Load all data from a file into a given buffer.
*/
int load_file( char *path, unsigned char **buf, size_t *n )
{
FILE *f;
if( ( f = fopen( path, "rb" ) ) == NULL )
return( 1 );
fseek( f, 0, SEEK_END );
*n = (size_t) ftell( f );
fseek( f, 0, SEEK_SET );
if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
return( 1 );
if( fread( *buf, 1, *n, f ) != *n )
{
fclose( f );
free( *buf );
return( 1 );
}
fclose( f );
(*buf)[*n] = '\0';
return( 0 );
}
/* /*
* Load one or more certificates and add them to the chained list * Load one or more certificates and add them to the chained list
*/ */
int x509parse_crtfile( x509_cert *chain, char *path ) int x509parse_crtfile( x509_cert *chain, char *path )
{ {
int ret; int ret;
FILE *f;
size_t n; size_t n;
unsigned char *buf; unsigned char *buf;
if( ( f = fopen( path, "rb" ) ) == NULL ) if ( load_file( path, &buf, &n ) )
return( 1 ); return( 1 );
fseek( f, 0, SEEK_END );
n = (size_t) ftell( f );
fseek( f, 0, SEEK_SET );
if( ( buf = (unsigned char *) malloc( n + 1 ) ) == NULL )
return( 1 );
if( fread( buf, 1, n, f ) != n )
{
fclose( f );
free( buf );
return( 1 );
}
buf[n] = '\0';
ret = x509parse_crt( chain, buf, (int) n ); ret = x509parse_crt( chain, buf, (int) n );
memset( buf, 0, n + 1 ); memset( buf, 0, n + 1 );
free( buf ); free( buf );
fclose( f );
return( ret ); return( ret );
} }
@ -1299,29 +1312,12 @@ int x509parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
int x509parse_keyfile( rsa_context *rsa, char *path, char *pwd ) int x509parse_keyfile( rsa_context *rsa, char *path, char *pwd )
{ {
int ret; int ret;
FILE *f;
size_t n; size_t n;
unsigned char *buf; unsigned char *buf;
if( ( f = fopen( path, "rb" ) ) == NULL ) if ( load_file( path, &buf, &n ) )
return( 1 ); return( 1 );
fseek( f, 0, SEEK_END );
n = (size_t) ftell( f );
fseek( f, 0, SEEK_SET );
if( ( buf = (unsigned char *) malloc( n + 1 ) ) == NULL )
return( 1 );
if( fread( buf, 1, n, f ) != n )
{
fclose( f );
free( buf );
return( 1 );
}
buf[n] = '\0';
if( pwd == NULL ) if( pwd == NULL )
ret = x509parse_key( rsa, buf, (int) n, NULL, 0 ); ret = x509parse_key( rsa, buf, (int) n, NULL, 0 );
else else
@ -1330,7 +1326,6 @@ int x509parse_keyfile( rsa_context *rsa, char *path, char *pwd )
memset( buf, 0, n + 1 ); memset( buf, 0, n + 1 );
free( buf ); free( buf );
fclose( f );
return( ret ); return( ret );
} }