/* * ____ _________ __ _ * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____ * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/ * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__ * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/ * /____/ * * SharkSSL Embedded SSL/TLS Stack **************************************************************************** * PROGRAM MODULE * TLS over memory BIOs. * * $Id: SharkSslOsslTlsExample.c 6928 2026-11-25 23:04:29Z wini $ * * COPYRIGHT: Real Time Logic LLC, 2026 * * This software is copyrighted by and is the sole property of Real * Time Logic LLC. All rights, title, ownership, or other interests in * the software remain the property of Real Time Logic LLC. This * software may only be used in accordance with the terms and * conditions stipulated in the corresponding license agreement under * which the software has been supplied. Any unauthorized use, * duplication, transmission, distribution, or disclosure of this * software is expressly forbidden. * * This Copyright notice may not be removed or modified without prior * written consent of Real Time Logic LLC. * * Real Time Logic LLC. reserves the right to modify this software * without notice. * * http://www.realtimelogic.com * http://www.sharkssl.com **************************************************************************** * */ /* [tls-example] */ #include "SharkSslOsslExample.h" #include #include #include static int attachBuffers(SSL* ssl) { /* Separate incoming and outgoing ciphertext buffers belong to each peer. */ BIO* input = BIO_new(BIO_s_mem()); BIO* output = BIO_new(BIO_s_mem()); if (!input || !output) { BIO_free(input); BIO_free(output); return 0; } /* Each setter consumes one reference. SSL_free releases both BIOs. */ SSL_set0_rbio(ssl, input); SSL_set0_wbio(ssl, output); return 1; } static int transfer(SSL* sender, SSL* receiver) { /* Move encrypted bytes as a network would. BIO getters borrow references; this helper must not free them or treat these bytes as plaintext. */ unsigned char buffer[512]; BIO* output = SSL_get_wbio(sender); BIO* input = SSL_get_rbio(receiver); while (BIO_ctrl_pending(output)) { size_t received = 0, written = 0; if (!BIO_read_ex(output, buffer, sizeof(buffer), &received) || !received || !BIO_write_ex(input, buffer, received, &written) || written != received) { return 0; } } return 1; } static int retry(SSL* ssl, int result) { /* Call immediately after the unsuccessful operation on the same SSL. */ int error = SSL_get_error(ssl, result); return error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE; } static int handshake(SSL* client, SSL* server) { int clientDone = 0, serverDone = 0; unsigned int step; /* Alternate peers so each can consume the other's handshake output. This bounded in-memory pump is not a network timeout or event loop. */ for (step = 0; step < 1000; ++step) { int result; if (!clientDone) { result = SSL_connect(client); clientDone = result == 1; if (!clientDone && !retry(client, result)) { return 0; } } if (!transfer(client, server)) { return 0; } if (!serverDone) { result = SSL_accept(server); serverDone = result == 1; if (!serverDone && !retry(server, result)) { return 0; } } if (!transfer(server, client)) { return 0; } if (clientDone && serverDone) { return 1; } } return 0; } static int exchange(SSL* sender, SSL* receiver, const char* message) { unsigned char buffer[64]; size_t size = strlen(message), received = 0; int sent = 0; unsigned int step; if (size > sizeof(buffer)) { return 0; } for (step = 0; step < 1000; ++step) { size_t count = 0; int result; if (!sent) { /* Keep the same pointer and length until the write succeeds. */ result = SSL_write_ex(sender, message, size, &count); if (result) { if (count != size) { return 0; } sent = 1; } else if (!retry(sender, result)) { return 0; } } if (!transfer(sender, receiver)) { return 0; } result = SSL_read_ex(receiver, buffer + received, size - received, &count); if (result) { /* A read may return only part of the expected application message. */ received += count; } else if (!retry(receiver, result)) { return 0; } if (!transfer(receiver, sender)) { return 0; } if (sent && received == size) { return memcmp(buffer, message, size) == 0; } } return 0; } static int shutdownPair(SSL* client, SSL* server) { /* Zero from SSL_shutdown means only our close alert is complete. Exchange the peer's alert too; only negative results need SSL_get_error. */ int clientDone = 0, serverDone = 0; unsigned int step; for (step = 0; step < 1000; ++step) { int result; if (!clientDone) { result = SSL_shutdown(client); if (result < 0 && !retry(client, result)) { return 0; } clientDone = result == 1; } if (!transfer(client, server)) { return 0; } if (!serverDone) { result = SSL_shutdown(server); if (result < 0 && !retry(server, result)) { return 0; } serverDone = result == 1; } if (!transfer(server, client)) { return 0; } if (clientDone && serverDone) { return 1; } } return 0; } int main(int argc, char** argv) { SSL_CTX* clientCtx = NULL; SSL_CTX* serverCtx = NULL; SSL* client = NULL; SSL* server = NULL; int result = 1, version = 0; const char* operation = "TLS setup"; if (argc != 5 && argc != 6) { fprintf(stderr, "Usage: %s server.pem key.pem ca.pem hostname [1.2|1.3]\n", argv[0]); return 2; } if (argc == 6) { version = strcmp(argv[5], "1.2") == 0 ? TLS1_2_VERSION : strcmp(argv[5], "1.3") == 0 ? TLS1_3_VERSION : 0; if (!version) { fputs("Select TLS 1.2 or 1.3\n", stderr); return 2; } } /* The application must provide real entropy and initialized UTC time. Creating an SSL context does not initialize either service. */ if (!SharkSslOsslExample_startup()) { return SharkSslOsslExample_error("Application startup"); } /* Finish identity and trust setup before creating any SSL objects. */ clientCtx = SSL_CTX_new(TLS_client_method()); serverCtx = SSL_CTX_new(TLS_server_method()); if (!clientCtx || !serverCtx || !SSL_CTX_use_certificate_chain_file(serverCtx, argv[1]) || !SSL_CTX_use_PrivateKey_file(serverCtx, argv[2], SSL_FILETYPE_PEM) || !SSL_CTX_check_private_key(serverCtx) || !SSL_CTX_load_verify_file(clientCtx, argv[3])) { goto done; } /* Enable CA/date checks. The expected hostname is set separately below. */ SSL_CTX_set_verify(clientCtx, SSL_VERIFY_PEER, NULL); if (version && (!SSL_CTX_set_min_proto_version(clientCtx, version) || !SSL_CTX_set_max_proto_version(clientCtx, version) || !SSL_CTX_set_min_proto_version(serverCtx, version) || !SSL_CTX_set_max_proto_version(serverCtx, version))) { goto done; } /* SNI selects the server identity; SSL_set1_host verifies that identity. */ client = SSL_new(clientCtx); server = SSL_new(serverCtx); if (!client || !server || !SSL_set1_host(client, argv[4]) || !SSL_set_tlsext_host_name(client, argv[4]) || !attachBuffers(client) || !attachBuffers(server)) { goto done; } operation = "TLS handshake"; if (!handshake(client, server)) { goto done; } operation = "TLS data exchange"; if (!exchange(client, server, "Hello from the client") || !exchange(server, client, "Hello from the server")) { goto done; } printf("%s %s: exchanged both messages\n", SSL_get_version(client), SSL_get_cipher_name(client)); operation = "TLS shutdown"; if (!shutdownPair(client, server)) { goto done; } puts("TLS example completed"); result = 0; done: if (result) { SharkSslOsslExample_error(operation); } /* SSL owns the attached BIO references and retains its context. Release connections first, then the application's context references. */ SSL_free(server); SSL_free(client); SSL_CTX_free(serverCtx); SSL_CTX_free(clientCtx); return result; } /* [tls-example] */