2020-03-13 14:43:22 +01:00
|
|
|
/*
|
|
|
|
* MbedTLS SSL context deserializer from base64 code
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2020-03-13 14:43:22 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-05-19 17:54:54 +02:00
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2021-12-09 15:48:47 +01:00
|
|
|
#include "mbedtls/debug.h"
|
2020-03-26 11:45:42 +01:00
|
|
|
|
2020-03-13 16:26:08 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-04-09 17:00:57 +02:00
|
|
|
|
2020-11-09 14:55:35 +01:00
|
|
|
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_ERROR_C) || \
|
|
|
|
!defined(MBEDTLS_SSL_TLS_C)
|
2020-04-09 17:00:57 +02:00
|
|
|
int main( void )
|
|
|
|
{
|
2020-11-09 14:55:35 +01:00
|
|
|
printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_ERROR_C and/or "
|
|
|
|
"MBEDTLS_SSL_TLS_C not defined.\n");
|
2020-04-09 17:00:57 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE 1
|
|
|
|
#endif
|
|
|
|
|
2020-03-16 14:05:22 +01:00
|
|
|
#include <stdint.h>
|
2020-03-13 16:26:08 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2020-06-22 14:08:57 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2020-03-20 12:16:33 +01:00
|
|
|
#include <time.h>
|
2020-06-22 14:08:57 +02:00
|
|
|
#endif
|
2020-03-20 12:16:33 +01:00
|
|
|
#include "mbedtls/ssl.h"
|
2020-03-17 09:51:31 +01:00
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/base64.h"
|
2020-03-18 17:27:29 +01:00
|
|
|
#include "mbedtls/md.h"
|
2020-03-20 12:16:33 +01:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/ssl_ciphersuites.h"
|
2020-03-13 16:26:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This program version
|
|
|
|
*/
|
2020-03-26 12:49:15 +01:00
|
|
|
#define PROG_NAME "ssl_context_info"
|
2020-03-13 16:26:08 +01:00
|
|
|
#define VER_MAJOR 0
|
|
|
|
#define VER_MINOR 1
|
|
|
|
|
2020-03-17 15:36:52 +01:00
|
|
|
/*
|
2020-04-09 17:00:57 +02:00
|
|
|
* Flags copied from the Mbed TLS library.
|
2020-03-17 15:36:52 +01:00
|
|
|
*/
|
|
|
|
#define SESSION_CONFIG_TIME_BIT ( 1 << 0 )
|
|
|
|
#define SESSION_CONFIG_CRT_BIT ( 1 << 1 )
|
|
|
|
#define SESSION_CONFIG_CLIENT_TICKET_BIT ( 1 << 2 )
|
|
|
|
#define SESSION_CONFIG_MFL_BIT ( 1 << 3 )
|
|
|
|
#define SESSION_CONFIG_TRUNC_HMAC_BIT ( 1 << 4 )
|
|
|
|
#define SESSION_CONFIG_ETM_BIT ( 1 << 5 )
|
|
|
|
#define SESSION_CONFIG_TICKET_BIT ( 1 << 6 )
|
|
|
|
|
|
|
|
#define CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ( 1 << 0 )
|
|
|
|
#define CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ( 1 << 1 )
|
|
|
|
#define CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ( 1 << 2 )
|
|
|
|
#define CONTEXT_CONFIG_ALPN_BIT ( 1 << 3 )
|
|
|
|
|
2020-03-18 15:12:41 +01:00
|
|
|
#define TRANSFORM_RANDBYTE_LEN 64
|
|
|
|
|
2020-03-30 17:09:33 +02:00
|
|
|
/*
|
|
|
|
* Minimum and maximum number of bytes for specific data: context, sessions,
|
|
|
|
* certificates, tickets and buffers in the program. The context and session
|
|
|
|
* size values have been calculated based on the 'print_deserialized_ssl_context()'
|
|
|
|
* and 'print_deserialized_ssl_session()' content.
|
|
|
|
*/
|
|
|
|
#define MIN_CONTEXT_LEN 84
|
|
|
|
#define MIN_SESSION_LEN 88
|
|
|
|
|
|
|
|
#define MAX_CONTEXT_LEN 875 /* without session data */
|
|
|
|
#define MAX_SESSION_LEN 109 /* without certificate and ticket data */
|
|
|
|
#define MAX_CERTIFICATE_LEN ( ( 1 << 24 ) - 1 )
|
|
|
|
#define MAX_TICKET_LEN ( ( 1 << 24 ) - 1 )
|
|
|
|
|
|
|
|
#define MIN_SERIALIZED_DATA ( MIN_CONTEXT_LEN + MIN_SESSION_LEN )
|
|
|
|
#define MAX_SERIALIZED_DATA ( MAX_CONTEXT_LEN + MAX_SESSION_LEN + \
|
|
|
|
MAX_CERTIFICATE_LEN + MAX_TICKET_LEN )
|
|
|
|
|
|
|
|
#define MIN_BASE64_LEN ( MIN_SERIALIZED_DATA * 4 / 3 )
|
|
|
|
#define MAX_BASE64_LEN ( MAX_SERIALIZED_DATA * 4 / 3 + 3 )
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
/*
|
|
|
|
* A macro that prevents from reading out of the ssl buffer range.
|
|
|
|
*/
|
|
|
|
#define CHECK_SSL_END( LEN ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if( end - ssl < (int)( LEN ) ) \
|
|
|
|
{ \
|
|
|
|
printf_err( "%s", buf_ln_err ); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
} while( 0 )
|
|
|
|
|
2020-03-13 16:26:08 +01:00
|
|
|
/*
|
|
|
|
* Global values
|
|
|
|
*/
|
2020-03-20 12:16:33 +01:00
|
|
|
FILE *b64_file = NULL; /* file with base64 codes to deserialize */
|
|
|
|
char conf_keep_peer_certificate = 1; /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE from mbedTLS configuration */
|
|
|
|
char conf_dtls_proto = 1; /* MBEDTLS_SSL_PROTO_DTLS from mbedTLS configuration */
|
|
|
|
char debug = 0; /* flag for debug messages */
|
2020-03-30 17:09:33 +02:00
|
|
|
const char alloc_err[] = "Cannot allocate memory\n";
|
2020-03-20 12:16:33 +01:00
|
|
|
const char buf_ln_err[] = "Buffer does not have enough data to complete the parsing\n";
|
2020-03-13 16:26:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic printing functions
|
|
|
|
*/
|
|
|
|
void print_version( )
|
|
|
|
{
|
|
|
|
printf( "%s v%d.%d\n", PROG_NAME, VER_MAJOR, VER_MINOR );
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_usage( )
|
|
|
|
{
|
|
|
|
print_version();
|
2020-04-09 17:00:57 +02:00
|
|
|
printf( "\nThis program is used to deserialize an Mbed TLS SSL session from the base64 code provided\n"
|
2020-03-20 12:16:33 +01:00
|
|
|
"in the text file. The program can deserialize many codes from one file, but they must be\n"
|
|
|
|
"separated, e.g. by a newline.\n\n" );
|
2020-03-13 16:26:08 +01:00
|
|
|
printf(
|
|
|
|
"Usage:\n"
|
2020-03-20 12:16:33 +01:00
|
|
|
"\t-f path - Path to the file with base64 code\n"
|
|
|
|
"\t-v - Show version\n"
|
|
|
|
"\t-h - Show this usage\n"
|
|
|
|
"\t-d - Print more information\n"
|
2020-04-09 17:00:57 +02:00
|
|
|
"\t--keep-peer-cert=0 - Use this option if you know that the Mbed TLS library\n"
|
2020-03-20 12:16:33 +01:00
|
|
|
"\t has been compiled with the MBEDTLS_SSL_KEEP_PEER_CERTIFICATE\n"
|
|
|
|
"\t flag. You can also use it if there are some problems with reading\n"
|
|
|
|
"\t the information about certificate\n"
|
2020-04-09 17:00:57 +02:00
|
|
|
"\t--dtls-protocol=0 - Use this option if you know that the Mbed TLS library\n"
|
2020-03-20 12:16:33 +01:00
|
|
|
"\t has been compiled without the MBEDTLS_SSL_PROTO_DTLS flag\n"
|
2020-03-13 16:26:08 +01:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printf_dbg( const char *str, ... )
|
|
|
|
{
|
|
|
|
if( debug )
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start( args, str );
|
|
|
|
printf( "debug: " );
|
|
|
|
vprintf( str, args );
|
|
|
|
fflush( stdout );
|
|
|
|
va_end( args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:48:47 +01:00
|
|
|
MBEDTLS_PRINTF_ATTRIBUTE( 1, 2 )
|
2020-03-13 16:26:08 +01:00
|
|
|
void printf_err( const char *str, ... )
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start( args, str );
|
2020-03-20 12:16:33 +01:00
|
|
|
fflush( stdout );
|
2020-03-13 16:26:08 +01:00
|
|
|
fprintf( stderr, "ERROR: " );
|
|
|
|
vfprintf( stderr, str, args );
|
|
|
|
fflush( stderr );
|
|
|
|
va_end( args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit from the program in case of error
|
|
|
|
*/
|
|
|
|
void error_exit()
|
|
|
|
{
|
|
|
|
if( NULL != b64_file )
|
|
|
|
{
|
|
|
|
fclose( b64_file );
|
|
|
|
}
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function takes the input arguments of this program
|
|
|
|
*/
|
|
|
|
void parse_arguments( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
if( argc < 2 )
|
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
while( i < argc )
|
|
|
|
{
|
|
|
|
if( strcmp( argv[i], "-d" ) == 0 )
|
|
|
|
{
|
|
|
|
debug = 1;
|
|
|
|
}
|
|
|
|
else if( strcmp( argv[i], "-h" ) == 0 )
|
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
else if( strcmp( argv[i], "-v" ) == 0 )
|
|
|
|
{
|
|
|
|
print_version();
|
|
|
|
}
|
|
|
|
else if( strcmp( argv[i], "-f" ) == 0 )
|
|
|
|
{
|
|
|
|
if( ++i >= argc )
|
|
|
|
{
|
|
|
|
printf_err( "File path is empty\n" );
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
|
2021-12-09 13:48:51 +01:00
|
|
|
if( NULL != b64_file )
|
|
|
|
{
|
|
|
|
printf_err( "Cannot specify more than one file with -f\n" );
|
|
|
|
error_exit( );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( b64_file = fopen( argv[i], "r" )) == NULL )
|
2020-03-13 16:26:08 +01:00
|
|
|
{
|
|
|
|
printf_err( "Cannot find file \"%s\"\n", argv[i] );
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 12:16:33 +01:00
|
|
|
else if( strcmp( argv[i], "--keep-peer-cert=0" ) == 0 )
|
|
|
|
{
|
|
|
|
conf_keep_peer_certificate = 0;
|
|
|
|
}
|
|
|
|
else if( strcmp( argv[i], "--dtls-protocol=0" ) == 0 )
|
|
|
|
{
|
|
|
|
conf_dtls_proto = 0;
|
|
|
|
}
|
2020-03-13 16:26:08 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:52:56 +01:00
|
|
|
/*
|
|
|
|
* This function prints base64 code to the stdout
|
|
|
|
*/
|
2020-03-18 15:12:41 +01:00
|
|
|
void print_b64( const uint8_t *b, size_t len )
|
2020-03-16 17:52:56 +01:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
2020-03-18 15:12:41 +01:00
|
|
|
const uint8_t *end = b + len;
|
2020-03-17 09:51:31 +01:00
|
|
|
printf("\t");
|
2020-03-16 17:52:56 +01:00
|
|
|
while( b < end )
|
|
|
|
{
|
2020-03-17 09:51:31 +01:00
|
|
|
if( ++i > 75 )
|
2020-03-16 17:52:56 +01:00
|
|
|
{
|
2020-03-17 09:51:31 +01:00
|
|
|
printf( "\n\t" );
|
2020-03-16 17:52:56 +01:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
printf( "%c", *b++ );
|
|
|
|
}
|
|
|
|
printf( "\n" );
|
|
|
|
fflush( stdout );
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:51:31 +01:00
|
|
|
/*
|
|
|
|
* This function prints hex code from the buffer to the stdout.
|
2020-03-20 12:16:33 +01:00
|
|
|
*
|
|
|
|
* /p b buffer with data to print
|
|
|
|
* /p len number of bytes to print
|
|
|
|
* /p in_line number of bytes in one line
|
|
|
|
* /p prefix prefix for the new lines
|
2020-03-17 09:51:31 +01:00
|
|
|
*/
|
2020-03-20 12:16:33 +01:00
|
|
|
void print_hex( const uint8_t *b, size_t len,
|
|
|
|
const size_t in_line, const char *prefix )
|
2020-03-17 09:51:31 +01:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
2020-03-18 15:12:41 +01:00
|
|
|
const uint8_t *end = b + len;
|
2020-03-20 12:16:33 +01:00
|
|
|
|
|
|
|
if( prefix == NULL )
|
|
|
|
{
|
|
|
|
prefix = "";
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:51:31 +01:00
|
|
|
while( b < end )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
if( ++i > in_line )
|
2020-03-17 09:51:31 +01:00
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\n%s", prefix );
|
|
|
|
i = 1;
|
2020-03-17 09:51:31 +01:00
|
|
|
}
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "%02X ", (uint8_t) *b++ );
|
2020-03-17 09:51:31 +01:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
/*
|
|
|
|
* Print the value of time_t in format e.g. 2020-01-23 13:05:59
|
|
|
|
*/
|
2022-02-28 11:51:57 +01:00
|
|
|
void print_time( const uint64_t *time )
|
2020-03-20 12:16:33 +01:00
|
|
|
{
|
2022-02-28 11:51:57 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2020-03-20 12:16:33 +01:00
|
|
|
char buf[20];
|
2022-02-28 11:51:57 +01:00
|
|
|
struct tm *t = gmtime( (time_t*) time );
|
2020-03-20 12:16:33 +01:00
|
|
|
static const char format[] = "%Y-%m-%d %H:%M:%S";
|
|
|
|
if( NULL != t )
|
|
|
|
{
|
|
|
|
strftime( buf, sizeof( buf ), format, t );
|
|
|
|
printf( "%s\n", buf );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "unknown\n" );
|
|
|
|
}
|
2022-02-28 11:51:57 +01:00
|
|
|
#else
|
|
|
|
(void) time;
|
|
|
|
printf( "not supported\n" );
|
2020-06-22 14:08:57 +02:00
|
|
|
#endif
|
2022-02-28 11:51:57 +01:00
|
|
|
}
|
2020-03-20 12:16:33 +01:00
|
|
|
|
2020-03-17 15:36:52 +01:00
|
|
|
/*
|
|
|
|
* Print the input string if the bit is set in the value
|
|
|
|
*/
|
|
|
|
void print_if_bit( const char *str, int bit, int val )
|
|
|
|
{
|
|
|
|
if( bit & val )
|
|
|
|
{
|
|
|
|
printf( "\t%s\n", str );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
/*
|
|
|
|
* Return pointer to hardcoded "enabled" or "disabled" depending on the input value
|
|
|
|
*/
|
|
|
|
const char * get_enabled_str( int is_en )
|
|
|
|
{
|
|
|
|
return ( is_en ) ? "enabled" : "disabled";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return pointer to hardcoded MFL string value depending on the MFL code at the input
|
|
|
|
*/
|
|
|
|
const char * get_mfl_str( int mfl_code )
|
|
|
|
{
|
|
|
|
switch( mfl_code )
|
|
|
|
{
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
|
|
|
|
return "none";
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_512:
|
|
|
|
return "512";
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
|
|
|
|
return "1024";
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
|
|
|
|
return "2048";
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
|
|
|
|
return "4096";
|
|
|
|
default:
|
|
|
|
return "error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:05:22 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* indicator of the global b64_file is advanced.
|
|
|
|
*
|
2020-03-30 17:09:33 +02:00
|
|
|
* Note - This function checks the size of the input buffer and if necessary,
|
|
|
|
* increases it to the maximum MAX_BASE64_LEN
|
|
|
|
*
|
|
|
|
* /p b64 pointer to the pointer of the buffer for input data
|
|
|
|
* /p max_len pointer to the current buffer capacity. It can be changed if
|
|
|
|
* the buffer needs to be increased
|
2020-03-16 14:05:22 +01:00
|
|
|
*
|
|
|
|
* \retval number of bytes written in to the b64 buffer or 0 in case no more
|
|
|
|
* data was found
|
|
|
|
*/
|
2020-03-30 17:09:33 +02:00
|
|
|
size_t read_next_b64_code( uint8_t **b64, size_t *max_len )
|
2020-03-16 14:05:22 +01:00
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
int valid_balance = 0; /* balance between valid and invalid characters */
|
2020-03-16 14:05:22 +01:00
|
|
|
size_t len = 0;
|
|
|
|
char pad = 0;
|
2020-08-13 21:17:53 +02:00
|
|
|
int c = 0;
|
2020-03-16 14:05:22 +01:00
|
|
|
|
|
|
|
while( EOF != c )
|
|
|
|
{
|
|
|
|
char c_valid = 0;
|
|
|
|
|
2020-08-13 21:17:53 +02:00
|
|
|
c = fgetc( b64_file );
|
2020-03-16 14:05:22 +01:00
|
|
|
|
2020-03-30 17:09:33 +02:00
|
|
|
if( pad > 0 )
|
2020-03-16 14:05:22 +01:00
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
if( c == '=' && pad == 1 )
|
2020-03-16 14:05:22 +01:00
|
|
|
{
|
|
|
|
c_valid = 1;
|
|
|
|
pad = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ( c >= 'A' && c <= 'Z' ) ||
|
|
|
|
( c >= 'a' && c <= 'z' ) ||
|
|
|
|
( c >= '0' && c <= '9' ) ||
|
|
|
|
c == '+' || c == '/' )
|
|
|
|
{
|
|
|
|
c_valid = 1;
|
|
|
|
}
|
|
|
|
else if( c == '=' )
|
|
|
|
{
|
|
|
|
c_valid = 1;
|
|
|
|
pad = 1;
|
|
|
|
}
|
|
|
|
else if( c == '-' )
|
|
|
|
{
|
|
|
|
c = '+';
|
|
|
|
c_valid = 1;
|
|
|
|
}
|
|
|
|
else if( c == '_' )
|
|
|
|
{
|
|
|
|
c = '/';
|
|
|
|
c_valid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c_valid )
|
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
/* A string of characters that could be a base64 code. */
|
|
|
|
valid_balance++;
|
|
|
|
|
|
|
|
if( len < *max_len )
|
|
|
|
{
|
|
|
|
( *b64 )[ len++ ] = c;
|
|
|
|
}
|
|
|
|
else if( *max_len < MAX_BASE64_LEN )
|
2020-03-16 14:05:22 +01:00
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
/* Current buffer is too small, but can be resized. */
|
|
|
|
void *ptr;
|
|
|
|
size_t new_size = ( MAX_BASE64_LEN - 4096 > *max_len ) ?
|
|
|
|
*max_len + 4096 : MAX_BASE64_LEN;
|
|
|
|
|
|
|
|
ptr = realloc( *b64, new_size );
|
|
|
|
if( NULL == ptr )
|
|
|
|
{
|
|
|
|
printf_err( alloc_err );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*b64 = ptr;
|
|
|
|
*max_len = new_size;
|
|
|
|
( *b64 )[ len++ ] = c;
|
2020-03-16 14:05:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
/* Too much data so it will be treated as invalid */
|
|
|
|
len++;
|
2020-03-16 14:05:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( len > 0 )
|
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
/* End of a string that could be a base64 code, but need to check
|
|
|
|
* that the length of the characters is correct. */
|
|
|
|
|
|
|
|
valid_balance--;
|
|
|
|
|
|
|
|
if( len < MIN_CONTEXT_LEN )
|
2020-03-16 14:05:22 +01:00
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
printf_dbg( "The code found is too small to be a SSL context.\n" );
|
|
|
|
len = pad = 0;
|
|
|
|
}
|
|
|
|
else if( len > *max_len )
|
|
|
|
{
|
2021-12-09 15:48:47 +01:00
|
|
|
printf_err( "The code found is too large by %" MBEDTLS_PRINTF_SIZET " bytes.\n",
|
|
|
|
len - *max_len );
|
2020-03-30 17:09:33 +02:00
|
|
|
len = pad = 0;
|
|
|
|
}
|
|
|
|
else if( len % 4 != 0 )
|
|
|
|
{
|
|
|
|
printf_err( "The length of the base64 code found should be a multiple of 4.\n" );
|
|
|
|
len = pad = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Base64 code with valid character length. */
|
|
|
|
return len;
|
2020-03-16 14:05:22 +01:00
|
|
|
}
|
2020-03-30 17:09:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
valid_balance--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detection of potentially wrong file format like: binary, zip, ISO, etc. */
|
|
|
|
if( valid_balance < -100 )
|
|
|
|
{
|
|
|
|
printf_err( "Too many bad symbols detected. File check aborted.\n" );
|
|
|
|
return 0;
|
2020-03-16 14:05:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf_dbg( "End of file\n" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:36:23 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2020-03-20 12:16:33 +01:00
|
|
|
/*
|
|
|
|
* This function deserializes and prints to the stdout all obtained information
|
|
|
|
* about the certificates from provided data.
|
|
|
|
*
|
|
|
|
* /p ssl pointer to serialized certificate
|
|
|
|
* /p len number of bytes in the buffer
|
|
|
|
*/
|
|
|
|
void print_deserialized_ssl_cert( const uint8_t *ssl, uint32_t len )
|
|
|
|
{
|
|
|
|
enum { STRLEN = 4096 };
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
int ret;
|
|
|
|
char str[STRLEN];
|
|
|
|
|
|
|
|
printf( "\nCertificate:\n" );
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init( &crt );
|
|
|
|
ret = mbedtls_x509_crt_parse_der( &crt, ssl, len );
|
|
|
|
if( 0 != ret )
|
|
|
|
{
|
|
|
|
mbedtls_strerror( ret, str, STRLEN );
|
|
|
|
printf_err( "Invalid format of X.509 - %s\n", str );
|
|
|
|
printf( "Cannot deserialize:\n\t" );
|
|
|
|
print_hex( ssl, len, 25, "\t" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mbedtls_x509_crt *current = &crt;
|
|
|
|
|
|
|
|
while( current != NULL )
|
|
|
|
{
|
|
|
|
ret = mbedtls_x509_crt_info( str, STRLEN, "\t", current );
|
|
|
|
if( 0 > ret )
|
|
|
|
{
|
|
|
|
mbedtls_strerror( ret, str, STRLEN );
|
|
|
|
printf_err( "Cannot write to the output - %s\n", str );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "%s", str );
|
|
|
|
}
|
|
|
|
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
if( current )
|
|
|
|
{
|
|
|
|
printf( "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
}
|
2020-10-09 10:36:23 +02:00
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_INFO */
|
2020-03-20 12:16:33 +01:00
|
|
|
|
2020-03-18 15:12:41 +01:00
|
|
|
/*
|
|
|
|
* This function deserializes and prints to the stdout all obtained information
|
|
|
|
* about the session from provided data. This function was built based on
|
|
|
|
* mbedtls_ssl_session_load(). mbedtls_ssl_session_load() could not be used
|
|
|
|
* due to dependencies on the mbedTLS configuration.
|
|
|
|
*
|
|
|
|
* The data structure in the buffer:
|
|
|
|
* uint64 start_time;
|
|
|
|
* uint8 ciphersuite[2]; // defined by the standard
|
|
|
|
* uint8 compression; // 0 or 1
|
|
|
|
* uint8 session_id_len; // at most 32
|
|
|
|
* opaque session_id[32];
|
|
|
|
* opaque master[48]; // fixed length in the standard
|
|
|
|
* uint32 verify_result;
|
|
|
|
* opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
|
|
|
|
* opaque ticket<0..2^24-1>; // length 0 means no ticket
|
|
|
|
* uint32 ticket_lifetime;
|
|
|
|
* uint8 mfl_code; // up to 255 according to standard
|
|
|
|
* uint8 trunc_hmac; // 0 or 1
|
|
|
|
* uint8 encrypt_then_mac; // 0 or 1
|
|
|
|
*
|
|
|
|
* /p ssl pointer to serialized session
|
|
|
|
* /p len number of bytes in the buffer
|
|
|
|
* /p session_cfg_flag session configuration flags
|
|
|
|
*/
|
|
|
|
void print_deserialized_ssl_session( const uint8_t *ssl, uint32_t len,
|
|
|
|
int session_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
const struct mbedtls_ssl_ciphersuite_t * ciphersuite_info;
|
|
|
|
int ciphersuite_id;
|
|
|
|
uint32_t cert_len, ticket_len;
|
2020-03-18 17:27:29 +01:00
|
|
|
uint32_t verify_result, ticket_lifetime;
|
2020-03-18 15:12:41 +01:00
|
|
|
const uint8_t *end = ssl + len;
|
2020-03-18 17:27:29 +01:00
|
|
|
|
|
|
|
printf( "\nSession info:\n" );
|
|
|
|
|
|
|
|
if( session_cfg_flag & SESSION_CONFIG_TIME_BIT )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
uint64_t start;
|
|
|
|
CHECK_SSL_END( 8 );
|
|
|
|
start = ( (uint64_t) ssl[0] << 56 ) |
|
|
|
|
( (uint64_t) ssl[1] << 48 ) |
|
|
|
|
( (uint64_t) ssl[2] << 40 ) |
|
|
|
|
( (uint64_t) ssl[3] << 32 ) |
|
|
|
|
( (uint64_t) ssl[4] << 24 ) |
|
|
|
|
( (uint64_t) ssl[5] << 16 ) |
|
|
|
|
( (uint64_t) ssl[6] << 8 ) |
|
|
|
|
( (uint64_t) ssl[7] );
|
2020-03-18 17:27:29 +01:00
|
|
|
ssl += 8;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tstart time : " );
|
2022-02-28 11:51:57 +01:00
|
|
|
print_time( &start );
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 2 );
|
|
|
|
ciphersuite_id = ( (int) ssl[0] << 8 ) | (int) ssl[1];
|
|
|
|
printf_dbg( "Ciphersuite ID: %d\n", ciphersuite_id );
|
2020-03-18 17:27:29 +01:00
|
|
|
ssl += 2;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
|
|
|
|
if( ciphersuite_info == NULL )
|
|
|
|
{
|
|
|
|
printf_err( "Cannot find ciphersuite info\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2022-08-30 23:56:08 +02:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2020-03-20 12:16:33 +01:00
|
|
|
const mbedtls_md_info_t *md_info;
|
2022-08-30 23:56:08 +02:00
|
|
|
#endif
|
2020-03-20 12:16:33 +01:00
|
|
|
|
|
|
|
printf( "\tciphersuite : %s\n", ciphersuite_info->name );
|
|
|
|
printf( "\tcipher flags : 0x%02X\n", ciphersuite_info->flags );
|
2020-03-18 17:27:29 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
|
|
|
|
if( cipher_info == NULL )
|
|
|
|
{
|
|
|
|
printf_err( "Cannot find cipher info\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "\tcipher : %s\n", cipher_info->name );
|
|
|
|
}
|
2022-08-17 22:17:00 +02:00
|
|
|
#if defined(MBEDTLS_MD_C)
|
2020-03-20 12:16:33 +01:00
|
|
|
md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
|
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
|
|
|
printf_err( "Cannot find Message-Digest info\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-11 18:44:43 +01:00
|
|
|
printf( "\tMessage-Digest : %s\n", mbedtls_md_get_name( md_info ) );
|
2020-03-20 12:16:33 +01:00
|
|
|
}
|
2022-08-19 09:42:11 +02:00
|
|
|
#endif /* MBEDTLS_MD_C */
|
2020-03-20 12:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
printf( "\tcompression : %s\n", get_enabled_str( *ssl++ ) );
|
|
|
|
|
|
|
|
/* Note - Here we can get session ID length from serialized data, but we
|
|
|
|
* use hardcoded 32-bytes length. This approach was taken from
|
|
|
|
* 'mbedtls_ssl_session_load()'. */
|
|
|
|
CHECK_SSL_END( 1 + 32 );
|
|
|
|
printf_dbg( "Session id length: %u\n", (uint32_t) *ssl++ );
|
|
|
|
printf( "\tsession ID : ");
|
|
|
|
print_hex( ssl, 32, 16, "\t " );
|
2020-03-18 17:27:29 +01:00
|
|
|
ssl += 32;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tmaster secret : ");
|
|
|
|
CHECK_SSL_END( 48 );
|
|
|
|
print_hex( ssl, 48, 16, "\t " );
|
2020-03-18 17:27:29 +01:00
|
|
|
ssl += 48;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 4 );
|
2020-03-18 17:27:29 +01:00
|
|
|
verify_result = ( (uint32_t) ssl[0] << 24 ) |
|
|
|
|
( (uint32_t) ssl[1] << 16 ) |
|
|
|
|
( (uint32_t) ssl[2] << 8 ) |
|
|
|
|
( (uint32_t) ssl[3] );
|
|
|
|
ssl += 4;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tverify result : 0x%08X\n", verify_result );
|
2020-03-18 17:27:29 +01:00
|
|
|
|
|
|
|
if( SESSION_CONFIG_CRT_BIT & session_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
if( conf_keep_peer_certificate )
|
2020-03-18 17:27:29 +01:00
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 3 );
|
2020-03-18 17:27:29 +01:00
|
|
|
cert_len = ( (uint32_t) ssl[0] << 16 ) |
|
|
|
|
( (uint32_t) ssl[1] << 8 ) |
|
|
|
|
( (uint32_t) ssl[2] );
|
|
|
|
ssl += 3;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf_dbg( "Certificate length: %u\n", cert_len );
|
2020-03-18 17:27:29 +01:00
|
|
|
|
|
|
|
if( cert_len > 0 )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( cert_len );
|
2020-10-09 10:36:23 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2020-03-20 12:16:33 +01:00
|
|
|
print_deserialized_ssl_cert( ssl, cert_len );
|
2020-10-09 10:36:23 +02:00
|
|
|
#endif
|
2020-03-18 17:27:29 +01:00
|
|
|
ssl += cert_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tPeer digest : " );
|
|
|
|
|
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
switch( (mbedtls_md_type_t) *ssl++ )
|
|
|
|
{
|
|
|
|
case MBEDTLS_MD_NONE:
|
|
|
|
printf( "none\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
printf( "MD5\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
printf( "SHA1\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_SHA224:
|
|
|
|
printf( "SHA224\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
printf( "SHA256\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
printf( "SHA384\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
printf( "SHA512\n" );
|
|
|
|
break;
|
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
printf( "RIPEMD160\n" );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf( "undefined or erroneous\n" );
|
|
|
|
break;
|
|
|
|
}
|
2020-03-18 17:27:29 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
cert_len = (uint32_t) *ssl++;
|
|
|
|
printf_dbg( "Message-Digest length: %u\n", cert_len );
|
2020-03-18 17:27:29 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
if( cert_len > 0 )
|
2020-03-18 17:27:29 +01:00
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tPeer digest cert : " );
|
|
|
|
CHECK_SSL_END( cert_len );
|
|
|
|
print_hex( ssl, cert_len, 16, "\t " );
|
|
|
|
ssl += cert_len;
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( SESSION_CONFIG_CLIENT_TICKET_BIT & session_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\nTicket:\n" );
|
|
|
|
|
|
|
|
CHECK_SSL_END( 3 );
|
2020-03-18 17:27:29 +01:00
|
|
|
ticket_len = ( (uint32_t) ssl[0] << 16 ) |
|
|
|
|
( (uint32_t) ssl[1] << 8 ) |
|
|
|
|
( (uint32_t) ssl[2] );
|
|
|
|
ssl += 3;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf_dbg( "Ticket length: %u\n", ticket_len );
|
2020-03-18 17:27:29 +01:00
|
|
|
|
|
|
|
if( ticket_len > 0 )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\t" );
|
|
|
|
CHECK_SSL_END( ticket_len );
|
|
|
|
print_hex( ssl, ticket_len, 22, "\t" );
|
2020-03-18 17:27:29 +01:00
|
|
|
ssl += ticket_len;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\n" );
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 4 );
|
2020-03-18 17:27:29 +01:00
|
|
|
ticket_lifetime = ( (uint32_t) ssl[0] << 24 ) |
|
|
|
|
( (uint32_t) ssl[1] << 16 ) |
|
|
|
|
( (uint32_t) ssl[2] << 8 ) |
|
|
|
|
( (uint32_t) ssl[3] );
|
|
|
|
ssl += 4;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tlifetime : %u sec.\n", ticket_lifetime );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl < end )
|
|
|
|
{
|
|
|
|
printf( "\nSession others:\n" );
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( SESSION_CONFIG_MFL_BIT & session_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
printf( "\tMFL : %s\n", get_mfl_str( *ssl++ ) );
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( SESSION_CONFIG_TRUNC_HMAC_BIT & session_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
printf( "\tnegotiate truncated HMAC : %s\n", get_enabled_str( *ssl++ ) );
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( SESSION_CONFIG_ETM_BIT & session_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
printf( "\tEncrypt-then-MAC : %s\n", get_enabled_str( *ssl++ ) );
|
2020-03-18 17:27:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( 0 != ( end - ssl ) )
|
|
|
|
{
|
|
|
|
printf_err( "%i bytes left to analyze from session\n", (int32_t)( end - ssl ) );
|
|
|
|
}
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:36:52 +01:00
|
|
|
/*
|
|
|
|
* This function deserializes and prints to the stdout all obtained information
|
|
|
|
* about the context from provided data. This function was built based on
|
|
|
|
* mbedtls_ssl_context_load(). mbedtls_ssl_context_load() could not be used
|
|
|
|
* due to dependencies on the mbedTLS configuration and the configuration of
|
|
|
|
* the context when serialization was created.
|
|
|
|
*
|
|
|
|
* The data structure in the buffer:
|
2020-03-30 17:09:33 +02:00
|
|
|
* // header
|
|
|
|
* uint8 version[3];
|
|
|
|
* uint8 configuration[5];
|
2020-03-17 15:36:52 +01:00
|
|
|
* // session sub-structure
|
2020-03-30 17:09:33 +02:00
|
|
|
* uint32_t session_len;
|
2020-03-17 15:36:52 +01:00
|
|
|
* opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
|
|
|
|
* // transform sub-structure
|
|
|
|
* uint8 random[64]; // ServerHello.random+ClientHello.random
|
2020-03-30 17:09:33 +02:00
|
|
|
* uint8 in_cid_len;
|
2020-03-17 15:36:52 +01:00
|
|
|
* uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
|
2020-03-30 17:09:33 +02:00
|
|
|
* uint8 out_cid_len;
|
2020-03-17 15:36:52 +01:00
|
|
|
* uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
|
|
|
|
* // fields from ssl_context
|
|
|
|
* uint32 badmac_seen; // DTLS: number of records with failing MAC
|
|
|
|
* uint64 in_window_top; // DTLS: last validated record seq_num
|
|
|
|
* uint64 in_window; // DTLS: bitmask for replay protection
|
|
|
|
* uint8 disable_datagram_packing; // DTLS: only one record per datagram
|
|
|
|
* uint64 cur_out_ctr; // Record layer: outgoing sequence number
|
|
|
|
* uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
|
2020-03-30 17:09:33 +02:00
|
|
|
* uint8 alpn_chosen_len;
|
2020-03-17 15:36:52 +01:00
|
|
|
* uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
|
|
|
|
*
|
|
|
|
* /p ssl pointer to serialized session
|
|
|
|
* /p len number of bytes in the buffer
|
|
|
|
*/
|
2020-03-18 15:12:41 +01:00
|
|
|
void print_deserialized_ssl_context( const uint8_t *ssl, size_t len )
|
2020-03-17 15:36:52 +01:00
|
|
|
{
|
2020-03-18 15:12:41 +01:00
|
|
|
const uint8_t *end = ssl + len;
|
2020-03-20 12:16:33 +01:00
|
|
|
uint32_t session_len;
|
2020-03-17 15:36:52 +01:00
|
|
|
int session_cfg_flag;
|
|
|
|
int context_cfg_flag;
|
|
|
|
|
|
|
|
printf( "\nMbed TLS version:\n" );
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 3 + 2 + 3 );
|
|
|
|
|
|
|
|
printf( "\tmajor %u\n", (uint32_t) *ssl++ );
|
|
|
|
printf( "\tminor %u\n", (uint32_t) *ssl++ );
|
|
|
|
printf( "\tpath %u\n", (uint32_t) *ssl++ );
|
2020-03-18 15:12:41 +01:00
|
|
|
|
|
|
|
printf( "\nEnabled session and context configuration:\n" );
|
2020-03-17 15:36:52 +01:00
|
|
|
|
|
|
|
session_cfg_flag = ( (int) ssl[0] << 8 ) | ( (int) ssl[1] );
|
|
|
|
ssl += 2;
|
|
|
|
|
|
|
|
context_cfg_flag = ( (int) ssl[0] << 16 ) |
|
|
|
|
( (int) ssl[1] << 8 ) |
|
|
|
|
( (int) ssl[2] ) ;
|
|
|
|
ssl += 3;
|
|
|
|
|
|
|
|
printf_dbg( "Session config flags 0x%04X\n", session_cfg_flag );
|
|
|
|
printf_dbg( "Context config flags 0x%06X\n", context_cfg_flag );
|
|
|
|
|
|
|
|
print_if_bit( "MBEDTLS_HAVE_TIME", SESSION_CONFIG_TIME_BIT, session_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_X509_CRT_PARSE_C", SESSION_CONFIG_CRT_BIT, session_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH", SESSION_CONFIG_MFL_BIT, session_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_SSL_ENCRYPT_THEN_MAC", SESSION_CONFIG_ETM_BIT, session_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_SSL_SESSION_TICKETS", SESSION_CONFIG_TICKET_BIT, session_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_SSL_SESSION_TICKETS and client", SESSION_CONFIG_CLIENT_TICKET_BIT, session_cfg_flag );
|
|
|
|
|
|
|
|
print_if_bit( "MBEDTLS_SSL_DTLS_CONNECTION_ID", CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT, context_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_SSL_DTLS_ANTI_REPLAY", CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT, context_cfg_flag );
|
|
|
|
print_if_bit( "MBEDTLS_SSL_ALPN", CONTEXT_CONFIG_ALPN_BIT, context_cfg_flag );
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 4 );
|
2020-03-17 15:36:52 +01:00
|
|
|
session_len = ( (uint32_t) ssl[0] << 24 ) |
|
|
|
|
( (uint32_t) ssl[1] << 16 ) |
|
|
|
|
( (uint32_t) ssl[2] << 8 ) |
|
|
|
|
( (uint32_t) ssl[3] );
|
|
|
|
ssl += 4;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf_dbg( "Session length %u\n", session_len );
|
2020-03-17 15:36:52 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( session_len );
|
2020-03-18 15:12:41 +01:00
|
|
|
print_deserialized_ssl_session( ssl, session_len, session_cfg_flag );
|
|
|
|
ssl += session_len;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\nRandom bytes:\n\t");
|
|
|
|
|
|
|
|
CHECK_SSL_END( TRANSFORM_RANDBYTE_LEN );
|
|
|
|
print_hex( ssl, TRANSFORM_RANDBYTE_LEN, 22, "\t" );
|
2020-03-18 15:12:41 +01:00
|
|
|
ssl += TRANSFORM_RANDBYTE_LEN;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\nContext others:\n" );
|
|
|
|
|
2020-03-18 15:12:41 +01:00
|
|
|
if( CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT & context_cfg_flag )
|
|
|
|
{
|
|
|
|
uint8_t cid_len;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
2020-03-18 15:12:41 +01:00
|
|
|
cid_len = *ssl++;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf_dbg( "In CID length %u\n", (uint32_t) cid_len );
|
2020-03-18 15:12:41 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tin CID : " );
|
|
|
|
if( cid_len > 0 )
|
|
|
|
{
|
|
|
|
CHECK_SSL_END( cid_len );
|
|
|
|
print_hex( ssl, cid_len, 20, "\t" );
|
|
|
|
ssl += cid_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "none\n" );
|
|
|
|
}
|
2020-03-18 15:12:41 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
2020-03-18 15:12:41 +01:00
|
|
|
cid_len = *ssl++;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf_dbg( "Out CID length %u\n", (uint32_t) cid_len );
|
2020-03-18 15:12:41 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tout CID : " );
|
|
|
|
if( cid_len > 0 )
|
|
|
|
{
|
|
|
|
CHECK_SSL_END( cid_len );
|
|
|
|
print_hex( ssl, cid_len, 20, "\t" );
|
|
|
|
ssl += cid_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "none\n" );
|
|
|
|
}
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT & context_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
uint32_t badmac_seen;
|
|
|
|
|
|
|
|
CHECK_SSL_END( 4 );
|
|
|
|
badmac_seen = ( (uint32_t) ssl[0] << 24 ) |
|
|
|
|
( (uint32_t) ssl[1] << 16 ) |
|
|
|
|
( (uint32_t) ssl[2] << 8 ) |
|
|
|
|
( (uint32_t) ssl[3] );
|
2020-03-18 15:12:41 +01:00
|
|
|
ssl += 4;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tbad MAC seen number : %u\n", badmac_seen );
|
2020-03-18 15:12:41 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
/* value 'in_window_top' from mbedtls_ssl_context */
|
|
|
|
printf( "\tlast validated record sequence no. : " );
|
|
|
|
CHECK_SSL_END( 8 );
|
|
|
|
print_hex( ssl, 8, 20, "" );
|
2020-03-18 15:12:41 +01:00
|
|
|
ssl += 8;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
/* value 'in_window' from mbedtls_ssl_context */
|
|
|
|
printf( "\tbitmask for replay detection : " );
|
|
|
|
CHECK_SSL_END( 8 );
|
|
|
|
print_hex( ssl, 8, 20, "" );
|
2020-03-18 15:12:41 +01:00
|
|
|
ssl += 8;
|
|
|
|
}
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
if( conf_dtls_proto )
|
2020-03-18 15:12:41 +01:00
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
printf( "\tDTLS datagram packing : %s\n",
|
|
|
|
get_enabled_str( ! ( *ssl++ ) ) );
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
/* value 'cur_out_ctr' from mbedtls_ssl_context */
|
|
|
|
printf( "\toutgoing record sequence no. : ");
|
|
|
|
CHECK_SSL_END( 8 );
|
|
|
|
print_hex( ssl, 8, 20, "" );
|
2020-03-18 15:12:41 +01:00
|
|
|
ssl += 8;
|
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
if( conf_dtls_proto )
|
2020-03-18 15:12:41 +01:00
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
uint16_t mtu;
|
|
|
|
CHECK_SSL_END( 2 );
|
|
|
|
mtu = ( ssl[0] << 8 ) | ssl[1];
|
2020-03-18 15:12:41 +01:00
|
|
|
ssl += 2;
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\tMTU : %u\n", mtu );
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( CONTEXT_CONFIG_ALPN_BIT & context_cfg_flag )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
uint8_t alpn_len;
|
|
|
|
|
|
|
|
CHECK_SSL_END( 1 );
|
|
|
|
alpn_len = *ssl++;
|
|
|
|
printf_dbg( "ALPN length %u\n", (uint32_t) alpn_len );
|
|
|
|
|
|
|
|
printf( "\tALPN negotiation : " );
|
|
|
|
CHECK_SSL_END( alpn_len );
|
2020-03-18 15:12:41 +01:00
|
|
|
if( alpn_len > 0 )
|
|
|
|
{
|
|
|
|
if( strlen( (const char*) ssl ) == alpn_len )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "%s\n", ssl );
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\n" );
|
|
|
|
printf_err( "\tALPN negotiation is incorrect\n" );
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
ssl += alpn_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "not selected\n" );
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 17:27:29 +01:00
|
|
|
if( 0 != ( end - ssl ) )
|
2020-03-18 15:12:41 +01:00
|
|
|
{
|
2020-03-18 17:27:29 +01:00
|
|
|
printf_err( "%i bytes left to analyze from context\n", (int32_t)( end - ssl ) );
|
2020-03-18 15:12:41 +01:00
|
|
|
}
|
2020-03-17 15:36:52 +01:00
|
|
|
printf( "\n" );
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:43:22 +01:00
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
enum { SSL_INIT_LEN = 4096 };
|
2020-03-17 09:51:31 +01:00
|
|
|
|
2020-03-16 17:52:56 +01:00
|
|
|
uint32_t b64_counter = 0;
|
2020-03-30 17:09:33 +02:00
|
|
|
uint8_t *b64_buf = NULL;
|
|
|
|
uint8_t *ssl_buf = NULL;
|
|
|
|
size_t b64_max_len = SSL_INIT_LEN;
|
|
|
|
size_t ssl_max_len = SSL_INIT_LEN;
|
|
|
|
size_t ssl_len = 0;
|
|
|
|
|
|
|
|
/* The 'b64_file' is opened when parsing arguments to check that the
|
|
|
|
* file name is correct */
|
2020-03-13 16:26:08 +01:00
|
|
|
parse_arguments( argc, argv );
|
2020-03-13 14:43:22 +01:00
|
|
|
|
2020-03-30 17:09:33 +02:00
|
|
|
if( NULL != b64_file )
|
|
|
|
{
|
|
|
|
b64_buf = malloc( SSL_INIT_LEN );
|
|
|
|
ssl_buf = malloc( SSL_INIT_LEN );
|
|
|
|
|
|
|
|
if( NULL == b64_buf || NULL == ssl_buf )
|
|
|
|
{
|
|
|
|
printf_err( alloc_err );
|
|
|
|
fclose( b64_file );
|
|
|
|
b64_file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:05:22 +01:00
|
|
|
while( NULL != b64_file )
|
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
size_t b64_len = read_next_b64_code( &b64_buf, &b64_max_len );
|
2020-03-17 09:51:31 +01:00
|
|
|
if( b64_len > 0)
|
2020-03-16 14:05:22 +01:00
|
|
|
{
|
2020-03-17 09:51:31 +01:00
|
|
|
int ret;
|
2020-03-30 17:09:33 +02:00
|
|
|
size_t ssl_required_len = b64_len * 3 / 4 + 1;
|
|
|
|
|
|
|
|
/* Allocate more memory if necessary. */
|
|
|
|
if( ssl_required_len > ssl_max_len )
|
|
|
|
{
|
|
|
|
void *ptr = realloc( ssl_buf, ssl_required_len );
|
|
|
|
if( NULL == ptr )
|
|
|
|
{
|
|
|
|
printf_err( alloc_err );
|
|
|
|
fclose( b64_file );
|
|
|
|
b64_file = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssl_buf = ptr;
|
|
|
|
ssl_max_len = ssl_required_len;
|
|
|
|
}
|
2020-03-17 09:51:31 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\nDeserializing number %u:\n", ++b64_counter );
|
2020-03-16 17:52:56 +01:00
|
|
|
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\nBase64 code:\n" );
|
2020-03-30 17:09:33 +02:00
|
|
|
print_b64( b64_buf, b64_len );
|
2020-03-17 09:51:31 +01:00
|
|
|
|
2020-03-30 17:09:33 +02:00
|
|
|
ret = mbedtls_base64_decode( ssl_buf, ssl_max_len, &ssl_len, b64_buf, b64_len );
|
2020-03-17 09:51:31 +01:00
|
|
|
if( ret != 0)
|
|
|
|
{
|
2020-03-30 17:09:33 +02:00
|
|
|
mbedtls_strerror( ret, (char*) b64_buf, b64_max_len );
|
|
|
|
printf_err( "base64 code cannot be decoded - %s\n", b64_buf );
|
2020-03-17 09:51:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( debug )
|
|
|
|
{
|
2020-03-20 12:16:33 +01:00
|
|
|
printf( "\nDecoded data in hex:\n\t");
|
2020-03-30 17:09:33 +02:00
|
|
|
print_hex( ssl_buf, ssl_len, 25, "\t" );
|
2020-03-16 17:52:56 +01:00
|
|
|
}
|
2020-03-16 14:05:22 +01:00
|
|
|
|
2020-03-30 17:09:33 +02:00
|
|
|
print_deserialized_ssl_context( ssl_buf, ssl_len );
|
2020-03-16 17:52:56 +01:00
|
|
|
|
2020-03-16 14:05:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fclose( b64_file );
|
|
|
|
b64_file = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 17:09:33 +02:00
|
|
|
free( b64_buf );
|
|
|
|
free( ssl_buf );
|
|
|
|
|
|
|
|
if( b64_counter > 0 )
|
|
|
|
{
|
|
|
|
printf_dbg( "Finished. Found %u base64 codes\n", b64_counter );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "Finished. No valid base64 code found\n" );
|
|
|
|
}
|
2020-03-16 17:52:56 +01:00
|
|
|
|
2020-03-13 14:43:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2020-04-09 17:00:57 +02:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|