Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3646,11 +3646,15 @@ int wolfSSL_BIO_dump(WOLFSSL_BIO *bio, const char *buf, int length)
#ifndef NO_FILESYSTEM
XFILE fp;

WOLFSSL_ENTER("wolfSSL_BIO_new_file");
WOLFSSL_ENTER("wolfSSL_BIO_read_filename");

if ((wolfSSL_BIO_get_fp(b, &fp) == WOLFSSL_SUCCESS) && (fp != XBADFILE))
{
XFCLOSE(fp);
/* Only close the old file if the BIO owns it. */
if (b->shutdown == WOLFSSL_BIO_CLOSE) {
XFCLOSE(fp);
}
b->ptr.fh = XBADFILE;
}

fp = XFOPEN(name, "rb");
Expand Down
5 changes: 0 additions & 5 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -10112,11 +10112,6 @@ int wolfSSL_X509_load_crl_file(WOLFSSL_X509_LOOKUP *ctx,
return ret;
}

if (wolfSSL_BIO_read_filename(bio, file) <= 0) {
wolfSSL_BIO_free(bio);
return ret;
}

if (type == WOLFSSL_FILETYPE_PEM) {
do {
crl = wolfSSL_PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL);
Expand Down
36 changes: 36 additions & 0 deletions tests/api/test_ossl_bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,42 @@ int test_wolfSSL_BIO_get_len(void)
return EXPECT_RESULT();
}

int test_wolfSSL_BIO_read_filename(void)
{
EXPECT_DECLS;
#if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM)
const char* missing = "./certs/does-not-exist.pem";
BIO* bio = NULL;
XFILE f = XBADFILE;
XFILE fp = XBADFILE;
byte buf[16];

/* The old file is dropped even when the new one can't be opened. */
ExpectNotNull(bio = BIO_new_file(svrCertFile, "rb"));
ExpectIntLE(BIO_read_filename(bio, missing), 0);
ExpectIntEQ((int)BIO_get_fp(bio, &fp), WOLFSSL_SUCCESS);
ExpectTrue(fp == XBADFILE);
ExpectIntEQ(BIO_read_filename(bio, caCertFile), WOLFSSL_SUCCESS);
ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), sizeof(buf));
BIO_free(bio);
bio = NULL;

/* A BIO_NOCLOSE file is not closed. */
ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
ExpectNotNull(bio = BIO_new_fp(f, BIO_NOCLOSE));
ExpectIntEQ(BIO_read_filename(bio, caCertFile), WOLFSSL_SUCCESS);
BIO_free(bio);
bio = NULL;
ExpectNotNull(bio = BIO_new_fp(f, BIO_NOCLOSE));
ExpectIntLE(BIO_read_filename(bio, missing), 0);
BIO_free(bio);
ExpectIntEQ(XFREAD(buf, 1, sizeof(buf), f), sizeof(buf));
if (f != XBADFILE)
XFCLOSE(f);
#endif
return EXPECT_RESULT();
}

#if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
long argl, long ret)
Expand Down
2 changes: 2 additions & 0 deletions tests/api/test_ossl_bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ int test_wolfSSL_BIO_f_md(void);
int test_wolfSSL_BIO_up_ref(void);
int test_wolfSSL_BIO_reset(void);
int test_wolfSSL_BIO_get_len(void);
int test_wolfSSL_BIO_read_filename(void);
int test_wolfSSL_BIO(void);
int test_wolfSSL_BIO_BIO_ring_read(void);
int test_wolfSSL_BIO_custom_method(void);
Expand All @@ -67,6 +68,7 @@ int test_wolfSSL_BIO_get_new_index_threaded(void);
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_up_ref), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_reset), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_get_len), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_read_filename), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_BIO_ring_read), \
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_custom_method), \
Expand Down
Loading