Build problem for echo client sample with (D)TLS

Hi,

I am trying to compile the sample 

  zephyr/samples/net/sockets/echo_client

from the v2.3.0-rc1 version for the Thingy91, with TLS support:

$ west build -b thingy91_nrf9160_ns -- -DOVERLAY_CONFIG=overlay-tls.conf

This does not build, see below for the output. When omitting the overlay for TLS, the build works. What do I have to change to make it work?

Thanks, Thomas

-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client
-- Using NCS Toolchain 2.2.0 for building. (/home/tludwig/ncs/toolchains/v2.2.0/cmake)
-- Found Python3: /home/tludwig/ncs/toolchains/v2.2.0/usr/local/bin/python3 (found suitable exact version "3.8.10") found components: Interpreter
-- Cache files will be written to: /home/tludwig/.cache/zephyr
-- Zephyr version: 3.2.99 (/home/tludwig/ncs/v2.3.0-rc1/zephyr)
-- Found west (found suitable version "0.13.1", minimum required is "0.7.1")
-- Board: thingy91_nrf9160_ns
-- Found host-tools: zephyr 0.15.2 (/home/tludwig/ncs/toolchains/v2.3.0-rc1/opt/zephyr-sdk)
-- Found toolchain: zephyr 0.15.2 (/home/tludwig/ncs/toolchains/v2.3.0-rc1/opt/zephyr-sdk)
-- Found Dtc: /home/tludwig/ncs/toolchains/v2.2.0/usr/local/bin/dtc (found suitable version "1.4.7", minimum required is "1.4.6")
-- Found BOARD.dts: /home/tludwig/ncs/v2.3.0-rc1/nrf/boards/arm/thingy91_nrf9160/thingy91_nrf9160_ns.dts
-- Generated zephyr.dts: /home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/build/zephyr/zephyr.dts
-- Generated devicetree_generated.h: /home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/build/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: /home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/build/zephyr/dts.cmake

warning: MBEDTLS_SSL_MAX_CONTENT_LEN (defined at modules/mbedtls/Kconfig:69,
modules/mbedtls/Kconfig:69) was assigned the value '2048' but got the value ''. Check these
unsatisfied dependencies: ((MBEDTLS_BUILTIN && MBEDTLS) || (MBEDTLS_BUILTIN && MBEDTLS && 0)) (=n).
See docs.zephyrproject.org/.../kconfig.html and/or look
up MBEDTLS_SSL_MAX_CONTENT_LEN in the menuconfig/guiconfig interface. The Application Development
Primer, Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual
might be helpful too.


warning: TEST_RANDOM_GENERATOR (defined at subsys/random/Kconfig:8) was assigned the value 'y' but
got the value 'n'. Check these unsatisfied dependencies: (!ENTROPY_HAS_DRIVER) (=n). See
docs.zephyrproject.org/.../kconfig.html and/or look up
TEST_RANDOM_GENERATOR in the menuconfig/guiconfig interface. The Application Development Primer,
Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual might be
helpful too.


warning: The choice symbol MBEDTLS_BUILTIN (defined at modules/mbedtls/Kconfig:27,
modules/mbedtls/Kconfig:27) was selected (set =y), but MBEDTLS_LIBRARY_NRF_SECURITY (defined at
/home/tludwig/ncs/v2.3.0-rc1/nrf/Kconfig.nrf:31) ended up as the choice selection. See
docs.zephyrproject.org/.../kconfig.html and/or look up
MBEDTLS_BUILTIN in the menuconfig/guiconfig interface. The Application Development Primer, Setting
Configuration Values, and Kconfig - Tips and Best Practices sections of the manual might be helpful
too.


warning: Experimental symbol NET_SOCKETS_SOCKOPT_TLS is enabled.


warning: Experimental symbol NET_SOCKETS_ENABLE_DTLS is enabled.


warning: Experimental symbol NET_CONNECTION_MANAGER is enabled.


