From c7d681c5bd2c11ee49c2d5ce51f1b3ab5df24814 Mon Sep 17 00:00:00 2001 From: Piotr Nowicki Date: Tue, 17 Mar 2020 09:51:31 +0100 Subject: [PATCH] Add base64 code decoding Signed-off-by: Piotr Nowicki --- programs/ssl/ssl_base64_dump.c | 64 ++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/programs/ssl/ssl_base64_dump.c b/programs/ssl/ssl_base64_dump.c index 1ea02d075..9c7c21b4b 100644 --- a/programs/ssl/ssl_base64_dump.c +++ b/programs/ssl/ssl_base64_dump.c @@ -24,6 +24,8 @@ #include #include #include +#include "mbedtls/error.h" +#include "mbedtls/base64.h" /* * This program version @@ -148,15 +150,16 @@ void parse_arguments( int argc, char *argv[] ) /* * This function prints base64 code to the stdout */ -void print_b64( const char *b, const size_t len ) +void print_b64( const unsigned char *b, size_t len ) { size_t i = 0; - const char *end = b + len; + const unsigned char *end = b + len; + printf("\t"); while( b < end ) { - if( ++i > 70 ) + if( ++i > 75 ) { - printf( "\n" ); + printf( "\n\t" ); i = 0; } printf( "%c", *b++ ); @@ -165,6 +168,27 @@ void print_b64( const char *b, const size_t len ) fflush( stdout ); } +/* + * This function prints hex code from the buffer to the stdout. + */ +void print_hex( const unsigned char *b, size_t len ) +{ + size_t i = 0; + const unsigned char *end = b + len; + printf("\t"); + while( b < end ) + { + printf( "%02X ", (unsigned char) *b++ ); + if( ++i > 25 ) + { + printf("\n\t"); + i = 0; + } + } + printf("\n"); + fflush(stdout); +} + /* * Read next base64 code from the 'b64_file'. The 'b64_file' must be opened * previously. After each call to this function, the internal file position @@ -176,7 +200,7 @@ void print_b64( const char *b, const size_t len ) * \retval number of bytes written in to the b64 buffer or 0 in case no more * data was found */ -size_t read_next_b64_code( char *b64, const size_t max_len ) +size_t read_next_b64_code( unsigned char *b64, size_t max_len ) { size_t len = 0; uint32_t missed = 0; @@ -248,22 +272,42 @@ size_t read_next_b64_code( char *b64, const size_t max_len ) int main( int argc, char *argv[] ) { enum { B64BUF_LEN = 4 * 1024 }; - char b64[ B64BUF_LEN ]; + enum { SSLBUF_LEN = B64BUF_LEN * 3 / 4 + 1 }; + + unsigned char b64[ B64BUF_LEN ]; + unsigned char ssl[ SSLBUF_LEN ]; uint32_t b64_counter = 0; parse_arguments( argc, argv ); while( NULL != b64_file ) { - size_t len = read_next_b64_code( b64, B64BUF_LEN ); - if( len > 0) + size_t ssl_len; + size_t b64_len = read_next_b64_code( b64, B64BUF_LEN ); + if( b64_len > 0) { + int ret; + b64_counter++; if( debug ) { - printf( "%u.\n", b64_counter ); - print_b64( b64, len ); + printf( "%u. Base64 code:\n", b64_counter ); + print_b64( b64, b64_len ); + } + + ret = mbedtls_base64_decode( ssl, SSLBUF_LEN, &ssl_len, b64, b64_len ); + if( ret != 0) + { + mbedtls_strerror( ret, (char*) b64, B64BUF_LEN ); + printf_err( "base64 code cannot be decoded - %s\n", b64 ); + continue; + } + + if( debug ) + { + printf( "\n Decoded data in hex:\n"); + print_hex( ssl, ssl_len ); } /* TODO: deserializing */