@@ -244,6 +244,7 @@ static PKCS7 *pkcs7_get_sigfile(FILE_FORMAT_CTX *ctx);
244244static void print_cert (X509 * cert , int i );
245245static int x509_store_load_crlfile (X509_STORE * store , char * cafile , char * crlfile );
246246static void load_objects_from_store (const char * url , char * pass , EVP_PKEY * * pkey , STACK_OF (X509 ) * certs , STACK_OF (X509_CRL ) * crls );
247+ static BIO * bio_new_file (const char * filename , const char * mode );
247248#ifndef OPENSSL_NO_ENGINE
248249static void engine_control_set (GLOBAL_OPTIONS * options , const char * arg );
249250#endif /* OPENSSL_NO_ENGINE */
@@ -1277,15 +1278,15 @@ static int add_timestamp_builtin(PKCS7 *p7, FILE_FORMAT_CTX *ctx)
12771278 TS_RESP * response = NULL ;
12781279 int i , res = 1 ;
12791280
1280- btmp = BIO_new_file (ctx -> options -> tsa_certfile , "rb" );
1281+ btmp = bio_new_file (ctx -> options -> tsa_certfile , "rb" );
12811282 if (!btmp ) {
12821283 fprintf (stderr , "Failed to read Time-Stamp Authority certificate file: %s\n" , ctx -> options -> tsa_certfile );
12831284 return 0 ; /* FAILED */
12841285 }
12851286 /* .pem certificate file */
12861287 chain = X509_chain_read_certs (btmp , NULL );
12871288 BIO_free (btmp );
1288- btmp = BIO_new_file (ctx -> options -> tsa_keyfile , "rb" );
1289+ btmp = bio_new_file (ctx -> options -> tsa_keyfile , "rb" );
12891290 if (!btmp ) {
12901291 fprintf (stderr , "Failed to read private key file: %s\n" , ctx -> options -> tsa_keyfile );
12911292 return 0 ; /* FAILED */
@@ -4104,7 +4105,7 @@ static STACK_OF(X509_CRL) *X509_CRL_chain_up_ref(STACK_OF(X509_CRL) *chain)
41044105 */
41054106static int read_der_keyfile (GLOBAL_OPTIONS * options )
41064107{
4107- BIO * btmp = BIO_new_file (options -> keyfile , "rb" );
4108+ BIO * btmp = bio_new_file (options -> keyfile , "rb" );
41084109
41094110 if (!btmp ) {
41104111 fprintf (stderr , "Failed to read private key file: %s\n" , options -> keyfile );
@@ -4130,7 +4131,7 @@ static int read_der_keyfile(GLOBAL_OPTIONS *options)
41304131static int read_pkcs7_certfile (GLOBAL_OPTIONS * options )
41314132{
41324133 PKCS7 * p7 ;
4133- BIO * btmp = BIO_new_file (options -> certfile , "rb" );
4134+ BIO * btmp = bio_new_file (options -> certfile , "rb" );
41344135
41354136 if (!btmp ) {
41364137 fprintf (stderr , "Failed to read certificate from: %s\n" ,
@@ -4609,6 +4610,26 @@ static int file_exists(const char *filename)
46094610 return 0 ; /* File does not exist */
46104611}
46114612
4613+ static BIO * bio_new_file (const char * filename , const char * mode )
4614+ {
4615+ FILE * file ;
4616+ BIO * bio ;
4617+
4618+ if (!filename )
4619+ return NULL ;
4620+
4621+ file = fopen (filename , mode );
4622+ if (!file )
4623+ return NULL ;
4624+
4625+ bio = BIO_new_fp (file , BIO_CLOSE );
4626+ if (!bio ) {
4627+ fclose (file );
4628+ return NULL ;
4629+ }
4630+ return bio ;
4631+ }
4632+
46124633/*
46134634 * [in] argc, argv
46144635 * [in, out] options: structure holds the input data
@@ -5110,7 +5131,6 @@ int main(int argc, char **argv)
51105131 DO_EXIT_0 ("Failed to read key or certificates\n" );
51115132
51125133 if (options .cmd != CMD_VERIFY ) {
5113- FILE * fp ;
51145134 /* Create message digest BIO */
51155135 hash = BIO_new (BIO_f_md ());
51165136#if defined(__GNUC__ )
@@ -5123,17 +5143,11 @@ int main(int argc, char **argv)
51235143#if defined(__GNUC__ )
51245144#pragma GCC diagnostic pop
51255145#endif
5126- /* Create outdata file */
5127- fp = fopen (options .outfile , "w+b" );
5128- if (!fp ) {
5129- BIO_free_all (hash );
5130- DO_EXIT_1 ("Failed to create file: %s\n" , options .outfile );
5131- }
5132- outdata = BIO_new_fp (fp , BIO_CLOSE );
5146+ /* Create output file — file existence already verified via file_exists() */
5147+ outdata = bio_new_file (options .outfile , "w+b" );
51335148 if (!outdata ) {
5134- fclose (fp );
51355149 BIO_free_all (hash );
5136- DO_EXIT_1 ("Failed to wrap FILE in BIO : %s\n" , options .outfile );
5150+ DO_EXIT_1 ("Failed to create file : %s\n" , options .outfile );
51375151 }
51385152 }
51395153 ctx = file_format_script .ctx_new (& options , hash , outdata );
0 commit comments