Skip to content

Commit

Permalink
improve: PCLL by allow compiling wo/SSL libraries
Browse files Browse the repository at this point in the history
This commit improves PCLL by allowing to compile software that may not use SSL.
  • Loading branch information
ThePedroo committed Sep 23, 2024
1 parent d07e2ba commit e2860cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
30 changes: 26 additions & 4 deletions external/pcll.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ void pcll_init_ssl_library(void) {
}

int pcll_init_tls_server(struct pcll_server *server, char *cert, char *key) {
(void) server; (void) cert; (void) key;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
server->ctx = SSL_CTX_new(TLS_server_method());
if (server->ctx == NULL) {
Expand Down Expand Up @@ -118,6 +120,8 @@ int pcll_init_tls_server(struct pcll_server *server, char *cert, char *key) {
}

int pcll_init_ssl(struct pcll_connection *connection) {
(void) connection;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
connection->ctx = SSL_CTX_new(TLS_client_method());
if (connection->ctx == NULL) {
Expand Down Expand Up @@ -168,10 +172,15 @@ int pcll_init_ssl(struct pcll_connection *connection) {
if (WSAStartup(MAKEWORD(2, 2), &connection->wsa_data) != 0) return PCLL_ERROR;

return PCLL_SUCCESS;
#else
/* INFO: No found SSL implementation */
return PCLL_ERROR;
#endif
}

int pcll_init_only_ssl(struct pcll_connection *connection) {
(void) connection;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
connection->ssl = SSL_new(connection->ctx);
if (connection->ssl == NULL) {
Expand Down Expand Up @@ -206,9 +215,9 @@ int pcll_init_only_ssl(struct pcll_connection *connection) {
#else
int pcll_set_safe_mode(struct pcll_connection* connection, char* hostname, unsigned short port, int fd) {
#endif
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
(void) port;
(void) connection; (void) hostname; (void) port; (void) fd;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
SSL_CTX_set_verify(connection->ctx, SSL_VERIFY_PEER, NULL);

/* TODO: Get SSL root and CA trust store on PCLL */
Expand All @@ -234,8 +243,7 @@ int pcll_init_only_ssl(struct pcll_connection *connection) {

return PCLL_SUCCESS;
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL
(void) port;
(void) hostname; /* No SNI */
/* INFO: No SNI for WolfSSL */

wolfSSL_CTX_set_verify(connection->ctx, WOLFSSL_VERIFY_PEER, NULL);

Expand Down Expand Up @@ -285,6 +293,8 @@ int pcll_init_only_ssl(struct pcll_connection *connection) {
}

int pcll_connect(struct pcll_connection *connection) {
(void) connection;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
int ret = SSL_connect(connection->ssl);
if (ret != SSL_SUCCESS) return ret;
Expand Down Expand Up @@ -385,6 +395,8 @@ int pcll_connect(struct pcll_connection *connection) {
}

int pcll_accept(struct pcll_connection *connection) {
(void) connection;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
int ret = SSL_accept(connection->ssl);
if (ret != SSL_SUCCESS) {
Expand Down Expand Up @@ -415,6 +427,8 @@ int pcll_accept(struct pcll_connection *connection) {
}

int pcll_get_error(struct pcll_connection *connection, int error) {
(void) connection; (void) error;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
return SSL_get_error(connection->ssl, error);
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL
Expand All @@ -429,6 +443,8 @@ int pcll_get_error(struct pcll_connection *connection, int error) {
}

int pcll_send(struct pcll_connection* connection, char *data, int length) {
(void) connection; (void) data; (void) length;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
int ret = SSL_write(connection->ssl, data, length);
if (ret != length) {
Expand Down Expand Up @@ -496,6 +512,8 @@ int pcll_send(struct pcll_connection* connection, char *data, int length) {
}

int pcll_recv(struct pcll_connection *connection, char *data, int length) {
(void) connection; (void) data; (void) length;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
int recv_length = SSL_read(connection->ssl, data, length);
if (recv_length == -1) {
Expand Down Expand Up @@ -607,6 +625,8 @@ int pcll_recv(struct pcll_connection *connection, char *data, int length) {
}

void pcll_free(struct pcll_connection *connection) {
(void) connection;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
if (connection->ssl != NULL) SSL_free(connection->ssl);
if (connection->ctx != NULL) SSL_CTX_free(connection->ctx);
Expand All @@ -620,6 +640,8 @@ void pcll_free(struct pcll_connection *connection) {
}

void pcll_shutdown(struct pcll_connection *connection) {
(void) connection;

#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
SSL_shutdown(connection->ssl);
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL
Expand Down
14 changes: 14 additions & 0 deletions external/pcll.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/*
(PerformanC's) C(ross-compatible) SSL Library
License available on: licenses/performanc.license
*/

#ifndef PCLL_H
#define PCLL_H

#include <stdint.h>

#define PCLL_OPENSSL 1
#define PCLL_WOLFSSL 2
#define PCLL_SCHANNEL 3
Expand Down Expand Up @@ -49,6 +57,9 @@ struct pcll_connection {
char *decrypted;
char incoming[TCPLIMITS_PACKET_SIZE];
char *hostname;
#else
/* INFO: This is a dummy structure to avoid compilation errors */
uint8_t dummy;
#endif
};

Expand All @@ -62,6 +73,9 @@ struct pcll_server {
#elif PCLL_SSL_LIBRARY == PCLL_SCHANNEL
CtxtHandle *ssl;
CredHandle *ctx;
#else
/* INFO: This is a dummy structure to avoid compilation errors */
uint8_t dummy;
#endif
};

Expand Down

0 comments on commit e2860cd

Please sign in to comment.