亲爱的大家,
我正在使用 Nucleo-F767ZI + FreeRTOS + LWIP + mbedtls 生成我的 STM32CubeMX(最新版本)。
当我使用我的代码使用 HTTPS 连接并将数据发送到 www.google.com时,一切正常,但是当使用相同的代码使用 HTTPS 连接到 httpbin.org时,代码在返回的函数 mbedtls_ssl_handshake(&ssl)中失败代码 76(它也是返回代码函数 mbedtls_net_recv())。失败发生在函数 int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)步骤 MBEDTLS_SSL_SERVER_HELLO中。
我的代码如下:
- void tcp_client_deinit(void)
- {
- uart_print("Deinitrn");
- // mbedtls_net_free(&server_fd);
- mbedtls_ssl_free(&ssl);
- mbedtls_ssl_config_free(&conf);
- mbedtls_ctr_drbg_free(&ctr_drbg);
- mbedtls_entropy_free(&entropy);
- mbedtls_x509_crt_free(&cacert);
- }
- void tcp_client_init(void)
- {
- // mbedtls_net_init(&server_fd);
- while ((gnetif.ip_addr.addr == 0) || (gnetif.netmask.addr == 0) || (gnetif.gw.addr == 0))
- {
- }
- #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
- mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
- #endif
- uart_print("DHCP OK!rn");
- mbedtls_ssl_init(&ssl);
- mbedtls_ssl_config_init(&conf);
- mbedtls_x509_crt_init(&cacert);
- mbedtls_ctr_drbg_init(&ctr_drbg);
- uart_print("Seeding the random number generator...rn");
- mbedtls_entropy_init(&entropy);
- if (0 != mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, strlen(pers)))
- {
- uart_print("Seeding random number generator failed!rn");
- tcp_client_deinit();
- }
- uart_print("Loading the CA root certificate...rn");
- if (0 > mbedtls_x509_crt_parse(&cacert, (const unsigned char *)ca, calen))
- {
- uart_print("Loading CA root certificate failedrn");
- tcp_client_deinit();
- }
- mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
- uart_print("TCP client initialized!rn");
- }
- /**
- * @brief Connects to the TCP echo server
- * @param None
- * @retval None
- */
- void tcp_client_handle(void)
- {
- s32_t ret;
- u32_t verify_flags;
- u32_t time_out = 0;
- u8_t verify_buf[256];
- uart_print("Connecting to: %s Port: %srn", SERVER_NAME, SERVER_PORT);
- time_out = osKernelSysTick();
- while (0 != mbedtls_net_connect(&server_fd, SERVER_NAME, SERVER_PORT, MBEDTLS_NET_PROTO_TCP))
- {
- if ((uint32_t)(osKernelSysTick() - time_out) > 8192)
- {
- uart_print("Failed to connect to serverrn");
- return;
- }
- }
- uart_print("Network successfully connected!rn");
- if (0 != mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT))
- {
- uart_print("SSL config failedrn");
- return;
- }
- uart_print("SSL successfully configured!rn");
- 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);
- if (0 != mbedtls_ssl_setup(&ssl, &conf))
- {
- uart_print("SSL setup failedrn");
- return;
- }
- if (0 != mbedtls_ssl_set_hostname(&ssl, SERVER_NAME))
- {
- uart_print("Hostname setup failedrn");
- return;
- }
- uart_print("Performing handshakern");
- time_out = osKernelSysTick();
- while (0 != (ret = mbedtls_ssl_handshake(&ssl)))
- {
- if ((uint32_t)(osKernelSysTick() - time_out) > 8192)
- {
- uart_print("Handshake failed, error %drn", ret);
- return;
- // break;
- }
- }
- uart_print("Handshake succeededrn");
- uart_print("Verifying peer X.509 certificate...rn");
- if (0 != (verify_flags = mbedtls_ssl_get_verify_result(&ssl)))
- {
- mbedtls_x509_crt_verify_info((char *)verify_buf, sizeof(verify_buf), " ! ", verify_flags);
- uart_print("Failed to verify certificate, reason: %srn", verify_buf);
- // return;
- }
- else
- {
- uart_print("Certificate verifiedrn");
- }
- uart_print(" > Write to the serverrn");
- sprintf((char *)data_buffer, "%s", GET_REQUEST);
- time_out = osKernelSysTick();
- while (0 > mbedtls_ssl_write(&ssl, data_buffer, strlen((const char *)data_buffer)))
- {
- if ((uint32_t)(osKernelSysTick() - time_out) > 8192)
- {
- uart_print("Write failedrn");
- return;
- }
- }
- memset(data_buffer, 0, SIZE(data_buffer));
- uart_print(" < Read from the serverrn");
- time_out = osKernelSysTick();
- while (0 != mbedtls_ssl_read(&ssl, data_buffer, SIZE(data_buffer)))
- {
- if ((uint32_t)(osKernelSysTick() - time_out) > 8192)
- {
- uart_print("Read failedrn");
- return;
- }
- }
- uart_print("%srn", data_buffer);
- uart_print("Closing connectionrn");
- time_out = osKernelSysTick();
- while (0 != mbedtls_ssl_close_notify(&ssl))
- {
- if ((uint32_t)(osKernelSysTick() - time_out) > 8192)
- {
- uart_print("Failed to close connectionrn");
- return;
- }
- }
- uart_print("Reset connectionrn");
- time_out = osKernelSysTick();
- while (0 != (ret = mbedtls_ssl_session_reset(&ssl)))
- {
- if ((uint32_t)(osKernelSysTick() - time_out) > 8192)
- {
- uart_print("Connection reset failedrn");
- return;
- }
- }
- }
你能给我任何关于如何解决这个问题或者是什么原因的建议吗?
0
|
|
|
|