mbedtls 自带SSL demo调试

news2024/11/20 12:23:08

概述:

        运行mbedtls自带  ssl demo的记录;

操作过程:

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

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

mbedtls-3.2.1\mbedtls-3.2.1\programs\ssl> .\ssl_server.exe
  . Seeding the random number generator... ok

  . Loading the server cert. and key... ok
  . Bind on https://localhost:4433/ ... ok
  . Setting up the SSL data.... ok
  . Waiting for a remote connection ... ok
  . Performing the SSL/TLS handshake... ok
  < Read from client: 18 bytes read

GET / HTTP/1.0

  > Write to client: 156 bytes written

HTTP/1.0 200 OK
Content-Type: text/html

<h2>mbed TLS Test Server</h2>
<p>Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256</p>

  . Closing the connection... ok
  . Waiting for a remote connection ... ok
  . Performing the SSL/TLS handshake... ok
  < Read from client: 18 bytes read

GET / HTTP/1.0

  > Write to client: 156 bytes written

HTTP/1.0 200 OK
Content-Type: text/html

<h2>mbed TLS Test Server</h2>
<p>Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256</p>

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

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

mbedtls-3.2.1\mbedtls-3.2.1\programs\ssl> .\ssl_client1.exe

  . Seeding the random number generator... ok
  . Loading the CA root certificate ... ok (0 skipped)
  . Connecting to tcp/localhost/4433... ok
  . Setting up the SSL/TLS structure... ok
  . Performing the SSL/TLS handshake...ssl_client.c:0261: got supported group(001d)
ssl_client.c:0261: got supported group(0017)
ssl_client.c:0261: got supported group(0018)
ssl_client.c:0261: got supported group(001e)
ssl_client.c:0261: got supported group(0019)
ssl_client.c:0261: got supported group(001a)
ssl_client.c:0261: got supported group(001b)
ssl_client.c:0261: got supported group(001c)
 ok
  . Verifying peer X.509 certificate... ok
  > Write to server: 18 bytes written

GET / HTTP/1.0

  < Read from server: 156 bytes read

HTTP/1.0 200 OK
Content-Type: text/html

<h2>mbed TLS Test Server</h2>
<p>Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256</p>
ssl_msg.c:3900: mbedtls_ssl_handle_message_type() returned -30848 (-0x7880)
ssl_msg.c:5472: mbedtls_ssl_read_record() returned -30848 (-0x7880)
PS D:\Drive\SynologyDrive\NFC\16_开源项目\mbedtls-3.2.1\mbedtls-3.2.1\programs\ssl> .\ssl_client1.exe

  . Seeding the random number generator... ok
  . Loading the CA root certificate ... ok (0 skipped)
  . Connecting to tcp/localhost/4433... ok
  . Setting up the SSL/TLS structure... ok
  . Performing the SSL/TLS handshake...ssl_client.c:0261: got supported group(001d)
ssl_client.c:0261: got supported group(0017)
ssl_client.c:0261: got supported group(0018)
ssl_client.c:0261: got supported group(001e)
ssl_client.c:0261: got supported group(0019)
ssl_client.c:0261: got supported group(001a)
ssl_client.c:0261: got supported group(001b)
ssl_client.c:0261: got supported group(001c)
 ok
  . Verifying peer X.509 certificate... ok
  > Write to server: 18 bytes written

GET / HTTP/1.0

  < Read from server: 156 bytes read

HTTP/1.0 200 OK
Content-Type: text/html

<h2>mbed TLS Test Server</h2>
<p>Successful connection using: TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256</p>
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 端代码