warning: MBEDTLS_DTLS (defined at modules/mbedtls/Kconfig.tls-generic:34, /home/tludwig/ncs/v2.3.0-rc1/nrfxlib/nrf_security/Kconfig:120, modules/mbedtls/Kconfig.tls-generic:34) has direct dependencies ((MBEDTLS_TLS_VERSION_1_1 || MBEDTLS_TLS_VERSION_1_2) && !(NRF_SECURITY || NORDIC_SECURITY_BACKEND) && MBEDTLS_BUILTIN && MBEDTLS_CFG_FILE = "config-tls-generic.h" && MBEDTLS) || ((MBEDTLS_TLS_VERSION_1_2 || MBEDTLS_SSL_PROTO_TLS1_2) && !MBEDTLS_BUILTIN) || ((MBEDTLS_TLS_VERSION_1_1 || MBEDTLS_TLS_VERSION_1_2) && !(NRF_SECURITY || NORDIC_SECURITY_BACKEND) && MBEDTLS_BUILTIN && MBEDTLS_CFG_FILE = "config-tls-generic.h" && MBEDTLS && 0) with value n, but is currently being y-selected by the following symbols:
- NET_SOCKETS_ENABLE_DTLS (defined at subsys/net/lib/sockets/Kconfig:99), with value y, direct dependencies NET_SOCKETS_SOCKOPT_TLS && NET_SOCKETS && NETWORKING (value: y), and select condition NET_NATIVE && NET_SOCKETS_SOCKOPT_TLS && NET_SOCKETS && NETWORKING (value: y)
Parsing /home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/Kconfig
Loaded configuration '/home/tludwig/ncs/v2.3.0-rc1/nrf/boards/arm/thingy91_nrf9160/thingy91_nrf9160_ns_defconfig'
Merged configuration '/home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/prj.conf'
Merged configuration '/home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/overlay-tls.conf'

error: Aborting due to Kconfig warnings

CMake Error at /home/tludwig/ncs/v2.3.0-rc1/zephyr/cmake/modules/kconfig.cmake:328 (message):
command failed with return code: 1
Call Stack (most recent call first):
/home/tludwig/ncs/v2.3.0-rc1/nrf/cmake/modules/kconfig.cmake:29 (include)
/home/tludwig/ncs/v2.3.0-rc1/zephyr/cmake/modules/zephyr_default.cmake:108 (include)
/home/tludwig/ncs/v2.3.0-rc1/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
/home/tludwig/ncs/v2.3.0-rc1/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
CMakeLists.txt:5 (find_package)


-- Configuring incomplete, errors occurred!
FATAL ERROR: command exited with status 1: /home/tludwig/ncs/toolchains/v2.3.0-rc1/usr/local/bin/cmake -DWEST_PYTHON=/home/tludwig/ncs/toolchains/v2.3.0-rc1/usr/local/bin/python3.8 -B/home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client/build -GNinja -DBOARD=thingy91_nrf9160_ns -DOVERLAY_CONFIG=overlay-tls.conf -S/home/tludwig/ncs/v2.3.0-rc1/zephyr/samples/net/sockets/echo_client

