blob: 19054eb2fda28145eabdf27bb79244113b704976 [file] [log] [blame]
Piotr Nowicki9370f902020-03-13 14:43:22 +01001/*
2 * MbedTLS SSL context deserializer from base64 code
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Piotr Nowicki9370f902020-03-13 14:43:22 +01005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Piotr Nowicki9370f902020-03-13 14:43:22 +010018 */
19
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Bence Szépkútic662b362021-05-27 11:25:03 +020022#include "mbedtls/build_info.h"
Paul Elliottef9ccca2021-12-09 14:48:47 +000023#include "mbedtls/debug.h"
Piotr Nowickif86192f2020-03-26 11:45:42 +010024
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +010025#include <stdio.h>
26#include <stdlib.h>
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +020027
Gilles Peskinef4a6a052020-11-09 14:55:35 +010028#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_ERROR_C) || \
29 !defined(MBEDTLS_SSL_TLS_C)
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +020030int main( void )
31{
Gilles Peskinef4a6a052020-11-09 14:55:35 +010032 printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_ERROR_C and/or "
33 "MBEDTLS_SSL_TLS_C not defined.\n");
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +020034 return( 0 );
35}
36#else
37
38#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
39#define _CRT_SECURE_NO_DEPRECATE 1
40#endif
41
Piotr Nowicki14d31052020-03-16 14:05:22 +010042#include <stdint.h>
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +010043#include <stdarg.h>
44#include <string.h>
Raoul Strackx9ed9bc92020-06-22 14:08:57 +020045#if defined(MBEDTLS_HAVE_TIME)
Piotr Nowickie5fa8b72020-03-20 12:16:33 +010046#include <time.h>
Raoul Strackx9ed9bc92020-06-22 14:08:57 +020047#endif
Piotr Nowickie5fa8b72020-03-20 12:16:33 +010048#include "mbedtls/ssl.h"
Piotr Nowickic7d681c2020-03-17 09:51:31 +010049#include "mbedtls/error.h"
50#include "mbedtls/base64.h"
Piotr Nowicki4e192002020-03-18 17:27:29 +010051#include "mbedtls/md.h"
Piotr Nowickie5fa8b72020-03-20 12:16:33 +010052#include "mbedtls/x509_crt.h"
53#include "mbedtls/ssl_ciphersuites.h"
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +010054
55/*
56 * This program version
57 */
Piotr Nowickibc876d42020-03-26 12:49:15 +010058#define PROG_NAME "ssl_context_info"
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +010059#define VER_MAJOR 0
60#define VER_MINOR 1
61
62/*
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +020063 * Flags copied from the Mbed TLS library.
Piotr Nowicki6b2baf92020-03-17 15:36:52 +010064 */
65#define SESSION_CONFIG_TIME_BIT ( 1 << 0 )
66#define SESSION_CONFIG_CRT_BIT ( 1 << 1 )
67#define SESSION_CONFIG_CLIENT_TICKET_BIT ( 1 << 2 )
68#define SESSION_CONFIG_MFL_BIT ( 1 << 3 )
69#define SESSION_CONFIG_TRUNC_HMAC_BIT ( 1 << 4 )
70#define SESSION_CONFIG_ETM_BIT ( 1 << 5 )
71#define SESSION_CONFIG_TICKET_BIT ( 1 << 6 )
72
73#define CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ( 1 << 0 )
74#define CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ( 1 << 1 )
75#define CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ( 1 << 2 )
76#define CONTEXT_CONFIG_ALPN_BIT ( 1 << 3 )
77
Piotr Nowickiab3ecd82020-03-18 15:12:41 +010078#define TRANSFORM_RANDBYTE_LEN 64
79
Piotr Nowicki6b2baf92020-03-17 15:36:52 +010080/*
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +020081 * Minimum and maximum number of bytes for specific data: context, sessions,
82 * certificates, tickets and buffers in the program. The context and session
83 * size values have been calculated based on the 'print_deserialized_ssl_context()'
84 * and 'print_deserialized_ssl_session()' content.
85 */
86#define MIN_CONTEXT_LEN 84
87#define MIN_SESSION_LEN 88
88
89#define MAX_CONTEXT_LEN 875 /* without session data */
90#define MAX_SESSION_LEN 109 /* without certificate and ticket data */
91#define MAX_CERTIFICATE_LEN ( ( 1 << 24 ) - 1 )
92#define MAX_TICKET_LEN ( ( 1 << 24 ) - 1 )
93
94#define MIN_SERIALIZED_DATA ( MIN_CONTEXT_LEN + MIN_SESSION_LEN )
95#define MAX_SERIALIZED_DATA ( MAX_CONTEXT_LEN + MAX_SESSION_LEN + \
96 MAX_CERTIFICATE_LEN + MAX_TICKET_LEN )
97
98#define MIN_BASE64_LEN ( MIN_SERIALIZED_DATA * 4 / 3 )
99#define MAX_BASE64_LEN ( MAX_SERIALIZED_DATA * 4 / 3 + 3 )
100
101/*
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100102 * A macro that prevents from reading out of the ssl buffer range.
103 */
104#define CHECK_SSL_END( LEN ) \
105do \
106{ \
107 if( end - ssl < (int)( LEN ) ) \
108 { \
109 printf_err( "%s", buf_ln_err ); \
110 return; \
111 } \
112} while( 0 )
113
114/*
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100115 * Global values
116 */
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100117FILE *b64_file = NULL; /* file with base64 codes to deserialize */
118char conf_keep_peer_certificate = 1; /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE from mbedTLS configuration */
119char conf_dtls_proto = 1; /* MBEDTLS_SSL_PROTO_DTLS from mbedTLS configuration */
120char debug = 0; /* flag for debug messages */
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200121const char alloc_err[] = "Cannot allocate memory\n";
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100122const char buf_ln_err[] = "Buffer does not have enough data to complete the parsing\n";
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100123
124/*
125 * Basic printing functions
126 */
127void print_version( )
128{
129 printf( "%s v%d.%d\n", PROG_NAME, VER_MAJOR, VER_MINOR );
130}
131
132void print_usage( )
133{
134 print_version();
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +0200135 printf( "\nThis program is used to deserialize an Mbed TLS SSL session from the base64 code provided\n"
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100136 "in the text file. The program can deserialize many codes from one file, but they must be\n"
137 "separated, e.g. by a newline.\n\n" );
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100138 printf(
139 "Usage:\n"
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100140 "\t-f path - Path to the file with base64 code\n"
141 "\t-v - Show version\n"
142 "\t-h - Show this usage\n"
143 "\t-d - Print more information\n"
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +0200144 "\t--keep-peer-cert=0 - Use this option if you know that the Mbed TLS library\n"
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100145 "\t has been compiled with the MBEDTLS_SSL_KEEP_PEER_CERTIFICATE\n"
146 "\t flag. You can also use it if there are some problems with reading\n"
147 "\t the information about certificate\n"
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +0200148 "\t--dtls-protocol=0 - Use this option if you know that the Mbed TLS library\n"
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100149 "\t has been compiled without the MBEDTLS_SSL_PROTO_DTLS flag\n"
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100150 "\n"
151 );
152}
153
154void printf_dbg( const char *str, ... )
155{
156 if( debug )
157 {
158 va_list args;
159 va_start( args, str );
160 printf( "debug: " );
161 vprintf( str, args );
162 fflush( stdout );
163 va_end( args );
164 }
165}
166
Paul Elliottef9ccca2021-12-09 14:48:47 +0000167MBEDTLS_PRINTF_ATTRIBUTE( 1, 2 )
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100168void printf_err( const char *str, ... )
169{
170 va_list args;
171 va_start( args, str );
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100172 fflush( stdout );
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100173 fprintf( stderr, "ERROR: " );
174 vfprintf( stderr, str, args );
175 fflush( stderr );
176 va_end( args );
177}
178
179/*
180 * Exit from the program in case of error
181 */
182void error_exit()
183{
184 if( NULL != b64_file )
185 {
186 fclose( b64_file );
187 }
188 exit( -1 );
189}
190
191/*
192 * This function takes the input arguments of this program
193 */
194void parse_arguments( int argc, char *argv[] )
195{
196 int i = 1;
197
198 if( argc < 2 )
199 {
200 print_usage();
201 error_exit();
202 }
203
204 while( i < argc )
205 {
206 if( strcmp( argv[i], "-d" ) == 0 )
207 {
208 debug = 1;
209 }
210 else if( strcmp( argv[i], "-h" ) == 0 )
211 {
212 print_usage();
213 }
214 else if( strcmp( argv[i], "-v" ) == 0 )
215 {
216 print_version();
217 }
218 else if( strcmp( argv[i], "-f" ) == 0 )
219 {
220 if( ++i >= argc )
221 {
222 printf_err( "File path is empty\n" );
223 error_exit();
224 }
225
Paul Elliott3820c152021-12-09 12:48:51 +0000226 if( NULL != b64_file )
227 {
228 printf_err( "Cannot specify more than one file with -f\n" );
229 error_exit( );
230 }
231
232 if( ( b64_file = fopen( argv[i], "r" )) == NULL )
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100233 {
234 printf_err( "Cannot find file \"%s\"\n", argv[i] );
235 error_exit();
236 }
237 }
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100238 else if( strcmp( argv[i], "--keep-peer-cert=0" ) == 0 )
239 {
240 conf_keep_peer_certificate = 0;
241 }
242 else if( strcmp( argv[i], "--dtls-protocol=0" ) == 0 )
243 {
244 conf_dtls_proto = 0;
245 }
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +0100246 else
247 {
248 print_usage();
249 error_exit();
250 }
251
252 i++;
253 }
254}
255
Piotr Nowicki14d31052020-03-16 14:05:22 +0100256/*
Piotr Nowicki6842c9b2020-03-16 17:52:56 +0100257 * This function prints base64 code to the stdout
258 */
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100259void print_b64( const uint8_t *b, size_t len )
Piotr Nowicki6842c9b2020-03-16 17:52:56 +0100260{
261 size_t i = 0;
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100262 const uint8_t *end = b + len;
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100263 printf("\t");
Piotr Nowicki6842c9b2020-03-16 17:52:56 +0100264 while( b < end )
265 {
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100266 if( ++i > 75 )
Piotr Nowicki6842c9b2020-03-16 17:52:56 +0100267 {
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100268 printf( "\n\t" );
Piotr Nowicki6842c9b2020-03-16 17:52:56 +0100269 i = 0;
270 }
271 printf( "%c", *b++ );
272 }
273 printf( "\n" );
274 fflush( stdout );
275}
276
277/*
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100278 * This function prints hex code from the buffer to the stdout.
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100279 *
280 * /p b buffer with data to print
281 * /p len number of bytes to print
282 * /p in_line number of bytes in one line
283 * /p prefix prefix for the new lines
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100284 */
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100285void print_hex( const uint8_t *b, size_t len,
286 const size_t in_line, const char *prefix )
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100287{
288 size_t i = 0;
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100289 const uint8_t *end = b + len;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100290
291 if( prefix == NULL )
292 {
293 prefix = "";
294 }
295
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100296 while( b < end )
297 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100298 if( ++i > in_line )
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100299 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100300 printf( "\n%s", prefix );
301 i = 1;
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100302 }
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100303 printf( "%02X ", (uint8_t) *b++ );
Piotr Nowickic7d681c2020-03-17 09:51:31 +0100304 }
305 printf("\n");
306 fflush(stdout);
307}
308
309/*
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100310 * Print the value of time_t in format e.g. 2020-01-23 13:05:59
311 */
Andrzej Kurek541318a2022-02-28 05:51:57 -0500312void print_time( const uint64_t *time )
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100313{
Andrzej Kurek541318a2022-02-28 05:51:57 -0500314#if defined(MBEDTLS_HAVE_TIME)
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100315 char buf[20];
Andrzej Kurek541318a2022-02-28 05:51:57 -0500316 struct tm *t = gmtime( (time_t*) time );
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100317 static const char format[] = "%Y-%m-%d %H:%M:%S";
318 if( NULL != t )
319 {
320 strftime( buf, sizeof( buf ), format, t );
321 printf( "%s\n", buf );
322 }
323 else
324 {
325 printf( "unknown\n" );
326 }
Andrzej Kurek541318a2022-02-28 05:51:57 -0500327#else
328 (void) time;
329 printf( "not supported\n" );
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200330#endif
Andrzej Kurek541318a2022-02-28 05:51:57 -0500331}
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100332
333/*
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100334 * Print the input string if the bit is set in the value
335 */
336void print_if_bit( const char *str, int bit, int val )
337{
338 if( bit & val )
339 {
340 printf( "\t%s\n", str );
341 }
342}
343
344/*
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100345 * Return pointer to hardcoded "enabled" or "disabled" depending on the input value
346 */
347const char * get_enabled_str( int is_en )
348{
349 return ( is_en ) ? "enabled" : "disabled";
350}
351
352/*
353 * Return pointer to hardcoded MFL string value depending on the MFL code at the input
354 */
355const char * get_mfl_str( int mfl_code )
356{
357 switch( mfl_code )
358 {
359 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
360 return "none";
361 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
362 return "512";
363 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
364 return "1024";
365 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
366 return "2048";
367 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
368 return "4096";
369 default:
370 return "error";
371 }
372}
373
374/*
Piotr Nowicki14d31052020-03-16 14:05:22 +0100375 * Read next base64 code from the 'b64_file'. The 'b64_file' must be opened
376 * previously. After each call to this function, the internal file position
377 * indicator of the global b64_file is advanced.
378 *
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200379 * Note - This function checks the size of the input buffer and if necessary,
380 * increases it to the maximum MAX_BASE64_LEN
381 *
382 * /p b64 pointer to the pointer of the buffer for input data
383 * /p max_len pointer to the current buffer capacity. It can be changed if
384 * the buffer needs to be increased
Piotr Nowicki14d31052020-03-16 14:05:22 +0100385 *
386 * \retval number of bytes written in to the b64 buffer or 0 in case no more
387 * data was found
388 */
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200389size_t read_next_b64_code( uint8_t **b64, size_t *max_len )
Piotr Nowicki14d31052020-03-16 14:05:22 +0100390{
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200391 int valid_balance = 0; /* balance between valid and invalid characters */
Piotr Nowicki14d31052020-03-16 14:05:22 +0100392 size_t len = 0;
Piotr Nowicki14d31052020-03-16 14:05:22 +0100393 char pad = 0;
Nayna Jaind696e7d2020-08-13 19:17:53 +0000394 int c = 0;
Piotr Nowicki14d31052020-03-16 14:05:22 +0100395
396 while( EOF != c )
397 {
398 char c_valid = 0;
399
Nayna Jaind696e7d2020-08-13 19:17:53 +0000400 c = fgetc( b64_file );
Piotr Nowicki14d31052020-03-16 14:05:22 +0100401
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200402 if( pad > 0 )
Piotr Nowicki14d31052020-03-16 14:05:22 +0100403 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200404 if( c == '=' && pad == 1 )
Piotr Nowicki14d31052020-03-16 14:05:22 +0100405 {
406 c_valid = 1;
407 pad = 2;
408 }
409 }
410 else if( ( c >= 'A' && c <= 'Z' ) ||
411 ( c >= 'a' && c <= 'z' ) ||
412 ( c >= '0' && c <= '9' ) ||
413 c == '+' || c == '/' )
414 {
415 c_valid = 1;
416 }
417 else if( c == '=' )
418 {
419 c_valid = 1;
420 pad = 1;
421 }
422 else if( c == '-' )
423 {
424 c = '+';
425 c_valid = 1;
426 }
427 else if( c == '_' )
428 {
429 c = '/';
430 c_valid = 1;
431 }
432
433 if( c_valid )
434 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200435 /* A string of characters that could be a base64 code. */
436 valid_balance++;
437
438 if( len < *max_len )
Piotr Nowicki14d31052020-03-16 14:05:22 +0100439 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200440 ( *b64 )[ len++ ] = c;
441 }
442 else if( *max_len < MAX_BASE64_LEN )
443 {
444 /* Current buffer is too small, but can be resized. */
445 void *ptr;
446 size_t new_size = ( MAX_BASE64_LEN - 4096 > *max_len ) ?
447 *max_len + 4096 : MAX_BASE64_LEN;
448
449 ptr = realloc( *b64, new_size );
450 if( NULL == ptr )
451 {
452 printf_err( alloc_err );
453 return 0;
454 }
455 *b64 = ptr;
456 *max_len = new_size;
457 ( *b64 )[ len++ ] = c;
Piotr Nowicki14d31052020-03-16 14:05:22 +0100458 }
459 else
460 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200461 /* Too much data so it will be treated as invalid */
462 len++;
Piotr Nowicki14d31052020-03-16 14:05:22 +0100463 }
464 }
465 else if( len > 0 )
466 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200467 /* End of a string that could be a base64 code, but need to check
468 * that the length of the characters is correct. */
469
470 valid_balance--;
471
472 if( len < MIN_CONTEXT_LEN )
Piotr Nowicki14d31052020-03-16 14:05:22 +0100473 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200474 printf_dbg( "The code found is too small to be a SSL context.\n" );
475 len = pad = 0;
Piotr Nowicki14d31052020-03-16 14:05:22 +0100476 }
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200477 else if( len > *max_len )
478 {
Paul Elliottef9ccca2021-12-09 14:48:47 +0000479 printf_err( "The code found is too large by %" MBEDTLS_PRINTF_SIZET " bytes.\n",
480 len - *max_len );
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200481 len = pad = 0;
482 }
483 else if( len % 4 != 0 )
484 {
485 printf_err( "The length of the base64 code found should be a multiple of 4.\n" );
486 len = pad = 0;
487 }
488 else
489 {
490 /* Base64 code with valid character length. */
491 return len;
492 }
493 }
494 else
495 {
496 valid_balance--;
497 }
498
499 /* Detection of potentially wrong file format like: binary, zip, ISO, etc. */
500 if( valid_balance < -100 )
501 {
502 printf_err( "Too many bad symbols detected. File check aborted.\n" );
503 return 0;
Piotr Nowicki14d31052020-03-16 14:05:22 +0100504 }
505 }
506
507 printf_dbg( "End of file\n" );
508 return 0;
509}
510
Hanno Becker2c2722d2020-10-09 09:36:23 +0100511#if !defined(MBEDTLS_X509_REMOVE_INFO)
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100512/*
513 * This function deserializes and prints to the stdout all obtained information
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100514 * about the certificates from provided data.
515 *
516 * /p ssl pointer to serialized certificate
517 * /p len number of bytes in the buffer
518*/
519void print_deserialized_ssl_cert( const uint8_t *ssl, uint32_t len )
520{
521 enum { STRLEN = 4096 };
522 mbedtls_x509_crt crt;
523 int ret;
524 char str[STRLEN];
525
526 printf( "\nCertificate:\n" );
527
528 mbedtls_x509_crt_init( &crt );
529 ret = mbedtls_x509_crt_parse_der( &crt, ssl, len );
530 if( 0 != ret )
531 {
532 mbedtls_strerror( ret, str, STRLEN );
533 printf_err( "Invalid format of X.509 - %s\n", str );
534 printf( "Cannot deserialize:\n\t" );
535 print_hex( ssl, len, 25, "\t" );
536 }
537 else
538 {
539 mbedtls_x509_crt *current = &crt;
540
541 while( current != NULL )
542 {
543 ret = mbedtls_x509_crt_info( str, STRLEN, "\t", current );
544 if( 0 > ret )
545 {
546 mbedtls_strerror( ret, str, STRLEN );
547 printf_err( "Cannot write to the output - %s\n", str );
548 }
549 else
550 {
551 printf( "%s", str );
552 }
553
554 current = current->next;
555
556 if( current )
557 {
558 printf( "\n" );
559 }
560
561 }
562 }
563
564 mbedtls_x509_crt_free( &crt );
565}
Hanno Becker2c2722d2020-10-09 09:36:23 +0100566#endif /* !MBEDTLS_X509_REMOVE_INFO */
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100567
568/*
569 * This function deserializes and prints to the stdout all obtained information
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100570 * about the session from provided data. This function was built based on
571 * mbedtls_ssl_session_load(). mbedtls_ssl_session_load() could not be used
572 * due to dependencies on the mbedTLS configuration.
573 *
574 * The data structure in the buffer:
575 * uint64 start_time;
576 * uint8 ciphersuite[2]; // defined by the standard
577 * uint8 compression; // 0 or 1
578 * uint8 session_id_len; // at most 32
579 * opaque session_id[32];
580 * opaque master[48]; // fixed length in the standard
581 * uint32 verify_result;
582 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
583 * opaque ticket<0..2^24-1>; // length 0 means no ticket
584 * uint32 ticket_lifetime;
585 * uint8 mfl_code; // up to 255 according to standard
586 * uint8 trunc_hmac; // 0 or 1
587 * uint8 encrypt_then_mac; // 0 or 1
588 *
589 * /p ssl pointer to serialized session
590 * /p len number of bytes in the buffer
591 * /p session_cfg_flag session configuration flags
592 */
593void print_deserialized_ssl_session( const uint8_t *ssl, uint32_t len,
594 int session_cfg_flag )
595{
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100596 const struct mbedtls_ssl_ciphersuite_t * ciphersuite_info;
597 int ciphersuite_id;
598 uint32_t cert_len, ticket_len;
Piotr Nowicki4e192002020-03-18 17:27:29 +0100599 uint32_t verify_result, ticket_lifetime;
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100600 const uint8_t *end = ssl + len;
Piotr Nowicki4e192002020-03-18 17:27:29 +0100601
602 printf( "\nSession info:\n" );
603
604 if( session_cfg_flag & SESSION_CONFIG_TIME_BIT )
605 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100606 uint64_t start;
607 CHECK_SSL_END( 8 );
608 start = ( (uint64_t) ssl[0] << 56 ) |
609 ( (uint64_t) ssl[1] << 48 ) |
610 ( (uint64_t) ssl[2] << 40 ) |
611 ( (uint64_t) ssl[3] << 32 ) |
612 ( (uint64_t) ssl[4] << 24 ) |
613 ( (uint64_t) ssl[5] << 16 ) |
614 ( (uint64_t) ssl[6] << 8 ) |
615 ( (uint64_t) ssl[7] );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100616 ssl += 8;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100617 printf( "\tstart time : " );
Andrzej Kurek541318a2022-02-28 05:51:57 -0500618 print_time( &start );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100619 }
620
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100621 CHECK_SSL_END( 2 );
622 ciphersuite_id = ( (int) ssl[0] << 8 ) | (int) ssl[1];
623 printf_dbg( "Ciphersuite ID: %d\n", ciphersuite_id );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100624 ssl += 2;
625
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100626 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
627 if( ciphersuite_info == NULL )
628 {
629 printf_err( "Cannot find ciphersuite info\n" );
630 }
631 else
632 {
633 const mbedtls_cipher_info_t *cipher_info;
634 const mbedtls_md_info_t *md_info;
Piotr Nowicki4e192002020-03-18 17:27:29 +0100635
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100636 printf( "\tciphersuite : %s\n", ciphersuite_info->name );
637 printf( "\tcipher flags : 0x%02X\n", ciphersuite_info->flags );
638
639 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
640 if( cipher_info == NULL )
641 {
642 printf_err( "Cannot find cipher info\n" );
643 }
644 else
645 {
646 printf( "\tcipher : %s\n", cipher_info->name );
647 }
648
649 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
650 if( md_info == NULL )
651 {
652 printf_err( "Cannot find Message-Digest info\n" );
653 }
654 else
655 {
Chris Jonesa1df4942021-03-11 17:44:43 +0000656 printf( "\tMessage-Digest : %s\n", mbedtls_md_get_name( md_info ) );
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100657 }
658 }
659
660 CHECK_SSL_END( 1 );
661 printf( "\tcompression : %s\n", get_enabled_str( *ssl++ ) );
662
663 /* Note - Here we can get session ID length from serialized data, but we
664 * use hardcoded 32-bytes length. This approach was taken from
665 * 'mbedtls_ssl_session_load()'. */
666 CHECK_SSL_END( 1 + 32 );
667 printf_dbg( "Session id length: %u\n", (uint32_t) *ssl++ );
668 printf( "\tsession ID : ");
669 print_hex( ssl, 32, 16, "\t " );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100670 ssl += 32;
671
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100672 printf( "\tmaster secret : ");
673 CHECK_SSL_END( 48 );
674 print_hex( ssl, 48, 16, "\t " );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100675 ssl += 48;
676
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100677 CHECK_SSL_END( 4 );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100678 verify_result = ( (uint32_t) ssl[0] << 24 ) |
679 ( (uint32_t) ssl[1] << 16 ) |
680 ( (uint32_t) ssl[2] << 8 ) |
681 ( (uint32_t) ssl[3] );
682 ssl += 4;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100683 printf( "\tverify result : 0x%08X\n", verify_result );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100684
685 if( SESSION_CONFIG_CRT_BIT & session_cfg_flag )
686 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100687 if( conf_keep_peer_certificate )
Piotr Nowicki4e192002020-03-18 17:27:29 +0100688 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100689 CHECK_SSL_END( 3 );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100690 cert_len = ( (uint32_t) ssl[0] << 16 ) |
691 ( (uint32_t) ssl[1] << 8 ) |
692 ( (uint32_t) ssl[2] );
693 ssl += 3;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100694 printf_dbg( "Certificate length: %u\n", cert_len );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100695
696 if( cert_len > 0 )
697 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100698 CHECK_SSL_END( cert_len );
Hanno Becker2c2722d2020-10-09 09:36:23 +0100699#if !defined(MBEDTLS_X509_REMOVE_INFO)
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100700 print_deserialized_ssl_cert( ssl, cert_len );
Hanno Becker2c2722d2020-10-09 09:36:23 +0100701#endif
Piotr Nowicki4e192002020-03-18 17:27:29 +0100702 ssl += cert_len;
703 }
704 }
705 else
706 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100707 printf( "\tPeer digest : " );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100708
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100709 CHECK_SSL_END( 1 );
710 switch( (mbedtls_md_type_t) *ssl++ )
Piotr Nowicki4e192002020-03-18 17:27:29 +0100711 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100712 case MBEDTLS_MD_NONE:
713 printf( "none\n" );
714 break;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100715 case MBEDTLS_MD_MD5:
716 printf( "MD5\n" );
717 break;
718 case MBEDTLS_MD_SHA1:
719 printf( "SHA1\n" );
720 break;
721 case MBEDTLS_MD_SHA224:
722 printf( "SHA224\n" );
723 break;
724 case MBEDTLS_MD_SHA256:
725 printf( "SHA256\n" );
726 break;
727 case MBEDTLS_MD_SHA384:
728 printf( "SHA384\n" );
729 break;
730 case MBEDTLS_MD_SHA512:
731 printf( "SHA512\n" );
732 break;
733 case MBEDTLS_MD_RIPEMD160:
734 printf( "RIPEMD160\n" );
735 break;
736 default:
737 printf( "undefined or erroneous\n" );
738 break;
739 }
740
741 CHECK_SSL_END( 1 );
742 cert_len = (uint32_t) *ssl++;
743 printf_dbg( "Message-Digest length: %u\n", cert_len );
744
745 if( cert_len > 0 )
746 {
747 printf( "\tPeer digest cert : " );
748 CHECK_SSL_END( cert_len );
749 print_hex( ssl, cert_len, 16, "\t " );
750 ssl += cert_len;
Piotr Nowicki4e192002020-03-18 17:27:29 +0100751 }
752 }
753 }
754
755 if( SESSION_CONFIG_CLIENT_TICKET_BIT & session_cfg_flag )
756 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100757 printf( "\nTicket:\n" );
758
759 CHECK_SSL_END( 3 );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100760 ticket_len = ( (uint32_t) ssl[0] << 16 ) |
761 ( (uint32_t) ssl[1] << 8 ) |
762 ( (uint32_t) ssl[2] );
763 ssl += 3;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100764 printf_dbg( "Ticket length: %u\n", ticket_len );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100765
766 if( ticket_len > 0 )
767 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100768 printf( "\t" );
769 CHECK_SSL_END( ticket_len );
770 print_hex( ssl, ticket_len, 22, "\t" );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100771 ssl += ticket_len;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100772 printf( "\n" );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100773 }
774
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100775 CHECK_SSL_END( 4 );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100776 ticket_lifetime = ( (uint32_t) ssl[0] << 24 ) |
777 ( (uint32_t) ssl[1] << 16 ) |
778 ( (uint32_t) ssl[2] << 8 ) |
779 ( (uint32_t) ssl[3] );
780 ssl += 4;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100781 printf( "\tlifetime : %u sec.\n", ticket_lifetime );
782 }
783
784 if( ssl < end )
785 {
786 printf( "\nSession others:\n" );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100787 }
788
789 if( SESSION_CONFIG_MFL_BIT & session_cfg_flag )
790 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100791 CHECK_SSL_END( 1 );
792 printf( "\tMFL : %s\n", get_mfl_str( *ssl++ ) );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100793 }
794
795 if( SESSION_CONFIG_TRUNC_HMAC_BIT & session_cfg_flag )
796 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100797 CHECK_SSL_END( 1 );
798 printf( "\tnegotiate truncated HMAC : %s\n", get_enabled_str( *ssl++ ) );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100799 }
800
801 if( SESSION_CONFIG_ETM_BIT & session_cfg_flag )
802 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100803 CHECK_SSL_END( 1 );
804 printf( "\tEncrypt-then-MAC : %s\n", get_enabled_str( *ssl++ ) );
Piotr Nowicki4e192002020-03-18 17:27:29 +0100805 }
806
807 if( 0 != ( end - ssl ) )
808 {
809 printf_err( "%i bytes left to analyze from session\n", (int32_t)( end - ssl ) );
810 }
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100811}
812
813/*
814 * This function deserializes and prints to the stdout all obtained information
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100815 * about the context from provided data. This function was built based on
816 * mbedtls_ssl_context_load(). mbedtls_ssl_context_load() could not be used
817 * due to dependencies on the mbedTLS configuration and the configuration of
818 * the context when serialization was created.
819 *
820 * The data structure in the buffer:
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200821 * // header
822 * uint8 version[3];
823 * uint8 configuration[5];
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100824 * // session sub-structure
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200825 * uint32_t session_len;
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100826 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
827 * // transform sub-structure
828 * uint8 random[64]; // ServerHello.random+ClientHello.random
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200829 * uint8 in_cid_len;
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100830 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200831 * uint8 out_cid_len;
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100832 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
833 * // fields from ssl_context
834 * uint32 badmac_seen; // DTLS: number of records with failing MAC
835 * uint64 in_window_top; // DTLS: last validated record seq_num
836 * uint64 in_window; // DTLS: bitmask for replay protection
837 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
838 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
839 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +0200840 * uint8 alpn_chosen_len;
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100841 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
842 *
843 * /p ssl pointer to serialized session
844 * /p len number of bytes in the buffer
845 */
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100846void print_deserialized_ssl_context( const uint8_t *ssl, size_t len )
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100847{
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100848 const uint8_t *end = ssl + len;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100849 uint32_t session_len;
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100850 int session_cfg_flag;
851 int context_cfg_flag;
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100852
853 printf( "\nMbed TLS version:\n" );
854
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100855 CHECK_SSL_END( 3 + 2 + 3 );
856
857 printf( "\tmajor %u\n", (uint32_t) *ssl++ );
858 printf( "\tminor %u\n", (uint32_t) *ssl++ );
859 printf( "\tpath %u\n", (uint32_t) *ssl++ );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100860
861 printf( "\nEnabled session and context configuration:\n" );
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100862
863 session_cfg_flag = ( (int) ssl[0] << 8 ) | ( (int) ssl[1] );
864 ssl += 2;
865
866 context_cfg_flag = ( (int) ssl[0] << 16 ) |
867 ( (int) ssl[1] << 8 ) |
868 ( (int) ssl[2] ) ;
869 ssl += 3;
870
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100871 printf_dbg( "Session config flags 0x%04X\n", session_cfg_flag );
872 printf_dbg( "Context config flags 0x%06X\n", context_cfg_flag );
873
874 print_if_bit( "MBEDTLS_HAVE_TIME", SESSION_CONFIG_TIME_BIT, session_cfg_flag );
875 print_if_bit( "MBEDTLS_X509_CRT_PARSE_C", SESSION_CONFIG_CRT_BIT, session_cfg_flag );
876 print_if_bit( "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH", SESSION_CONFIG_MFL_BIT, session_cfg_flag );
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100877 print_if_bit( "MBEDTLS_SSL_ENCRYPT_THEN_MAC", SESSION_CONFIG_ETM_BIT, session_cfg_flag );
878 print_if_bit( "MBEDTLS_SSL_SESSION_TICKETS", SESSION_CONFIG_TICKET_BIT, session_cfg_flag );
879 print_if_bit( "MBEDTLS_SSL_SESSION_TICKETS and client", SESSION_CONFIG_CLIENT_TICKET_BIT, session_cfg_flag );
880
881 print_if_bit( "MBEDTLS_SSL_DTLS_CONNECTION_ID", CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT, context_cfg_flag );
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100882 print_if_bit( "MBEDTLS_SSL_DTLS_ANTI_REPLAY", CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT, context_cfg_flag );
883 print_if_bit( "MBEDTLS_SSL_ALPN", CONTEXT_CONFIG_ALPN_BIT, context_cfg_flag );
884
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100885 CHECK_SSL_END( 4 );
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100886 session_len = ( (uint32_t) ssl[0] << 24 ) |
887 ( (uint32_t) ssl[1] << 16 ) |
888 ( (uint32_t) ssl[2] << 8 ) |
889 ( (uint32_t) ssl[3] );
890 ssl += 4;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100891 printf_dbg( "Session length %u\n", session_len );
Piotr Nowicki6b2baf92020-03-17 15:36:52 +0100892
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100893 CHECK_SSL_END( session_len );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100894 print_deserialized_ssl_session( ssl, session_len, session_cfg_flag );
895 ssl += session_len;
896
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100897 printf( "\nRandom bytes:\n\t");
898
899 CHECK_SSL_END( TRANSFORM_RANDBYTE_LEN );
900 print_hex( ssl, TRANSFORM_RANDBYTE_LEN, 22, "\t" );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100901 ssl += TRANSFORM_RANDBYTE_LEN;
902
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100903 printf( "\nContext others:\n" );
904
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100905 if( CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT & context_cfg_flag )
906 {
907 uint8_t cid_len;
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100908
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100909 CHECK_SSL_END( 1 );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100910 cid_len = *ssl++;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100911 printf_dbg( "In CID length %u\n", (uint32_t) cid_len );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100912
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100913 printf( "\tin CID : " );
914 if( cid_len > 0 )
915 {
916 CHECK_SSL_END( cid_len );
917 print_hex( ssl, cid_len, 20, "\t" );
918 ssl += cid_len;
919 }
920 else
921 {
922 printf( "none\n" );
923 }
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100924
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100925 CHECK_SSL_END( 1 );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100926 cid_len = *ssl++;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100927 printf_dbg( "Out CID length %u\n", (uint32_t) cid_len );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100928
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100929 printf( "\tout CID : " );
930 if( cid_len > 0 )
931 {
932 CHECK_SSL_END( cid_len );
933 print_hex( ssl, cid_len, 20, "\t" );
934 ssl += cid_len;
935 }
936 else
937 {
938 printf( "none\n" );
939 }
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100940 }
941
942 if( CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT & context_cfg_flag )
943 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100944 uint32_t badmac_seen;
945
946 CHECK_SSL_END( 4 );
947 badmac_seen = ( (uint32_t) ssl[0] << 24 ) |
948 ( (uint32_t) ssl[1] << 16 ) |
949 ( (uint32_t) ssl[2] << 8 ) |
950 ( (uint32_t) ssl[3] );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100951 ssl += 4;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100952 printf( "\tbad MAC seen number : %u\n", badmac_seen );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100953
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100954 /* value 'in_window_top' from mbedtls_ssl_context */
955 printf( "\tlast validated record sequence no. : " );
956 CHECK_SSL_END( 8 );
957 print_hex( ssl, 8, 20, "" );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100958 ssl += 8;
959
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100960 /* value 'in_window' from mbedtls_ssl_context */
961 printf( "\tbitmask for replay detection : " );
962 CHECK_SSL_END( 8 );
963 print_hex( ssl, 8, 20, "" );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100964 ssl += 8;
965 }
966
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100967 if( conf_dtls_proto )
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100968 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100969 CHECK_SSL_END( 1 );
970 printf( "\tDTLS datagram packing : %s\n",
971 get_enabled_str( ! ( *ssl++ ) ) );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100972 }
973
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100974 /* value 'cur_out_ctr' from mbedtls_ssl_context */
975 printf( "\toutgoing record sequence no. : ");
976 CHECK_SSL_END( 8 );
977 print_hex( ssl, 8, 20, "" );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100978 ssl += 8;
979
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100980 if( conf_dtls_proto )
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100981 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100982 uint16_t mtu;
983 CHECK_SSL_END( 2 );
984 mtu = ( ssl[0] << 8 ) | ssl[1];
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100985 ssl += 2;
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100986 printf( "\tMTU : %u\n", mtu );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +0100987 }
988
989
990 if( CONTEXT_CONFIG_ALPN_BIT & context_cfg_flag )
991 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +0100992 uint8_t alpn_len;
993
994 CHECK_SSL_END( 1 );
995 alpn_len = *ssl++;
996 printf_dbg( "ALPN length %u\n", (uint32_t) alpn_len );
997
998 printf( "\tALPN negotiation : " );
999 CHECK_SSL_END( alpn_len );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +01001000 if( alpn_len > 0 )
1001 {
1002 if( strlen( (const char*) ssl ) == alpn_len )
1003 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +01001004 printf( "%s\n", ssl );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +01001005 }
1006 else
1007 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +01001008 printf( "\n" );
1009 printf_err( "\tALPN negotiation is incorrect\n" );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +01001010 }
1011 ssl += alpn_len;
1012 }
1013 else
1014 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +01001015 printf( "not selected\n" );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +01001016 }
1017 }
1018
Piotr Nowicki4e192002020-03-18 17:27:29 +01001019 if( 0 != ( end - ssl ) )
Piotr Nowickiab3ecd82020-03-18 15:12:41 +01001020 {
Piotr Nowicki4e192002020-03-18 17:27:29 +01001021 printf_err( "%i bytes left to analyze from context\n", (int32_t)( end - ssl ) );
Piotr Nowickiab3ecd82020-03-18 15:12:41 +01001022 }
Piotr Nowicki6b2baf92020-03-17 15:36:52 +01001023 printf( "\n" );
1024}
1025
Piotr Nowicki9370f902020-03-13 14:43:22 +01001026int main( int argc, char *argv[] )
1027{
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001028 enum { SSL_INIT_LEN = 4096 };
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001029
Piotr Nowicki6842c9b2020-03-16 17:52:56 +01001030 uint32_t b64_counter = 0;
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001031 uint8_t *b64_buf = NULL;
1032 uint8_t *ssl_buf = NULL;
1033 size_t b64_max_len = SSL_INIT_LEN;
1034 size_t ssl_max_len = SSL_INIT_LEN;
1035 size_t ssl_len = 0;
Piotr Nowicki6842c9b2020-03-16 17:52:56 +01001036
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001037 /* The 'b64_file' is opened when parsing arguments to check that the
1038 * file name is correct */
Piotr Nowicki88ebbbf2020-03-13 16:26:08 +01001039 parse_arguments( argc, argv );
Piotr Nowicki9370f902020-03-13 14:43:22 +01001040
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001041 if( NULL != b64_file )
1042 {
1043 b64_buf = malloc( SSL_INIT_LEN );
1044 ssl_buf = malloc( SSL_INIT_LEN );
1045
1046 if( NULL == b64_buf || NULL == ssl_buf )
1047 {
1048 printf_err( alloc_err );
1049 fclose( b64_file );
1050 b64_file = NULL;
1051 }
1052 }
1053
Piotr Nowicki14d31052020-03-16 14:05:22 +01001054 while( NULL != b64_file )
1055 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001056 size_t b64_len = read_next_b64_code( &b64_buf, &b64_max_len );
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001057 if( b64_len > 0)
Piotr Nowicki14d31052020-03-16 14:05:22 +01001058 {
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001059 int ret;
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001060 size_t ssl_required_len = b64_len * 3 / 4 + 1;
1061
1062 /* Allocate more memory if necessary. */
1063 if( ssl_required_len > ssl_max_len )
1064 {
1065 void *ptr = realloc( ssl_buf, ssl_required_len );
1066 if( NULL == ptr )
1067 {
1068 printf_err( alloc_err );
1069 fclose( b64_file );
1070 b64_file = NULL;
1071 break;
1072 }
1073 ssl_buf = ptr;
1074 ssl_max_len = ssl_required_len;
1075 }
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001076
Piotr Nowickie5fa8b72020-03-20 12:16:33 +01001077 printf( "\nDeserializing number %u:\n", ++b64_counter );
Piotr Nowicki6842c9b2020-03-16 17:52:56 +01001078
Piotr Nowickie5fa8b72020-03-20 12:16:33 +01001079 printf( "\nBase64 code:\n" );
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001080 print_b64( b64_buf, b64_len );
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001081
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001082 ret = mbedtls_base64_decode( ssl_buf, ssl_max_len, &ssl_len, b64_buf, b64_len );
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001083 if( ret != 0)
1084 {
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001085 mbedtls_strerror( ret, (char*) b64_buf, b64_max_len );
1086 printf_err( "base64 code cannot be decoded - %s\n", b64_buf );
Piotr Nowickic7d681c2020-03-17 09:51:31 +01001087 continue;
1088 }
1089
1090 if( debug )
1091 {
Piotr Nowickie5fa8b72020-03-20 12:16:33 +01001092 printf( "\nDecoded data in hex:\n\t");
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001093 print_hex( ssl_buf, ssl_len, 25, "\t" );
Piotr Nowicki6842c9b2020-03-16 17:52:56 +01001094 }
Piotr Nowicki14d31052020-03-16 14:05:22 +01001095
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001096 print_deserialized_ssl_context( ssl_buf, ssl_len );
Piotr Nowicki6842c9b2020-03-16 17:52:56 +01001097
Piotr Nowicki14d31052020-03-16 14:05:22 +01001098 }
1099 else
1100 {
1101 fclose( b64_file );
1102 b64_file = NULL;
1103 }
1104 }
1105
Piotr Nowicki02cc3fb2020-03-30 17:09:33 +02001106 free( b64_buf );
1107 free( ssl_buf );
1108
1109 if( b64_counter > 0 )
1110 {
1111 printf_dbg( "Finished. Found %u base64 codes\n", b64_counter );
1112 }
1113 else
1114 {
1115 printf( "Finished. No valid base64 code found\n" );
1116 }
Piotr Nowicki6842c9b2020-03-16 17:52:56 +01001117
Piotr Nowicki9370f902020-03-13 14:43:22 +01001118 return 0;
1119}
Piotr Nowicki97dcb1c2020-04-09 17:00:57 +02001120
1121#endif /* MBEDTLS_X509_CRT_PARSE_C */