Skip to content

pkg/mbedtls: add TLS support for LWIP#17519

Open
mariemC wants to merge 3 commits intoRIOT-OS:masterfrom
mariemC:pkg/mbedtls
Open

pkg/mbedtls: add TLS support for LWIP#17519
mariemC wants to merge 3 commits intoRIOT-OS:masterfrom
mariemC:pkg/mbedtls

Conversation

@mariemC
Copy link
Contributor

@mariemC mariemC commented Jan 14, 2022

Contribution description

  • The client node connects to openssl server and sends an encrypted message after a handshake and key exchange.
  • mbedtls package is added.
  • lwip package is also modified.

Testing procedure

  • Board used for the test: same5-xpro.
  • A wifi shield is used to connect the board to wifi.

Issues/PRs references

-Depends on PR #15671

@github-actions github-actions bot added Area: doc Area: Documentation Area: Kconfig Area: Kconfig integration Area: network Area: Networking Area: pkg Area: External package ports labels Jan 14, 2022
@github-actions github-actions bot added the Area: tests Area: tests and testing framework label Jan 14, 2022
@benpicco benpicco changed the title Pkg/mbedtls pkg/mbedtls: add TLS support for LWIP Jan 14, 2022
@benpicco benpicco added the Area: security Area: Security-related libraries and subsystems label Jan 14, 2022
@benpicco
Copy link
Contributor

benpicco commented Jan 18, 2022

Please rebase.
While you are at it, please also prefix the commit messages with the relevant subsystem, so pkg/mbedtls: and pkg/lwip:

@PeterKietzmann
Copy link
Member

It should not be necessary to copy the mbedtls header files into pkg/mbedtls/include/

@mariemC mariemC force-pushed the pkg/mbedtls branch 2 times, most recently from 5db9d9f to f31f73e Compare January 25, 2022 22:27
@github-actions github-actions bot removed the Area: Kconfig Area: Kconfig integration label Jan 25, 2022
@benpicco benpicco requested a review from yarrick January 26, 2022 16:07
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, tested this on native and looks like it works!
There are some whitespace and code style issues, but first round of review:

