mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Switch to the new code style
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
@ -44,12 +44,12 @@
|
||||
!defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_CLI_C) || \
|
||||
!defined(UNIX)
|
||||
|
||||
int main( void )
|
||||
int main(void)
|
||||
{
|
||||
mbedtls_printf( "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_NET_C and/or MBEDTLS_SSL_CLI_C and/or UNIX "
|
||||
"not defined.\n");
|
||||
mbedtls_exit( 0 );
|
||||
mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_NET_C and/or MBEDTLS_SSL_CLI_C and/or UNIX "
|
||||
"not defined.\n");
|
||||
mbedtls_exit(0);
|
||||
}
|
||||
#else
|
||||
|
||||
@ -141,8 +141,7 @@ const unsigned char ca_cert[] = {
|
||||
};
|
||||
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
||||
|
||||
enum exit_codes
|
||||
{
|
||||
enum exit_codes {
|
||||
exit_ok = 0,
|
||||
ctr_drbg_seed_failed,
|
||||
ssl_config_defaults_failed,
|
||||
@ -156,7 +155,7 @@ enum exit_codes
|
||||
};
|
||||
|
||||
|
||||
int main( void )
|
||||
int main(void)
|
||||
{
|
||||
int ret = exit_ok;
|
||||
mbedtls_net_context server_fd;
|
||||
@ -169,62 +168,57 @@ int main( void )
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ssl_context ssl;
|
||||
mbedtls_ssl_config conf;
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
/*
|
||||
* 0. Initialize and setup stuff
|
||||
*/
|
||||
mbedtls_net_init( &server_fd );
|
||||
mbedtls_ssl_init( &ssl );
|
||||
mbedtls_ssl_config_init( &conf );
|
||||
mbedtls_net_init(&server_fd);
|
||||
mbedtls_ssl_init(&ssl);
|
||||
mbedtls_ssl_config_init(&conf);
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_init( &ca );
|
||||
mbedtls_x509_crt_init(&ca);
|
||||
#endif
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers, strlen( pers ) ) != 0 )
|
||||
{
|
||||
mbedtls_entropy_init(&entropy);
|
||||
if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers, strlen(pers)) != 0) {
|
||||
ret = ctr_drbg_seed_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mbedtls_ssl_config_defaults( &conf,
|
||||
MBEDTLS_SSL_IS_CLIENT,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT ) != 0 )
|
||||
{
|
||||
if (mbedtls_ssl_config_defaults(&conf,
|
||||
MBEDTLS_SSL_IS_CLIENT,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
|
||||
ret = ssl_config_defaults_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
mbedtls_ssl_conf_psk( &conf, psk, sizeof( psk ),
|
||||
(const unsigned char *) psk_id, sizeof( psk_id ) - 1 );
|
||||
mbedtls_ssl_conf_psk(&conf, psk, sizeof(psk),
|
||||
(const unsigned char *) psk_id, sizeof(psk_id) - 1);
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if( mbedtls_x509_crt_parse_der( &ca, ca_cert, sizeof( ca_cert ) ) != 0 )
|
||||
{
|
||||
if (mbedtls_x509_crt_parse_der(&ca, ca_cert, sizeof(ca_cert)) != 0) {
|
||||
ret = x509_crt_parse_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_ca_chain( &conf, &ca, NULL );
|
||||
mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_REQUIRED );
|
||||
mbedtls_ssl_conf_ca_chain(&conf, &ca, NULL);
|
||||
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
#endif
|
||||
|
||||
if( mbedtls_ssl_setup( &ssl, &conf ) != 0 )
|
||||
{
|
||||
if (mbedtls_ssl_setup(&ssl, &conf) != 0) {
|
||||
ret = ssl_setup_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if( mbedtls_ssl_set_hostname( &ssl, HOSTNAME ) != 0 )
|
||||
{
|
||||
if (mbedtls_ssl_set_hostname(&ssl, HOSTNAME) != 0) {
|
||||
ret = hostname_failed;
|
||||
goto exit;
|
||||
}
|
||||
@ -233,7 +227,7 @@ int main( void )
|
||||
/*
|
||||
* 1. Start the connection
|
||||
*/
|
||||
memset( &addr, 0, sizeof( addr ) );
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
|
||||
ret = 1; /* for endianness detection */
|
||||
@ -241,23 +235,20 @@ int main( void )
|
||||
addr.sin_addr.s_addr = *((char *) &ret) == ret ? ADDR_LE : ADDR_BE;
|
||||
ret = 0;
|
||||
|
||||
if( ( server_fd.fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
|
||||
{
|
||||
if ((server_fd.fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
ret = socket_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( connect( server_fd.fd,
|
||||
(const struct sockaddr *) &addr, sizeof( addr ) ) < 0 )
|
||||
{
|
||||
if (connect(server_fd.fd,
|
||||
(const struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||
ret = connect_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
|
||||
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
|
||||
|
||||
if( mbedtls_ssl_handshake( &ssl ) != 0 )
|
||||
{
|
||||
if (mbedtls_ssl_handshake(&ssl) != 0) {
|
||||
ret = ssl_handshake_failed;
|
||||
goto exit;
|
||||
}
|
||||
@ -265,26 +256,25 @@ int main( void )
|
||||
/*
|
||||
* 2. Write the GET request and close the connection
|
||||
*/
|
||||
if( mbedtls_ssl_write( &ssl, (const unsigned char *) GET_REQUEST,
|
||||
sizeof( GET_REQUEST ) - 1 ) <= 0 )
|
||||
{
|
||||
if (mbedtls_ssl_write(&ssl, (const unsigned char *) GET_REQUEST,
|
||||
sizeof(GET_REQUEST) - 1) <= 0) {
|
||||
ret = ssl_write_failed;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_ssl_close_notify( &ssl );
|
||||
mbedtls_ssl_close_notify(&ssl);
|
||||
|
||||
exit:
|
||||
mbedtls_net_free( &server_fd );
|
||||
mbedtls_net_free(&server_fd);
|
||||
|
||||
mbedtls_ssl_free( &ssl );
|
||||
mbedtls_ssl_config_free( &conf );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_ssl_free(&ssl);
|
||||
mbedtls_ssl_config_free(&conf);
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
mbedtls_entropy_free(&entropy);
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free( &ca );
|
||||
mbedtls_x509_crt_free(&ca);
|
||||
#endif
|
||||
|
||||
mbedtls_exit( ret );
|
||||
mbedtls_exit(ret);
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user