Hi,
I am trying to test some code I wrote which should establish a TLS client connection, using ECC certificate. I am creating self signed certificate using openssl (see process below) and then I started the openssl server. I keep getting the error code MBEDTLS_ERR_X509_CERT_VERIFY_FAILED when I try to do the handshake. This only happens when I set the authentication mode to MBEDTLS_SSL_VERIFY_REQUIRED.
I am not entirely sure where I may be going wrong. Please see code snippets below.
I then take “certificate.pem” and assign it to “ctx->cert_signer” this is the ca certificate (Or do I have that wrong?). The certificate.pem, is generated using the openSSL command below.
The call Int_Client_Credentials() doesn’t do anything but just return success.
Code
static const int CIPHER_SUITES[] =
{
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
0
};
mbedtls_ssl_init(&(ctx->ssl_ctx));
mbedtls_ssl_config_init(&(ctx->ssl_config));
mbedtls_x509_crt_init(&(ctx->cert_signer));
/*Parse & load the signer cert*/
int rc = mbedtls_x509_crt_parse(&(ctx->cert_signer), config->signer_cert.buf, config->signer_cert.size);
ASSERT_DEBUG_RETURN_RELEASE(0 == rc, rc);
/*Init TLS/SSL protocol defaults*/
rc = mbedtls_ssl_config_defaults(&(ctx->ssl_config), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
ASSERT_DEBUG_RETURN_RELEASE(0 == rc, rc);
/*FIXME is this required?*/
mbedtls_ssl_conf_ciphersuites(&(ctx->ssl_config), CIPHER_SUITES);
/* Server certificate validation is mandatory. */
mbedtls_ssl_conf_authmode(&(ctx->ssl_config), MBEDTLS_SSL_VERIFY_REQUIRED);
/* Set the RNG callback. */
mbedtls_ssl_conf_rng(&(ctx->ssl_config), DRNG_Callback, NULL);
/*Setup the chain of trust. The issuer cert for the device cert is the signer cert*/
mbedtls_ssl_conf_ca_chain(&(ctx->ssl_config), &ctx->cert_signer, NULL);
/*FIXME This currently does nothing*/
rc = Int_Client_Credentials(ctx, config);
ASSERT_DEBUG_RETURN_RELEASE(0 == rc, rc);
/* Set the resulting protocol configuration. */
rc = mbedtls_ssl_setup(&(ctx->ssl_ctx), &(ctx->ssl_config));
ASSERT_DEBUG_RETURN_RELEASE(0 == rc, rc);
/*Set the server to connect to, optional must make sure we always connecto to this server only*/
rc = mbedtls_ssl_set_hostname(&(ctx->ssl_ctx), config->server_url);
ASSERT_DEBUG_RETURN_RELEASE(0 == rc, rc);
/* Set the socket Callback for TLS*/
mbedtls_ssl_set_bio(&(ctx->ssl_ctx), ctx->sock, Socket_Send_Callback, Socket_Recv_Callback, NULL);
Openssl Command To Generate Cert
openssl ecparam -genkey -name prime256v1 -out key.pem
openssl req -new -sha256 -key key.pem -out csr.csr
openssl req -x509 -sha256 -days 365 -key key.pem -in csr.csr -out certificate.pem
PenSSL start the server
The certifcate.pem is the same one i use in the code.
openssl s_server -status_verbose -key key.pem -cert certificate.pem -accept 30000 -www