TLS handshake issue

Hello, guys. I’m trying to make a secure connection between the server and the client. To find out, how to use available api (from mbedtls) I used to compile examples from github repository of mbedtls.

After successful compilation I launched the server and the client: ssl_client2.exe and ssl_server2.exe. Yes, the binaries were built on Windows with visual studio 2017. I’ve got next messages. From the server:

. Performing the SSL/TLS handshake… failed
! mbedtls_ssl_handshake returned -0x4e

Last error was: -78 - NET - Sending information through the socket failed

And from the client:

. Performing the SSL/TLS handshake… failed
! mbedtls_ssl_handshake returned -0x2700
Unable to verify the server’s certificate. Either it is invalid,
or you didn’t set ca_file or ca_path to an appropriate value.
Alternatively, you may want to use auth_mode=optional for testing purposes.

Last error was: -0x2700 - X509 - Certificate verification failed, e.g. CRL, CA or signature check failed

Can somebody explain me what I’m doing wrong?

Next I tried to look at the first example: ssl_server.exe and ssl_client1.exe

I used them as an example for my own implementation. But first I wanted to check if the algo I planned to use was correct. So the algo was to generate certificate for the server and the root certificate with openssl utility and use them for the handshake in next way:

  1. First I generated keys for certificates:

openssl genrsa -out rootca.key 4096
openssl genrsa -out user.key 2048

  1. Then I crafted certificates for the server and the root:

openssl req -x509 -new -nodes -key rootca.key -days 365 -out rootca.crt
openssl req -new -key user.key -out user.csr
openssl x509 -req -in user.csr -CA rootca.crt -CAkey rootca.key -CAcreateserial -out user.crt -days 365

  1. Then I verified the certificates to check that everything was done right:

openssl verify -CAfile rootca.crt rootca.crt
openssl verify -CAfile rootca.crt user.crt
openssl verify -CAfile user.crt user.crt //that was a fail

Client stops it’s working with the next error message:

. Performing the SSL/TLS handshake…c:\work\code\mbedtls-2.12.0\mbedtls-2.12.0\library\ssl_tls.c:4837: x509_verify_cert() returned -9984 (-0x2700)
failed
! mbedtls_ssl_handshake returned -0x2700

Last error was: -9984 - X509 - Certificate verification failed, e.g. CRL, CA or signature check failed

What is the reason of such errors?

Hi @Ievgen

You mention:

openssl verify -CAfile user.crt user.crt //that was a fail

Does this mean that this operation failed?
If so, then the certificate is not probably signed by the CA.

The error you are receiving means that the certificate the the server sent to the client was not verified correct by the client, and that it couldn’t find a proper CA.
Have you set rootCA.crt as the ca_file parameter for your client? Have you set user.crt as the user.crt as the crt_file parameter and user.key as the key_file parameter for your server application?

Regards,
Mbed TLS Team member
Ron

Hi, @roneld01

If we are talking about server side, I’m using the root certificate and user certificate in the next way:

    ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) UserCert,
                              sizeof(UserCert) );
if( ret != 0 )
{
    mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
    goto exit;
}

ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) RootCA,
                              sizeof(RootCA) );

if( ret != 0 )
{
    mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
    goto exit;
}

ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) UserKey,
                             sizeof(UserKey), NULL, 0 );
if( ret != 0 )

I hardcoded both certificates inside my application to avoid usage of files. What was wrong there?

@Ievgen
Mbed TLS is delivered with sample applications, that you should look at as reference.

If you are hardcoding the certificate, keep in mind that they will expire some day, and you will need a way to securely replace them.
Have you set the root CA in the client application as a trusted root CA certificate?

In addition, on the server side, you don’t need the root CA.

I suggest you first run the sample applications ssl_client2 and ssl_server2 with your certificates, to eliminate any platform specific issues.
Regards,
Mbed TLS Team member
Ron

Hi, Ron! Now I’ve got a stable versions of client/server for testing, and It seems they are working fine with default settings. But I need to use boost.asio on server side and I’ve implemented async version with openssl library. So now I have:

server with openssl;
client with mbedtls;

During handshake client gets error -0x07200, MBEDTLS_ERR_SSL_INVALID_RECORD. I found your answer to some user that this error can be occured if the certificate parsing was wrong or some data in certificates was wrong.

So could you please provide any article/book/some other material where I can find how I should generate certificates and how to make operations with them (checking, validation, etc.)?

Regards,
Ievgen.

Hi @Ievgen
MBEDTLS_ERR_SSL_INVALID_RECORD can be returned in numerous locations. In general, it is because the received record failed in parsing.
I suggest you look at this article so you will have better information where this error is returned.
Am I right to assume you have configured the MBEDTLS_SSL_MAX_CONTENT_LEN ?
In case the received certificate is larger than the configured MBEDTLS_SSL_MAX_CONTENT_LEN, you might get this error as well.
Have you considered using certificates signed with ECDSA, as te ECC keys are shorter with same security strength?
Regards,
Mbed TLS Team member
Ron