int main( void )
{
    int ret = 1, len;
    int exit_code = MBEDTLS_EXIT_FAILURE;
    mbedtls_net_context server_fd;
    uint32_t flags;
    unsigned char buf[1024];
    const char *pers = "ssl_client1";

    mbedtls_entropy_context entropy;
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_ssl_context ssl;
    mbedtls_ssl_config conf;
    mbedtls_x509_crt cacert;

#if defined(MBEDTLS_DEBUG_C)
    mbedtls_debug_set_threshold( DEBUG_LEVEL );
#endif

    /*
     * 0. Initialize the RNG and the session data
     */
    mbedtls_net_init( &server_fd );
    mbedtls_ssl_init( &ssl );
    mbedtls_ssl_config_init( &conf );
    mbedtls_x509_crt_init( &cacert );
    mbedtls_ctr_drbg_init( &ctr_drbg );

    mbedtls_printf( "\n  . Seeding the random number generator..." );
    fflush( stdout );

    mbedtls_entropy_init( &entropy );
    if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
                               (const unsigned char *) pers,
                               strlen( pers ) ) ) != 0 )
    {
        mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
        goto exit;
    }

    mbedtls_printf( " ok\n" );

    /*
     * 0. Initialize certificates
     */
    mbedtls_printf( "  . Loading the CA root certificate ..." );
    fflush( stdout );

    ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
                          mbedtls_test_cas_pem_len );
    if( ret < 0 )
    {
        mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
        goto exit;
    }

    mbedtls_printf( " ok (%d skipped)\n", ret );

    /*
     * 1. Start the connection
     */
    mbedtls_printf( "  . Connecting to tcp/%s/%s...", SERVER_NAME, SERVER_PORT );
    fflush( stdout );

    if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
                                         SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
    {
        mbedtls_printf( " failed\n  ! mbedtls_net_connect returned %d\n\n", ret );
        goto exit;
    }

    mbedtls_printf( " ok\n" );

    /*
     * 2. Setup stuff
     */
    mbedtls_printf( "  . Setting up the SSL/TLS structure..." );
    fflush( stdout );

    if( ( ret = mbedtls_ssl_config_defaults( &conf,
                    MBEDTLS_SSL_IS_CLIENT,
                    MBEDTLS_SSL_TRANSPORT_STREAM,
                    MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
    {
        mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
        goto exit;
    }

    mbedtls_printf( " ok\n" );

    /* OPTIONAL is not optimal for security,
     * but makes interop easier in this simplified example */
    mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
    mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
    mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
    mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );

    if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
    {
        mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
        goto exit;
    }

    if( ( ret = mbedtls_ssl_set_hostname( &ssl, SERVER_NAME ) ) != 0 )
    {
        mbedtls_printf( " failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
        goto exit;
    }

    mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );

    /*
     * 4. Handshake
     */
    mbedtls_printf( "  . Performing the SSL/TLS handshake..." );
    fflush( stdout );

    while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
    {
        if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
        {
            mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
            goto exit;
        }
    }

    mbedtls_printf( " ok\n" );

    /*
     * 5. Verify the server certificate
     */
    mbedtls_printf( "  . Verifying peer X.509 certificate..." );

    /* In real life, we probably want to bail out when ret != 0 */
    if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
    {
#if !defined(MBEDTLS_X509_REMOVE_INFO)
        char vrfy_buf[512];
#endif

        mbedtls_printf( " failed\n" );

#if !defined(MBEDTLS_X509_REMOVE_INFO)
        mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), "  ! ", flags );

        mbedtls_printf( "%s\n", vrfy_buf );
#endif
    }
    else
        mbedtls_printf( " ok\n" );

    /*
     * 3. Write the GET request
     */
    mbedtls_printf( "  > Write to server:" );
    fflush( stdout );

    len = sprintf( (char *) buf, GET_REQUEST );

    while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
    {
        if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
        {
            mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
            goto exit;
        }
    }

    len = ret;
    mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );

    /*
     * 7. Read the HTTP response
     */
    mbedtls_printf( "  < Read from server:" );
    fflush( stdout );

    do
    {
        len = sizeof( buf ) - 1;
        memset( buf, 0, sizeof( buf ) );
        ret = mbedtls_ssl_read( &ssl, buf, len );

        if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
            continue;

        if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
            break;

        if( ret < 0 )
        {
            mbedtls_printf( "failed\n  ! mbedtls_ssl_read returned %d\n\n", ret );
            break;
        }

        if( ret == 0 )
        {
            mbedtls_printf( "\n\nEOF\n\n" );
            break;
        }

        len = ret;
        mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
    }
    while( 1 );

    mbedtls_ssl_close_notify( &ssl );

    exit_code = MBEDTLS_EXIT_SUCCESS;

exit:

#ifdef MBEDTLS_ERROR_C
    if( exit_code != MBEDTLS_EXIT_SUCCESS )
    {
        char error_buf[100];
        mbedtls_strerror( ret, error_buf, 100 );
        mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
    }
