-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds experimental support to WolfSSL via PCLL
- Loading branch information
Showing
6 changed files
with
343 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
/* | ||
(PerformanC's) C(ross-compatible) SSL Library | ||
License available on: licenses/performanc.license | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "pcll.h" | ||
|
||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
#include <openssl/err.h> | ||
#include <openssl/ssl.h> | ||
#include <openssl/x509_vfy.h> | ||
#include <openssl/x509v3.h> | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
#include <wolfssl/ssl.h> | ||
#endif | ||
|
||
int pcll_init_ssl_library(void) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
SSL_library_init(); | ||
SSL_load_error_strings(); | ||
OpenSSL_add_all_algorithms(); | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
wolfSSL_Init(); | ||
#endif | ||
|
||
return 0; | ||
} | ||
|
||
int pcll_init_ssl(struct pcll_connection *connection) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
connection->ctx = SSL_CTX_new(TLS_client_method()); | ||
if (!connection->ctx) { | ||
SSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
if (!SSL_CTX_set_min_proto_version(connection->ctx, TLS1_2_VERSION)) { | ||
SSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
connection->ssl = SSL_new(connection->ctx); | ||
if (!connection->ssl) { | ||
SSL_free(connection->ssl); | ||
SSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return 0; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
/* TODO: How portable is to use wolfTLSv1_3_client_method? */ | ||
connection->ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); | ||
if (connection->ctx == NULL) { | ||
wolfSSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
connection->ssl = wolfSSL_new(connection->ctx); | ||
if (connection->ssl == NULL) { | ||
wolfSSL_free(connection->ssl); | ||
wolfSSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return 0; | ||
#endif | ||
|
||
return -1; | ||
} | ||
|
||
int pcll_set_fd(struct pcll_connection *connection, int fd) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
if (!SSL_set_fd(connection->ssl, fd)) { | ||
SSL_free(connection->ssl); | ||
SSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return 0; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
if (wolfSSL_set_fd(connection->ssl, fd) != WOLFSSL_SUCCESS) { | ||
wolfSSL_free(connection->ssl); | ||
wolfSSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return 0; | ||
#endif | ||
|
||
return 0; | ||
} | ||
|
||
int pcll_set_safe_mode(struct pcll_connection *connection, char *hostname) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
SSL_CTX_set_verify(connection->ctx, SSL_VERIFY_PEER, NULL); | ||
|
||
if (!SSL_CTX_set_default_verify_paths(connection->ctx)) return -1; | ||
|
||
if (SSL_set1_host(connection->ssl, hostname) != 1) return -1; | ||
|
||
if (SSL_set_tlsext_host_name(connection->ssl, hostname) != 1) return -1; | ||
|
||
if (SSL_get_verify_result(connection->ssl) != X509_V_OK) return -1; | ||
|
||
return 0; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
(void)hostname; /* No SNI */ | ||
|
||
wolfSSL_CTX_set_verify(connection->ctx, WOLFSSL_VERIFY_PEER, NULL); | ||
|
||
/* TODO: WolfSSL SNI support */ | ||
|
||
if (connection->ssl == NULL) return -1; | ||
|
||
return 0; | ||
#endif | ||
|
||
return -1; | ||
} | ||
|
||
int pcll_connect(struct pcll_connection *connection) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
if (SSL_connect(connection->ssl) != 1) return -1; | ||
|
||
return 0; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
if (wolfSSL_connect(connection->ssl) != SSL_SUCCESS) return -1; | ||
|
||
return 0; | ||
#endif | ||
|
||
return -1; | ||
} | ||
|
||
int pcll_get_error(struct pcll_connection *connection) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
return SSL_get_error(connection->ssl, 0); | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
return wolfSSL_get_error(connection->ssl, 0); | ||
#endif | ||
|
||
return 0; | ||
} | ||
|
||
int pcll_send(struct pcll_connection *connection, char *data, int length) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
if (SSL_write(connection->ssl, data, length) == -1) { | ||
/* TODO: Should we clean up here or let the dev decide? */ | ||
SSL_free(connection->ssl); | ||
SSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return 0; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
if (wolfSSL_write(connection->ssl, data, length) == -1) { | ||
/* TODO: Should we clean up here or let the dev decide? */ | ||
wolfSSL_free(connection->ssl); | ||
wolfSSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return 0; | ||
#endif | ||
|
||
return -1; | ||
} | ||
|
||
int pcll_recv(struct pcll_connection *connection, char *data, int length) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
int recv_length = SSL_read(connection->ssl, data, length); | ||
if (recv_length == -1) { | ||
/* TODO: Should we clean up here or let the dev decide? */ | ||
SSL_free(connection->ssl); | ||
SSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return recv_length; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
int recv_length = wolfSSL_read(connection->ssl, data, length); | ||
if (recv_length == -1) { | ||
/* TODO: Should we clean up here or let the dev decide? */ | ||
wolfSSL_free(connection->ssl); | ||
wolfSSL_CTX_free(connection->ctx); | ||
|
||
return -1; | ||
} | ||
|
||
return recv_length; | ||
#endif | ||
|
||
return -1; | ||
} | ||
|
||
void pcll_free(struct pcll_connection *connection) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
SSL_free(connection->ssl); | ||
SSL_CTX_free(connection->ctx); | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
wolfSSL_free(connection->ssl); | ||
wolfSSL_CTX_free(connection->ctx); | ||
wolfSSL_Cleanup(); | ||
#endif | ||
} | ||
|
||
void pcll_shutdown(struct pcll_connection *connection) { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
SSL_shutdown(connection->ssl); | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
wolfSSL_shutdown(connection->ssl); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef PCLL_H | ||
#define PCLL_H | ||
|
||
#define PCLL_OPENSSL 1 | ||
#define PCLL_WOLFSSL 2 | ||
|
||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
#include <openssl/err.h> | ||
#include <openssl/ssl.h> | ||
#include <openssl/x509_vfy.h> | ||
#include <openssl/x509v3.h> | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
#pragma message "Using wolfSSL is experimental and may not work as expected. Security is NOT guaranteed." | ||
|
||
#include <wolfssl/ssl.h> | ||
#endif | ||
|
||
struct pcll_connection { | ||
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL | ||
SSL *ssl; | ||
SSL_CTX *ctx; | ||
#elif PCLL_SSL_LIBRARY == PCLL_WOLFSSL | ||
WOLFSSL *ssl; | ||
WOLFSSL_CTX *ctx; | ||
#endif | ||
}; | ||
|
||
int pcll_init_ssl_library(void); | ||
|
||
int pcll_init_ssl(struct pcll_connection *connection); | ||
|
||
int pcll_set_fd(struct pcll_connection *connection, int fd); | ||
|
||
int pcll_set_safe_mode(struct pcll_connection *connection, char *hostname); | ||
|
||
int pcll_connect(struct pcll_connection *connection); | ||
|
||
int pcll_get_error(struct pcll_connection *connection); | ||
|
||
int pcll_send(struct pcll_connection *connection, char *data, int length); | ||
|
||
int pcll_recv(struct pcll_connection *connection, char *data, int length); | ||
|
||
void pcll_free(struct pcll_connection *connection); | ||
|
||
void pcll_shutdown(struct pcll_connection *connection); | ||
|
||
#endif /* PCLL_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.