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

Merged
merged 6 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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);

// 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);

// 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;
}
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