Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit bda0ae3

Browse files
author
Antti Yli-Tokola
committed
Merge pull request #40 from ARMmbed/retry
Move handshake retry logic to connectionhandler side
2 parents a6803a6 + 8b285c2 commit bda0ae3

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

source/m2mconnectionsecuritypimpl.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "mbed-client/m2mtimer.h"
2020
#include "mbed-client/m2msecurity.h"
2121
#include "mbed-trace/mbed_trace.h"
22-
22+
#include "mbedtls/debug.h"
2323
#include <string.h>
2424

2525
#define TRACE_GROUP "mClt"
@@ -37,6 +37,15 @@ random_number_cb __random_number_callback;
3737
entropy_cb __entropy_callback;
3838

3939

40+
/*
41+
static void mbedtls_debug( void *ctx, int level,
42+
const char *file, int line, const char *str )
43+
{
44+
((void) level);
45+
tr_debug("%s", str);
46+
}
47+
*/
48+
4049
M2MConnectionSecurityPimpl::M2MConnectionSecurityPimpl(M2MConnectionSecurity::SecurityMode mode)
4150
: _flags(0),
4251
_sec_mode(mode)
@@ -61,19 +70,20 @@ M2MConnectionSecurityPimpl::~M2MConnectionSecurityPimpl(){
6170
mbedtls_pk_free(&_pkey);
6271
mbedtls_ctr_drbg_free( &_ctr_drbg );
6372
mbedtls_entropy_free( &_entropy );
64-
delete _timer;
73+
delete _timer;
6574
}
6675

6776
void M2MConnectionSecurityPimpl::timer_expired(M2MTimerObserver::Type type){
6877
tr_debug("M2MConnectionSecurityPimpl::timer_expired");
6978
if(type == M2MTimerObserver::Dtls && !cancelled){
7079
int error = continue_connecting();
71-
if(MBEDTLS_ERR_SSL_TIMEOUT == error) {
80+
if(MBEDTLS_ERR_SSL_TIMEOUT == error || error == -1) {
7281
tr_error("M2MConnectionSecurityPimpl::timer_expired - handshake timeout");
7382
if(_ssl.p_bio) {
7483
M2MConnectionHandler* ptr = (M2MConnectionHandler*)_ssl.p_bio;
75-
ptr->handle_connection_error(M2MConnectionHandler::SSL_CONNECTION_ERROR);
84+
ptr->handle_connection_error(M2MConnectionHandler::SSL_HANDSHAKE_ERROR);
7685
}
86+
reset();
7787
}
7888
}
7989
}
@@ -176,6 +186,10 @@ int M2MConnectionSecurityPimpl::init(const M2MSecurity *security)
176186
ret = -1;
177187
}
178188

189+
/* Enable following two lines to get traces from mbedtls */
190+
/*mbedtls_ssl_conf_dbg( &_conf, mbedtls_debug, stdout );
191+
mbedtls_debug_set_threshold(3);*/
192+
179193
free(srv_public_key);
180194
free(public_key);
181195
free(sec_key);
@@ -192,18 +206,12 @@ int M2MConnectionSecurityPimpl::init(const M2MSecurity *security)
192206
int M2MConnectionSecurityPimpl::start_handshake(){
193207
tr_debug("M2MConnectionSecurityPimpl::start_handshake");
194208
int ret = -1;
195-
int retry_count = 0;
196209
do
197210
{
198211
ret = mbedtls_ssl_handshake( &_ssl );
199-
if (ret == -1) {
200-
retry_count++;
201-
tr_debug("M2MConnectionSecurityPimpl::start_handshake - try again");
202-
}
203212
}
204213
while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
205-
ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
206-
(ret == -1 && retry_count <= RETRY_COUNT));
214+
ret == MBEDTLS_ERR_SSL_WANT_WRITE);
207215

208216
if( ret != 0 ) {
209217
ret = -1;
@@ -300,6 +308,10 @@ int M2MConnectionSecurityPimpl::continue_connecting()
300308
if( MBEDTLS_ERR_SSL_WANT_READ == ret ){
301309
ret = M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ;
302310
}
311+
else if (ret == -1) {
312+
return -1;
313+
}
314+
303315
if(MBEDTLS_ERR_SSL_TIMEOUT == ret ||
304316
MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO == ret ||
305317
MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE == ret ||

test/mbed-client-mbed-tls/unittest/m2mconnectionsecuritypimpl_mbedtls/test_m2mconnectionsecuritypimpl_mbedtls.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TestObserver : public M2MConnectionObserver {
4949
uint16_t,
5050
const M2MConnectionObserver::SocketAddress &){}
5151

52-
void socket_error(uint8_t error_code){}
52+
void socket_error(uint8_t error_code, bool retry = true){}
5353

5454
void address_ready(const M2MConnectionObserver::SocketAddress &,
5555
M2MConnectionObserver::ServerType,
@@ -319,6 +319,10 @@ void Test_M2MConnectionSecurityPimpl::test_continue_connecting()
319319
mbedtls_stub::expected_int = MBEDTLS_ERR_SSL_WANT_READ;
320320
CHECK( M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ == impl.continue_connecting());
321321

322+
mbedtls_stub::expected_int = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST;
323+
impl._ssl.state = MBEDTLS_SSL_CLIENT_HELLO;
324+
CHECK( MBEDTLS_ERR_SSL_TIMEOUT == impl.continue_connecting());
325+
322326
mbedtls_stub::expected_int = -6;
323327
impl._ssl.state = MBEDTLS_SSL_HANDSHAKE_OVER;
324328
CHECK( 0 == impl.continue_connecting());

test/mbed-client-mbed-tls/unittest/stub/m2mbase_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,8 @@ const String& M2MBase::uri_path() const
308308
{
309309
return *m2mbase_stub::string_value;
310310
}
311+
312+
bool M2MBase::is_under_observation() const
313+
{
314+
return m2mbase_stub::bool_value;
315+
}

0 commit comments

Comments
 (0)