#endif

    mbedtls_net_free( &server_fd );

    mbedtls_x509_crt_free( &cacert );
    mbedtls_ssl_free( &ssl );
    mbedtls_ssl_config_free( &conf );
    mbedtls_ctr_drbg_free( &ctr_drbg );
    mbedtls_entropy_free( &entropy );

    mbedtls_exit( exit_code );
}

Demo代码比较简单,

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/10788.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

通用后台管理系统前端界面

1、项目搭建 局部安装vue-cli 4.5.15 和创建项目 1.1 局部安装vue-cli 工具&#xff0c;方便不同版本进行切换 ###安装指定版本的vue脚手架工具 或者是已安装了&#xff0c;该命令则为使用4.5.15版本的vue/cli npm i -D vue/cli4.5.15 ###查看当前vue脚手架工具版本…

第七节.常用Linux命令—查找文件,软硬链接,软件安装,系统信息

第七节.常用Linux命令—查找文件&#xff0c;软硬链接&#xff0c;软件安装&#xff0c;系统信息 1.查找文件&#xff1a; 序号命令作用01find [路径] -name “.py”查找指定路径下扩展名是.py的文件&#xff0c;包括子目录 功能&#xff1a;通常用来在特定目录下搜索符合条件…

SpringMVC之全局异常管理

在开发过程中一个项目可以要抛出很多很多的异常&#xff0c;这对于用户来说非常地不方便&#xff0c;而且非常地不安全&#xff0c;特别容易导致用户信息地泄露。在SpringMVC中有一种全局异常管理方法&#xff0c;这种方法收集项目中存在所有地异常&#xff0c;以保证项目正常地…

windows 定制 terminal 上手实践

文章目录1. 前言2. 安装 Windows Terminal3. 安装 nerd-fonts4. Microsoft store 安装 powershell5. 改变终端背景颜色6. 安装 scoop7. 安装 git8. 安装 Neovim9. 设置别名10. 安装 oh My Posh11. 自定义 prompt&#xff08;path&#xff09;12. 安装 nodejs13. 安装 Terminal …

数据库高级 I

数据库高级 I 双向链表 什么是双向链表 双向链表是一种数据结构,由若干个节点构成,其中每个节点均由三部分构成,分别是前驱节点,元素,后继节点.双向链表中的节点在内存中是游离状态存在的.双向链表的应用:LinkedList 双向链表中的元素部分保存的都是对象,实际上保存的是元素对…

Tomcat 源码解析一请求处理的整体过程-黄泉天怒(下)

上一篇博客 Tomcat 源码解析一请求处理的整体过程-黄泉天怒(上) NonLoginAuthenticator NonLoginAuthenticator并没有实现invoke()方法&#xff0c;而是由父类AuthenticatorBase实现了invoke()方法&#xff0c;AuthenticatorBase类关系如下。   先弄清楚NonLoginAuthentica…

07.URL调度器工作原理

1.工作原理 django通过urlconf来映射视图函数&#xff0c;只区分路径&#xff0c;不区分http方法 Django确定要使用的根URLconf模块&#xff0c;一般实在settings中的ROOT_URLCONF设置的值&#xff0c;但是如果传入HttpRequest对象具有一个urlconf属性&#xff08;由中间件设置…

直播预告|OceanBase 社区版 4.0 全解析

小就是大&#xff0c;OceanBase 社区版 4.0 | 单机到分布式一体化与最佳实践 随着云原生时代的到来&#xff0c;越来越多的业务系统会采用云原生架构。云原生数据库通过资源解耦和资源池化等技术支撑了不同业务领域的场景需求。 2022 年 11 月 3 日&#xff0c;OceanBase 社区版…

Linux结构目录说明以及相关作用【重点】

目录目录结构目录结构介绍&#xff1a;/bin [重点] (/usr/bin 、 /usr/local/bin)/home/root/sbin/lib/lost found/etc/usr/boot/dev/media/mnt/opt/usr/local/var目录结构 1&#xff1a;图解目录结构 2&#xff1a;用命令行看目录结构&#xff08;里面自己装了一些东西&…

1024程序员节过后,我才知道JVM有多重要

为什么要学习JVM? 和大部分程序员一样&#xff0c;我最初接触 Java 虚拟机只是因为面试官会问Java虚拟机的相关问题&#xff0c;所以不得不看&#xff0c;而且刷了很多面试题&#xff1b;那段时间刷面试题刷的心烦甚至会觉得&#xff0c;我面试的岗位平时也用不上Java虚拟机&…

