2
votes

EDIT: I tested with a static IP on both the board and my computer with a python SSL server and it works as expected, leading me to believe that the DHCP is the problem. If anyone has a lead on what may be occuring it would be greatly appreciated.

I am using the mbedTLS library on a STM32F746-NUCLEO board and I want to use it as both a SSL client and server. The server works well, so i tried to use the client example code (as is, in a separate project).

The following mbedtls_net_connect call returns -68 (MBEDTLS_ERR_NET_CONNECT_FAILED). Digging deeper reveals that it is due to a routing error (line 900 in tcp.c from LwIP), because local_ip is set to 0. The board is in DHCP mode on a home router which is connected to the internet. The destination server is up and running and the SERVER_NAME is the IP address in plain text.

mbedtls_entropy_context client_entropy;
static mbedtls_net_context server_fd;
mbedtls_x509_crt cacert;
static uint32_t flags;
static uint8_t vrfy_buf[512];
static const uint8_t* client_pers = "ssl_client";
mbedtls_ssl_config client_config;
mbedtls_ctr_drbg_context client_ctr_drbg;
mbedtls_ssl_context client_ssl;
static uint8_t client_buf[1024];

void SSL_Server(void const *argument) {
    int ret, len;
    UNUSED(argument);

    mbedtls_net_init(&server_fd);
    mbedtls_ssl_init(&client_ssl);
    mbedtls_ssl_config_init(&client_config);
    mbedtls_x509_crt_init(&cacert);
    mbedtls_ctr_drbg_init(&client_ctr_drbg);

    // Seeding the random number generator

    mbedtls_entropy_init( &client_entropy );
    len = strlen((char *) client_pers);
    if((ret = mbedtls_ctr_drbg_seed(&client_ctr_drbg, mbedtls_entropy_func,
            &client_entropy, (const unsigned char *) client_pers, len)) != 0)
    {
    goto exit;
    }


    // 1. Initialize certificates

    ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
            mbedtls_test_cas_pem_len );

    if( ret < 0 )
    {
    goto exit;
    }


    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;
    }
}

Here the SSL_Server function is a FreeRTOS thread called in the main(). I can also confirm that the network interface has been assigned an IP address when the error occurs. I expect the connection call to return 0 and connect to the server to initiate the SSL handshake.

1
Hard to tell without the whole code. Maybe you try to connect before the board gets a DHCP IP?Ctx
the code is the client example from mbedtls, i'll edit my post to include the relevant partsMDL

1 Answers

2
votes

You need to set the default netif route for LWIP to be able to route the remote address. Simply add netif_set_default(&netif); after dhcp_start() inside the function mbedtls_net_init().

void mbedtls_net_init( mbedtls_net_context *ctx ) {

  ...

  /* add the network interface */    
  netif_add(&netif, &addr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);

  /* register the default network interface */
  netif_set_up(&netif);

#ifdef USE_DHCP
  netif.ip_addr.addr = 0;
  dhcp_start(&netif);
#endif

  netif_set_default(&netif);  // <-- Here

  osDelay(500);

  start = HAL_GetTick();

  while((netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
  {
  }

  if (netif.ip_addr.addr == 0) {
    printf(" Failed to get ip address! Please check your network configuration.\n");
    Error_Handler();
  }

  ...

The documentation for MbedTLS can be kinda tricky, hope this helps.