19
19
#include " mbed-client/m2mtimer.h"
20
20
#include " mbed-client/m2msecurity.h"
21
21
#include " mbed-trace/mbed_trace.h"
22
-
22
+ # include " mbedtls/debug.h "
23
23
#include < string.h>
24
24
25
25
#define TRACE_GROUP " mClt"
@@ -37,6 +37,15 @@ random_number_cb __random_number_callback;
37
37
entropy_cb __entropy_callback;
38
38
39
39
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
+
40
49
M2MConnectionSecurityPimpl::M2MConnectionSecurityPimpl (M2MConnectionSecurity::SecurityMode mode)
41
50
: _flags(0 ),
42
51
_sec_mode(mode)
@@ -61,19 +70,20 @@ M2MConnectionSecurityPimpl::~M2MConnectionSecurityPimpl(){
61
70
mbedtls_pk_free (&_pkey);
62
71
mbedtls_ctr_drbg_free ( &_ctr_drbg );
63
72
mbedtls_entropy_free ( &_entropy );
64
- delete _timer;
73
+ delete _timer;
65
74
}
66
75
67
76
void M2MConnectionSecurityPimpl::timer_expired (M2MTimerObserver::Type type){
68
77
tr_debug (" M2MConnectionSecurityPimpl::timer_expired" );
69
78
if (type == M2MTimerObserver::Dtls && !cancelled){
70
79
int error = continue_connecting ();
71
- if (MBEDTLS_ERR_SSL_TIMEOUT == error) {
80
+ if (MBEDTLS_ERR_SSL_TIMEOUT == error || error == - 1 ) {
72
81
tr_error (" M2MConnectionSecurityPimpl::timer_expired - handshake timeout" );
73
82
if (_ssl.p_bio ) {
74
83
M2MConnectionHandler* ptr = (M2MConnectionHandler*)_ssl.p_bio ;
75
- ptr->handle_connection_error (M2MConnectionHandler::SSL_CONNECTION_ERROR );
84
+ ptr->handle_connection_error (M2MConnectionHandler::SSL_HANDSHAKE_ERROR );
76
85
}
86
+ reset ();
77
87
}
78
88
}
79
89
}
@@ -176,6 +186,10 @@ int M2MConnectionSecurityPimpl::init(const M2MSecurity *security)
176
186
ret = -1 ;
177
187
}
178
188
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
+
179
193
free (srv_public_key);
180
194
free (public_key);
181
195
free (sec_key);
@@ -192,18 +206,12 @@ int M2MConnectionSecurityPimpl::init(const M2MSecurity *security)
192
206
int M2MConnectionSecurityPimpl::start_handshake (){
193
207
tr_debug (" M2MConnectionSecurityPimpl::start_handshake" );
194
208
int ret = -1 ;
195
- int retry_count = 0 ;
196
209
do
197
210
{
198
211
ret = mbedtls_ssl_handshake ( &_ssl );
199
- if (ret == -1 ) {
200
- retry_count++;
201
- tr_debug (" M2MConnectionSecurityPimpl::start_handshake - try again" );
202
- }
203
212
}
204
213
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);
207
215
208
216
if ( ret != 0 ) {
209
217
ret = -1 ;
@@ -300,6 +308,10 @@ int M2MConnectionSecurityPimpl::continue_connecting()
300
308
if ( MBEDTLS_ERR_SSL_WANT_READ == ret ){
301
309
ret = M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ;
302
310
}
311
+ else if (ret == -1 ) {
312
+ return -1 ;
313
+ }
314
+
303
315
if (MBEDTLS_ERR_SSL_TIMEOUT == ret ||
304
316
MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO == ret ||
305
317
MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE == ret ||
0 commit comments