Skip to content

Commit

Permalink
Rename mbedTLS folder to match official naming
Browse files Browse the repository at this point in the history
Signed-off-by: José Simões <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes committed Jul 23, 2018
1 parent 8a12e76 commit 8d0e58c
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 3 deletions.
6 changes: 3 additions & 3 deletions CMake/Modules/FindNF_Networking.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ list(APPEND NF_Networking_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/PAL/Com/sockets
list(APPEND NF_Networking_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/PAL/Lwip)

if(USE_SECURITY_MBEDTLS_OPTION)
#list(APPEND NF_Networking_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/PAL/Com/sockets/ssl/MbedTls)
#list(APPEND NF_Networking_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/PAL/Com/sockets/ssl/mbedTLS)
elseif(USE_SECURITY_OPENSSL_OPTION)
list(APPEND NF_Networking_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/src/PAL/Com/sockets/ssl/openssl)
endif()
Expand All @@ -29,7 +29,7 @@ set(NF_Networking_SRCS
if(USE_SECURITY_MBEDTLS_OPTION)
list(APPEND NF_Networking_SRCS
ssl.cpp
# FIXME - SSL MbedTLS specific interface (WIP)
# FIXME - SSL mbedTLS specific interface (WIP)
#MbedTls_parse_certificate.cpp
#MbedTls.cpp
)
Expand Down Expand Up @@ -69,7 +69,7 @@ foreach(SRC_FILE ${NF_Networking_SRCS})
${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets
${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl
if(USE_SECURITY_MBEDTLS_OPTION)
#${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/MbedTls
#${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS
elseif(USE_SECURITY_OPENSSL_OPTION)
${PROJECT_SOURCE_DIR}/src/PAL/COM/sockets/ssl/openssl
endif()
Expand Down
172 changes: 172 additions & 0 deletions src/PAL/COM/sockets/ssl/mbedTLS/MbedTls.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#include <MbedTls.h>



int ssl_connect_internal(int sd, const char* szTargetHost, int sslContextHandle)
{
int ret;


// if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
// SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
// {
// //printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
// goto exit;
// }

return 0;
}

int sslRecv(void *ctx, unsigned char *buf, size_t len)
{
int sd = (int)ctx;

int ret = socket->recv(buf, len);

if (ret == NSAPI_ERROR_WOULD_BLOCK)
ret = MBEDTLS_ERR_SSL_WANT_READ;
else if (ret < 0)
mbedtls_printf("socket.recv() returned %d\n", ret);

return ret;
}

int sslRecv(void *ctx, unsigned char *buf, size_t len)
{
int sd = static_cast<int>(ctx);

int ret = socket->recv(buf, len);

if (ret == NSAPI_ERROR_WOULD_BLOCK)
ret = MBEDTLS_ERR_SSL_WANT_READ;
else if (ret < 0)
mbedtls_printf("socket.recv() returned %d\n", ret);

return ret;
}

int ssl_accept_internal( int sd, int sslContextHandle )
{
int err = SOCK_SOCKET_ERROR;
int nonblock = 0;
mbedtls_ssl_config *pSsl = NULL;

// Check sslContextHandle range
if((sslContextHandle >= ARRAYSIZE(g_SSL_Driver.m_sslContextArray)) || (sslContextHandle < 0))
{
goto error;
}

pSsl = (mbedtls_ssl_config*)g_SSL_Driver.m_sslContextArray[sslContextHandle].SslContext;
if (ssl == NULL)
{
goto error;
}

// Set socket against this SSL context
mbedtls_ssl_set_bio(pSsl, static_cast<void *>(sd), sslSend, sslRecv, NULL);

// Set non blocking socket
SOCK_ioctl(sd, SOCK_FIONBIO, &nonblock);

// Connecion is set up now do SSL handshake
mbedtls_ssl_handshake()

nonblock = 1;
SOCK_ioctl(sd, SOCK_FIONBIO, &nonblock);

// Save SSL context against socket
SOCKET_DRIVER.SetSocketSslData(sd, (void*)ssl);

error:
return err;
}


int ssl_read_internal( int sd, char* Data, size_t size )
{
mbedtls_ssl_config *pSsl = (mbedtls_ssl_config*)SOCKET_DRIVER.GetSocketSslData(sd);

int ret = mbedtls_ssl_read(pSsl, (unsigned char *)(Data), size);
if ( ret < 0 )
{
return 0;
}

return ret;
}

// Probaly don't need to loop on write, done at higher level
//
int ssl_write_internal( int sd, const char* Data, size_t req_len)
{
mbedtls_ssl_config *pSsl = (mbedtls_ssl_config*)SOCKET_DRIVER.GetSocketSslData(sd);
int ret;

// Loop until all data has been sent or error
int req_offset = 0;
do {
ret = mbedtls_ssl_write( pSsl, (const unsigned char *)(Data + req_offset), req_len - req_offset);
if (ret > 0)
req_offset += static_cast<size_t>(ret);
}
while( req_offset < req_len &&
(ret > 0 ||
ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
ret == MBEDTLS_ERR_SSL_WANT_READ) );

if (ret < 0) {
mbedtls_printf("mbedtls_ssl_write() returned -0x%04X\n", -ret);
return 0;
}

return req_len;

}

int ssl_closesocket_internal( int sd )
{

}

int ssl_pending_internal( int sd )
{

}

bool ssl_exit_context_internal(int sslContextHandle )
{
return false;
}

bool ssl_generic_init_internal( int sslMode, int sslVerify, const char* certificate, int cert_len, const char* pwd, int& sslContextHandle, bool isServer )
{
return false;
}

bool ssl_initialize_internal()
{
// mbedtls_ssl_init(&g_SSL_Driver.SslContext);
return true;
}

bool ssl_uninitialize_internal()
{
return false;
}

void ssl_clear_cert_auth_internal(int sslContextHandle )
{

}

bool ssl_add_cert_auth_internal( int sslContextHandle, const char* certificate, int cert_len, const char* szCertPwd )
{
return false;
}
25 changes: 25 additions & 0 deletions src/PAL/COM/sockets/ssl/mbedTLS/MbedTls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//
#ifndef MBEDTLS_H
#define MBEDTLS_H

#include <nanoHAL.h>

#include <sockets_lwip.h>

#include "mbedtls/platform.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/esp_debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/error.h"
#include "mbedtls/certs.h"


#define SOCKET_DRIVER g_Sockets_LWIP_Driver

#endif
44 changes: 44 additions & 0 deletions src/PAL/COM/sockets/ssl/mbedTLS/MbedTls_parse_certificate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Copyright (c) 2017 The nanoFramework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#include <MbedTls.h>

void SSL_GetCertDateTime_internal(DATE_TIME_INFO * dt, mbedtls_x509_time * mt )
{
dt->year = mt->year;
dt->month = mt->mon;
dt->day = mt->day;
dt->hour = mt->hour;
dt->minute = mt->min;
dt->second = mt->sec;

dt->dlsTime = 0;
dt->tzOffset = 0;
}

bool ssl_parse_certificate_internal(void * certificate, size_t size, void* pwd, void* x509CertData)
{
int ret;
X509CertData* x509 = (X509CertData*)x509CertData;

mbedtls_x509_crt cacert;
mbedtls_x509_crt_init(&cacert);

ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *)certificate, size );
if(ret < 0)
{
// ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x\n\n", -ret);
return false;
}

mbedtls_x509_dn_gets( x509->Issuer, sizeof(x509->Issuer)-1, &cacert.issuer );
mbedtls_x509_dn_gets( x509->Subject, sizeof(x509->Subject)-1, &cacert.subject );

SSL_GetCertDateTime_internal( &x509->EffectiveDate,&cacert.valid_from );
SSL_GetCertDateTime_internal( &x509->ExpirationDate,&cacert.valid_to );

return true;
}

0 comments on commit 8d0e58c

Please sign in to comment.