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?