ssl_context_info.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. * MbedTLS SSL context deserializer from base64 code
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * 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.
  18. */
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #include "mbedtls/debug.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_ERROR_C) || \
  28. !defined(MBEDTLS_SSL_TLS_C)
  29. int main( void )
  30. {
  31. printf("MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_ERROR_C and/or "
  32. "MBEDTLS_SSL_TLS_C not defined.\n");
  33. return( 0 );
  34. }
  35. #else
  36. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  37. #define _CRT_SECURE_NO_DEPRECATE 1
  38. #endif
  39. #include <stdint.h>
  40. #include <stdarg.h>
  41. #include <string.h>
  42. #include <time.h>
  43. #include "mbedtls/ssl.h"
  44. #include "mbedtls/error.h"
  45. #include "mbedtls/base64.h"
  46. #include "mbedtls/md.h"
  47. #include "mbedtls/md_internal.h"
  48. #include "mbedtls/x509_crt.h"
  49. #include "mbedtls/ssl_ciphersuites.h"
  50. /*
  51. * This program version
  52. */
  53. #define PROG_NAME "ssl_context_info"
  54. #define VER_MAJOR 0
  55. #define VER_MINOR 1
  56. /*
  57. * Flags copied from the Mbed TLS library.
  58. */
  59. #define SESSION_CONFIG_TIME_BIT ( 1 << 0 )
  60. #define SESSION_CONFIG_CRT_BIT ( 1 << 1 )
  61. #define SESSION_CONFIG_CLIENT_TICKET_BIT ( 1 << 2 )
  62. #define SESSION_CONFIG_MFL_BIT ( 1 << 3 )
  63. #define SESSION_CONFIG_TRUNC_HMAC_BIT ( 1 << 4 )
  64. #define SESSION_CONFIG_ETM_BIT ( 1 << 5 )
  65. #define SESSION_CONFIG_TICKET_BIT ( 1 << 6 )
  66. #define CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ( 1 << 0 )
  67. #define CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ( 1 << 1 )
  68. #define CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ( 1 << 2 )
  69. #define CONTEXT_CONFIG_ALPN_BIT ( 1 << 3 )
  70. #define TRANSFORM_RANDBYTE_LEN 64
  71. /*
  72. * Minimum and maximum number of bytes for specific data: context, sessions,
  73. * certificates, tickets and buffers in the program. The context and session
  74. * size values have been calculated based on the 'print_deserialized_ssl_context()'
  75. * and 'print_deserialized_ssl_session()' content.
  76. */
  77. #define MIN_CONTEXT_LEN 84
  78. #define MIN_SESSION_LEN 88
  79. #define MAX_CONTEXT_LEN 875 /* without session data */
  80. #define MAX_SESSION_LEN 109 /* without certificate and ticket data */
  81. #define MAX_CERTIFICATE_LEN ( ( 1 << 24 ) - 1 )
  82. #define MAX_TICKET_LEN ( ( 1 << 24 ) - 1 )
  83. #define MIN_SERIALIZED_DATA ( MIN_CONTEXT_LEN + MIN_SESSION_LEN )
  84. #define MAX_SERIALIZED_DATA ( MAX_CONTEXT_LEN + MAX_SESSION_LEN + \
  85. MAX_CERTIFICATE_LEN + MAX_TICKET_LEN )
  86. #define MIN_BASE64_LEN ( MIN_SERIALIZED_DATA * 4 / 3 )
  87. #define MAX_BASE64_LEN ( MAX_SERIALIZED_DATA * 4 / 3 + 3 )
  88. /*
  89. * A macro that prevents from reading out of the ssl buffer range.
  90. */
  91. #define CHECK_SSL_END( LEN ) \
  92. do \
  93. { \
  94. if( end - ssl < (int)( LEN ) ) \
  95. { \
  96. printf_err( "%s", buf_ln_err ); \
  97. return; \
  98. } \
  99. } while( 0 )
  100. /*
  101. * Global values
  102. */
  103. FILE *b64_file = NULL; /* file with base64 codes to deserialize */
  104. char conf_keep_peer_certificate = 1; /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE from mbedTLS configuration */
  105. char conf_dtls_proto = 1; /* MBEDTLS_SSL_PROTO_DTLS from mbedTLS configuration */
  106. char debug = 0; /* flag for debug messages */
  107. const char alloc_err[] = "Cannot allocate memory\n";
  108. const char buf_ln_err[] = "Buffer does not have enough data to complete the parsing\n";
  109. /*
  110. * Basic printing functions
  111. */
  112. void print_version( )
  113. {
  114. printf( "%s v%d.%d\n", PROG_NAME, VER_MAJOR, VER_MINOR );
  115. }
  116. void print_usage( )
  117. {
  118. print_version();
  119. printf( "\nThis program is used to deserialize an Mbed TLS SSL session from the base64 code provided\n"
  120. "in the text file. The program can deserialize many codes from one file, but they must be\n"
  121. "separated, e.g. by a newline.\n\n" );
  122. printf(
  123. "Usage:\n"
  124. "\t-f path - Path to the file with base64 code\n"
  125. "\t-v - Show version\n"
  126. "\t-h - Show this usage\n"
  127. "\t-d - Print more information\n"
  128. "\t--keep-peer-cert=0 - Use this option if you know that the Mbed TLS library\n"
  129. "\t has been compiled with the MBEDTLS_SSL_KEEP_PEER_CERTIFICATE\n"
  130. "\t flag. You can also use it if there are some problems with reading\n"
  131. "\t the information about certificate\n"
  132. "\t--dtls-protocol=0 - Use this option if you know that the Mbed TLS library\n"
  133. "\t has been compiled without the MBEDTLS_SSL_PROTO_DTLS flag\n"
  134. "\n"
  135. );
  136. }
  137. void printf_dbg( const char *str, ... )
  138. {
  139. if( debug )
  140. {
  141. va_list args;
  142. va_start( args, str );
  143. printf( "debug: " );
  144. vprintf( str, args );
  145. fflush( stdout );
  146. va_end( args );
  147. }
  148. }
  149. MBEDTLS_PRINTF_ATTRIBUTE( 1, 2 )
  150. void printf_err( const char *str, ... )
  151. {
  152. va_list args;
  153. va_start( args, str );
  154. fflush( stdout );
  155. fprintf( stderr, "ERROR: " );
  156. vfprintf( stderr, str, args );
  157. fflush( stderr );
  158. va_end( args );
  159. }
  160. /*
  161. * Exit from the program in case of error
  162. */
  163. void error_exit()
  164. {
  165. if( NULL != b64_file )
  166. {
  167. fclose( b64_file );
  168. }
  169. exit( -1 );
  170. }
  171. /*
  172. * This function takes the input arguments of this program
  173. */
  174. void parse_arguments( int argc, char *argv[] )
  175. {
  176. int i = 1;
  177. if( argc < 2 )
  178. {
  179. print_usage();
  180. error_exit();
  181. }
  182. while( i < argc )
  183. {
  184. if( strcmp( argv[i], "-d" ) == 0 )
  185. {
  186. debug = 1;
  187. }
  188. else if( strcmp( argv[i], "-h" ) == 0 )
  189. {
  190. print_usage();
  191. }
  192. else if( strcmp( argv[i], "-v" ) == 0 )
  193. {
  194. print_version();
  195. }
  196. else if( strcmp( argv[i], "-f" ) == 0 )
  197. {
  198. if( ++i >= argc )
  199. {
  200. printf_err( "File path is empty\n" );
  201. error_exit();
  202. }
  203. if( NULL != b64_file )
  204. {
  205. printf_err( "Cannot specify more than one file with -f\n" );
  206. error_exit( );
  207. }
  208. if( ( b64_file = fopen( argv[i], "r" )) == NULL )
  209. {
  210. printf_err( "Cannot find file \"%s\"\n", argv[i] );
  211. error_exit();
  212. }
  213. }
  214. else if( strcmp( argv[i], "--keep-peer-cert=0" ) == 0 )
  215. {
  216. conf_keep_peer_certificate = 0;
  217. }
  218. else if( strcmp( argv[i], "--dtls-protocol=0" ) == 0 )
  219. {
  220. conf_dtls_proto = 0;
  221. }
  222. else
  223. {
  224. print_usage();
  225. error_exit();
  226. }
  227. i++;
  228. }
  229. }
  230. /*
  231. * This function prints base64 code to the stdout
  232. */
  233. void print_b64( const uint8_t *b, size_t len )
  234. {
  235. size_t i = 0;
  236. const uint8_t *end = b + len;
  237. printf("\t");
  238. while( b < end )
  239. {
  240. if( ++i > 75 )
  241. {
  242. printf( "\n\t" );
  243. i = 0;
  244. }
  245. printf( "%c", *b++ );
  246. }
  247. printf( "\n" );
  248. fflush( stdout );
  249. }
  250. /*
  251. * This function prints hex code from the buffer to the stdout.
  252. *
  253. * /p b buffer with data to print
  254. * /p len number of bytes to print
  255. * /p in_line number of bytes in one line
  256. * /p prefix prefix for the new lines
  257. */
  258. void print_hex( const uint8_t *b, size_t len,
  259. const size_t in_line, const char *prefix )
  260. {
  261. size_t i = 0;
  262. const uint8_t *end = b + len;
  263. if( prefix == NULL )
  264. {
  265. prefix = "";
  266. }
  267. while( b < end )
  268. {
  269. if( ++i > in_line )
  270. {
  271. printf( "\n%s", prefix );
  272. i = 1;
  273. }
  274. printf( "%02X ", (uint8_t) *b++ );
  275. }
  276. printf("\n");
  277. fflush(stdout);
  278. }
  279. /*
  280. * Print the value of time_t in format e.g. 2020-01-23 13:05:59
  281. */
  282. void print_time( const time_t *time )
  283. {
  284. char buf[20];
  285. struct tm *t = gmtime( time );
  286. static const char format[] = "%Y-%m-%d %H:%M:%S";
  287. if( NULL != t )
  288. {
  289. strftime( buf, sizeof( buf ), format, t );
  290. printf( "%s\n", buf );
  291. }
  292. else
  293. {
  294. printf( "unknown\n" );
  295. }
  296. }
  297. /*
  298. * Print the input string if the bit is set in the value
  299. */
  300. void print_if_bit( const char *str, int bit, int val )
  301. {
  302. if( bit & val )
  303. {
  304. printf( "\t%s\n", str );
  305. }
  306. }
  307. /*
  308. * Return pointer to hardcoded "enabled" or "disabled" depending on the input value
  309. */
  310. const char * get_enabled_str( int is_en )
  311. {
  312. return ( is_en ) ? "enabled" : "disabled";
  313. }
  314. /*
  315. * Return pointer to hardcoded MFL string value depending on the MFL code at the input
  316. */
  317. const char * get_mfl_str( int mfl_code )
  318. {
  319. switch( mfl_code )
  320. {
  321. case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
  322. return "none";
  323. case MBEDTLS_SSL_MAX_FRAG_LEN_512:
  324. return "512";
  325. case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
  326. return "1024";
  327. case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
  328. return "2048";
  329. case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
  330. return "4096";
  331. default:
  332. return "error";
  333. }
  334. }
  335. /*
  336. * Read next base64 code from the 'b64_file'. The 'b64_file' must be opened
  337. * previously. After each call to this function, the internal file position
  338. * indicator of the global b64_file is advanced.
  339. *
  340. * Note - This function checks the size of the input buffer and if necessary,
  341. * increases it to the maximum MAX_BASE64_LEN
  342. *
  343. * /p b64 pointer to the pointer of the buffer for input data
  344. * /p max_len pointer to the current buffer capacity. It can be changed if
  345. * the buffer needs to be increased
  346. *
  347. * \retval number of bytes written in to the b64 buffer or 0 in case no more
  348. * data was found
  349. */
  350. size_t read_next_b64_code( uint8_t **b64, size_t *max_len )
  351. {
  352. int valid_balance = 0; /* balance between valid and invalid characters */
  353. size_t len = 0;
  354. char pad = 0;
  355. int c = 0;
  356. while( EOF != c )
  357. {
  358. char c_valid = 0;
  359. c = fgetc( b64_file );
  360. if( pad > 0 )
  361. {
  362. if( c == '=' && pad == 1 )
  363. {
  364. c_valid = 1;
  365. pad = 2;
  366. }
  367. }
  368. else if( ( c >= 'A' && c <= 'Z' ) ||
  369. ( c >= 'a' && c <= 'z' ) ||
  370. ( c >= '0' && c <= '9' ) ||
  371. c == '+' || c == '/' )
  372. {
  373. c_valid = 1;
  374. }
  375. else if( c == '=' )
  376. {
  377. c_valid = 1;
  378. pad = 1;
  379. }
  380. else if( c == '-' )
  381. {
  382. c = '+';
  383. c_valid = 1;
  384. }
  385. else if( c == '_' )
  386. {
  387. c = '/';
  388. c_valid = 1;
  389. }
  390. if( c_valid )
  391. {
  392. /* A string of characters that could be a base64 code. */
  393. valid_balance++;
  394. if( len < *max_len )
  395. {
  396. ( *b64 )[ len++ ] = c;
  397. }
  398. else if( *max_len < MAX_BASE64_LEN )
  399. {
  400. /* Current buffer is too small, but can be resized. */
  401. void *ptr;
  402. size_t new_size = ( MAX_BASE64_LEN - 4096 > *max_len ) ?
  403. *max_len + 4096 : MAX_BASE64_LEN;
  404. ptr = realloc( *b64, new_size );
  405. if( NULL == ptr )
  406. {
  407. printf_err( alloc_err );
  408. return 0;
  409. }
  410. *b64 = ptr;
  411. *max_len = new_size;
  412. ( *b64 )[ len++ ] = c;
  413. }
  414. else
  415. {
  416. /* Too much data so it will be treated as invalid */
  417. len++;
  418. }
  419. }
  420. else if( len > 0 )
  421. {
  422. /* End of a string that could be a base64 code, but need to check
  423. * that the length of the characters is correct. */
  424. valid_balance--;
  425. if( len < MIN_CONTEXT_LEN )
  426. {
  427. printf_dbg( "The code found is too small to be a SSL context.\n" );
  428. len = pad = 0;
  429. }
  430. else if( len > *max_len )
  431. {
  432. printf_err( "The code found is too large by %" MBEDTLS_PRINTF_SIZET " bytes.\n",
  433. len - *max_len );
  434. len = pad = 0;
  435. }
  436. else if( len % 4 != 0 )
  437. {
  438. printf_err( "The length of the base64 code found should be a multiple of 4.\n" );
  439. len = pad = 0;
  440. }
  441. else
  442. {
  443. /* Base64 code with valid character length. */
  444. return len;
  445. }
  446. }
  447. else
  448. {
  449. valid_balance--;
  450. }
  451. /* Detection of potentially wrong file format like: binary, zip, ISO, etc. */
  452. if( valid_balance < -100 )
  453. {
  454. printf_err( "Too many bad symbols detected. File check aborted.\n" );
  455. return 0;
  456. }
  457. }
  458. printf_dbg( "End of file\n" );
  459. return 0;
  460. }
  461. /*
  462. * This function deserializes and prints to the stdout all obtained information
  463. * about the certificates from provided data.
  464. *
  465. * /p ssl pointer to serialized certificate
  466. * /p len number of bytes in the buffer
  467. */
  468. void print_deserialized_ssl_cert( const uint8_t *ssl, uint32_t len )
  469. {
  470. enum { STRLEN = 4096 };
  471. mbedtls_x509_crt crt;
  472. int ret;
  473. char str[STRLEN];
  474. printf( "\nCertificate:\n" );
  475. mbedtls_x509_crt_init( &crt );
  476. ret = mbedtls_x509_crt_parse_der( &crt, ssl, len );
  477. if( 0 != ret )
  478. {
  479. mbedtls_strerror( ret, str, STRLEN );
  480. printf_err( "Invalid format of X.509 - %s\n", str );
  481. printf( "Cannot deserialize:\n\t" );
  482. print_hex( ssl, len, 25, "\t" );
  483. }
  484. else
  485. {
  486. mbedtls_x509_crt *current = &crt;
  487. while( current != NULL )
  488. {
  489. ret = mbedtls_x509_crt_info( str, STRLEN, "\t", current );
  490. if( 0 > ret )
  491. {
  492. mbedtls_strerror( ret, str, STRLEN );
  493. printf_err( "Cannot write to the output - %s\n", str );
  494. }
  495. else
  496. {
  497. printf( "%s", str );
  498. }
  499. current = current->next;
  500. if( current )
  501. {
  502. printf( "\n" );
  503. }
  504. }
  505. }
  506. mbedtls_x509_crt_free( &crt );
  507. }
  508. /*
  509. * This function deserializes and prints to the stdout all obtained information
  510. * about the session from provided data. This function was built based on
  511. * mbedtls_ssl_session_load(). mbedtls_ssl_session_load() could not be used
  512. * due to dependencies on the mbedTLS configuration.
  513. *
  514. * The data structure in the buffer:
  515. * uint64 start_time;
  516. * uint8 ciphersuite[2]; // defined by the standard
  517. * uint8 compression; // 0 or 1
  518. * uint8 session_id_len; // at most 32
  519. * opaque session_id[32];
  520. * opaque master[48]; // fixed length in the standard
  521. * uint32 verify_result;
  522. * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
  523. * opaque ticket<0..2^24-1>; // length 0 means no ticket
  524. * uint32 ticket_lifetime;
  525. * uint8 mfl_code; // up to 255 according to standard
  526. * uint8 trunc_hmac; // 0 or 1
  527. * uint8 encrypt_then_mac; // 0 or 1
  528. *
  529. * /p ssl pointer to serialized session
  530. * /p len number of bytes in the buffer
  531. * /p session_cfg_flag session configuration flags
  532. */
  533. void print_deserialized_ssl_session( const uint8_t *ssl, uint32_t len,
  534. int session_cfg_flag )
  535. {
  536. const struct mbedtls_ssl_ciphersuite_t * ciphersuite_info;
  537. int ciphersuite_id;
  538. uint32_t cert_len, ticket_len;
  539. uint32_t verify_result, ticket_lifetime;
  540. const uint8_t *end = ssl + len;
  541. printf( "\nSession info:\n" );
  542. if( session_cfg_flag & SESSION_CONFIG_TIME_BIT )
  543. {
  544. uint64_t start;
  545. CHECK_SSL_END( 8 );
  546. start = ( (uint64_t) ssl[0] << 56 ) |
  547. ( (uint64_t) ssl[1] << 48 ) |
  548. ( (uint64_t) ssl[2] << 40 ) |
  549. ( (uint64_t) ssl[3] << 32 ) |
  550. ( (uint64_t) ssl[4] << 24 ) |
  551. ( (uint64_t) ssl[5] << 16 ) |
  552. ( (uint64_t) ssl[6] << 8 ) |
  553. ( (uint64_t) ssl[7] );
  554. ssl += 8;
  555. printf( "\tstart time : " );
  556. print_time( (time_t*) &start );
  557. }
  558. CHECK_SSL_END( 2 );
  559. ciphersuite_id = ( (int) ssl[0] << 8 ) | (int) ssl[1];
  560. printf_dbg( "Ciphersuite ID: %d\n", ciphersuite_id );
  561. ssl += 2;
  562. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
  563. if( ciphersuite_info == NULL )
  564. {
  565. printf_err( "Cannot find ciphersuite info\n" );
  566. }
  567. else
  568. {
  569. const mbedtls_cipher_info_t *cipher_info;
  570. const mbedtls_md_info_t *md_info;
  571. printf( "\tciphersuite : %s\n", ciphersuite_info->name );
  572. printf( "\tcipher flags : 0x%02X\n", ciphersuite_info->flags );
  573. cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
  574. if( cipher_info == NULL )
  575. {
  576. printf_err( "Cannot find cipher info\n" );
  577. }
  578. else
  579. {
  580. printf( "\tcipher : %s\n", cipher_info->name );
  581. }
  582. md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
  583. if( md_info == NULL )
  584. {
  585. printf_err( "Cannot find Message-Digest info\n" );
  586. }
  587. else
  588. {
  589. printf( "\tMessage-Digest : %s\n", md_info->name );
  590. }
  591. }
  592. CHECK_SSL_END( 1 );
  593. printf( "\tcompression : %s\n", get_enabled_str( *ssl++ ) );
  594. /* Note - Here we can get session ID length from serialized data, but we
  595. * use hardcoded 32-bytes length. This approach was taken from
  596. * 'mbedtls_ssl_session_load()'. */
  597. CHECK_SSL_END( 1 + 32 );
  598. printf_dbg( "Session id length: %u\n", (uint32_t) *ssl++ );
  599. printf( "\tsession ID : ");
  600. print_hex( ssl, 32, 16, "\t " );
  601. ssl += 32;
  602. printf( "\tmaster secret : ");
  603. CHECK_SSL_END( 48 );
  604. print_hex( ssl, 48, 16, "\t " );
  605. ssl += 48;
  606. CHECK_SSL_END( 4 );
  607. verify_result = ( (uint32_t) ssl[0] << 24 ) |
  608. ( (uint32_t) ssl[1] << 16 ) |
  609. ( (uint32_t) ssl[2] << 8 ) |
  610. ( (uint32_t) ssl[3] );
  611. ssl += 4;
  612. printf( "\tverify result : 0x%08X\n", verify_result );
  613. if( SESSION_CONFIG_CRT_BIT & session_cfg_flag )
  614. {
  615. if( conf_keep_peer_certificate )
  616. {
  617. CHECK_SSL_END( 3 );
  618. cert_len = ( (uint32_t) ssl[0] << 16 ) |
  619. ( (uint32_t) ssl[1] << 8 ) |
  620. ( (uint32_t) ssl[2] );
  621. ssl += 3;
  622. printf_dbg( "Certificate length: %u\n", cert_len );
  623. if( cert_len > 0 )
  624. {
  625. CHECK_SSL_END( cert_len );
  626. print_deserialized_ssl_cert( ssl, cert_len );
  627. ssl += cert_len;
  628. }
  629. }
  630. else
  631. {
  632. printf( "\tPeer digest : " );
  633. CHECK_SSL_END( 1 );
  634. switch( (mbedtls_md_type_t) *ssl++ )
  635. {
  636. case MBEDTLS_MD_NONE:
  637. printf( "none\n" );
  638. break;
  639. case MBEDTLS_MD_MD2:
  640. printf( "MD2\n" );
  641. break;
  642. case MBEDTLS_MD_MD4:
  643. printf( "MD4\n" );
  644. break;
  645. case MBEDTLS_MD_MD5:
  646. printf( "MD5\n" );
  647. break;
  648. case MBEDTLS_MD_SHA1:
  649. printf( "SHA1\n" );
  650. break;
  651. case MBEDTLS_MD_SHA224:
  652. printf( "SHA224\n" );
  653. break;
  654. case MBEDTLS_MD_SHA256:
  655. printf( "SHA256\n" );
  656. break;
  657. case MBEDTLS_MD_SHA384:
  658. printf( "SHA384\n" );
  659. break;
  660. case MBEDTLS_MD_SHA512:
  661. printf( "SHA512\n" );
  662. break;
  663. case MBEDTLS_MD_RIPEMD160:
  664. printf( "RIPEMD160\n" );
  665. break;
  666. default:
  667. printf( "undefined or erroneous\n" );
  668. break;
  669. }
  670. CHECK_SSL_END( 1 );
  671. cert_len = (uint32_t) *ssl++;
  672. printf_dbg( "Message-Digest length: %u\n", cert_len );
  673. if( cert_len > 0 )
  674. {
  675. printf( "\tPeer digest cert : " );
  676. CHECK_SSL_END( cert_len );
  677. print_hex( ssl, cert_len, 16, "\t " );
  678. ssl += cert_len;
  679. }
  680. }
  681. }
  682. if( SESSION_CONFIG_CLIENT_TICKET_BIT & session_cfg_flag )
  683. {
  684. printf( "\nTicket:\n" );
  685. CHECK_SSL_END( 3 );
  686. ticket_len = ( (uint32_t) ssl[0] << 16 ) |
  687. ( (uint32_t) ssl[1] << 8 ) |
  688. ( (uint32_t) ssl[2] );
  689. ssl += 3;
  690. printf_dbg( "Ticket length: %u\n", ticket_len );
  691. if( ticket_len > 0 )
  692. {
  693. printf( "\t" );
  694. CHECK_SSL_END( ticket_len );
  695. print_hex( ssl, ticket_len, 22, "\t" );
  696. ssl += ticket_len;
  697. printf( "\n" );
  698. }
  699. CHECK_SSL_END( 4 );
  700. ticket_lifetime = ( (uint32_t) ssl[0] << 24 ) |
  701. ( (uint32_t) ssl[1] << 16 ) |
  702. ( (uint32_t) ssl[2] << 8 ) |
  703. ( (uint32_t) ssl[3] );
  704. ssl += 4;
  705. printf( "\tlifetime : %u sec.\n", ticket_lifetime );
  706. }
  707. if( ssl < end )
  708. {
  709. printf( "\nSession others:\n" );
  710. }
  711. if( SESSION_CONFIG_MFL_BIT & session_cfg_flag )
  712. {
  713. CHECK_SSL_END( 1 );
  714. printf( "\tMFL : %s\n", get_mfl_str( *ssl++ ) );
  715. }
  716. if( SESSION_CONFIG_TRUNC_HMAC_BIT & session_cfg_flag )
  717. {
  718. CHECK_SSL_END( 1 );
  719. printf( "\tnegotiate truncated HMAC : %s\n", get_enabled_str( *ssl++ ) );
  720. }
  721. if( SESSION_CONFIG_ETM_BIT & session_cfg_flag )
  722. {
  723. CHECK_SSL_END( 1 );
  724. printf( "\tEncrypt-then-MAC : %s\n", get_enabled_str( *ssl++ ) );
  725. }
  726. if( 0 != ( end - ssl ) )
  727. {
  728. printf_err( "%i bytes left to analyze from session\n", (int32_t)( end - ssl ) );
  729. }
  730. }
  731. /*
  732. * This function deserializes and prints to the stdout all obtained information
  733. * about the context from provided data. This function was built based on
  734. * mbedtls_ssl_context_load(). mbedtls_ssl_context_load() could not be used
  735. * due to dependencies on the mbedTLS configuration and the configuration of
  736. * the context when serialization was created.
  737. *
  738. * The data structure in the buffer:
  739. * // header
  740. * uint8 version[3];
  741. * uint8 configuration[5];
  742. * // session sub-structure
  743. * uint32_t session_len;
  744. * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
  745. * // transform sub-structure
  746. * uint8 random[64]; // ServerHello.random+ClientHello.random
  747. * uint8 in_cid_len;
  748. * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
  749. * uint8 out_cid_len;
  750. * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
  751. * // fields from ssl_context
  752. * uint32 badmac_seen; // DTLS: number of records with failing MAC
  753. * uint64 in_window_top; // DTLS: last validated record seq_num
  754. * uint64 in_window; // DTLS: bitmask for replay protection
  755. * uint8 disable_datagram_packing; // DTLS: only one record per datagram
  756. * uint64 cur_out_ctr; // Record layer: outgoing sequence number
  757. * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
  758. * uint8 alpn_chosen_len;
  759. * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
  760. *
  761. * /p ssl pointer to serialized session
  762. * /p len number of bytes in the buffer
  763. */
  764. void print_deserialized_ssl_context( const uint8_t *ssl, size_t len )
  765. {
  766. const uint8_t *end = ssl + len;
  767. uint32_t session_len;
  768. int session_cfg_flag;
  769. int context_cfg_flag;
  770. printf( "\nMbed TLS version:\n" );
  771. CHECK_SSL_END( 3 + 2 + 3 );
  772. printf( "\tmajor %u\n", (uint32_t) *ssl++ );
  773. printf( "\tminor %u\n", (uint32_t) *ssl++ );
  774. printf( "\tpath %u\n", (uint32_t) *ssl++ );
  775. printf( "\nEnabled session and context configuration:\n" );
  776. session_cfg_flag = ( (int) ssl[0] << 8 ) | ( (int) ssl[1] );
  777. ssl += 2;
  778. context_cfg_flag = ( (int) ssl[0] << 16 ) |
  779. ( (int) ssl[1] << 8 ) |
  780. ( (int) ssl[2] ) ;
  781. ssl += 3;
  782. printf_dbg( "Session config flags 0x%04X\n", session_cfg_flag );
  783. printf_dbg( "Context config flags 0x%06X\n", context_cfg_flag );
  784. print_if_bit( "MBEDTLS_HAVE_TIME", SESSION_CONFIG_TIME_BIT, session_cfg_flag );
  785. print_if_bit( "MBEDTLS_X509_CRT_PARSE_C", SESSION_CONFIG_CRT_BIT, session_cfg_flag );
  786. print_if_bit( "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH", SESSION_CONFIG_MFL_BIT, session_cfg_flag );
  787. print_if_bit( "MBEDTLS_SSL_TRUNCATED_HMAC", SESSION_CONFIG_TRUNC_HMAC_BIT, session_cfg_flag );
  788. print_if_bit( "MBEDTLS_SSL_ENCRYPT_THEN_MAC", SESSION_CONFIG_ETM_BIT, session_cfg_flag );
  789. print_if_bit( "MBEDTLS_SSL_SESSION_TICKETS", SESSION_CONFIG_TICKET_BIT, session_cfg_flag );
  790. print_if_bit( "MBEDTLS_SSL_SESSION_TICKETS and client", SESSION_CONFIG_CLIENT_TICKET_BIT, session_cfg_flag );
  791. print_if_bit( "MBEDTLS_SSL_DTLS_CONNECTION_ID", CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT, context_cfg_flag );
  792. print_if_bit( "MBEDTLS_SSL_DTLS_BADMAC_LIMIT", CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT, context_cfg_flag );
  793. print_if_bit( "MBEDTLS_SSL_DTLS_ANTI_REPLAY", CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT, context_cfg_flag );
  794. print_if_bit( "MBEDTLS_SSL_ALPN", CONTEXT_CONFIG_ALPN_BIT, context_cfg_flag );
  795. CHECK_SSL_END( 4 );
  796. session_len = ( (uint32_t) ssl[0] << 24 ) |
  797. ( (uint32_t) ssl[1] << 16 ) |
  798. ( (uint32_t) ssl[2] << 8 ) |
  799. ( (uint32_t) ssl[3] );
  800. ssl += 4;
  801. printf_dbg( "Session length %u\n", session_len );
  802. CHECK_SSL_END( session_len );
  803. print_deserialized_ssl_session( ssl, session_len, session_cfg_flag );
  804. ssl += session_len;
  805. printf( "\nRandom bytes:\n\t");
  806. CHECK_SSL_END( TRANSFORM_RANDBYTE_LEN );
  807. print_hex( ssl, TRANSFORM_RANDBYTE_LEN, 22, "\t" );
  808. ssl += TRANSFORM_RANDBYTE_LEN;
  809. printf( "\nContext others:\n" );
  810. if( CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT & context_cfg_flag )
  811. {
  812. uint8_t cid_len;
  813. CHECK_SSL_END( 1 );
  814. cid_len = *ssl++;
  815. printf_dbg( "In CID length %u\n", (uint32_t) cid_len );
  816. printf( "\tin CID : " );
  817. if( cid_len > 0 )
  818. {
  819. CHECK_SSL_END( cid_len );
  820. print_hex( ssl, cid_len, 20, "\t" );
  821. ssl += cid_len;
  822. }
  823. else
  824. {
  825. printf( "none\n" );
  826. }
  827. CHECK_SSL_END( 1 );
  828. cid_len = *ssl++;
  829. printf_dbg( "Out CID length %u\n", (uint32_t) cid_len );
  830. printf( "\tout CID : " );
  831. if( cid_len > 0 )
  832. {
  833. CHECK_SSL_END( cid_len );
  834. print_hex( ssl, cid_len, 20, "\t" );
  835. ssl += cid_len;
  836. }
  837. else
  838. {
  839. printf( "none\n" );
  840. }
  841. }
  842. if( CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT & context_cfg_flag )
  843. {
  844. uint32_t badmac_seen;
  845. CHECK_SSL_END( 4 );
  846. badmac_seen = ( (uint32_t) ssl[0] << 24 ) |
  847. ( (uint32_t) ssl[1] << 16 ) |
  848. ( (uint32_t) ssl[2] << 8 ) |
  849. ( (uint32_t) ssl[3] );
  850. ssl += 4;
  851. printf( "\tbad MAC seen number : %u\n", badmac_seen );
  852. /* value 'in_window_top' from mbedtls_ssl_context */
  853. printf( "\tlast validated record sequence no. : " );
  854. CHECK_SSL_END( 8 );
  855. print_hex( ssl, 8, 20, "" );
  856. ssl += 8;
  857. /* value 'in_window' from mbedtls_ssl_context */
  858. printf( "\tbitmask for replay detection : " );
  859. CHECK_SSL_END( 8 );
  860. print_hex( ssl, 8, 20, "" );
  861. ssl += 8;
  862. }
  863. if( conf_dtls_proto )
  864. {
  865. CHECK_SSL_END( 1 );
  866. printf( "\tDTLS datagram packing : %s\n",
  867. get_enabled_str( ! ( *ssl++ ) ) );
  868. }
  869. /* value 'cur_out_ctr' from mbedtls_ssl_context */
  870. printf( "\toutgoing record sequence no. : ");
  871. CHECK_SSL_END( 8 );
  872. print_hex( ssl, 8, 20, "" );
  873. ssl += 8;
  874. if( conf_dtls_proto )
  875. {
  876. uint16_t mtu;
  877. CHECK_SSL_END( 2 );
  878. mtu = ( ssl[0] << 8 ) | ssl[1];
  879. ssl += 2;
  880. printf( "\tMTU : %u\n", mtu );
  881. }
  882. if( CONTEXT_CONFIG_ALPN_BIT & context_cfg_flag )
  883. {
  884. uint8_t alpn_len;
  885. CHECK_SSL_END( 1 );
  886. alpn_len = *ssl++;
  887. printf_dbg( "ALPN length %u\n", (uint32_t) alpn_len );
  888. printf( "\tALPN negotiation : " );
  889. CHECK_SSL_END( alpn_len );
  890. if( alpn_len > 0 )
  891. {
  892. if( strlen( (const char*) ssl ) == alpn_len )
  893. {
  894. printf( "%s\n", ssl );
  895. }
  896. else
  897. {
  898. printf( "\n" );
  899. printf_err( "\tALPN negotiation is incorrect\n" );
  900. }
  901. ssl += alpn_len;
  902. }
  903. else
  904. {
  905. printf( "not selected\n" );
  906. }
  907. }
  908. if( 0 != ( end - ssl ) )
  909. {
  910. printf_err( "%i bytes left to analyze from context\n", (int32_t)( end - ssl ) );
  911. }
  912. printf( "\n" );
  913. }
  914. int main( int argc, char *argv[] )
  915. {
  916. enum { SSL_INIT_LEN = 4096 };
  917. uint32_t b64_counter = 0;
  918. uint8_t *b64_buf = NULL;
  919. uint8_t *ssl_buf = NULL;
  920. size_t b64_max_len = SSL_INIT_LEN;
  921. size_t ssl_max_len = SSL_INIT_LEN;
  922. size_t ssl_len = 0;
  923. /* The 'b64_file' is opened when parsing arguments to check that the
  924. * file name is correct */
  925. parse_arguments( argc, argv );
  926. if( NULL != b64_file )
  927. {
  928. b64_buf = malloc( SSL_INIT_LEN );
  929. ssl_buf = malloc( SSL_INIT_LEN );
  930. if( NULL == b64_buf || NULL == ssl_buf )
  931. {
  932. printf_err( alloc_err );
  933. fclose( b64_file );
  934. b64_file = NULL;
  935. }
  936. }
  937. while( NULL != b64_file )
  938. {
  939. size_t b64_len = read_next_b64_code( &b64_buf, &b64_max_len );
  940. if( b64_len > 0)
  941. {
  942. int ret;
  943. size_t ssl_required_len = b64_len * 3 / 4 + 1;
  944. /* Allocate more memory if necessary. */
  945. if( ssl_required_len > ssl_max_len )
  946. {
  947. void *ptr = realloc( ssl_buf, ssl_required_len );
  948. if( NULL == ptr )
  949. {
  950. printf_err( alloc_err );
  951. fclose( b64_file );
  952. b64_file = NULL;
  953. break;
  954. }
  955. ssl_buf = ptr;
  956. ssl_max_len = ssl_required_len;
  957. }
  958. printf( "\nDeserializing number %u:\n", ++b64_counter );
  959. printf( "\nBase64 code:\n" );
  960. print_b64( b64_buf, b64_len );
  961. ret = mbedtls_base64_decode( ssl_buf, ssl_max_len, &ssl_len, b64_buf, b64_len );
  962. if( ret != 0)
  963. {
  964. mbedtls_strerror( ret, (char*) b64_buf, b64_max_len );
  965. printf_err( "base64 code cannot be decoded - %s\n", b64_buf );
  966. continue;
  967. }
  968. if( debug )
  969. {
  970. printf( "\nDecoded data in hex:\n\t");
  971. print_hex( ssl_buf, ssl_len, 25, "\t" );
  972. }
  973. print_deserialized_ssl_context( ssl_buf, ssl_len );
  974. }
  975. else
  976. {
  977. fclose( b64_file );
  978. b64_file = NULL;
  979. }
  980. }
  981. free( b64_buf );
  982. free( ssl_buf );
  983. if( b64_counter > 0 )
  984. {
  985. printf_dbg( "Finished. Found %u base64 codes\n", b64_counter );
  986. }
  987. else
  988. {
  989. printf( "Finished. No valid base64 code found\n" );
  990. }
  991. return 0;
  992. }
  993. #endif /* MBEDTLS_X509_CRT_PARSE_C */