奉加微蓝牙芯片PHY6222,支持mesh,SRAM、可选128K-8M

奉加微蓝牙芯片PHY6222&#xff0c;支持mesh&#xff0c;SRAM、可选128K-8M ​SIG mesh和ZigBee两种2.4G的无线mesh&#xff0c;有Nordic的NRF52840&#xff1b;泰凌微的TLSR825X、TLSR9X&#xff1b;还有奉加微的PHY62xx系列是一款支持BLE 5.2功能和IEEE 802.15.4&#xff08;…

【数据结构基础】时间复杂度和空间复杂度

&#x1f648;个人主页&#xff1a;阿伟t &#x1f449;系列专栏&#xff1a;【C语言–大佬之路】 &#x1f388;今日心语&#xff1a;你所看到的惊艳&#xff0c;都曾被平庸所历练。 数据结构指的是“一组数据的存储结构”&#xff0c;算法指的是“操作数据的一组方法”。 数…

Spring Boot快速入门

目录 1、Spring Boot概述 1.1、什么是Spring Boot 1.2、Spring Boot的特点 2、Spring Boot快速入门 2.1、起步依赖 2.2、创建启动类 2.3、测试启动类 2.4、自定义banner 2.5、自动配置原理 2.6、启动类注解 3、Spring Boot整合SpringMVC 3.1、Controller类 3.2、…

效率为王,居家办公必备的5款小工具

疫情反反复复&#xff0c;好多小伙伴依然处在居家办公的境地中&#xff0c;所以像小编一样早早的干完活躺床上是非常有必要的。 1.极速复制粘贴工具——TeraCopy TeraCopy是一款老牌免费的 Windows 文件复制/移动辅助加速工具软件&#xff0c;复制文件时若是遇上数据有问题也…

《FFmpeg Basics》中文版-03-比特率/帧率/文件大小

正文 比特率和帧速率是视频的基本特征&#xff0c;它们的正确设置对整体视频质量非常重要。 如果我们知道所有包含的媒体流的比特率和持续时间&#xff0c;我们可以计算输出文件的最终大小。 由于在使用FFmpeg工具时对帧速率和比特率的理解很重要&#xff0c;因此包含每个术语…

文件存储解决方案-云存储阿里 OSS

文件存储解决方案-云存储阿里 OSS 1.文件存储&#xff08;上传&#xff09;解决方案讨论 1.图解 文件存储解决方案-云存储阿里 OSS 解读上图 普通上传并不是分布式&#xff0c;也不是集群&#xff0c;可用性不高普通上传的分布式情况&#xff0c;使用了集群&#xff0c;但是…

数据结构每日亿题(七)

文章目录一题目二.思路2.1链表2.2数组三.代码一题目 原题传送门&#xff1a;力扣 题目&#xff1a; 题目的意思是让你写一个数据结构&#xff0c;这个结构的特点和队列一样先进先出&#xff0c;然后完成&#xff1a;判断是否为空&#xff0c;判断是否为满&#xff0c;添加一个…

实验29:循迹传感器实验

今天讲一个基本实验 循迹实验 循迹传感器的原理是: CTRT5000传感器的红外发射管不断发射红外光。由于黑色吸收光线,当红外发射管照射黑色表面时,反射光较少,接收管接收的红外线较少。这表明黑色吸收光线的强度大,那么比较器输出高电平,指示灯熄灭。同样,当它在白色表面…

MCE虚拟筛选化合物库

Discovery Diversity Sets 新颖的化合物库&#xff01; 药物筛选是发现药物先导物的重要途径&#xff0c;好的化合物库则是药物筛选的必备武器。MCE 拥有丰富的数据库资源&#xff0c;助力您的药物筛选研究&#xff01;药物筛选研究与化合物新颖性密切相关。Discovery Divers…

单目标应用:求解旅行商问题(TSP)的猎豹优化算法(The Cheetah Optimizer,CO)提供MATLAB代码

一、猎豹优化算法 猎豹优化算法&#xff08;The Cheetah Optimizer&#xff0c;CO&#xff09;由MohammadAminAkbari等人于2022年提出&#xff0c;该算法性能高效&#xff0c;思路新颖。 参考文献&#xff1a; Akbari, M.A., Zare, M., Azizipanah-abarghooee, R. et al. The…