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 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
97 changes: 97 additions & 0 deletions crypto/bio/bio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <openssl/err.h>
#include <openssl/mem.h>

#include <array>

#include "../internal.h"
#include "../test/file_util.h"
#include "../test/test_util.h"
Expand Down Expand Up @@ -1250,3 +1252,98 @@ 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);

static constexpr std::array<uint8_t, 12> data = {{
0x00, 0x01, 0x02, 0x7f, 0x80, 0xff, 'A', 'B', 'C', '\n', '\r', '\t'
}};

int ret = BIO_dump(bio.get(), data.data(), data.size());

static constexpr std::array<char, 75> kExpectedOutput = {{
'0', '0', '0', '0', '0', '0', '0', '0', // offset
' ', ' ', // spacing
'0', '0', ' ', '0', '1', ' ', '0', '2', ' ', '7', 'f', ' ',
'8', '0', ' ', 'f', 'f', ' ', '4', '1', ' ', '4', '2', ' ', // first 8 bytes hex
' ', // group separator
'4', '3', ' ', '0', 'a', ' ', '0', 'd', ' ', '0', '9', // remaining 4 bytes hex
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // padding
'|', // separator
'.', '.', '.', '.', '.', '.', 'A', 'B', 'C', '.', '.', '.', // ASCII
'|', '\n' // closing
}};

const uint8_t *contents = nullptr;
size_t len = 0;
ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
std::string output(reinterpret_cast<const char *>(contents), len);

EXPECT_EQ(output, std::string(kExpectedOutput.data(), kExpectedOutput.size()))
<< "BIO_dump output should exactly match expected format";
EXPECT_EQ(ret, static_cast<int>(kExpectedOutput.size()))
<< "Return value should match expected length";

// Test edge cases
bio.reset(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio);
EXPECT_EQ(BIO_dump(bio.get(), data.data(), 0), 0)
<< "Valid data with zero length should return 0";

EXPECT_EQ(BIO_dump(bio.get(), data.data(), -1), -1)
<< "Negative length should return -1";

EXPECT_EQ(BIO_dump(bio.get(), nullptr, 0), -1)
<< "BIO_dump with NULL data should return -1";

EXPECT_EQ(BIO_dump(nullptr, data.data(), data.size()), -1)
<< "BIO_dump with NULL BIO should return -1";

// Test with larger data
static constexpr std::array<uint8_t, 32> large_data = {{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
}};

bio.reset(BIO_new(BIO_s_mem()));
ASSERT_TRUE(bio);
ret = BIO_dump(bio.get(), large_data.data(), large_data.size());

static constexpr std::array<char, 158> kExpectedLargeOutput = {{
// First line
'0', '0', '0', '0', '0', '0', '0', '0', // offset
' ', ' ', // spacing
'0', '0', ' ', '0', '1', ' ', '0', '2', ' ', '0', '3', ' ',
'0', '4', ' ', '0', '5', ' ', '0', '6', ' ', '0', '7', ' ', // first 8 bytes hex
' ', // group separator
'0', '8', ' ', '0', '9', ' ', '0', 'a', ' ', '0', 'b', ' ',
'0', 'c', ' ', '0', 'd', ' ', '0', 'e', ' ', '0', 'f', ' ', // last 8 bytes hex
' ', '|', // separator
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', // ASCII
'|', '\n', // closing

// Second line
'0', '0', '0', '0', '0', '0', '1', '0', // offset
' ', ' ', // spacing
'1', '0', ' ', '1', '1', ' ', '1', '2', ' ', '1', '3', ' ',
'1', '4', ' ', '1', '5', ' ', '1', '6', ' ', '1', '7', ' ', // first 8 bytes hex
' ', // group separator
'1', '8', ' ', '1', '9', ' ', '1', 'a', ' ', '1', 'b', ' ',
'1', 'c', ' ', '1', 'd', ' ', '1', 'e', ' ', '1', 'f', ' ', // last 8 bytes hex
' ', '|', // separator
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', // ASCII
'|', '\n' // closing
}};

ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
output = std::string(reinterpret_cast<const char *>(contents), len);

EXPECT_EQ(output, std::string(kExpectedLargeOutput.data(), kExpectedLargeOutput.size()))
<< "BIO_dump output should exactly match expected format";
EXPECT_EQ(ret, static_cast<int>(kExpectedLargeOutput.size()))
<< "Return value should match expected length";
}
32 changes: 32 additions & 0 deletions crypto/bio/hexdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,35 @@ 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 a temporary memory BIO to capture the formatted output
int ret = -1;
BIO *mbio = BIO_new(BIO_s_mem());
if (mbio == NULL) {
return -1;
}

// Generate the hexdump to the memory BIO
if (!BIO_hexdump(mbio, (const uint8_t *)data, (size_t)len, 0)) {
goto err;
}

// Get the formatted content
const uint8_t *contents = NULL;
size_t content_len = 0;
if (!BIO_mem_contents(mbio, &contents, &content_len)) {
goto err;
}

// Write to the original BIO and return the exact bytes written
ret = BIO_write(bio, contents, content_len);

err:
BIO_free(mbio);
return ret;
}
5 changes: 5 additions & 0 deletions include/openssl/bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ 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 exact number of bytes written to |bio| on success, or a negative value on error.
// If |len| is zero or |data| is NULL with |len| being zero, it returns 0.
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