cancel
Showing results for 
Search instead for 
Did you mean: 

mbedTLS STM32F429I-Eval handshake problem

Bence Stocker
Associate II

Hi!

We try connect to a server with STM32F4 Cube mbedTLS Client example application (on an STM324x9I_EVAL-1 board).

On the server side we use letsencrypt certifcates with nginx.

Access log: SSL_do_handshake() failed (SSL: error:1417A0C1:SSL routines:tls_post_process_client_hello:no shared cipher) while SSL handshaking

On the display of the board we got this error: "mbedtls_ssl_handshake returned -0x7780"

We spent hours to find soltion on the internet and we tried a lots of configurations but nothing changed.

Please help us!

7 REPLIES 7

It seems that the Client and the Server don't have common ciphersuites.

I checked the code of your mentioned example, it uses TLSv1.2 and 2 AES-GCM-based ciphersuites (in Inc/mbedtls_config.h):

#define MBEDTLS_SSL_PROTO_TLS1_2
 ...
#define MBEDTLS_SSL_CIPHERSUITES                        \
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,    \
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256

Make sure Nginx is configured to use those ciphersutes, for example this configuration options should do the trick (don't forget to restart Nginx after making changes in the configuration):

ssl_ciphers "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256";
ssl_protocols TLSv1.2;

 Added:

Take note that the example's default configuration will work only with ECDSA keys, most likely you have RSA, so you'll have to edit the MBEDTLS_SSL_CIPHERSUITES definition to include the RSA ciphersuites too:

#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
 
#define MBEDTLS_SSL_CIPHERSUITES                        \
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,    \
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,    \
    MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,      \
    MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256

Bence Stocker
Associate II

Thanks the answer!

Now we have an other error, on the demo board: "mbedtls_ssl_handshake returned -0x3b00"

On the server side (nginx) we got this error: SSL_do_handshake() failed (SSL: error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate:SSL alert number 42

Our mbedtls_config.h:

/**

 *

 * Portions COPYRIGHT 2016 STMicroelectronics

 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved

 *

 ******************************************************************************

 * @file   mbedtls_config.h

 * @author MCD Application Team

 * @brief  the mbedtls custom config header file.

 ******************************************************************************

 * @attention

 *

 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>

 *

 * Redistribution and us   e in source and binary forms, with or without modification,

 * are permitted provided that the following conditions are met:

 *  1. Redistributions of source code must retain the above copyright notice,

 *     this list of conditions and the following disclaimer.

 *  2. Redistributions in binary form must reproduce the above copyright notice,

 *     this list of conditions and the following disclaimer in the documentation

 *     and/or other materials provided with the distribution.

 *  3. Neither the name of STMicroelectronics nor the names of its contributors

 *     may be used to endorse or promote products derived from this software

 *     without specific prior written permission.

 *

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 *

 ******************************************************************************

 */

#ifndef MBEDTLS_CONFIG_H

#define MBEDTLS_CONFIG_H

/* System support */

#define MBEDTLS_HAVE_ASM

#define MBEDTLS_HAVE_TIME

/* mbed TLS feature support */

#define MBEDTLS_ECP_DP_SECP256R1_ENABLED

#define MBEDTLS_ECP_DP_SECP384R1_ENABLED

#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED

#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED

#define MBEDTLS_SSL_PROTO_TLS1_2

/* mbed TLS modules */

#define MBEDTLS_AES_C

#define MBEDTLS_ASN1_PARSE_C

#define MBEDTLS_ASN1_WRITE_C

#define MBEDTLS_BIGNUM_C

#define MBEDTLS_CIPHER_C

#define MBEDTLS_CTR_DRBG_C

#define MBEDTLS_ECDH_C

#define MBEDTLS_ECDSA_C

#define MBEDTLS_ECP_C

#define MBEDTLS_ENTROPY_C

#define MBEDTLS_GCM_C

#define MBEDTLS_MD_C

#define MBEDTLS_NET_C

#define MBEDTLS_OID_C

#define MBEDTLS_PK_C

#define MBEDTLS_PK_PARSE_C

#define MBEDTLS_SHA256_C

#define MBEDTLS_SHA512_C

#define MBEDTLS_SSL_CLI_C

#define MBEDTLS_SSL_SRV_C

#define MBEDTLS_SSL_TLS_C

#define MBEDTLS_X509_CRT_PARSE_C

#define MBEDTLS_SSL_SERVER_NAME_INDICATION

#define MBEDTLS_X509_USE_C

#define MBEDTLS_RSA_C

#define MBEDTLS_PKCS1_V15

/* For test certificates */

#define MBEDTLS_BASE64_C

#define MBEDTLS_CERTS_C

#define MBEDTLS_PEM_PARSE_C

/* Save RAM at the expense of ROM */

#define MBEDTLS_AES_ROM_TABLES

/* Disable double-width division */

#define MBEDTLS_NO_UDBL_DIVISION

/* Save RAM by adjusting to our exact needs */

#define MBEDTLS_ECP_MAX_BITS  384

#define MBEDTLS_MPI_MAX_SIZE   48 // 384 bits is 48 bytes

/* Save RAM at the expense of speed, see ecp.h */

#define MBEDTLS_ECP_WINDOW_SIZE       2

#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0

/* Significant speed benefit at the expense of some ROM */

#define MBEDTLS_ECP_NIST_OPTIM

/*

 * You should adjust this to the exact number of sources you're using: default

 * is the "mbedtls_platform_entropy_poll" source, but you may want to add other ones.

 * Minimum is 2 for the entropy test suite.

 */

#define MBEDTLS_ENTROPY_MAX_SOURCES 2

/* Save ROM and a few bytes of RAM by specifying our own ciphersuite list */

#define MBEDTLS_SSL_CIPHERSUITES                       \

   MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,   \

   MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,   \

   MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,     \

   MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256

/*

 * Save RAM at the expense of interoperability: do this only if you control

 * both ends of the connection! (See coments in "mbedtls/ssl.h".)

 * The minimum size here depends on the certificate chain used as well as the

 * typical size of records.

 */

//#define MBEDTLS_SSL_MAX_CONTENT_LEN            2048

#define MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE

#define MBEDTLS_CAMELLIA_C

#ifdef USE_LCD

#include "lcd_log.h"

#define MBEDTLS_PLATFORM_C

#define MBEDTLS_PLATFORM_PRINTF_MACRO LCD_UsrLog

#endif

/* Customize the entropy data generation */

#define MBEDTLS_NO_PLATFORM_ENTROPY

#define MBEDTLS_ENTROPY_HARDWARE_ALT

#include "mbedtls/check_config.h"

#endif /* MBEDTLS_CONFIG_H */

Can you access your server using a standard browser with no issues? Please check your server using this tool and see if there are errors/warnings.

Bence Stocker
Associate II

Yes,

We tested the server with browser and with an ESP32 wifi modul too. It works both way correctly.

Btw, the nginx we use is a linuxserver/letsencrypt docker image. So the config is not specific.

We run the ssl test, the server got an overall A+ everythings looks fine.

The issue must be that the client doesn't recognize your server's certificate's authority. You'll have to include Let's Encrypt's CA (download from here with right-click -> save as) in your root certificates list. Here in ssl_client.c your example loads mbedTLS's test CA (from mbedTLS/library/certs.c) as root certificate, so it doesn't accept your certificate signed with a real CA:

/*
   * 0. Initialize certificates
   */
  mbedtls_printf( "  . Loading the CA root certificate ..." );
 
  ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_cas_pem,
                        mbedtls_test_cas_pem_len );

Bence Stocker
Associate II

Thanks,

it could be the problem. We tried to change the certificate in certs.c like this:

#define TEST_CA_CRT_RSA_SHA256                                         \

"-----BEGIN CERTIFICATE-----\r\n" \

"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\r\n" \

"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\r\n" \

"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\r\n" \

"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\r\n" \

"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\r\n" \

"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\r\n" \

"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\r\n" \

"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\r\n" \

"Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\r\n" \

"a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7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

"-----END CERTIFICATE-----\r\n"

const char  mbedtls_test_ca_crt_rsa[]  = TEST_CA_CRT_RSA_SHA256;

const size_t mbedtls_test_ca_crt_rsa_len = sizeof( mbedtls_test_ca_crt_rsa );

#define TEST_CA_CRT_RSA_SOME

We got this error on the board: mbedtls_x509_crt_parse returned -03b00

Could you explain where and how should we use this letsencrypt cert?

Thanks

Can you debug and see where mbedTLS returns the error code? It's possible that you need to set correct date & time (and implement/define the mbedtls_time function used by mbedTLS), because mbedTLS is checking if the current datetime is within certificate signing and expiration dates in the x509_crt_verify_chain function:

/* Check time-validity (all certificates) */
        if( mbedtls_x509_time_is_past( &child->valid_to ) )
            *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
 
        if( mbedtls_x509_time_is_future( &child->valid_from ) )
            *flags |= MBEDTLS_X509_BADCERT_FUTURE;

Or it can be 100 other things, debugging will make it easy to diagnose.

P.S.

I have never used mbedTLS, just skimming through the code for some clues.

P.S.2

Embedded TLS is hard (: