|
@@ -2959,24 +2959,25 @@ void ssl_server(void *pvParameters)
|
|
|
}
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
for (;;) {
|
|
|
switch (ssl_state)
|
|
|
{
|
|
|
case SSL_ACCEPT :
|
|
|
|
|
|
- mbedtls_net_free( &client_fd );
|
|
|
+
|
|
|
mbedtls_ssl_session_reset( &ssl );
|
|
|
mbedtls_printf( " . Waiting for a remote connection ...\r\n" );
|
|
|
|
|
|
if((ret = mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL)) != 0) {
|
|
|
mbedtls_printf( " failed\r\n ! mbedtls_net_accept returned %d\r\n", ret );
|
|
|
- ssl_state = SSL_CRITICAL_ERROR;
|
|
|
+ ssl_state = SSL_ERROR;
|
|
|
}
|
|
|
else {
|
|
|
+
|
|
|
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
+
|
|
|
ssl_state = SSL_HANDSHAKE;
|
|
|
}
|
|
|
|
|
@@ -2990,13 +2991,13 @@ void ssl_server(void *pvParameters)
|
|
|
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
{
|
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_handshake returned %d\r\n", ret );
|
|
|
- ssl_state = SSL_ACCEPT;
|
|
|
+ ssl_state = SSL_ERROR;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (ret != 0)
|
|
|
- ssl_state = SSL_ACCEPT;
|
|
|
+ ssl_state = SSL_ERROR;
|
|
|
else {
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
ssl_state = SSL_READ;
|
|
@@ -3006,9 +3007,10 @@ void ssl_server(void *pvParameters)
|
|
|
|
|
|
case SSL_READ :
|
|
|
|
|
|
- SSL_ReadRoutine(&ssl, (unsigned char*)receiveBuf);
|
|
|
- ssl_state = SSL_PROCESSING;
|
|
|
-
|
|
|
+ if (SSL_ReadRoutine(&ssl, (unsigned char*)receiveBuf) <= 0)
|
|
|
+ ssl_state = SSL_ERROR;
|
|
|
+ else
|
|
|
+ ssl_state = SSL_PROCESSING;
|
|
|
break;
|
|
|
|
|
|
case SSL_PROCESSING :
|
|
@@ -3017,16 +3019,27 @@ void ssl_server(void *pvParameters)
|
|
|
if (sendPtr)
|
|
|
ssl_state = SSL_WRITE;
|
|
|
else
|
|
|
- ssl_state = SSL_ACCEPT;
|
|
|
+ ssl_state = SSL_ACCEPT;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SSL_WRITE :
|
|
|
- ssl_state = SSL_WriteRoutine(&ssl, sendPtr, sendBufLoadLen);
|
|
|
+ if (SSL_WriteRoutine(&ssl, sendPtr, sendBufLoadLen) == SSL_WRITE_OK)
|
|
|
+ ssl_state = SSL_CLOSE;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case SSL_CLOSE :
|
|
|
+ mbedtls_ssl_close_notify(&ssl);
|
|
|
+ mbedtls_net_free(&client_fd);
|
|
|
+
|
|
|
+ ssl_state = SSL_ACCEPT;
|
|
|
break;
|
|
|
|
|
|
case SSL_ERROR :
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ mbedtls_net_free(&client_fd);
|
|
|
+ ssl_state = SSL_ACCEPT;
|
|
|
break;
|
|
|
|
|
|
case SSL_CRITICAL_ERROR:
|
|
@@ -3067,28 +3080,20 @@ void HTTPS_Init()
|
|
|
}
|
|
|
|
|
|
|
|
|
-void SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
|
|
|
+int SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
|
|
|
{
|
|
|
int ret;
|
|
|
|
|
|
mbedtls_printf( " < Read from client:" );
|
|
|
do
|
|
|
{
|
|
|
- receivedBufLen = sizeof(receiveBuf) - 1;
|
|
|
- memset(recvBuf, 0, sizeof(receiveBuf));
|
|
|
- ret = mbedtls_ssl_read(ssl, receiveBuf, receivedBufLen);
|
|
|
-
|
|
|
receivedBufLen = RECIVE_BUF_MAX_LEN - 1;
|
|
|
memset(recvBuf, 0, RECIVE_BUF_MAX_LEN);
|
|
|
- ret = mbedtls_ssl_read(ssl, recvBuf, receivedBufLen);
|
|
|
- */
|
|
|
-
|
|
|
-
|
|
|
+ ret = mbedtls_ssl_read(ssl, receiveBuf, receivedBufLen);
|
|
|
|
|
|
if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
- {
|
|
|
continue;
|
|
|
- }
|
|
|
+
|
|
|
if( ret <= 0 )
|
|
|
{
|
|
|
switch( ret )
|
|
@@ -3105,20 +3110,17 @@ void SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf)
|
|
|
mbedtls_printf( " mbedtls_ssl_read returned -0x%x\r\n", -ret );
|
|
|
break;
|
|
|
}
|
|
|
- break;
|
|
|
+ return ret;
|
|
|
}
|
|
|
|
|
|
receivedBufLen = ret;
|
|
|
|
|
|
- mbedtls_printf( " %d bytes read\r\n", receivedBufLen);
|
|
|
+
|
|
|
|
|
|
- printf(receiveBuf);
|
|
|
- printf("\r\n");
|
|
|
-
|
|
|
- if( ret > 0 )
|
|
|
- break;
|
|
|
+
|
|
|
+
|
|
|
|
|
|
- } while(1);
|
|
|
+ } while(0);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -3684,8 +3686,8 @@ SSL_SERVER_STATE SSL_SendFrames(mbedtls_ssl_context *ssl, char *data, int datale
|
|
|
{
|
|
|
index = k * FRAME_SIZE;
|
|
|
|
|
|
- if (SSL_Write(ssl, (data + index), FRAME_SIZE ) == SSL_CRITICAL_ERROR)
|
|
|
- return SSL_CRITICAL_ERROR;
|
|
|
+ if (SSL_Write(ssl, (data + index), FRAME_SIZE ) == SSL_WRITE_ERROR)
|
|
|
+ return SSL_WRITE_ERROR;
|
|
|
|
|
|
nbrframes--;
|
|
|
k++;
|
|
@@ -3693,9 +3695,9 @@ SSL_SERVER_STATE SSL_SendFrames(mbedtls_ssl_context *ssl, char *data, int datale
|
|
|
|
|
|
index = k * FRAME_SIZE;
|
|
|
lastframe = datalen % FRAME_SIZE ;
|
|
|
- if (SSL_Write(ssl, (data + index), lastframe ) == SSL_CRITICAL_ERROR)
|
|
|
- return SSL_CRITICAL_ERROR;
|
|
|
-
|
|
|
+ if (SSL_Write(ssl, (data + index), lastframe ) == SSL_WRITE_ERROR)
|
|
|
+ return SSL_WRITE_ERROR;
|
|
|
+
|
|
|
mbedtls_printf( " . Closing the connection..." );
|
|
|
|
|
|
while( ( retClose = mbedtls_ssl_close_notify( ssl ) ) < 0 )
|
|
@@ -3708,8 +3710,8 @@ SSL_SERVER_STATE SSL_SendFrames(mbedtls_ssl_context *ssl, char *data, int datale
|
|
|
}
|
|
|
|
|
|
mbedtls_printf( " ok\r\n" );
|
|
|
-
|
|
|
- return SSL_ACCEPT;
|
|
|
+*/
|
|
|
+ return SSL_WRITE_OK;
|
|
|
}
|
|
|
|
|
|
SSL_SERVER_STATE SSL_Write(mbedtls_ssl_context *ssl, char *data, int datalen)
|
|
@@ -3723,35 +3725,18 @@ SSL_SERVER_STATE SSL_Write(mbedtls_ssl_context *ssl, char *data, int datalen)
|
|
|
if( ret == MBEDTLS_ERR_NET_CONN_RESET )
|
|
|
{
|
|
|
mbedtls_printf( " failed\r\n ! peer closed the connection\r\n" );
|
|
|
- return SSL_ACCEPT;
|
|
|
+ return SSL_WRITE_ERROR;
|
|
|
}
|
|
|
|
|
|
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
{
|
|
|
mbedtls_printf( " failed\r\n ! mbedtls_ssl_write returned %d\r\n", ret );
|
|
|
-
|
|
|
-
|
|
|
- return SSL_ACCEPT;
|
|
|
+ return SSL_WRITE_ERROR;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
mbedtls_printf( " %d bytes written\r\n", ret);
|
|
|
-
|
|
|
- mbedtls_printf( " . Closing the connection..." );
|
|
|
-
|
|
|
- while( ( ret = mbedtls_ssl_close_notify( ssl ) ) < 0 )
|
|
|
- {
|
|
|
- if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
|
|
|
- {
|
|
|
- mbedtls_printf( " failed\r\n ! mbedtls_ssl_close_notify returned %d\r\n", ret );
|
|
|
- return SSL_ACCEPT;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- mbedtls_printf( " ok\r\n" );
|
|
|
-*/
|
|
|
- ret = 0;
|
|
|
- return SSL_ACCEPT;
|
|
|
+ return SSL_WRITE_OK;
|
|
|
}
|
|
|
|
|
|
|