• 运行mbedtls自带Demo ssl_client的记录


    概述:

            运行mbedtls自带  ssl demo的记录;

    操作过程:

            编译Demo,请看我专栏中的相关文章

            先运行服务端程序,运行ssl_server.exe,运行结果如下:

    1. mbedtls-3.2.1\mbedtls-3.2.1\programs\ssl> .\ssl_server.exe
    2. . Seeding the random number generator... ok
    3. . Loading the server cert. and key... ok
    4. . Bind on https://localhost:4433/ ... ok
    5. . Setting up the SSL data.... ok
    6. . Waiting for a remote connection ... ok
    7. . Performing the SSL/TLS handshake... ok
    8. < Read from client: 18 bytes read
    9. GET / HTTP/1.0
    10. > Write to client: 156 bytes written
    11. HTTP/1.0 200 OK
    12. Content-Type: text/html
    13. mbed TLS Test Server

    14. Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256

    15. . Closing the connection... ok
    16. . Waiting for a remote connection ... ok
    17. . Performing the SSL/TLS handshake... ok
    18. < Read from client: 18 bytes read
    19. GET / HTTP/1.0
    20. > Write to client: 156 bytes written
    21. HTTP/1.0 200 OK
    22. Content-Type: text/html
    23. mbed TLS Test Server

    24. Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256

    25. . Closing the connection... ok
    26. . Waiting for a remote connection ...

    再运行服务端,ssl_client1.exe, 运行结果如下;

    1. mbedtls-3.2.1\mbedtls-3.2.1\programs\ssl> .\ssl_client1.exe
    2. . Seeding the random number generator... ok
    3. . Loading the CA root certificate ... ok (0 skipped)
    4. . Connecting to tcp/localhost/4433... ok
    5. . Setting up the SSL/TLS structure... ok
    6. . Performing the SSL/TLS handshake...ssl_client.c:0261: got supported group(001d)
    7. ssl_client.c:0261: got supported group(0017)
    8. ssl_client.c:0261: got supported group(0018)
    9. ssl_client.c:0261: got supported group(001e)
    10. ssl_client.c:0261: got supported group(0019)
    11. ssl_client.c:0261: got supported group(001a)
    12. ssl_client.c:0261: got supported group(001b)
    13. ssl_client.c:0261: got supported group(001c)
    14. ok
    15. . Verifying peer X.509 certificate... ok
    16. > Write to server: 18 bytes written
    17. GET / HTTP/1.0
    18. < Read from server: 156 bytes read
    19. HTTP/1.0 200 OK
    20. Content-Type: text/html
    21. mbed TLS Test Server

    22. Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256

    23. ssl_msg.c:3900: mbedtls_ssl_handle_message_type() returned -30848 (-0x7880)
    24. ssl_msg.c:5472: mbedtls_ssl_read_record() returned -30848 (-0x7880)
    25. PS D:\Drive\SynologyDrive\NFC\16_开源项目\mbedtls-3.2.1\mbedtls-3.2.1\programs\ssl> .\ssl_client1.exe
    26. . Seeding the random number generator... ok
    27. . Loading the CA root certificate ... ok (0 skipped)
    28. . Connecting to tcp/localhost/4433... ok
    29. . Setting up the SSL/TLS structure... ok
    30. . Performing the SSL/TLS handshake...ssl_client.c:0261: got supported group(001d)
    31. ssl_client.c:0261: got supported group(0017)
    32. ssl_client.c:0261: got supported group(0018)
    33. ssl_client.c:0261: got supported group(001e)
    34. ssl_client.c:0261: got supported group(0019)
    35. ssl_client.c:0261: got supported group(001a)
    36. ssl_client.c:0261: got supported group(001b)
    37. ssl_client.c:0261: got supported group(001c)
    38. ok
    39. . Verifying peer X.509 certificate... ok
    40. > Write to server: 18 bytes written
    41. GET / HTTP/1.0
    42. < Read from server: 156 bytes read
    43. HTTP/1.0 200 OK
    44. Content-Type: text/html
    45. mbed TLS Test Server

    46. Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256

    47. ssl_msg.c:3900: mbedtls_ssl_handle_message_type() returned -30848 (-

    抓取SSL过程分析:

            使用wireshark 工具,可以实现抓包分析,抓取Demo的数据进行分析:

    从抓包中可以看出,在进行完TCP连接后,进行了SSL连接过程:

    Client Hello

    Server Hell

    Certification

    Server key exchange

    Server Hello done

    Client Key exchange

    Handshake 等操作

    Demo代码:

    贴出 client 端代码

    1. int main( void )
    2. {
    3. int ret = 1, len;
    4. int exit_code = MBEDTLS_EXIT_FAILURE;
    5. mbedtls_net_context server_fd;
    6. uint32_t flags;
    7. unsigned char buf[1024];
    8. const char *pers = "ssl_client1";
    9. mbedtls_entropy_context entropy;
    10. mbedtls_ctr_drbg_context ctr_drbg;
    11. mbedtls_ssl_context ssl;
    12. mbedtls_ssl_config conf;
    13. mbedtls_x509_crt cacert;
    14. #if defined(MBEDTLS_DEBUG_C)
    15. mbedtls_debug_set_threshold( DEBUG_LEVEL );
    16. #endif
    17. /*
    18. * 0. Initialize the RNG and the session data
    19. */
    20. mbedtls_net_init( &server_fd );
    21. mbedtls_ssl_init( &ssl );
    22. mbedtls_ssl_config_init( &conf );
    23. mbedtls_x509_crt_init( &cacert );
    24. mbedtls_ctr_drbg_init( &ctr_drbg );
    25. mbedtls_printf( "\n . Seeding the random number generator..." );
    26. fflush( stdout );
    27. mbedtls_entropy_init( &entropy );
    28. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
    29. (const unsigned char *) pers,
    30. strlen( pers ) ) ) != 0 )
    31. {
    32. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
    33. goto exit;
    34. }
    35. mbedtls_printf( " ok\n" );
    36. /*
    37. * 0. Initialize certificates
    38. */
    39. mbedtls_printf( " . Loading the CA root certificate ..." );
    40. fflush( stdout );
    41. ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
    42. mbedtls_test_cas_pem_len );
    43. if( ret < 0 )
    44. {
    45. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
    46. goto exit;
    47. }
    48. mbedtls_printf( " ok (%d skipped)\n", ret );
    49. /*
    50. * 1. Start the connection
    51. */
    52. mbedtls_printf( " . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
    53. fflush( stdout );
    54. if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
    55. SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
    56. {
    57. mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
    58. goto exit;
    59. }
    60. mbedtls_printf( " ok\n" );
    61. /*
    62. * 2. Setup stuff
    63. */
    64. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
    65. fflush( stdout );
    66. if( ( ret = mbedtls_ssl_config_defaults( &conf,
    67. MBEDTLS_SSL_IS_CLIENT,
    68. MBEDTLS_SSL_TRANSPORT_STREAM,
    69. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
    70. {
    71. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
    72. goto exit;
    73. }
    74. mbedtls_printf( " ok\n" );
    75. /* OPTIONAL is not optimal for security,
    76. * but makes interop easier in this simplified example */
    77. mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
    78. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
    79. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
    80. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
    81. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
    82. {
    83. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
    84. goto exit;
    85. }
    86. if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
    87. {
    88. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
    89. goto exit;
    90. }
    91. mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
    92. /*
    93. * 4. Handshake
    94. */
    95. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
    96. fflush( stdout );
    97. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
    98. {
    99. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
    100. {
    101. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
    102. goto exit;
    103. }
    104. }
    105. mbedtls_printf( " ok\n" );
    106. /*
    107. * 5. Verify the server certificate
    108. */
    109. mbedtls_printf( " . Verifying peer X.509 certificate..." );
    110. /* In real life, we probably want to bail out when ret != 0 */
    111. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
    112. {
    113. #if !defined(MBEDTLS_X509_REMOVE_INFO)
    114. char vrfy_buf[512];
    115. #endif
    116. mbedtls_printf( " failed\n" );
    117. #if !defined(MBEDTLS_X509_REMOVE_INFO)
    118. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
    119. mbedtls_printf( "%s\n", vrfy_buf );
    120. #endif
    121. }
    122. else
    123. mbedtls_printf( " ok\n" );
    124. /*
    125. * 3. Write the GET request
    126. */
    127. mbedtls_printf( " > Write to server:" );
    128. fflush( stdout );
    129. len = sprintf( (char *) buf, GET_REQUEST );
    130. while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
    131. {
    132. if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
    133. {
    134. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
    135. goto exit;
    136. }
    137. }
    138. len = ret;
    139. mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
    140. /*
    141. * 7. Read the HTTP response
    142. */
    143. mbedtls_printf( " < Read from server:" );
    144. fflush( stdout );
    145. do
    146. {
    147. len = sizeof( buf ) - 1;
    148. memset( buf, 0, sizeof( buf ) );
    149. ret = mbedtls_ssl_read( &ssl, buf, len );
    150. if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
    151. continue;
    152. if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
    153. break;
    154. if( ret < 0 )
    155. {
    156. mbedtls_printf( "failed\n ! mbedtls_ssl_read returned %d\n\n", ret );
    157. break;
    158. }
    159. if( ret == 0 )
    160. {
    161. mbedtls_printf( "\n\nEOF\n\n" );
    162. break;
    163. }
    164. len = ret;
    165. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
    166. }
    167. while( 1 );
    168. mbedtls_ssl_close_notify( &ssl );
    169. exit_code = MBEDTLS_EXIT_SUCCESS;
    170. exit:
    171. #ifdef MBEDTLS_ERROR_C
    172. if( exit_code != MBEDTLS_EXIT_SUCCESS )
    173. {
    174. char error_buf[100];
    175. mbedtls_strerror( ret, error_buf, 100 );
    176. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
    177. }
    178. #endif
    179. mbedtls_net_free( &server_fd );
    180. mbedtls_x509_crt_free( &cacert );
    181. mbedtls_ssl_free( &ssl );
    182. mbedtls_ssl_config_free( &conf );
    183. mbedtls_ctr_drbg_free( &ctr_drbg );
    184. mbedtls_entropy_free( &entropy );
    185. mbedtls_exit( exit_code );
    186. }

    Demo代码比较简单,

  • 相关阅读:
    尚硅谷尚优选项目教程发布
    JVM源码解析Java Attach处理流程
    Elasticsearch高级之-集群搭建,数据分片
    MySQ之备份与恢复
    Android 11编译第三弹 ADB开启ROOT权限
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    【Maven】SpringBoot多模块项目利用reversion占位符,进行版本管理.打包时版本号不能识别问题
    docker搭建私有仓库
    linux 时间和北京时间对不上
    分库分表知识内容
  • 原文地址:https://blog.csdn.net/wangzhiqin365/article/details/127878899