Skip to content

Commit

Permalink
more work with the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed May 15, 2024
1 parent 6337e81 commit 72d89f1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
52 changes: 45 additions & 7 deletions mbedtls/pbpal_connect_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if PUBNUB_USE_SSL

#include "pbpal.h"
#include "pubnub_netcore.h"
#include "pubnub_pal.h"
#include "pubnub_api_types.h"
#include "pubnub_internal_common.h"
Expand Down Expand Up @@ -75,31 +76,44 @@ static char pubnub_cert_GlobalSign[] =
"HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n"
"-----END CERTIFICATE-----\n";

static const char* get_origin(pubnub_t* pb)
{
#ifdef PUBNUB_ORIGIN_SETTABLE
return pb->origin;
#else
PUBNUB_UNUSED(pb);
return PUBNUB_ORIGIN;
#endif
}

#define PUBNUB_PORT "443"

// TODO: https://github.com/espressif/esp-idf/blob/v5.2.1/examples/protocols/https_mbedtls/main/https_mbedtls_example_main.c
// reference for mbedtls usage
enum pbpal_tls_result pbpal_start_tls(pubnub_t* pb)
{
struct pubnub_pal* pal = &pb->pal;
int net_result;

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

mbedtls_ssl_init(pal->ssl);
mbedtls_ssl_config_init(pal->ssl_config);

#ifndef ESP_PLATFORM
// TODO: not implemented yet
#error "MBedTLS has been implemented only for ESP32 platform. Contact PubNub support for an implementation on the other ones."
#else
if(esp_crt_bundle_attach(pal->ssl_config) != 0) {
PUBNUB_LOG_ERROR("Failed to attach CRT bundle\n");
return pbtlsFailed;
}
#endif

#ifdef PUBNUB_ORIGIN_SETTABLE
if (mbedtls_ssl_set_hostname(pal->ssl, pb->origin) != 0) {
#else
if (mbedtls_ssl_set_hostname(pal->ssl, PUBNUB_ORIGIN) != 0) {
#endif
if (mbedtls_ssl_set_hostname(pal->ssl, get_origin(pb)) != 0) {
PUBNUB_LOG_ERROR("Failed to set hostname\n");
return pbtlsFailed;
}
Expand All @@ -122,7 +136,31 @@ enum pbpal_tls_result pbpal_start_tls(pubnub_t* pb)
return pbtlsFailed;
}

return pbtlsStarted;
mbedtls_net_init(pb->pal.net);

PUBNUB_LOG_DEBUG("Connecting to %s:%s...\n", get_origin(pb), PUBNUB_PORT);
if (0 != mbedtls_net_connect(pb->pal.net, get_origin(pb), PUBNUB_PORT, MBEDTLS_NET_PROTO_TCP)) {
PUBNUB_LOG_ERROR("Failed to connect to %s:%s\n", get_origin(pb), PUBNUB_PORT);
return pbtlsFailed;
}

// TODO: HOW TO SET PEM CERTS?
mbedtls_ssl_set_bio(pal->ssl, pb->pal.net, mbedtls_net_send, mbedtls_net_recv, NULL);

return pbpal_check_tls(pb);
}

enum pbpal_tls_result pbpal_check_tls(pubnub_t* pb) {
int result;

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

result = mbedtls_ssl_handshake(pb->pal.ssl);
result = pbpal_handle_socket_condition(result, pb, __FILE__, __LINE__);

return pbtlsEstablished;
}


Expand Down
35 changes: 34 additions & 1 deletion mbedtls/pbpal_mbedtls.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "msstopwatch/msstopwatch.h"
#include "pubnub_internal.h"
#include "pubnub_log.h"

Expand Down Expand Up @@ -46,7 +47,39 @@ int pbpal_send_str(pubnub_t* pb, char const* s)

enum pubnub_res pbpal_handle_socket_condition(int result, pubnub_t* pb, char const* file, int line)
{
return 0;
if (pb->pal.ssl == NULL) {
// TODO: use pbpal_handle_socket_error() here
return -1;
}

PUBNUB_ASSERT(pb->options.useSSL);

switch(result) {
case 0: // success
break;
case MBEDTLS_ERR_SSL_WANT_READ:
case MBEDTLS_ERR_SSL_WANT_WRITE:
if (pbms_active(pb->pal.tryconn) // no field tryconn!?!?
|| (pbms_elapsed(pb->pal.tryconn) < pb->transaction_timeout_ms)) {
PUBNUB_LOG_TRACE("pb=%p TLS/SSL_I/O operation should retry\n", pb);
return PNR_IN_PROGRESS;
}

pb->pal.ip_timeout = 0; // it seems like a clue to the tryconn field

// TODO: session if in pbpal_openssl.c

PUBNUB_LOG_ERROR("pb=%p TLS/SSL_I/O operation failed, PNR_TIMEOUT\n", pb);

return PNR_TIMEOUT;
default:
// TODO: error handling
PUBNUB_LOG_ERROR("pb=%p TLS/SSL_I/O operation failed, PNR_IO_ERROR\n", pb);
return PNR_IO_ERROR;
}

PUBNUB_LOG_TRACE("pb=%p TLS/SSL_I/O operation successful\n", pb);
return PNR_OK;
}


Expand Down
1 change: 1 addition & 0 deletions mbedtls/pubnub_pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct pubnub_pal {
mbedtls_ssl_config* ssl_config;
mbedtls_x509_crt* ca_certificates;
mbedtls_net_context* net;
mbedtls_net_context* server_fd;
};

#endif /* PUBNUB_PAL_H */

0 comments on commit 72d89f1

Please sign in to comment.