From ca1ae8404e997fb7240f5d88b8305066b29e820e Mon Sep 17 00:00:00 2001 From: Jonathon Kingston Date: Fri, 11 Apr 2025 01:34:24 +0000 Subject: [PATCH 1/2] Implement BIO_dump function for hex dump output --- crypto/bio/bio_test.cc | 29 +++++++++++++++++++++++++++++ crypto/bio/hexdump.c | 14 ++++++++++++++ include/openssl/bio.h | 6 +++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc index bc733e9942..0dff180282 100644 --- a/crypto/bio/bio_test.cc +++ b/crypto/bio/bio_test.cc @@ -1398,3 +1398,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 9a0d9660e6..4c3923a3c3 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -387,9 +387,13 @@ OPENSSL_EXPORT int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent); // BIO_hexdump writes a hex dump of |data| to |bio|. Each line will be indented // by |indent| spaces. It returns one on success and zero otherwise. -OPENSSL_EXPORT int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, +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); From 78907d6738f29c9128609f093de3b5c8012b4fa9 Mon Sep 17 00:00:00 2001 From: kingstjo Date: Thu, 10 Apr 2025 18:58:01 -0700 Subject: [PATCH 2/2] fixed missing commas --- include/openssl/bio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/openssl/bio.h b/include/openssl/bio.h index cf21d797d6..36c8b93292 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -403,12 +403,12 @@ OPENSSL_EXPORT int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent); // BIO_hexdump writes a hex dump of |data| to |bio|. Each line will be indented // by |indent| spaces. It returns one on success and zero otherwise. -OPENSSL_EXPORT int BIO_hexdump(BIO *bio const uint8_t *data size_t len +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); +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.