Skip to content

Implement BIO_dump #2331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions crypto/bio/bio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1250,3 +1250,32 @@ TEST(BIOTest, GetMemDataBackwardsCompat) {
ASSERT_EQ((size_t)data_len, sizeof(contents));
EXPECT_EQ(Bytes(contents, sizeof(contents)), Bytes(ptr, data_len));
}

TEST(BIOTest, Dump) {
bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio);

// Test BIO_dump with binary data
const uint8_t data[] = {0x00, 0x01, 0x02, 0x7f, 0x80, 0xff, 'A', 'B', 'C', '\n', '\r', '\t'};

// BIO_dump should return an estimate of bytes written
int ret = BIO_dump(bio.get(), data, sizeof(data));
ASSERT_GT(ret, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this assertion more specific.

ASSERT_EQ(ret, 80);


// Check that BIO_dump produced the expected output format
const uint8_t *contents;
size_t len;
ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
std::string output(reinterpret_cast<const char *>(contents), len);
Comment on lines +1268 to +1269
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also make an assertion about the value of len. Should it return a value equal to (or greater than) the value returned above?


// Verify the output contains the expected elements (hex values and ASCII representation)
EXPECT_NE(output.find("00 01 02 7f 80 ff"), std::string::npos);
EXPECT_NE(output.find("ABC"), std::string::npos);
EXPECT_NE(output.find("|"), std::string::npos); // ASCII section divider

// Verify BIO_dump works with an empty buffer
bio.reset(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio);
ret = BIO_dump(bio.get(), data, 0);
ASSERT_GE(ret, 0); // Should return 0 or positive for empty input
}
14 changes: 14 additions & 0 deletions crypto/bio/hexdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,17 @@ int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, unsigned indent) {

return 1;
}

int BIO_dump(BIO *bio, const void *data, int len) {
if (bio == NULL || data == NULL || len < 0) {
return -1;
}

// Use existing hexdump functionality with no indentation
if (!BIO_hexdump(bio, (const uint8_t *)data, (size_t)len, 0)) {
return -1;
}

// Return estimate of bytes written (each line is ~80 bytes)
return ((len + 15) / 16) * 80;
Comment on lines +202 to +207
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of needing to estimate how many bytes were written, this could be exact. Why not modify hexdump_write above to return the actual number of bytes written (just bump a counter on each of its calls to BIO_write), call hexdump_write directly instead of calling it via BIO_hexdump, then return whatever value it returns?

}
4 changes: 4 additions & 0 deletions include/openssl/bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ OPENSSL_EXPORT int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent);
OPENSSL_EXPORT int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len,
unsigned indent);

// BIO_dump writes a hex dump of |data| for |len| bytes to |bio|.
// It returns the total number of bytes written on success, or a negative value on error.
OPENSSL_EXPORT int BIO_dump(BIO *bio, const void *data, int len);

// ERR_print_errors prints the current contents of the error stack to |bio|
// using human readable strings where possible.
OPENSSL_EXPORT void ERR_print_errors(BIO *bio);
Expand Down
Loading