2021-01-02 13:10:00 -05:00
|
|
|
#include "tls.h"
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef enum _direction_t {
|
|
|
|
k_direction_undetermined,
|
|
|
|
k_direction_accept,
|
|
|
|
k_direction_connect,
|
|
|
|
} direction_t;
|
|
|
|
|
|
|
|
typedef struct _tf_tls_context_t {
|
|
|
|
SSL_CTX* context;
|
|
|
|
} tf_tls_context_t;
|
|
|
|
|
|
|
|
typedef struct _tf_tls_session_t {
|
|
|
|
tf_tls_context_t* context;
|
|
|
|
BIO* bio_in;
|
|
|
|
BIO* bio_out;
|
|
|
|
SSL* ssl;
|
|
|
|
const char* hostname;
|
|
|
|
direction_t direction;
|
|
|
|
} tf_tls_session_t;
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_tls_context_t* tf_tls_context_create()
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_tls_context_t* context = tf_malloc(sizeof(tf_tls_context_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(context, 0, sizeof(*context));
|
2024-01-06 16:33:20 -05:00
|
|
|
OPENSSL_init_ssl(0, NULL);
|
2021-01-02 13:10:00 -05:00
|
|
|
context->context = SSL_CTX_new(SSLv23_method());
|
|
|
|
SSL_CTX_set_default_verify_paths(context->context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
bool tf_tls_context_set_certificate(tf_tls_context_t* context, const char* certificate)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = 0;
|
|
|
|
BIO* bio = BIO_new(BIO_s_mem());
|
|
|
|
BIO_puts(bio, certificate);
|
|
|
|
X509* x509 = PEM_read_bio_X509(bio, 0, 0, 0);
|
|
|
|
result = SSL_CTX_use_certificate(context->context, x509);
|
2024-02-07 18:45:04 -05:00
|
|
|
X509_free(x509);
|
2021-10-10 17:51:38 -04:00
|
|
|
while (true)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
x509 = PEM_read_bio_X509(bio, 0, 0, 0);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (x509)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
SSL_CTX_add_extra_chain_cert(context->context, x509);
|
2024-02-07 18:45:04 -05:00
|
|
|
/* Docs say don't x509_free: https://www.openssl.org/docs/man3.2/man3/SSL_CTX_add_extra_chain_cert.html. */
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIO_free(bio);
|
|
|
|
return result == 1;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
bool tf_tls_context_set_private_key(tf_tls_context_t* context, const char* private_key)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = 0;
|
|
|
|
BIO* bio = BIO_new(BIO_s_mem());
|
|
|
|
BIO_puts(bio, private_key);
|
|
|
|
EVP_PKEY* key = PEM_read_bio_PrivateKey(bio, 0, 0, 0);
|
|
|
|
result = SSL_CTX_use_PrivateKey(context->context, key);
|
2024-02-06 12:42:17 -05:00
|
|
|
EVP_PKEY_free(key);
|
2021-01-02 13:10:00 -05:00
|
|
|
BIO_free(bio);
|
|
|
|
return result == 1;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
bool tf_tls_context_add_trusted_certificate(tf_tls_context_t* context, const char* certificate)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
bool result = false;
|
|
|
|
BIO* bio = BIO_new_mem_buf(certificate, -1);
|
|
|
|
X509* x509 = PEM_read_bio_X509(bio, 0, 0, 0);
|
|
|
|
BIO_free(bio);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (x509)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
X509_STORE* store = SSL_CTX_get_cert_store(context->context);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (store && X509_STORE_add_cert(store, x509) == 1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
X509_free(x509);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_tls_session_t* tf_tls_context_create_session(tf_tls_context_t* context)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_tls_session_t* session = tf_malloc(sizeof(tf_tls_session_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(session, 0, sizeof(*session));
|
|
|
|
session->context = context;
|
|
|
|
session->bio_in = BIO_new(BIO_s_mem());
|
|
|
|
session->bio_out = BIO_new(BIO_s_mem());
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_tls_context_destroy(tf_tls_context_t* context)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
SSL_CTX_free(context->context);
|
2024-02-06 12:42:17 -05:00
|
|
|
OPENSSL_cleanup();
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(context);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_tls_session_destroy(tf_tls_session_t* session)
|
|
|
|
{
|
|
|
|
if (session->ssl)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
SSL_free(session->ssl);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (session->hostname)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free((void*)session->hostname);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(session);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_tls_session_set_hostname(tf_tls_session_t* session, const char* hostname)
|
|
|
|
{
|
|
|
|
if (session->hostname)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free((void*)session->hostname);
|
2021-01-02 13:10:00 -05:00
|
|
|
session->hostname = NULL;
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (hostname)
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
session->hostname = tf_strdup(hostname);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_tls_session_start_accept(tf_tls_session_t* session)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
session->direction = k_direction_accept;
|
|
|
|
session->ssl = SSL_new(session->context->context);
|
|
|
|
SSL_set_bio(session->ssl, session->bio_in, session->bio_out);
|
|
|
|
SSL_accept(session->ssl);
|
|
|
|
tf_tls_session_handshake(session);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_tls_session_start_connect(tf_tls_session_t* session)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
session->direction = k_direction_connect;
|
|
|
|
session->ssl = SSL_new(session->context->context);
|
|
|
|
X509_VERIFY_PARAM* param = SSL_get0_param(session->ssl);
|
|
|
|
X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
|
|
|
|
X509_VERIFY_PARAM_set1_host(param, session->hostname, 0);
|
2023-07-14 21:48:36 -04:00
|
|
|
SSL_set_tlsext_host_name(session->ssl, session->hostname);
|
2021-01-02 13:10:00 -05:00
|
|
|
SSL_set_bio(session->ssl, session->bio_in, session->bio_out);
|
|
|
|
SSL_connect(session->ssl);
|
|
|
|
tf_tls_session_handshake(session);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_tls_session_shutdown(tf_tls_session_t* session)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
SSL_shutdown(session->ssl);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_tls_session_get_peer_certificate(tf_tls_session_t* session, char* buffer, size_t bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = -1;
|
2024-01-11 19:11:03 -05:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L
|
|
|
|
X509* certificate = SSL_get_peer_certificate(session->ssl);
|
|
|
|
#else
|
2024-01-06 16:33:20 -05:00
|
|
|
X509* certificate = SSL_get1_peer_certificate(session->ssl);
|
2024-01-11 19:11:03 -05:00
|
|
|
#endif
|
2021-01-02 13:10:00 -05:00
|
|
|
BIO* bio = BIO_new(BIO_s_mem());
|
|
|
|
PEM_write_bio_X509(bio, certificate);
|
2024-01-06 16:33:20 -05:00
|
|
|
X509_free(certificate);
|
2021-01-02 13:10:00 -05:00
|
|
|
BUF_MEM* mem;
|
|
|
|
BIO_get_mem_ptr(bio, &mem);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (mem->length <= bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
memcpy(buffer, mem->data, mem->length);
|
|
|
|
result = mem->length;
|
|
|
|
}
|
|
|
|
BIO_free(bio);
|
2022-05-16 18:30:14 -04:00
|
|
|
return result;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
2021-10-10 17:51:38 -04:00
|
|
|
bool _tls_session_wildcard_match(const char* pattern, size_t pattern_length, const char* name)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* it = pattern;
|
2021-10-10 17:51:38 -04:00
|
|
|
while (it - pattern < pattern_length && *name)
|
|
|
|
{
|
|
|
|
if (*it == '*')
|
|
|
|
{
|
|
|
|
for (const char* p = name; *p; ++p)
|
|
|
|
{
|
|
|
|
if (_tls_session_wildcard_match(it + 1, pattern_length - 1, p))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (tolower(*it) == tolower(*name))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
++it;
|
|
|
|
++name;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return it - pattern <= pattern_length && *name == 0;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static bool _tls_session_verify_hostname(X509* certificate, const char* hostname)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
bool verified = false;
|
|
|
|
void* names = X509_get_ext_d2i(certificate, NID_subject_alt_name, 0, 0);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (names)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int count = sk_GENERAL_NAME_num(names);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const GENERAL_NAME* check = sk_GENERAL_NAME_value(names, i);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!verified)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
#if OPENSSL_VERSION_NUMBER <= 0x1000211fL
|
|
|
|
const unsigned char* name = ASN1_STRING_data(check->d.ia5);
|
|
|
|
#else
|
|
|
|
const char* name = ASN1_STRING_get0_data(check->d.ia5);
|
|
|
|
#endif
|
|
|
|
size_t length = ASN1_STRING_length(check->d.ia5);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (_tls_session_wildcard_match((const char*)name, length, hostname))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
verified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sk_GENERAL_NAMES_free(names);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!verified)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int index = X509_NAME_get_index_by_NID(X509_get_subject_name(certificate), NID_commonName, -1);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (index >= 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
X509_NAME_ENTRY* entry = X509_NAME_get_entry(X509_get_subject_name(certificate), index);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (entry)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
ASN1_STRING* asn1 = X509_NAME_ENTRY_get_data(entry);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (asn1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
#if OPENSSL_VERSION_NUMBER <= 0x1000211fL
|
|
|
|
const unsigned char* commonName = ASN1_STRING_data(asn1);
|
|
|
|
#else
|
|
|
|
const char* commonName = ASN1_STRING_get0_data(asn1);
|
|
|
|
#endif
|
2021-10-10 17:51:38 -04:00
|
|
|
if ((size_t)(ASN1_STRING_length(asn1)) == strlen((const char*)commonName))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
verified = _tls_session_wildcard_match((const char*)commonName, ASN1_STRING_length(asn1), hostname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return verified;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static bool _tls_session_verify_peer_certificate(tf_tls_session_t* session)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
bool verified = false;
|
2024-01-11 19:11:03 -05:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L
|
|
|
|
X509* certificate = SSL_get_peer_certificate(session->ssl);
|
|
|
|
#else
|
2024-01-06 16:33:20 -05:00
|
|
|
X509* certificate = SSL_get1_peer_certificate(session->ssl);
|
2024-01-11 19:11:03 -05:00
|
|
|
#endif
|
2021-10-10 17:51:38 -04:00
|
|
|
if (certificate)
|
|
|
|
{
|
|
|
|
if (SSL_get_verify_result(session->ssl) == X509_V_OK)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
2021-10-10 17:51:38 -04:00
|
|
|
if (_tls_session_verify_hostname(certificate, session->hostname))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
verified = true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
verified = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
X509_free(certificate);
|
|
|
|
}
|
|
|
|
return verified;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_tls_handshake_t tf_tls_session_handshake(tf_tls_session_t* session)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_tls_handshake_t result = k_tls_handshake_done;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!SSL_is_init_finished(session->ssl))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int value = SSL_do_handshake(session->ssl);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (value <= 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int error = SSL_get_error(session->ssl, value);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = k_tls_handshake_failed;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = k_tls_handshake_more;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result == k_tls_handshake_done && session->direction == k_direction_connect && !_tls_session_verify_peer_certificate(session))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = k_tls_handshake_failed;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_tls_session_read_plain(tf_tls_session_t* session, char* buffer, size_t bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int result = SSL_read(session->ssl, buffer, bytes);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (result <= 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int error = SSL_get_error(session->ssl, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (error == SSL_ERROR_ZERO_RETURN)
|
|
|
|
{
|
|
|
|
if ((SSL_get_shutdown(session->ssl) & SSL_RECEIVED_SHUTDOWN) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = k_tls_read_zero;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = 0;
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = k_tls_read_failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_tls_session_write_plain(tf_tls_session_t* session, const char* buffer, size_t bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return SSL_write(session->ssl, buffer, bytes);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_tls_session_read_encrypted(tf_tls_session_t* session, char* buffer, size_t bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return BIO_read(session->bio_out, buffer, bytes);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_tls_session_write_encrypted(tf_tls_session_t* session, const char* buffer, size_t bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return BIO_write(session->bio_in, buffer, bytes);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
bool tf_tls_session_get_error(tf_tls_session_t* session, char* buffer, size_t bytes)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
unsigned long error = ERR_get_error();
|
2021-10-10 17:51:38 -04:00
|
|
|
if (error != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
ERR_error_string_n(error, buffer, bytes);
|
|
|
|
}
|
|
|
|
return error != 0;
|
|
|
|
}
|