diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc index 5641b63beb..9618057c7d 100644 --- a/crypto/bio/bio_test.cc +++ b/crypto/bio/bio_test.cc @@ -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_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(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 +} diff --git a/crypto/bio/hexdump.c b/crypto/bio/hexdump.c index 019bd638b6..18979fa69d 100644 --- a/crypto/bio/hexdump.c +++ b/crypto/bio/hexdump.c @@ -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; +} diff --git a/include/openssl/bio.h b/include/openssl/bio.h index f5f8625c88..36c8b93292 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -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);