Skip to content

Commit

Permalink
connection seems to working - lack of RNG throws errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Jun 21, 2024
1 parent 5b22d7b commit a077cc2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
5 changes: 4 additions & 1 deletion freertos/pbpal_resolv_and_connect_freertos_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pubnub_internal.h"
#include "pubnub_assert.h"
#include "pubnub_log.h"
#include <string.h>


#define HTTP_PORT 80
Expand All @@ -26,12 +27,14 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t *pb)
PUBNUB_LOG_TRACE("pbpal_resolv_and_connect: gethostbyname(%s)\n",
PUBNUB_ORIGIN_SETTABLE ? pb->origin : PUBNUB_ORIGIN);

struct hostent *host = gethostbyname("www.wp.pl");
struct hostent *host = gethostbyname(PUBNUB_ORIGIN_SETTABLE ? pb->origin : PUBNUB_ORIGIN);
if (host == NULL) {
PUBNUB_LOG_ERROR("pbpal_resolv_and_connect: getting host failed!\n");
return pbpal_resolv_failed_processing;
}
addr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);
addr.sin_family = AF_INET;
memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
if (addr.sin_addr.s_addr == 0) {
PUBNUB_LOG_ERROR("pbpal_resolv_and_connect: no address found!\n");
return pbpal_resolv_failed_processing;
Expand Down
51 changes: 49 additions & 2 deletions mbedtls/pbpal_connect_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ static char pubnub_cert_GlobalSign[] =
"HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n"
"-----END CERTIFICATE-----\n";

static void alloc_setup(pubnub_t* pb);

static const char* get_origin(pubnub_t* pb)
{
#ifdef PUBNUB_ORIGIN_SETTABLE
Expand All @@ -98,16 +100,21 @@ static const char* get_origin(pubnub_t* pb)
enum pbpal_tls_result pbpal_start_tls(pubnub_t* pb)
{
struct pubnub_pal* pal = &pb->pal;
int net_result;

PUBNUB_LOG_TRACE("pbpal_start_tls(pb=%p)\n", pb);
PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
PUBNUB_ASSERT_OPT(PBS_CONNECTED == pb->state);
PUBNUB_LOG_TRACE("pbpal_start_tls(pb=%p)\n", pb);

alloc_setup(pb);

// TODO: Think about pubnub_config.h and where or which to use
// PUBNUB_ASSERT(SOCKET_INVALID != pb->pal.socket);
PUBNUB_LOG_TRACE("pbpal_start_tls(pb=%p) socket=%d\n", pb, pb->pal.socket);

mbedtls_ssl_init(pal->ssl);
PUBNUB_LOG_TRACE("mbedtls_ssl_init() returned\n");
mbedtls_ssl_config_init(pal->ssl_config);
PUBNUB_LOG_TRACE("mbedtls_ssl_config_init() returned\n");

#ifndef ESP_PLATFORM
#error "MBedTLS has been implemented only for ESP32 platform. Contact PubNub support for an implementation on the other ones."
Expand All @@ -116,12 +123,14 @@ enum pbpal_tls_result pbpal_start_tls(pubnub_t* pb)
PUBNUB_LOG_ERROR("Failed to attach CRT bundle\n");
return pbtlsFailed;
}
PUBNUB_LOG_TRACE("esp_crt_bundle_attach() returned\n");
#endif

if (mbedtls_ssl_set_hostname(pal->ssl, get_origin(pb)) != 0) {
PUBNUB_LOG_ERROR("Failed to set hostname\n");
return pbtlsFailed;
}
PUBNUB_LOG_TRACE("mbedtls_ssl_set_hostname() returned\n");

if (mbedtls_ssl_config_defaults(
pal->ssl_config,
Expand All @@ -132,14 +141,18 @@ enum pbpal_tls_result pbpal_start_tls(pubnub_t* pb)
PUBNUB_LOG_ERROR("Failed to set SSL config defaults\n");
return pbtlsFailed;
}
PUBNUB_LOG_TRACE("mbedtls_ssl_config_defaults() returned\n");

mbedtls_ssl_conf_authmode(pal->ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);
PUBNUB_LOG_TRACE("mbedtls_ssl_conf_authmode() returned\n");
mbedtls_ssl_conf_ca_chain(pal->ssl_config, pal->ca_certificates, NULL);
PUBNUB_LOG_TRACE("mbedtls_ssl_conf_ca_chain() returned\n");

if (mbedtls_ssl_setup(pal->ssl, pal->ssl_config) != 0) {
PUBNUB_LOG_ERROR("Failed to setup SSL\n");
return pbtlsFailed;
}
PUBNUB_LOG_TRACE("mbedtls_ssl_setup() returned\n");

mbedtls_net_init(pb->pal.net);

Expand Down Expand Up @@ -188,5 +201,39 @@ enum pbpal_tls_result pbpal_check_tls(pubnub_t* pb) {
return pbtlsEstablished;
}

static void alloc_setup(pubnub_t* pb)
{
pb->pal.ssl = (mbedtls_ssl_context*)malloc(sizeof(mbedtls_ssl_context));
if (pb->pal.ssl == NULL) {
PUBNUB_LOG_ERROR("Failed to allocate memory for mbedtls_ssl_context\n");
return;
}

pb->pal.ssl_config = (mbedtls_ssl_config*)malloc(sizeof(mbedtls_ssl_config));
if (pb->pal.ssl_config == NULL) {
PUBNUB_LOG_ERROR("Failed to allocate memory for mbedtls_ssl_config\n");
return;
}

pb->pal.net = (mbedtls_net_context*)malloc(sizeof(mbedtls_net_context));
if (pb->pal.net == NULL) {
PUBNUB_LOG_ERROR("Failed to allocate memory for mbedtls_net_context\n");
return;
}

pb->pal.ca_certificates = (mbedtls_x509_crt*)malloc(sizeof(mbedtls_x509_crt));
if (pb->pal.ca_certificates == NULL) {
PUBNUB_LOG_ERROR("Failed to allocate memory for mbedtls_x509_crt\n");
return;
}

pb->pal.server_fd = (mbedtls_net_context*)malloc(sizeof(mbedtls_net_context));
if (pb->pal.server_fd == NULL) {
PUBNUB_LOG_ERROR("Failed to allocate memory for mbedtls_net_context\n");
return;
}
}



#endif /* PUBNUB_USE_SSL */
28 changes: 28 additions & 0 deletions mbedtls/pbpal_mbedtls.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "mbedtls/net_sockets.h"
#include "mbedtls/ssl.h"
#include "pubnub_internal.h"

#if PUBNUB_USE_SSL
Expand Down Expand Up @@ -31,6 +33,7 @@ static void buffer_setup(pubnub_t* pb);

void pbpal_init(pubnub_t* pb)
{
PUBNUB_LOG_DEBUG("pbpal_init()\n");
memset(&pb->pal, 0, sizeof pb->pal);

pbntf_setup();
Expand All @@ -42,6 +45,7 @@ void pbpal_init(pubnub_t* pb)
int pbpal_send(pubnub_t* pb, void const* data, size_t n)
{
PUBNUB_ASSERT_INT_OPT(pb->sock_state, ==, STATE_NONE);
PUBNUB_LOG_DEBUG("pbpal_send(pb=%p, data=%p, n=%zu)\n", pb, data, n);

pb->ptr = (uint8_t*)data;
pb->left = n;
Expand All @@ -62,6 +66,8 @@ enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char con
{
char reason[100] = {0};

PUBNUB_LOG_TRACE("pbpal_handle_socket_condition(pb=%p, result=%d, file=%s, line=%d)\n", pb, result, file, line);

if (pb->pal.ssl == NULL) {
return pbpal_handle_socket_error(result, pb, file, line);
}
Expand Down Expand Up @@ -115,6 +121,8 @@ int pbpal_send_status(pubnub_t* pb)
{
int result = 0;

PUBNUB_LOG_TRACE("pbpal_send_status(pb=%p)\n", pb);

if (0 == pb->len) {
PUBNUB_LOG_TRACE("pb=%p pb->len=0, nothing to send\n", pb);
return 0;
Expand Down Expand Up @@ -149,6 +157,7 @@ int pbpal_send_status(pubnub_t* pb)
int pbpal_start_read_line(pubnub_t* pb)
{
unsigned distance;
PUBNUB_LOG_TRACE("pbpal_start_read_line(pb=%p)\n", pb);

PUBNUB_ASSERT_INT_OPT(pb->sock_state, ==, STATE_NONE);

Expand All @@ -172,6 +181,7 @@ int pbpal_start_read_line(pubnub_t* pb)

enum pubnub_res pbpal_line_read_status(pubnub_t* pb)
{
PUBNUB_LOG_TRACE("pbpal_line_read_status(pb=%p)\n", pb);
PUBNUB_ASSERT(STATE_READ_LINE == pb->sock_state);

for (;;) {
Expand Down Expand Up @@ -229,6 +239,7 @@ int pbpal_read_len(pubnub_t* pb)
int pbpal_start_read(pubnub_t* pb, size_t n)
{
unsigned distance;
PUBNUB_LOG_TRACE("pbpal_start_read(pb=%p, n=%zu)\n", pb, n);

PUBNUB_ASSERT_UINT_OPT(n, >, 0);
PUBNUB_ASSERT_INT_OPT(pb->sock_state, ==, STATE_NONE);
Expand Down Expand Up @@ -258,6 +269,7 @@ int pbpal_start_read(pubnub_t* pb, size_t n)
enum pubnub_res pbpal_read_status(pubnub_t* pb)
{
int have_read;
PUBNUB_LOG_TRACE("pbpal_read_status(pb=%p)\n", pb);

PUBNUB_ASSERT(STATE_READ == pb->sock_state);

Expand Down Expand Up @@ -312,13 +324,26 @@ void pbpal_forget(pubnub_t* pb)

int pbpal_close(pubnub_t* pb)
{
PUBNUB_LOG_TRACE("pbpal_close(pb=%p)\n", pb);
pb->unreadlen = 0;

if (pb->pal.ssl != NULL) {
mbedtls_ssl_close_notify(pb->pal.ssl);
mbedtls_ssl_session_reset(pb->pal.ssl);
mbedtls_ssl_free(pb->pal.ssl);
pb->pal.ssl = NULL;

mbedtls_ssl_config_free(pb->pal.ssl_config);
pb->pal.ssl_config = NULL;

mbedtls_net_free(pb->pal.server_fd);
pb->pal.server_fd = NULL;

mbedtls_net_free(pb->pal.net);
pb->pal.net = NULL;

mbedtls_x509_crt_free(pb->pal.ca_certificates);
pb->pal.ca_certificates = NULL;
}

PUBNUB_LOG_TRACE("pb=%p: pbpal_close() returning 0\n", pb);
Expand All @@ -329,6 +354,7 @@ int pbpal_close(pubnub_t* pb)

void pbpal_free(pubnub_t* pb)
{
PUBNUB_LOG_TRACE("pbpal_free(pb=%p)\n", pb);
if (NULL != pb->pal.ssl) {
mbedtls_ssl_free(pb->pal.ssl);
pb->pal.ssl = NULL;
Expand All @@ -355,8 +381,10 @@ void pbpal_free(pubnub_t* pb)
static void pbntf_setup(void)
{
static bool init_done = false;
PUBNUB_LOG_TRACE("pbntf_setup()\n");

if (init_done) {
PUBNUB_LOG_TRACE("pbntf_setup() already done\n");
return;
}

Expand Down

0 comments on commit a077cc2

Please sign in to comment.