Parents
  • Hi Thomas,

    Thank you for waiting!

    I should have thought of this earlier, but the nRF9160 already handles "native" TLS/DTLS through the modem, and so has no need for a TLS stack in the application itself. So the overlay is actually not necessary, as far as I can tell. You would only use it if you have special requirements. See https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/applications/serial_lte_modem/doc/slm_description.html#native-tls

    and see these two related DevZone posts:

     RE: How to interpret and resolve unsatisfied dependencies? 

     RE: Why does application that builds using nRF Connect SDK v2.0.0 fail to build under SDK v2.1.0  

    I hope that answers your question.

    Best regards,

    Raoul

  • Hi Raoul,

    Thanks for your answer and the pointers.

    While I prefer to configure mbedtls myself, using certificates, it would be also OK using the in-modem DTLS with PSK. However, there is no working example for a simple echo client (there are CoAP and LwM2M samples), that is why I tried the Zehyr echo client sample with overlay-tls.conf.

    Manually adding the defines

    #define CONFIG_NET_SOCKETS_SOCKOPT_TLS 
    #define CONFIG_NRF_MODEM_LIB 
    #define CONFIG_MODEM_KEY_MGMT 

    as suggested in the last link you sent, did not work.

    So, in another effort, I had created, based on the samples/nrf9160/udp sample, my own DTLS echo client. For testing, we have a machine on the Internet where I run OpenSSL's s_server:

    openssl s_server -dtls1_2 -accept 4567 -nocert -psk "01020304050607080901020304050607" --psk_identity "urn:imei:352656100832081" --psk_hint "My server" -state -trace

    The thingy91 connected to the server but the DTLS handshake never worked: The trace from s_server indicated that the client always wanted to renegotiate, endlessly. The OpenSSL s_sever works fine with other DTLS clients.

    This motivated me to use mbedtls directly instead of DTLS being encapsulated in BSD sockets. This way, I hoped that I could better understand what is going on. However, I realised that the modem needs mbedtls configured in a specific way; this seems to restrict what can be done in the application.

    To resolve this issue, I took the following approach: The modem is only used for the link layer and for UDP, using the nrf_* functions (I do not need to multiplex with other network interfaces). The nrf_* socket API is only used for UDP. I patched a copy of mbedtls-3.3.0 such that all symbols and all include files are renamed; this way, there is (hopefully) no interference with the nRF Connect SDK's mbedtls library.

    In this way, I managed to establish a working communication. I still need to test it more thoroughly but right now, this seems the way to go. I still would welcome a PSK-based DTLS echo client in nrf/samples/nrf9160.

    Of course this approach is a bit cumbersome and certainly more complex than using the DTLS extension of the socket API. It also precludes the use of any libraries that use the socket API. On the upside, one gets feedback about the progress of the DTLS handshake, the operations are non-blocking and features such as the protection against replay attacks can be activated.

    Best regards,
    Thomas

  • My guess: transmission timeout issue.

    The server retransmits the server_hello on receiving the clients flight.

    It's easier to check "wireshark captures". Some time ago I write a guide for that, see https://github.com/eclipse-californium/californium/wiki/Logs-and-IP-Capturing-%E2%80%90-How-To-Provide-The-Right-Information#ip-capturing

    > Actually I now think the problem might not be related to renegotiation (I think a saw this once but I am not sure anymore).

    My hint about RFC5746 is more based on my "long years experience" with PSK issues (I'm one developer of Eclispe/Californium and Eclispe/tinydtls, and so I handled a couple of "PSK is not working issues :-). The last was RFC 5746.

  • ClientHello

        {0x00, 0xFF} TLS_EMPTY_RENEGOTIATION_INFO_SCSV

    With that the client is RFC5746 compliant, so no need to use -legacy_renegotiation.

  • I appreciate your longtime experience with DTLS; it is a complicated subject indeed.

    Concerning the timeouts, here is the wireshark output.

    No.     Time            Source         Destination    Protocol   Length  Info
    23146   25.438375165    modem_iaddr    server_iaddr   DTLSv1.2   145     Client Hello
    23147   25.439115290    server_iaddr   modem_iaddr    DTLSv1.2   90      Hello Verify Request
    23251   25.592824864    modem_iaddr    server_iaddr   DTLSv1.2   165     Client Hello
    23252   25.593403772    server_iaddr   modem_iaddr    DTLSv1.2   205     Server Hello, Server Key Exchange, Server Hello Done
    23261   25.847556073    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    25047   26.614250327    server_iaddr   modem_iaddr    DTLSv1.2   144     Server Hello
    25048   26.614379464    server_iaddr   modem_iaddr    DTLSv1.2   78      Server Key Exchange
    25049   26.614466025    server_iaddr   modem_iaddr    DTLSv1.2   67      Server Hello Done
    32985   27.519012708    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    36243   28.180647865    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    36246   28.630243159    server_iaddr   modem_iaddr    DTLSv1.2   144     Server Hello
    36247   28.630322557    server_iaddr   modem_iaddr    DTLSv1.2   78      Server Key Exchange
    36248   28.630371468    server_iaddr   modem_iaddr    DTLSv1.2   67      Server Hello Done
    37892   28.999876157    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    40162   32.854262859    server_iaddr   modem_iaddr    DTLSv1.2   144     Server Hello
    40163   32.854323677    server_iaddr   modem_iaddr    DTLSv1.2   78      Server Key Exchange
    40164   32.854371348    server_iaddr   modem_iaddr    DTLSv1.2   67      Server Hello Done
    42164   35.101685272    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    44148   37.007838038    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    46465   41.046292415    server_iaddr   modem_iaddr    DTLSv1.2   144     Server Hello
    46466   41.046395467    server_iaddr   modem_iaddr    DTLSv1.2   78      Server Key Exchange
    46467   41.046472979    server_iaddr   modem_iaddr    DTLSv1.2   67      Server Hello Done
    48953   46.229800699    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    50963   48.541785982    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
    53974   57.174280969    server_iaddr   modem_iaddr    DTLSv1.2   144     Server Hello
    53975   57.174368081    server_iaddr   modem_iaddr    DTLSv1.2   78      Server Key Exchange
    53976   57.174413149    server_iaddr   modem_iaddr    DTLSv1.2   67      Server Hello Done
    54135   57.803507632    modem_iaddr    server_iaddr   DTLSv1.2   184     Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message

  • From the wiki the link points to

    > Provide NO SCREENSHOTS, the capture files are required

    "Text" nor "pictures" contain the details, but the capture does.

    < 200ms between two Client_Hello, that's quite fast.

    > 26.614250327    server_iaddr   modem_iaddr    DTLSv1.2   144     Server Hello

    That seems to be the first retransmission, maybe, I miss the details of the capture to see.

    So please, try to provide a capture. Maybe the server logs also some more hints, why the Server Hello is retransmitted.

Reply Children
  • Thanks!

    I opened it in wireshark. Even with set PSK secret, wireshark doesn't decrypt the Finish. That may be caused be different PSK secret, than assumed.

    So, back to the cause of causes for failing PSK (which I may have excluded too fast):

    Your PSK secret for openssl is "01020304050607080901020304050607" (hexadecimal).

    Please copy your snipet, how you provide that to the modem.

    (See nRF9160 modem_key_mgmt_cmp returns -1 )

  • You got it. The PSK is provided as a binary array:

    static const unsigned char psk[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};

    If I instead provide the PSK as a string with hexadecimal encoding, just like for s_server, together with strlen(psk), it works.

    static const char* psk = "01020304050607080901020304050607";

    The funny thing is that, in the first case, OpenSSL s_server does not complain about non-matching ciphers (it does in other cases). Neither does my golang DTLS echo server complain (which is using Pion DTLS). And in my mbedtls example, providing the PSK as a binary array works fine.

    Thanks!

  • You're welcome.

    For the most (D)TLS implementations it a good idea, to test them good enough, if they fit for your usage. In my experience, Eclipse/Californium (java) and mbedTLS (C) are good choices for the server side. For the client I use Eclipse/tinydtls (C) and also mbedTLS (C) works well. The other implementations stick to some interpretations of RFC6347 4.2.8, which then cause easily trouble. You will need to make your own experience.

    If you run the device in a public network (this usually comes with ip-address changes on sleep/wakeups), you need either frequently handshakes, or RFC9146 (DTLS 1.2 CID). With that it's mainly Eclipse/Californium in the cloud and tinydtls or mbedTLS (newer version with CID support) on the device, which works pretty well.

  •  

    you may have noticed, that other also struggle with that. Maybe it's possible to improve the documentation. Why did you use the "binary" 

    "static const unsigned char psk[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};"

    approach?

    Did you read Modem key management ? Did you miss a PSK example there?

    Did you read something else? Did you use an (wrong?) example?

  • Good question. Looking around, I found the following things that take binary PSKs:

    - tls_credential_add() in Zephyr. An example is found in samples/net/sockets/echo_client

    - mbedtls_my_ssl_conf_psk()

    - Pion DTLS

    There are probably more such examples. So it is kind of natural to fall to do it the same way. Also I think it is a bit odd to require the hex format in a library. It looks like it is a thin wrapper around the AT command.

    Further, I did not find any example using in-modem PSK. There is the echo-client example in the Zephyr project but it uses tls_credentials_add(). And it does not compile for the nrf9160, which was the reason to open this issue.


    Suggestions:

    - Add a brief section about PSK key format requirements in the modem key mgmt documentation.

    - Add a check for non 0123456789abcdefABCDEF characters in modem_key_mgmt_write etc. when MODEM_KEY_MGMT_CRED_TYPE_PSK is being used. This is not 100% but will help in most cases.

    - Add an example of an echo client that uses in-modem DTLS.



    Regarding your comments on implementations supporting RFC6347 4.2.8 and CID, I will test this. It looks indeed like mbedtls or Eclipse/Californium would be more suitable than Pion DTLS for the server side.

Related