Home > Mobile >  Libwebsockets: keep SSL context disabled and provide one from the modem
Libwebsockets: keep SSL context disabled and provide one from the modem

Time:09-27

I've recently been passed an embedded project where an MCU uses libwebsockets (version 3.1.0) to setup a websockets client. The MCU is connected to a SIMCOM modem for 4g connectivity.

Up until now the communication was non-secured: SSL context not set and modem configured to provide just a TCP link to the server. The server URI was a wss one, but security was not enforced.

I've now been asked to set the communication to use SSL/TLS with server and client authentication.

Having never used libwebsockets before and being short on time, my idea was to:

  • a) Leave libwebsockets set as it was, so with no SSL context set up.
  • b) Configure the modem to creat an SSL/TLS context and connect to the server through that one.

I did this and I can see my device sending the request to switch from http to websockets protocol, as well as the server's successful reply (code 101). But my problem is that the server's reply never gets processed by libwebsockets and the connection is dropped after a few seconds. I can se3 that the bytes that make up the server's reply are received by the modem and passed into the MCU's buffer used to pass data into libwebsockets, but then the library never calls its net_recv method to actually read from such buffer.

Also, libwebsockets is currently built with TLS support and uses mbedTLS as SSL/TLS library. But, as said about, SSL context is currently left disabled (ssl_connection in the connection info struct is left set to 0).

So, I was wondering:

  1. Is my approach something that can work at all? Or should I setup the SSL context from libwebsockets and let the modem setup just a TCP connection?

  2. If I were to setup the SSL context from libwebsockets, is there a way to pass certificates and keys tot the library as just C arrays? Or do I have to have them stored as files on a filesystem and then pass their paths to the lib?

Also, I should add that the MCU has a second connection to the server, an HTTPS one, that one too setup with client and server authentication and that works with no problems at all. Therfore, I'm sure that the modem is correctly configured.

If needed, I will be able to provide more info on library configuration and used from Monday, because I'm currently travelling and don't have access to the code. But I wanted to get the ball rolling.

Thanks in advance for your help.

CodePudding user response:

Yesterday, finally I had time to look at the code again. It turns out that the problem was in my code (this was always a strong possibility).

The local implementation of the net_send had a bug and returned always negative values. Thus, libwebsockets thought that the HTTP request to switch protocol had failed and hence was closing down the connection.

So, to answer the two questions above:

  1. Yes, it's possible to setup libwebsockets to not handle SSL/TLS and then provide a SSL/TLS connection from a lower layer (the modem in my case).

  2. It is possible to store certificates and keys in C arrays of bytes and pass them to libwebsockets if you create a custom platform implementation for your platform and create a custom implementation of POSIX like methods (open, close, read, etc). By default the library expects to work on a POSIX like filesystem, though. So, realising a custom implementation can be a bit of a job.

The above is true for libwebsockets version 3.1.0. I haven't used any other version of the library, so things might have changed since then.

All in all, I think that configuring libwebsockets to handle SSL and the TLS handshake and then provide it with only a TCP connection is the best way of using the library. I chose a different strategy for the wrong reasons (tight deadline not being familiar with the codebase), but I'm definitely planning on reviewing my approach at a later date.

  • Related