Topic: Wolfssl fails with error -313 after a series of read write

wolfSSL_read called from an STM32 microcontroller acting as a Server returns -313 (FATAL ERROR) after many succesfull readings from a CSharp application acting as a Client.

    ret = wolfSSL_read(Pt_ssl, A_prcl_msd_m1_m1s_data_link_rx_buffer, sizeof(A_prcl_msd_m1_m1s_data_link_rx_buffer)-1);
    err = wolfSSL_get_error(Pt_ssl, ret);


Our receive callback is like this and works fine

wolfSSL_CTX_SetIORecv(Pt_ctx, PRCL_MSD_M1_M1S_DLINK_uartIORx);

/*!
* \brief   WolfSSL receive callback
* \dotfile PRCL_MSD_M1_M1S_DLINK_uartIORx.dot
* \ingroup MSD_M1_M1S_PROTOCOL_DATA_LINK
*/
static int PRCL_MSD_M1_M1S_DLINK_uartIORx(WOLFSSL *ssl, char *buf, const int sz, void *ctx)
{
    bool b_message_received;
    static int8_t * p_data_received_buf;
    static int32_t total_amount_data_rcv = 0;
    int32_t bytes_available_fifo = 0;
    int32_t bytes_to_read;
    int32_t bytes_read;

    b_message_received = MDL_USARTS_Check_end_reception(USART_MSD_COMMS);

    if (b_message_received == TRUE)
    {
        total_amount_data_rcv = MDL_USARTS_Get_DMA_amount_of_rx_data(USART_MSD_COMMS);
        MDL_USARTS_Reset_DMA_amount_of_rx_data(USART_MSD_COMMS);

        MDL_USARTS_Start_receive(USART_MSD_COMMS);

        p_data_received_buf = (int8_t*)MDL_USARTS_Get_receive_buffer(USART_MSD_COMMS);
        MDL_CIRC_FIFO_Fifo_push_buf(&T_wolfSSL_rx_fifo, (uint8_t *)p_data_received_buf, total_amount_data_rcv);
    }

    bytes_available_fifo = MDL_CIRC_FIFO_Fifo_used_size(&T_wolfSSL_rx_fifo);

    if (bytes_available_fifo > 0)
    {
        DEBUG_PRINTF("FIFO used: %d, DMA received: %d\r\n", bytes_available_fifo, total_amount_data_rcv);
        bytes_to_read = (bytes_available_fifo < sz) ? bytes_available_fifo : sz;
        bytes_read        = MDL_CIRC_FIFO_Fifo_pop_buf(&T_wolfSSL_rx_fifo, (uint8_t *)buf, bytes_to_read);
        return bytes_read;
    }
    else
    {
        return WOLFSSL_CBIO_ERR_WANT_READ;
    }

}

Does anyone has any suggestion?
Many Thanks,
Edoardo

Share

Re: Wolfssl fails with error -313 after a series of read write

Hi Edoardo,

Thanks for contacting wolfSSL Support. Can you tell from a wireshark inspection which side terminates the connection? Does the client send a corrupted packet that could be causing this?

Thanks,
Eric - wolfSSL Support

Re: Wolfssl fails with error -313 after a series of read write

Hi Eric,

The client application is written in VB.NET and uses the wolfSSL.CSharp library to communicate with an STM32 device over a serial bus.
During initialization, I register the serial I/O callbacks:

_wolfSSL.SetIORecv(ctx, New wolfSSL.CSharp.wolfssl.CallbackIORecv_delegate(AddressOf wolfSSLCbIORecv))
_wolfSSL.SetIOSend(ctx, New wolfSSL.CSharp.wolfssl.CallbackIOSend_delegate(AddressOf wolfSSLCbIOSend))

The callback implementations are as follows :

