Build a memory BIO, hash a message, or run a verified TLS exchange. Each example lists its C sources directly in the Linux Makefile and its Windows project. Start with the memory BIO example. It needs only a C compiler.
| Example | Make target | What it demonstrates |
SharkSslOsslBioExample.c | bio | Write and read a memory BIO; no native SharkSSL required |
SharkSslOsslCryptoExample.c | crypto | SHA-256, HMAC-SHA256 and HKDF-SHA256 using fetch/context APIs |
SharkSslOsslTlsExample.c | tls | Client/server TLS through memory BIOs, PEM loading, peer checks, retry handling, messages and shutdown |
SharkSslOsslExample.c supplies diagnostic and hexadecimal-output helpers. The TLS example moves bytes between two memory BIO pairs in one process. It opens no socket and uses no dispatcher. Its bounded loops illustrate the API sequence; they are not an application's network event loop.
Build inputs
The prepared release supplies the Makefile and Visual Studio projects in compat/openssl/examples. Each target builds the adapter and its caller with one SHARKSSL_OSSL_COMPONENTS setting. BIO uses 5, crypto uses 19 and desktop TLS uses 101. The crypto and TLS examples compile the supplied native C sources with the host port. In a board application that already uses native SharkSSL, reuse the matching implementation and target configuration.
For a client with a CA certificate in flash, start with ThreadX-NetX/README.md in the release example directory. It gives the exact source list, port settings and callback sequence for a 32-bit ThreadX/NetX Duo project.
GNU Make on Linux
From the extracted package root:
cd compat/openssl/examples
make bio
./bio
make crypto
./crypto
make tls
./tls server.pem key.pem ca.pem localhost
BIO prints Hello from a memory BIO. Crypto prints SHA-256, HMAC and HKDF results. For TLS, supply the identity and trust files described below.
Visual Studio and MSBuild on Windows
Open Bio.vcxproj, Crypto.vcxproj or Tls.vcxproj and build Release for x64 with the v143 C++ tools and Windows 10 SDK. For example, from a Visual Studio developer command prompt in the release example directory:
msbuild Bio.vcxproj /p:Configuration=Release /p:Platform=x64
out\Bio\Bio.exe
The TLS project produces out/Tls/Tls.exe and accepts the same arguments as the Linux executable.
TLS startup and input files
The release supplies HostStartup.c for desktop TLS. It obtains 48 bytes of operating-system entropy through BCrypt on Windows or getrandom on Linux. Startup succeeds only when the native RNG accepts the seed. The desktop ports read UTC from the host clock. A board uses its own entropy source and initializes its clock before verifying certificates.
Pass the server certificate chain, its unencrypted private key, the CA trusted by the client, and the DNS name in the server certificate:
tls server.pem key.pem ca.pem hostname [1.2|1.3]
The optional final argument forces both peers to TLS 1.2 or TLS 1.3. Without it, the compiled version range applies. The example checks the certificate chain and expected hostname, sends one message in each direction and completes shutdown. A failed setup, handshake, exchange or shutdown returns a nonzero status with the available ERR diagnostics.
The TLS subset contract applies, including SharkSSL's accepted 24-hour certificate-start allowance, supported identity combinations and unencrypted PEM limitation. This example uses a filesystem. Embedded callers without one can use the PEM buffer extensions; the buffer APIs preserve the same trust and identity requirements. Supply your own identities and platform startup function.
Read the examples
The listings below come directly from the supplied C files. Use the download links when adding them to a project, together with SharkSslOsslExample.c and SharkSslOsslExample.h.
Memory BIO example
Download SharkSslOsslBioExample.c. The program creates a writable memory BIO, transfers message bytes, checks the returned lengths and releases the BIO. A successful run prints the message and returns zero. It uses neither sockets nor the native RNG.
#include "SharkSslOsslExample.h"
#include <openssl/bio.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
const char message[] = "Hello from a memory BIO";
char buffer[sizeof(message)];
size_t written = 0, received = 0;
BIO* bio = BIO_new(BIO_s_mem());
int result = 1;
if (!bio || !BIO_write_ex(bio, message, sizeof(message) - 1, &written) ||
written != sizeof(message) - 1 ||
!BIO_read_ex(bio, buffer, sizeof(buffer) - 1, &received) ||
received != written || memcmp(buffer, message, received) != 0)
{
SharkSslOsslExample_error("Memory BIO round trip");
}
else
{
buffer[received] = '\0';
puts(buffer);
result = 0;
}
BIO_free(bio);
return result;
}
Crypto example
Download SharkSslOsslCryptoExample.c. The program hashes a message, calculates its HMAC and derives a key. The comments explain descriptor ownership, parameter lists and cleanup. Its key and salt are demonstration data; supply your own key material in an application.
#include "SharkSslOsslExample.h"
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/core_names.h>
#include <string.h>
int main(void)
{
const unsigned char message[] = "abc";
unsigned char key[] = "example key";
unsigned char salt[] = "example salt";
unsigned char info[] = "example context";
char digestName[] = "SHA256";
unsigned char output[32];
unsigned int digestSize = 0;
size_t macSize = 0;
OSSL_PARAM params[5];
EVP_MD* digest = NULL;
EVP_MD_CTX* digestCtx = NULL;
EVP_MAC* mac = NULL;
EVP_MAC_CTX* macCtx = NULL;
EVP_KDF* kdf = NULL;
EVP_KDF_CTX* kdfCtx = NULL;
const char* operation = "SHA-256";
int result = 1;
digest = EVP_MD_fetch(NULL, digestName, NULL);
digestCtx = EVP_MD_CTX_new();
if (!digest || !digestCtx || !EVP_DigestInit_ex2(digestCtx, digest, NULL) ||
!EVP_DigestUpdate(digestCtx, message, sizeof(message) - 1) ||
!EVP_DigestFinal_ex(digestCtx, output, &digestSize) || digestSize != sizeof(output))
{
goto done;
}
SharkSslOsslExample_hex("SHA256", output, digestSize);
operation = "HMAC-SHA256";
mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
macCtx = mac ? EVP_MAC_CTX_new(mac) : NULL;
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digestName, 0);
params[1] = OSSL_PARAM_construct_end();
if (!macCtx || !EVP_MAC_init(macCtx, key, sizeof(key) - 1, params) ||
!EVP_MAC_update(macCtx, message, sizeof(message) - 1) ||
!EVP_MAC_final(macCtx, output, &macSize, sizeof(output)) || macSize != sizeof(output))
{
goto done;
}
SharkSslOsslExample_hex("HMAC", output, macSize);
operation = "HKDF-SHA256";
kdf = EVP_KDF_fetch(NULL, "HKDF", NULL);
kdfCtx = kdf ? EVP_KDF_CTX_new(kdf) : NULL;
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digestName, 0);
params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, sizeof(key) - 1);
params[2] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt) - 1);
params[3] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, info, sizeof(info) - 1);
params[4] = OSSL_PARAM_construct_end();
if (!kdfCtx || !EVP_KDF_derive(kdfCtx, output, sizeof(output), params))
{
goto done;
}
SharkSslOsslExample_hex("HKDF", output, sizeof(output));
result = 0;
done:
if (result)
{
SharkSslOsslExample_error(operation);
}
EVP_KDF_CTX_free(kdfCtx);
EVP_KDF_free(kdf);
EVP_MAC_CTX_free(macCtx);
EVP_MAC_free(mac);
EVP_MD_CTX_free(digestCtx);
EVP_MD_free(digest);
return result;
}
TLS example
Download SharkSslOsslTlsExample.c. Follow TLS startup and input files before running this example. The comments explain BIO reference transfer, retry handling, certificate and hostname checks, partial reads and two-way shutdown. Its bounded memory-BIO pump illustrates the call sequence; a socket application waits for transport readiness and supplies its own timeout policy.
#include "SharkSslOsslExample.h"
#include <openssl/ssl.h>
#include <stdio.h>
#include <string.h>
static int attachBuffers(SSL* ssl)
{
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;
}
SSL_set0_rbio(ssl, input);
SSL_set0_wbio(ssl, output);
return 1;
}
static int transfer(SSL* sender, SSL* receiver)
{
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)
{
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;
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)
{
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)
{
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)
{
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;
}
}
if (!SharkSslOsslExample_startup())
{
return SharkSslOsslExample_error("Application startup");
}
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;
}
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;
}
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_free(server);
SSL_free(client);
SSL_CTX_free(serverCtx);
SSL_CTX_free(clientCtx);
return result;
}