sandbox / mbed-client-mbedtls

Fork of mbed-client-mbedtls by Christopher Haster

Revision:
4:1ca4b8bd7dd2
Parent:
1:c93becfcbf22
Child:
5:840aa460b437
--- a/source/m2mconnectionsecuritypimpl.cpp	Fri Feb 19 17:47:54 2016 +0000
+++ b/source/m2mconnectionsecuritypimpl.cpp	Mon Apr 04 17:31:06 2016 +0300
@@ -18,8 +18,11 @@
 #include "mbed-client-mbedtls/m2mconnectionsecuritypimpl.h"
 #include "mbed-client/m2mtimer.h"
 #include "mbed-client/m2msecurity.h"
+#include "mbed-trace/mbed_trace.h"
 #include <string.h>
 
+#define TRACEW_GROUP "mClt"
+
 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
 int mbedtls_timing_get_delay( void *data );
 int entropy_poll( void *data, unsigned char *output, size_t len, size_t *olen );
@@ -59,15 +62,18 @@
 }
 
 void M2MConnectionSecurityPimpl::timer_expired(M2MTimerObserver::Type type){
-    if(type == M2MTimerObserver::Dtls && !cancelled && !_is_blocking){
+    if(type == M2MTimerObserver::Dtls && !cancelled){
+        tr_debug("M2MConnectionSecurityPimpl::timer_expired - continue non block");
         int error = continue_connecting();
         if(MBEDTLS_ERR_SSL_TIMEOUT == error) {
+            tr_debug("M2MConnectionSecurityPimpl::timer_expired - timeout");
             if(_ssl.p_bio) {
                 M2MConnectionHandler* ptr = (M2MConnectionHandler*)_ssl.p_bio;
                 ptr->handle_connection_error(4);
             }
         }
     } else {
+        tr_debug("M2MConnectionSecurityPimpl::timer_expired connection error");
         if(_ssl.p_bio) {
             M2MConnectionHandler* ptr = (M2MConnectionHandler*)_ssl.p_bio;
             ptr->handle_connection_error(4);
@@ -182,7 +188,6 @@
             {
                 return -1;
             }
-
             mbedtls_ssl_conf_own_cert(&_conf, &_owncert, &_pkey);
             //TODO: use MBEDTLS_SSL_VERIFY_REQUIRED instead of optional
             //MBEDTLS_SSL_VERIFY_NONE to test without verification (was MBEDTLS_SSL_VERIFY_OPTIONAL)
@@ -209,6 +214,7 @@
 }
 
 int M2MConnectionSecurityPimpl::connect(M2MConnectionHandler* connHandler){
+    tr_debug("M2MConnectionSecurityPimpl::connect");
     int ret=-1;
     if(!_init_done){
         return ret;
@@ -216,8 +222,8 @@
 
     _is_blocking = true;
 
-    // This is for blocking sockets timeout happens once at 60 seconds
-    mbedtls_ssl_conf_handshake_timeout( &_conf, 60000, 61000 );
+    // Use default handshake timeout values
+    mbedtls_ssl_conf_handshake_timeout( &_conf, 1000, 60000 );
     mbedtls_ssl_conf_rng( &_conf, mbedtls_ctr_drbg_random, &_ctr_drbg );
 
     if( ( ret = mbedtls_ssl_setup( &_ssl, &_conf ) ) != 0 )
@@ -241,20 +247,22 @@
     while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
            ret == MBEDTLS_ERR_SSL_WANT_WRITE );
 
-    if( ret != 0 )
-    {
+    tr_debug("M2MConnectionSecurityPimpl::connect - handshake, ret: %d", ret);
+
+    if( ret != 0 ) {
         ret = -1;
-    }else{
-        if( ( _flags = mbedtls_ssl_get_verify_result( &_ssl ) ) != 0 )
-        {
+    }else {
+        if( ( _flags = mbedtls_ssl_get_verify_result( &_ssl ) ) != 0 ) {
             ret = -1;
         }
     }
+    tr_debug("M2MConnectionSecurityPimpl::connect - out, ret: %d", ret);
     return ret;
 }
 
 int M2MConnectionSecurityPimpl::start_connecting_non_blocking(M2MConnectionHandler* connHandler)
 {
+    tr_debug("M2MConnectionSecurityPimpl::start_connecting_non_blocking");
     int ret=-1;
     if(!_init_done){
         return ret;
@@ -299,11 +307,13 @@
     {
         ret = -1;
     }
+    tr_debug("M2MConnectionSecurityPimpl::start_connecting_non_blocking, ret: %d", ret);
     return ret;
 }
 
 int M2MConnectionSecurityPimpl::continue_connecting()
 {
+    tr_debug("M2MConnectionSecurityPimpl::continue_connecting");
     int ret=-1;
     while( ret != M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ){
         ret = mbedtls_ssl_handshake_step( &_ssl );
@@ -324,10 +334,12 @@
             return 0;
         }
     }
+    tr_debug("M2MConnectionSecurityPimpl::continue_connecting, ret: %d", ret);
     return ret;
 }
 
 int M2MConnectionSecurityPimpl::send_message(unsigned char *message, int len){
+    tr_debug("M2MConnectionSecurityPimpl::send_message");
     int ret=-1;
     if(!_init_done){
         return ret;
@@ -337,6 +349,7 @@
     while( ret == MBEDTLS_ERR_SSL_WANT_READ ||
            ret == MBEDTLS_ERR_SSL_WANT_WRITE );
 
+    tr_debug("M2MConnectionSecurityPimpl::send_message - ret: %d", ret);
     return ret; //bytes written
 }
 
@@ -385,31 +398,41 @@
 }
 
 void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ){
+    tr_debug("mbedtls_timing_set_delay - intermediate: %d", int_ms);
+    tr_debug("mbedtls_timing_set_delay - final: %d", fin_ms);
     M2MTimer* timer = (M2MTimer*) data;
     if(!timer) {
         return;
     }
     if( int_ms > 0 && fin_ms > 0 ){
+        tr_debug("mbedtls_timing_set_delay - start");
         cancelled = false;
+        timer->stop_timer();
         timer->start_dtls_timer(int_ms, fin_ms);
     }else{
+        tr_debug("mbedtls_timing_set_delay - stop");
         cancelled = true;
         timer->stop_timer();
     }
 }
 
 int mbedtls_timing_get_delay( void *data ){
+    tr_debug("mbedtls_timing_get_delay");
     M2MTimer* timer = (M2MTimer*) data;
     if(!timer){
         return 0;
     }
     if(true == cancelled) {
+        tr_debug("mbedtls_timing_get_delay - ret -1");
         return -1;
     } else if( timer->is_total_interval_passed() ){
+        tr_debug("mbedtls_timing_get_delay - ret 2");
         return 2;
     }else if( timer->is_intermediate_interval_passed() ){
+        tr_debug("mbedtls_timing_get_delay - ret 1");
         return 1;
     }else{
+        tr_debug("mbedtls_timing_get_delay - ret 0");
         return 0;
     }
 }