Where:

    Private Function wolfSSLCbIORecv(ssl As IntPtr, buf As IntPtr, sz As Integer, ctx As IntPtr) As Integer
        If sz <= 0 Then
            Return _wolfSSL.CBIO_ERR_GENERAL
        End If

        Try
            Dim msg(sz - 1) As Byte

            If b_data_processing_running = True Then
                SyncLock serialLock

                    If sz > total_amount_data_rcv Then
                        Return _wolfSSL.CBIO_ERR_WANT_READ
                    End If

                    Array.Copy(rx_uart_buffer, starting_msg_offset, msg, 0, sz)
                    Marshal.Copy(msg, 0, buf, sz)

                    starting_msg_offset += sz
                    total_amount_data_rcv -= sz

                    If total_amount_data_rcv = 0 Then
                        starting_msg_offset = 0
                        b_data_processing_running = False
                    End If

                End SyncLock
                Return sz
            Else
                Return 0
            End If
        Catch ex As Exception
            Return _wolfSSL.CBIO_ERR_CONN_CLOSE
        End Try
    End Function

    Private Function wolfSSLCbIOSend(ssl As IntPtr, buf As IntPtr, sz As Integer, ctx As IntPtr) As Integer
        If sz <= 0 Then
            Return _wolfSSL.CBIO_ERR_GENERAL
        End If

        Try
            SyncLock serialLock
                Dim msg(sz - 1) As Byte
                Marshal.Copy(buf, msg, 0, sz)
                serialPort.Write(msg, 0, sz)
            End SyncLock
            Return sz
        Catch ex As Exception
            Return _wolfSSL.CBIO_ERR_CONN_CLOSE
        End Try
    End Function

   
During communication, the client sends a request and waits for the response via:

   Public Function Write(data As Byte()) As Integer
        If Not isConnected Then
            Throw New InvalidOperationException("Not connected.")
        End If
        DataToRead = False
        Return _wolfSSL.write(ssl, data, data.Length)
    End Function

    Public Function Read(buffer As Byte()) As Integer
        If Not isConnected Then
            Throw New InvalidOperationException("Not connected.")
        End If
        Try
            Return _wolfSSL.read(ssl, buffer, buffer.Length)
        Catch ex As Exception
            Return -1
        End Try
    End Function

   
At some point during serial communication, while the client is inside the Read() method, the wolfSSLCbIOSend callback is unexpectedly triggered with a buffer of 24 bytes, containing the following data:

1b 00 a0 aa e5 03 03 c0 ff ff 00 00 00 00 09 00
00 01 00 02 00 01 03 18 00 00 00 17 03 03 00 13
8f 24 29 c2 34 c0 b3 dc fa 47 d5 7b 38 31 8c fd
32 89 ea

This appears to happen spontaneously, without the client having called write().
From the call stack, we can see that we are inside:
wolfSSL_CSharp.dll → wolfSSL.CSharp.wolfssl.read()

We also provide a full PCAP captured in Wireshark.
Could you help us understand what conditions inside wolfSSL.read() would cause wolfSSL to internally trigger the send callback?

Share

Re: Wolfssl fails with error -313 after a series of read write

Hi Mattia,

That is not expected. If you'd like to create a support ticket, we can review this in more detail. Please send an email to support@wolfssl.com

Thanks,
Eric

Re: Wolfssl fails with error -313 after a series of read write

Hi everyone,

We were able to identify the root cause of the issue we were experiencing. The problem was due to a RAM test in our application that, at a certain point during execution, corrupted the memory region where wolfSSL was dynamically allocating its buffers and data structures, leading to transmission issues.

We have now switched from dynamic to static memory allocation, and everything is working correctly.

At this point, we would like to understand if there is a recommended method to determine the required size of the static memory used by wolfSSL for its buffers and data structures.

Currently, we are using a rather memory-intensive configuration:

(Stack max size = 0x4000)

#define WOLFSSL_NO_MALLOC
#define WOLFSSL_STATIC_MEMORY
#undef  WOLFSSL_SMALL_STACK

#define GEN_MEM_SIZE (80 * 1024)
#define IO_MEM_SIZE  (50 * 1024)

unsigned char GEN_MEM[GEN_MEM_SIZE];
unsigned char IO_MEM[IO_MEM_SIZE];

Any guidance on how to properly size these buffers would be greatly appreciated.

Thank you!

Share

Re: Wolfssl fails with error -313 after a series of read write

We developed a tool to help optimize the static memory allocations:
https://github.com/wolfSSL/wolfssl-exam … aticmemory

Give it a try!