#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
!defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
- !defined(__HAIKU__) && !defined(__midipix__)
+ !defined(__HAIKU__) && !defined(__midipix__) && defined(_RIOT_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be

Suggested change
+ !defined(__HAIKU__) && !defined(__midipix__) && defined(_RIOT_)
+ !defined(__HAIKU__) && !defined(__midipix__) && !defined(RIOT_VERSION)

USEMODULE += shell_commands
USEMODULE += ps
# Add atwinc15x0 driver
USEMODULE += atwinc15x0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to force the use of this driver, sam0_eth, netdev_tap or esp_wifi would work as well - just use netdev_default

-DATWINC15X0_PARAM_WAKE_PIN="GPIO_PIN(0, 7)"
CFLAGS += -DCONFIG_ENTROPY_SOURCE_ADC_HMIN="200"
CFLAGS += -DISR_STACK_SIZE=4096
CFLAGS += -DHOST_SERVER="\"10.0.110.117\""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set this using a shell command. Then we also don't need a separate thread - just bump THREAD_STACKSIZE_MAIN if necessary.

static int cmd_tls_connect(int argc, char **argv)
{
    if (argc < 3) {
        printf("usage: %s <host> <port> [data]\n", argv[0]);
        return -1;
    }

    return _lwip_mbedtls_client(argv[1], argv[2]);
}

static const shell_command_t shell_commands[] = {
    { "connect", "Perform a TLS connection", cmd_tls_connect },
    { NULL, NULL, NULL }
};

(void)arg;
int status;

xtimer_sleep(20);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you can also drop the delay

Comment on lines +185 to +195
while(1) {
DEBUG_PUTS("_lwip_mbedtls_client_thread: sending encrypted hello world to server is DONE :) ");
xtimer_sleep(10);
}

error:
while(1) {
DEBUG_PUTS("FAILED");
xtimer_sleep(20);
}
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to loop here - just return

DEBUG_PUTS("_lwip_mbedtls_client_thread: server connect succeeded");

//verify certificate belongs to the server
status = mbedtls_ssl_set_hostname(&ssl, "mariem.com");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example.com is usually used for this 😉

Comment on lines +64 to +69
mbedtls_net_context server_fd;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_x509_crt x509_certificate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do those have to be global?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they don't


// send hello world to the server
unsigned char write_buf[]="Hello world\n";
size_t write_buf_len = sizeof(write_buf) -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use data the user supplied as a shell command argument here

/**
* @brief Use `random_uint32()` to generate random numbers, if available
*/
#if MBEDTLS_ENABLED == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the trailing whitespaces and the horizontal tabs mentioned by the automatic checks.

@@ -0,0 +1 @@
#include "lwip/netdb.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this and the other one can be a symlink instead? It seems they are already used in the tree.

/** @ingroup socket */
#define close(s) lwip_close(s)
/** @ingroup socket */
+#if MBEDTLS_ENABLED == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please edit the patch to explain why this is done.

@mariemC mariemC force-pushed the pkg/mbedtls branch 2 times, most recently from 67d7b3a to 15a9608 Compare February 21, 2022 08:27
#endif
/** @endcond */

#if MBEDTLS_ENABLED == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please expose (only necessary) symbols via Kconfig, as requested by @miri64 and @aabadie in a conversation starting somewhere here

/**
* @brief Use `random_uint32()` to generate random numbers, if available
*/
#if MBEDTLS_ENABLED == 0
Copy link
Contributor

@yarrick yarrick Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems strange - it (LWIP_RAND) is set to the same value in lwipopts? Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added (LWIP_RAND) in lwipopts in order to be recognized in build/pkg/lwip/src/core/dns.c:103
But it is already defined in pkg/lwip/include/arch/sys_arch.h:133
=> This will create a redefiniton, therefore I added #if MBEDTLS_ENABLED == 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you define it to the same thing there: random_uint32(). Why not just keep this and remove your extra definition? Make sure the random module is enabled and things should be fine.

@github-actions github-actions bot added the Area: Kconfig Area: Kconfig integration label Apr 9, 2022
@mariemC mariemC force-pushed the pkg/mbedtls branch 2 times, most recently from 6f6a3b7 to a3fbe69 Compare April 10, 2022 12:12
*
* This module is required for SSL/TLS.
*/
#if CONFIG_MBEDTLS_SSL_TLS_C
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CONFIG_* symbols are expected to be generated by Kconfig, but they are not yet included to the Kconfig configuration system. The here and here for reference.

@mariemC mariemC force-pushed the pkg/mbedtls branch 3 times, most recently from 1570e1c to a8092c4 Compare April 24, 2022 22:07
bool "Enable debug messages"
default y

endif #KCONFIG_USEMODULE_MBEDTLS_TEST
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Do you actually need all symbols? Please only add those that are required.
  2. It seem to me that these configs should go into this file which is the Kconfig based point to generate the traditional mbedtls configuration header file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, all of them are needed, mainly each module requires a few other modules to work correctly therefore it seems quite a lot.
  2. Do you mean all of them should be added in pkg/mbedtls/Kconfig?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for responding so late, haven't seen this question. Yes, all of the configuration values that are needed. Please check the existing file Kconfig file (+ existing header file) that I pointed to earlier.

@PeterKietzmann
Copy link
Member

@mariemC thanks for cleaning up the configuration files. The implementation and its representation in menuconfig look good at first sight. I do, unfortunately, not have time for a full review + testing. Anyway, some thoughts on the rest of the PR:

  • Extend the test README so one knows what to execute (shell commands?), what to expect, and maybe give some context which certificates are included in the folder and how they are incorporated.
  • The name of the test folder is suboptimal. Usually, we prefix folders that relate to a package with pkg_. Now pkg_mbedtls exists already. Maybe something like pkg_mbedtls_lwip? In general I was wondering if we should execute some more of the built in mbedtls tests in RIOT. Possible to extract some of the tests that you include there?
  • Most of the Kconfig symbols that you include are enabled by default. I think they shouldn't, but the particular test case should enable the relevant ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: doc Area: Documentation Area: Kconfig Area: Kconfig integration Area: network Area: Networking Area: pkg Area: External package ports Area: security Area: Security-related libraries and subsystems Area: tests Area: tests and testing framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants