SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
SharkSSL API

Detailed Description

The SharkSSL API.

Use the groups below to find configuration, connection processing, session management, and certificate conversion APIs.

Modules

 SharkSsl Configuration (macros)
 Add/remove SharkSSL features and/or optimize for speed versus size.
 
 Core API
 The transport agnostic SharkSSL API.
 
 SSL Session Management
 Reduce the work needed to reconnect to an authenticated peer.
 
 Certificate Management API
 Optional high level certificate management API.
 
 SharkSsl info and status
 SharkSsl information classes and return values.
 
 Post-quantum cryptography
 Hybrid ML-KEM TLS key agreement and ML-DSA-65 signature verification.
 

Data Structures

struct  SharkSslCertVerifyHook
 Application-owned policy for a presented peer chain, called synchronously after native chain checks and before the handshake advances. More...
 

Macros

#define SharkSsl_setIntf(o, sharkSslIntf)   (o)->intf=sharkSslIntf
 
#define SharkSsl_getIntf(o)   (o)->intf
 
#define SharkSslCon_terminate(o)   SharkSsl_terminateCon(0, o)
 The SharkSslCon object must be reclaimed when you close the network connection. More...
 
#define SharkSsl_setCAList(o, caList)   SharkSsl_setCAListEx((o), (caList), 0)
 Install an application-trusted CA list without validating its allocation extent. More...
 
#define SHARKSSL_SET_FAVOR_RSA   1
 Enable RSA preference in SharkSslCon_favorRSA. More...
 
#define SHARKSSL_CLEAR_FAVOR_RSA   0
 Disable RSA preference in SharkSslCon_favorRSA. More...
 
#define SHARKSSL_PQC_ALLOW_X25519   1
 Permit compiled PQC groups and classical X25519 when available. More...
 
#define SHARKSSL_PQC_REQUIRE_HYBRID   2
 Permit only compiled hybrid classical/ML-KEM groups. More...
 
#define SHARKSSL_PQC_REQUIRE_PQC   3
 Permit compiled hybrid and standalone ML-KEM groups, with no classical-only exchange. More...
 
#define SharkSslCon_requestClientCert(o, caList)   SharkSslCon_requestClientCertEx((o), (caList), 0)
 Request client authentication with an application-trusted CA list. More...
 
#define SharkSslCon_setCertificateAuthorities(o, caList)   SharkSslCon_setCertificateAuthoritiesEx((o), (caList), 0)
 Configure the extension with an application-trusted CA list. More...
 
#define SHARKSSL_ALPN_SELECT   1
 Select the returned offered protocol. More...
 
#define SHARKSSL_ALPN_NOACK   0
 Continue without an ALPN selection. More...
 
#define SHARKSSL_ALPN_FATAL   (-1)
 Reject with no_application_protocol. More...
 

Typedefs

typedef struct SharkSslCertVerifyHook SharkSslCertVerifyHook
 Application-owned policy for a presented peer chain, called synchronously after native chain checks and before the handshake advances. More...
 
typedef int(* ALPNFunction) (SharkSslCon *, const char *, void *)
 
typedef int(* ALPNFunctionEx) (SharkSslCon *o, const U8 **selected, U8 *selectedLen, const U8 *offered, U16 offeredLen, void *pvoid)
 Select a protocol from a validated complete binary ALPN list. More...
 

Functions

SHARKSSL_API void SharkSsl_constructor (SharkSsl *o, SharkSsl_Role role, U16 cacheSize, U16 inBufStartSize, U16 outBufSize)
 A SharkSsl object is the coordinator for managing SharkSslCon objects. More...
 
SHARKSSL_API void SharkSsl_destructor (SharkSsl *o)
 Close the SharkSsl object. More...
 
SharkSslCon * SharkSsl_createCon (SharkSsl *o)
 Create a SharkSslCon object. More...
 
void SharkSsl_terminateCon (const SharkSsl *o, SharkSslCon *con)
 Terminate a SharkSslCon object created by function SharkSsl_createCon. More...
 
SHARKSSL_API U16 SharkSsl_getCacheSize (SharkSsl *o)
 Returns the SharkSsl session cache size. More...
 
SHARKSSL_API U16 SharkSslCon_getCiphersuite (SharkSslCon *o)
 Returns the active session's chiper suite. More...
 
SHARKSSL_API U8 SharkSslCon_getProtocol (SharkSslCon *o)
 Returns the active session's protocol version. More...
 
SHARKSSL_API U8 SharkSslCon_setSNI (SharkSslCon *o, const char *name, U16 length)
 set Server Name Indication for TLS client connections More...
 
U8 SharkSslCon_certificateRequested (SharkSslCon *o)
 Returns TRUE if the server requested a certificate from the client to verify that the client's identity (authentication) More...
 
SHARKSSL_API U8 SharkSslCon_setCertVerifyHook (SharkSslCon *o, const SharkSslCertVerifyHook *hook)
 Install a certificate policy before the first I/O. More...
 
SHARKSSL_API SharkSslCertInfo * SharkSslCon_getCertInfo (SharkSslCon *o)
 Returns the peer's certificate if the handshaking has completed. More...
 
SHARKSSL_API U8 SharkSslCon_receivedEmptyCertificate (SharkSslCon *o)
 Test a completed server connection's retained empty-certificate state. More...
 
SHARKSSL_API U8 SharkSsl_addCertificate (SharkSsl *o, SharkSslCert cert)
 Add a trusted, generated certificate to the SharkSsl object. More...
 
SHARKSSL_API U8 SharkSsl_addCertificateEx (SharkSsl *o, SharkSslCert cert, U32 certLen)
 Add a certificate blob after checking its actual size, chain boundaries, key fields, and leaf DER structure. More...
 
SHARKSSL_API U8 SharkSsl_addCertificatePSA (SharkSsl *o, SharkSslCert cert, psa_key_id_t psaKey)
 Add an RSA, EC, or EdDSA certificate with a provider-owned private signing key. More...
 
SHARKSSL_API U8 SharkSsl_addCertificatePSAEx (SharkSsl *o, SharkSslCert cert, U32 certLen, psa_key_id_t psaKey)
 Length-checked counterpart of SharkSsl_addCertificatePSA. More...
 
SHARKSSL_API U8 SharkSslCon_selectCertificatePSA (SharkSslCon *o, SharkSslCert cert)
 Select one PSA certificate already registered on this connection's SharkSsl object. More...
 
SHARKSSL_API U8 SharkSsl_setCAListEx (SharkSsl *o, SharkSslCAList caList, U32 caListLen)
 Set a Certificate Authority (CA) list so the SharkSSL object can perform certificate validation on the peer's certificate. More...
 
SHARKSSL_API U8 SharkSslCon_trustedCA (SharkSslCon *o)
 Returns TRUE if the certificate is valid and is signed with a root certificate trusted by SharkSSL. More...
 
U8 SharkSslCon_isCAListEmpty (SharkSslCon *o)
 Check whether the connection owner has no CA-list pointer. More...
 
U8 SharkSslCon_favorRSA (SharkSslCon *o, U8 flag)
 Configures TLS 1.2 cipher-suite selection to favor RSA-authenticated suites when both RSA and ECDSA certificates are available. More...
 
U8 SharkSslCon_selectProtocol (SharkSslCon *o, U8 protocol)
 Select an enabled TLS protocol before negotiation or resumption. More...
 
SHARKSSL_API U8 SharkSslCon_requestClientCertEx (SharkSslCon *o, const void *caList, U32 caListLen)
 Request client-certificate authentication. More...
 
SHARKSSL_API U8 SharkSslCon_setCertificateAuthoritiesEx (SharkSslCon *o, const void *caList, U32 caListLen)
 Configure the TLS 1.3 Certificate Authorities extension specified in RFC 9846, Section 4.2.4. More...
 
SHARKSSL_API U8 SharkSslCon_setALPNProtocols (SharkSslCon *o, const char *protList)
 This function is used by client solutions to specify a list of application layer protocols according to RFC 7301 (ALPN). More...
 
SHARKSSL_API U8 SharkSslCon_setALPNProtocolsEx (SharkSslCon *o, const U8 *protList, U16 protListLen)
 Set a borrowed binary ALPN list before the client handshake starts. More...
 
SHARKSSL_API const char * SharkSslCon_getALPNProtocol (SharkSslCon *o)
 This function is used by client solutions to return the application layer protocol selected by the server among the ones specified through SharkSslCon_setALPNProtocols. More...
 
SHARKSSL_API U8 SharkSslCon_setALPNFunction (SharkSslCon *o, ALPNFunction func, void *pvoid)
 Install a server-side application-protocol selection callback. More...
 
SHARKSSL_API U8 SharkSslCon_setALPNFunctionEx (SharkSslCon *o, ALPNFunctionEx func, void *pvoid)
 Install the binary ALPN server selector, replacing the legacy selector. More...
 
SHARKSSL_API const U8 * SharkSslCon_getALPNProtocolEx (SharkSslCon *o, U8 *protocolLen)
 Inspect the selected ALPN bytes without assuming string termination. More...
 
SHARKSSL_API U8 SharkSslCon_selectCiphersuite (SharkSslCon *o, U16 cipherSuite)
 This function enables you to limit the number of ciphers at runtime. More...
 
SHARKSSL_API U8 SharkSslCon_clearCiphersuiteSelection (SharkSslCon *o)
 Clears the selection, thus enabling all ciphers. More...
 
SHARKSSL_API U8 SharkSslCon_renegotiate (SharkSslCon *o)
 This function enables you to renegotiate an already established SSL/TLS connection. More...
 
SHARKSSL_API SharkSslConTrust SharkSslCon_trusted (SharkSslCon *o, const char *name, SharkSslCertInfo **cPtr)
 Return the peer's trust status and certificate. More...
 
SHARKSSL_API void SubjectAltNameEnumerator_constructor (SubjectAltNameEnumerator *o, U8 *ptr, SharkSslSANLen len)
 Initialize a cursor over encoded subject alternative names. More...
 
SHARKSSL_API void SubjectAltNameEnumerator_getElement (SubjectAltNameEnumerator *o, SubjectAltName *s)
 Return the next SAN and advance the cursor. More...
 
SHARKSSL_API void * sharkssl_mallocObject (size_t size)
 Allocate aligned storage for SharkSSL C++ objects. More...
 
SHARKSSL_API void sharkssl_freeObject (void *object)
 Release storage obtained from sharkssl_mallocObject. More...
 
SHARKSSL_API SharkSslKey sharkssl_PEM_extractPublicKey_ext (const char *certPEM, U8 *keyType)
 Extract a SharkSSL key allocation and report its internal key type. More...
 
SHARKSSL_API U32 sharkssl_B64Decode (U8 *outStr, U32 outStrSize, const char *b64beg, const char *b64end)
 Decode Base64 alphabet characters from a bounded text range. More...
 

Macro Definition Documentation

◆ SHARKSSL_ALPN_FATAL

#define SHARKSSL_ALPN_FATAL   (-1)

Reject with no_application_protocol.

◆ SHARKSSL_ALPN_NOACK

#define SHARKSSL_ALPN_NOACK   0

Continue without an ALPN selection.

◆ SHARKSSL_ALPN_SELECT

#define SHARKSSL_ALPN_SELECT   1

Select the returned offered protocol.

◆ SHARKSSL_CLEAR_FAVOR_RSA

#define SHARKSSL_CLEAR_FAVOR_RSA   0

Disable RSA preference in SharkSslCon_favorRSA.

◆ SharkSsl_getIntf

#define SharkSsl_getIntf (   o)    (o)->intf

◆ SHARKSSL_PQC_ALLOW_X25519

#define SHARKSSL_PQC_ALLOW_X25519   1

Permit compiled PQC groups and classical X25519 when available.

◆ SHARKSSL_PQC_REQUIRE_HYBRID

#define SHARKSSL_PQC_REQUIRE_HYBRID   2

Permit only compiled hybrid classical/ML-KEM groups.

◆ SHARKSSL_PQC_REQUIRE_PQC

#define SHARKSSL_PQC_REQUIRE_PQC   3

Permit compiled hybrid and standalone ML-KEM groups, with no classical-only exchange.

◆ SHARKSSL_SET_FAVOR_RSA

#define SHARKSSL_SET_FAVOR_RSA   1

Enable RSA preference in SharkSslCon_favorRSA.

◆ SharkSsl_setCAList

#define SharkSsl_setCAList (   o,
  caList 
)    SharkSsl_setCAListEx((o), (caList), 0)

Install an application-trusted CA list without validating its allocation extent.

◆ SharkSsl_setIntf

#define SharkSsl_setIntf (   o,
  sharkSslIntf 
)    (o)->intf=sharkSslIntf

◆ SharkSslCon_requestClientCert

#define SharkSslCon_requestClientCert (   o,
  caList 
)    SharkSslCon_requestClientCertEx((o), (caList), 0)

Request client authentication with an application-trusted CA list.

◆ SharkSslCon_setCertificateAuthorities

#define SharkSslCon_setCertificateAuthorities (   o,
  caList 
)    SharkSslCon_setCertificateAuthoritiesEx((o), (caList), 0)

Configure the extension with an application-trusted CA list.

◆ SharkSslCon_terminate

#define SharkSslCon_terminate (   o)    SharkSsl_terminateCon(0, o)

The SharkSslCon object must be reclaimed when you close the network connection.

See also
SharkSsl_createCon
SharkSsl_terminateCon

Typedef Documentation

◆ ALPNFunction

typedef int(* ALPNFunction) (SharkSslCon *, const char *, void *)

◆ ALPNFunctionEx

typedef int(* ALPNFunctionEx) (SharkSslCon *o, const U8 **selected, U8 *selectedLen, const U8 *offered, U16 offeredLen, void *pvoid)

Select a protocol from a validated complete binary ALPN list.

Called synchronously from decrypt only when the peer offers ALPN. Input is temporary and must not be retained. Set selected/selectedLen to a nonempty offered protocol; it may be in the input or other storage valid through callback return. The library checks membership and copies it. Return SHARKSSL_ALPN_SELECT, SHARKSSL_ALPN_NOACK, or SHARKSSL_ALPN_FATAL. Do not recursively process I/O or change ALPN settings from this callback.

Parameters
oSharkSslCon*; connection being processed.
selectedconst U8**; required output pointer to the chosen bytes.
selectedLenU8*; required output length, 1..255 on selection.
offeredconst U8*; borrowed validated length-prefixed list.
offeredLenU16; number of bytes in offered, excluding a list header.
pvoidvoid*; borrowed argument from the setter.
Returns
int; one of the three SHARKSSL_ALPN result constants.

◆ SharkSslCertVerifyHook

Application-owned policy for a presented peer chain, called synchronously after native chain checks and before the handshake advances.

A missing trust anchor is reported in trustedCA. Returning zero sends a fatal bad_certificate alert. Returning nonzero continues the handshake with the original CA trust result. Signature and subsequent TLS checks still apply. An empty Certificate message or a resumed handshake without a Certificate message does not invoke this hook.

Function Documentation

◆ SharkSsl_addCertificate()

U8 SharkSsl_addCertificate ( SharkSsl *  o,
SharkSslCert  cert 
)

Add a trusted, generated certificate to the SharkSsl object.

A SharkSsl object in server mode is required to have at least one certificate.

This pointer-only API cannot determine the size of a malformed or truncated blob. Use SharkSsl_addCertificateEx for data loaded from a file or another potentially untrusted source.

Note: You must install the certificate(s) before using the SharkSsl object – i.e. before calling SharkSsl_createCon.

Returns
TRUE on success or FALSE on error. The function fails if you have existing connections in the SharkSsl object.
Parameters
oSharkSsl *; required initialized object owned by the application.
certSharkSslCert; required certificate/identity blob in SharkSSL encoding. Borrowed for the object and connection lifetimes; install before creating connections.

◆ SharkSsl_addCertificateEx()

SHARKSSL_API U8 SharkSsl_addCertificateEx ( SharkSsl *  o,
SharkSslCert  cert,
U32  certLen 
)

Add a certificate blob after checking its actual size, chain boundaries, key fields, and leaf DER structure.

This does not verify certificate signatures or chain trust. The caller retains ownership of cert and must keep it alive for the lifetime of the SharkSsl object.

Parameters
oSharkSsl *; required initialized object with no live connections.
certSharkSslCert; required SharkSSL certificate/identity blob, borrowed for the object and connection lifetimes.
certLenU32; actual number of bytes available at cert, greater than 0.
Returns
U8: TRUE on success; FALSE for zero length, malformed or unsupported data, existing connections, or allocation/registration failure.

◆ SharkSsl_addCertificatePSA()

U8 SharkSsl_addCertificatePSA ( SharkSsl *  o,
SharkSslCert  cert,
psa_key_id_t  psaKey 
)

Add an RSA, EC, or EdDSA certificate with a provider-owned private signing key.

This pointer-only API requires a trusted, generated certificate blob. Use SharkSsl_addCertificatePSAEx when the blob may be malformed or truncated. The caller retains ownership of psaKey and must keep it valid until the SharkSsl object and all its connections are destroyed. The key must match the certificate and permit signing with the negotiated algorithm. With SHARKSSL_PSA_CONSUMER_STRICT_IDENTITY=1, every local certificate must be added through this function with a public-only certificate blob.

Parameters
oSharkSsl *; required initialized object owned by the application.
certSharkSslCert; required borrowed certificate matching psaKey. The strict PSA identity profile requires a public-only blob.
psaKeypsa_key_id_t; borrowed non-null private signing handle, kept valid until the SharkSsl object and all its connections are destroyed.
Returns
U8: 1 on successful registration; 0 for invalid/incompatible certificate or key, existing connections, or allocation/registration failure.

◆ SharkSsl_addCertificatePSAEx()

SHARKSSL_API U8 SharkSsl_addCertificatePSAEx ( SharkSsl *  o,
SharkSslCert  cert,
U32  certLen,
psa_key_id_t  psaKey 
)

Length-checked counterpart of SharkSsl_addCertificatePSA.

Applies the blob checks described by SharkSsl_addCertificateEx.

Parameters
oSharkSsl *; required initialized object with no live connections.
certSharkSslCert; required SharkSSL certificate blob matching psaKey, borrowed for the object and connection lifetimes. The strict PSA identity profile requires a public-only blob.
certLenU32; actual number of bytes available at cert, greater than 0.
psaKeypsa_key_id_t; required provider-owned private signing key handle. The caller keeps it valid until the SharkSsl object and all its connections are destroyed. It must permit the negotiated signing algorithm.
Returns
U8: TRUE on success; FALSE for zero length, malformed or incompatible certificate/key data, existing connections, or allocation/registration failure.

◆ sharkssl_B64Decode()

U32 sharkssl_B64Decode ( U8 *  outStr,
U32  outStrSize,
const char *  b64beg,
const char *  b64end 
)

Decode Base64 alphabet characters from a bounded text range.

Non-alphabet characters, including padding and whitespace, are skipped. This is not a strict Base64 validator.

Parameters
outStrU8 *; Required writable caller-owned destination.
outStrSizeU32; Destination capacity in bytes; decoding stops writing when full.
b64begconst char *; Required start of readable input range.
b64endconst char *; Required one-past-end pointer in the same input buffer.
Returns
U32: Bytes written, at most outStrSize. There is no separate malformed-input or truncation error.

◆ SharkSsl_constructor()

SHARKSSL_API void SharkSsl_constructor ( SharkSsl *  o,
SharkSsl_Role  role,
U16  cacheSize,
U16  inBufStartSize,
U16  outBufSize 
)

A SharkSsl object is the coordinator for managing SharkSslCon objects.

You must at a minimum create one SharkSsl object. A more advanced configuration may include several SharkSsl objects. For example, a system that includes both a client and server SSL stack must create and initialize two SharkSsl objects.

Parameters
oUninitialized data of size sizeof(SharkSsl).
roleSelect client or server SSL mode. See SharkSsl_Role for details.
cacheSizeThe session resumption cache size. Setting this to zero disables the cache management code. See SSL Session Management for details.
inBufStartSizeInitial input-buffer capacity, in bytes, for each SharkSslCon created from this object. The value does not include the private record headroom allocated by SharkSSL. For a client, the buffer must hold the complete ClientHello selected by the compiled features and the connection options, such as SNI and ALPN. ClientHello construction checks the complete size before writing and does not grow this buffer. For a server, SharkSSL raises the initial allocation when necessary to accommodate the largest installed certificate and the compiled key-exchange requirements. Later received records and fragmented handshake messages may grow the input buffer when SHARKSSL_DISABLE_INBUF_EXPANSION is zero.
outBufSizeOutput-buffer capacity, in bytes, for each SharkSslCon. The buffer must hold the compiled internal handshake state as well as the handshake message, cipher overhead, and temporary data required by the selected protocol, ciphers, certificates, and connection options.

There is no configuration-independent minimum for either buffer. If an allocation fails, or a required handshake message and its scratch space do not fit, the handshake API returns SharkSslCon_AllocationError. With SHARKSSL_DISABLE_INBUF_EXPANSION enabled, input which requires a larger receive buffer follows the same controlled error path. Terminate the connection after this result.

See also
SharkSsl_addCertificate, SharkSsl_setCAList, SharkSsl_createCon, and SharkSsl_terminateCon.

◆ SharkSsl_createCon()

SharkSslCon * SharkSsl_createCon ( SharkSsl *  o)

Create a SharkSslCon object.

You must create one SharkSslCon object for each active network connection that requires encryption.

The following code snippet is from the example programs.

int rc;
if( (rc=se_connect(&sock, "realtimelogic.com", 443)) == 0) // open socket
{
if( (scon = SharkSsl_createCon(&sharkSsl)) != NULL)
{
// success
SharkSslCon * SharkSsl_createCon(SharkSsl *o)
Create a SharkSslCon object.
struct SharkSslCon SharkSslCon
SharkSslCon is an opaque handle returned by function SharkSsl_createCon.
Definition: SharkSSL.h:514
int se_connect(SOCKET *sock, const char *address, U16 port)
Initializes a SOCKET object connected to a remote host/address at a given port.
Returns
a SharkSslCon object or NULL if memory is exhausted.
See also
SharkSsl_terminateCon and SharkSslCon_terminate
Parameters
oSharkSsl *; required initialized object owned by the application.

◆ SharkSsl_destructor()

void SharkSsl_destructor ( SharkSsl *  o)

Close the SharkSsl object.

Terminate every connection and release application-held session references first.

Closing a SharkSsl object is not common in embedded devices. You would normally keep the SharkSsl object for the lifetime of the application. You must make sure all SharkSslCon objects are terminated by calling SharkSslCon_terminate for each active connection prior to calling the SharkSsl_destructor.

This releases internal resources, not the caller-owned SharkSsl storage or borrowed certificate/key material.

Parameters
oSharkSsl *; required initialized object owned by the application.

◆ sharkssl_freeObject()

void sharkssl_freeObject ( void *  object)

Release storage obtained from sharkssl_mallocObject.

Run any required object destructor before freeing the storage.

Parameters
objectvoid *; Owned allocation to release, or NULL for no action. Do not pass other allocations.

◆ SharkSsl_getCacheSize()

U16 SharkSsl_getCacheSize ( SharkSsl *  o)

Returns the SharkSsl session cache size.

See also
SharkSsl_constructor (parameter cacheSize) and SHARKSSL_ENABLE_SESSION_CACHE
Parameters
oSharkSsl *; required initialized object owned by the application.
Returns
U16; configured number of session-cache entries, not the number currently in use.

◆ sharkssl_mallocObject()

void * sharkssl_mallocObject ( size_t  size)

Allocate aligned storage for SharkSSL C++ objects.

The caller owns the allocation and must release it with sharkssl_freeObject, not baFree. This helper does not construct an object.

Parameters
sizesize_t; Requested object-storage size in bytes.
Returns
void *: Allocated storage or NULL on allocation/size failure.

◆ sharkssl_PEM_extractPublicKey_ext()

SharkSslKey sharkssl_PEM_extractPublicKey_ext ( const char *  certPEM,
U8 *  keyType 
)

Extract a SharkSSL key allocation and report its internal key type.

Requires SHARKSSL_ENABLE_PEM_API. Certificate parsing does not establish trust. Key input is accepted by the PEM key parser and can preserve private key material.

Parameters
certPEMconst char *; Required NUL-terminated PEM certificate or supported key text, borrowed for the call.
keyTypeU8 *; Required output for the internal SharkSSL key-type discriminator; meaningful only on success.
Returns
SharkSslKey: Caller-owned SharkSSL key allocation, or NULL on parsing/allocation failure. Free a successful result with SharkSslKey_free.

◆ SharkSsl_setCAListEx()

SHARKSSL_API U8 SharkSsl_setCAListEx ( SharkSsl *  o,
SharkSslCAList  caList,
U32  caListLen 
)

Set a Certificate Authority (CA) list so the SharkSSL object can perform certificate validation on the peer's certificate.

Parameters
othe SharkSsl object.
caListlist created by calling SharkSslCertStore_assemble or by using the command line tool SharkSSLParseCAList.
caListLenallocation length when caList is a serialized index list created by SharkSSLParseCAList. A nonzero length validates the complete list before installing it. Lists created by SharkSslCertStore_assemble contain pointers and must use zero. Zero preserves the trusted-data behavior and requires the application to guarantee the list's lifetime and extent.

The CA list must include all root certificates required for your system. It must remain valid and unchanged while the SharkSsl object uses it. Replacing it requires that all connections have been terminated and that the caller serialize the call with connection creation and session use. A successful call invalidates cached sessions established with the previous list; externally held session handles remain releasable but cannot resume.

The example program certcheck.c includes code that shows how to perform complete certificate validation of the connected servers.

Returns
TRUE if the CA list was successfully installed. Returns FALSE if connections already exist or a length-aware validation fails.
See also
SharkSslCAList and SharkSslCon_trusted.

◆ SharkSsl_terminateCon()

void SharkSsl_terminateCon ( const SharkSsl *  o,
SharkSslCon *  con 
)

Terminate a SharkSslCon object created by function SharkSsl_createCon.

When called from an ALPN callback during SharkSslCon_decrypt, the connection is reclaimed when the outer decrypt call returns. The callback must stop using the connection after requesting termination.

Parameters
othe SharkSsl object. Setting this parameter to NULL is the same as calling macro SharkSslCon_terminate.
conthe SharkSslCon object to reclaim.

Code snippet from the example code: sessioncache.c

/* Release resources used by scon */
SharkSsl_terminateCon(&sharkSsl, scon);
}
se_close(&sock);
void SharkSsl_terminateCon(const SharkSsl *o, SharkSslCon *con)
Terminate a SharkSslCon object created by function SharkSsl_createCon.
void se_close(SOCKET *sock)
Close a connected socket connection.

◆ SharkSslCon_certificateRequested()

U8 SharkSslCon_certificateRequested ( SharkSslCon *  o)

Returns TRUE if the server requested a certificate from the client to verify that the client's identity (authentication)

Parameters
oSharkSslCon *; required live connection created by SharkSsl_createCon. The caller serializes access.
Returns
U8; nonzero if the server requested a client certificate, zero otherwise. This reports the request, not whether a certificate was accepted.

◆ SharkSslCon_clearCiphersuiteSelection()

U8 SharkSslCon_clearCiphersuiteSelection ( SharkSslCon *  o)

Clears the selection, thus enabling all ciphers.

Parameters
oSharkSslCon *; connection to reset before the initial handshake selection or during requested renegotiation.
Returns
U8: 1 when the selection list is cleared; 0 for NULL or an incompatible handshake state.

◆ SharkSslCon_favorRSA()

U8 SharkSslCon_favorRSA ( SharkSslCon *  o,
U8  flag 
)

Configures TLS 1.2 cipher-suite selection to favor RSA-authenticated suites when both RSA and ECDSA certificates are available.

This setting does not affect TLS 1.3 certificate selection.

Parameters
oconnection to configure before the initial or renegotiation handshake starts.
flagSHARKSSL_SET_FAVOR_RSA or SHARKSSL_CLEAR_FAVOR_RSA.
Returns
1 if the preference was updated; otherwise 0.

◆ SharkSslCon_getALPNProtocol()

SHARKSSL_API const char * SharkSslCon_getALPNProtocol ( SharkSslCon *  o)

This function is used by client solutions to return the application layer protocol selected by the server among the ones specified through SharkSslCon_setALPNProtocols.

This function has to be called after the handshake has been initiated.

Parameters
othe SharkSslCon object returned by function SharkSsl_createCon.
Returns
NULL if none of the application layer protocols specified by the client have been selected by the server, or a pointer to the protocol name selected by the server - this pointer is to the string passed via the function SharkSslCon_setALPNProtocols; please be aware that the protocol name can be terminated by a NULL or a comma, according to the passed string. Returns NULL when the binary list API is used; use SharkSslCon_getALPNProtocolEx for binary selections.

◆ SharkSslCon_getALPNProtocolEx()

SHARKSSL_API const U8 * SharkSslCon_getALPNProtocolEx ( SharkSslCon *  o,
U8 *  protocolLen 
)

Inspect the selected ALPN bytes without assuming string termination.

For clients this supports both list setters and borrows the configured list. For servers it supports SharkSslCon_setALPNFunctionEx only, using library storage valid until the next handshake, setting replacement or destruction. A selection during handshake processing does not establish peer identity or handshake completion. The legacy server selector has no retained result.

Parameters
oSharkSslCon*; connection, or NULL for no selection.
protocolLenU8*; required output, set to zero when none is selected.
Returns
const U8*; borrowed protocol bytes, or NULL; never free the result.

◆ SharkSslCon_getCertInfo()

SharkSslCertInfo * SharkSslCon_getCertInfo ( SharkSslCon *  o)

Returns the peer's certificate if the handshaking has completed.

The certificate is available at any time.

Parameters
oSharkSslCon *; connection to inspect, or NULL.
Returns
SharkSslCertInfo *; borrowed peer certificate information, or NULL when unavailable. Do not modify or free it; its lifetime follows the owning connection/session and certificate updates.

◆ SharkSslCon_getCiphersuite()

U16 SharkSslCon_getCiphersuite ( SharkSslCon *  o)

Returns the active session's chiper suite.

Parameters
oSharkSslCon *; required live connection created by SharkSsl_createCon. The caller serializes access.
Returns
U16; negotiated cipher-suite identifier after handshake completion, or zero if unavailable.

◆ SharkSslCon_getProtocol()

U8 SharkSslCon_getProtocol ( SharkSslCon *  o)

Returns the active session's protocol version.

Parameters
oSharkSslCon *; required live connection created by SharkSsl_createCon. The caller serializes access.
Returns
U8; SHARKSSL_PROTOCOL_TLS_1_2 or SHARKSSL_PROTOCOL_TLS_1_3, or SHARKSSL_PROTOCOL_UNKNOWN before a valid version is available. A single-protocol build uses a macro returning that configured version.

◆ SharkSslCon_isCAListEmpty()

U8 SharkSslCon_isCAListEmpty ( SharkSslCon *  o)

Check whether the connection owner has no CA-list pointer.

Parameters
oSharkSslCon *; Required connection with a valid owning SharkSsl object.
Returns
U8: 1 if the owner has no CA list; 0 otherwise. This does not verify peer trust.

◆ SharkSslCon_receivedEmptyCertificate()

SHARKSSL_API U8 SharkSslCon_receivedEmptyCertificate ( SharkSslCon *  o)

Test a completed server connection's retained empty-certificate state.

Use this when optional client authentication must distinguish an absent certificate from unavailable retained certificate information. A NULL result from SharkSslCon_getCertInfo alone does not distinguish those conditions. This read-only query does not establish certificate trust or authorization. It reports retained native state, not notification of a new exchange.

Parameters
oThe connection, or NULL.
Returns
TRUE only when the connection is a server, its handshake is complete, and its native empty-certificate flag is set. Returns FALSE otherwise, including NULL, client connections, or disabled client authentication.

◆ SharkSslCon_renegotiate()

SHARKSSL_API U8 SharkSslCon_renegotiate ( SharkSslCon *  o)

This function enables you to renegotiate an already established SSL/TLS connection.

Renegotiation is also known as rehandshake. The function informs the SharkSSL state machine for the active connection that you wish to renegotiate the connection. The actual renegotiation is initiated and performed the next time you call SharkSslCon_decrypt.

The function is typically used in combination with SharkSslCon_requestClientCert, when requesting client SSL certificate authentication on an existing connection. The function can also be used in combination with SharkSslCon_selectCiphersuite, to force a change in the cipher being used.

Parameters
othe SharkSslCon object returned by function SharkSsl_createCon.
Returns
TRUE (1) if the request is accepted. Returns FALSE (0) if the SharkSSL connection is currently in the handshaking phase.

◆ SharkSslCon_requestClientCertEx()

SHARKSSL_API U8 SharkSslCon_requestClientCertEx ( SharkSslCon *  o,
const void *  caList,
U32  caListLen 
)

Request client-certificate authentication.

The CA list specifies the authorities accepted by the server when validating the client certificate.

The function must be called before the initial handshake starts or just after calling SharkSslCon_renegotiate. Calling it after renegotiation lets an existing TLS 1.2 session request client authentication. It also initiates TLS 1.3 post-handshake authentication when that feature is enabled. A connection-specific CA list causes session resumption to fall back to a full handshake so the client certificate is checked against that list.

Parameters
othe SharkSslCon object returned by SharkSsl_createCon.
caLista CA list created by SharkSslCertStore_assemble or the SharkSSLParseCAList command-line tool. The list may be NULL when no CA list is supplied.
caListLenallocation length when caList is a serialized index list created by SharkSSLParseCAList. A nonzero length validates the complete list before accepting the request. Lists created by SharkSslCertStore_assemble contain pointers and must use zero. Zero requires application-trusted data.
Returns
TRUE if the request is accepted. Returns FALSE if the connection is in an incompatible handshake state or a length-aware validation fails.

◆ SharkSslCon_selectCertificatePSA()

U8 SharkSslCon_selectCertificatePSA ( SharkSslCon *  o,
SharkSslCert  cert 
)

Select one PSA certificate already registered on this connection's SharkSsl object.

Call this before handshake processing. The certificate and its PSA signing key remain owned by the caller for the existing SharkSsl lifetime. Passing NULL restores automatic certificate selection. Returns TRUE on success and FALSE if the certificate is not registered, has no PSA signing key, or the connection has started processing records.

Parameters
oSharkSslCon *; required live connection created by SharkSsl_createCon. The caller serializes access.
certSharkSslCert; one PSA identity already registered on the parent SharkSsl, or NULL to restore automatic selection.
Returns
U8: 1 if selection succeeds before record processing; 0 for an unregistered/non-PSA identity or an incompatible connection state.

◆ SharkSslCon_selectCiphersuite()

SHARKSSL_API U8 SharkSslCon_selectCiphersuite ( SharkSslCon *  o,
U16  cipherSuite 
)

This function enables you to limit the number of ciphers at runtime.

Cipher suites can be enabled/disabled at compile time, but your application may require that you also allow only specific ciphers at runtime. By default, all ciphers enabled at compile time can be used by SharkSSL. Calling this function disables the use of all ciphers except the selected cipher. You can call this function up to N times and re-enable N ciphers. N is by default set to 8 at compile time. You can change the N value by setting macro SHARKSSL_SELECT_CIPHERSUITE_LIST_DEPTH.

The function must be called before the initial handshake has completed or just after calling SharkSslCon_renegotiate. Calling SharkSslCon_selectCiphersuite and then SharkSslCon_renegotiate enables an existing session to change cipher.

Example:
SharkSslCon_selectCiphersuite(myCon, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)

Parameters
othe SharkSslCon object returned by function SharkSsl_createCon.
cipherSuiteis one of the supported ciphers enabled at compile time.
Returns
TRUE (1) if the request is accepted. Returns FALSE (0) if the SharkSSL connection is currently in the handshaking phase, if the selected cipher was not enabled at compile time, or if called more than N times.

◆ SharkSslCon_selectProtocol()

U8 SharkSslCon_selectProtocol ( SharkSslCon *  o,
U8  protocol 
)

Select an enabled TLS protocol before negotiation or resumption.

Select the protocol before configuring dependent cipher suites; incompatible selected suites are removed.

Parameters
oSharkSslCon *; Required connection; call before client ClientHello, or no later than receipt of server-side ClientHello, and before assigning a resumption session.
protocolU8; Enabled SHARKSSL_PROTOCOL_TLS_1_2 or SHARKSSL_PROTOCOL_TLS_1_3 selector.
Returns
U8: 1 when a protocol is selected; 0 when state or selection prevents it.

◆ SharkSslCon_setALPNFunction()

U8 SharkSslCon_setALPNFunction ( SharkSslCon *  o,
ALPNFunction  func,
void *  pvoid 
)

Install a server-side application-protocol selection callback.

Requires server ALPN support. The offered protocol string is temporary and must not be retained.

Parameters
oSharkSslCon *; Required server connection, no later than the ServerHello state.
funcALPNFunction; ALPNFunction callback, or NULL to remove it. Called synchronously with connection, offered NUL-terminated protocol and pvoid; return nonzero to select or 0 to continue. A NULL protocol reports no selection; return nonzero there to permit no negotiated ALPN, or 0 to reject the handshake.
pvoidvoid *; Optional caller-owned callback context, retained for negotiation.
Returns
U8: 1 when installed; 0 for invalid connection, role or state.

◆ SharkSslCon_setALPNFunctionEx()

SHARKSSL_API U8 SharkSslCon_setALPNFunctionEx ( SharkSslCon *  o,
ALPNFunctionEx  func,
void *  pvoid 
)

Install the binary ALPN server selector, replacing the legacy selector.

The existing SharkSslCon_setALPNFunction replaces this selector in turn. Only this new path allocates persistent selected-protocol storage, at most 256 bytes; the legacy callback's allocation behavior is unchanged.

Parameters
oSharkSslCon*; required server connection before handshake processing, outside decrypt callbacks.
funcALPNFunctionEx; callback, or NULL to remove the selector.
pvoidvoid*; optional borrowed callback argument, alive while used.
Returns
U8; 1 on success, 0 for invalid connection/state.

◆ SharkSslCon_setALPNProtocols()

SHARKSSL_API U8 SharkSslCon_setALPNProtocols ( SharkSslCon *  o,
const char *  protList 
)

This function is used by client solutions to specify a list of application layer protocols according to RFC 7301 (ALPN).

It must be called before the initial handshake has started.

Parameters
othe SharkSslCon object returned by function SharkSsl_createCon.
protLista comma-separated list of protocol names, or NULL to clear the currently configured list. Each protocol name must contain from 1 to 255 bytes. Leading, trailing, and consecutive commas are not permitted. The complete string must not exceed 65532 bytes. The string is not copied and must therefore remain valid and unmodified for as long as it may be used by the connection, including calls to SharkSslCon_getALPNProtocol (e.g., a constant string stored in ROM).
Returns
TRUE (1) if the protocol list is accepted or successfully cleared. Returns FALSE (0) if the connection is invalid, is not a client connection, has already started the handshake, or if protList is malformed or exceeds the permitted length.

◆ SharkSslCon_setALPNProtocolsEx()

SHARKSSL_API U8 SharkSslCon_setALPNProtocolsEx ( SharkSslCon *  o,
const U8 *  protList,
U16  protListLen 
)

Set a borrowed binary ALPN list before the client handshake starts.

Each entry is a nonzero U8 length followed by that many arbitrary bytes. This replaces the string-list setting; neither interface copies its list. Failed calls preserve the previous setting.

Parameters
oSharkSslCon*; required client connection, outside decrypt callbacks.
protListconst U8*; required for nonzero length, borrowed and unchanged until connection destruction or replacement before the handshake.
protListLenU16; byte length, 2..65533, or zero to clear the setting. Complete ClientHello size remains subject to native buffer limits.
Returns
U8; 1 on success, 0 for malformed input or invalid connection/state.

◆ SharkSslCon_setCertificateAuthoritiesEx()

SHARKSSL_API U8 SharkSslCon_setCertificateAuthoritiesEx ( SharkSslCon *  o,
const void *  caList,
U32  caListLen 
)

Configure the TLS 1.3 Certificate Authorities extension specified in RFC 9846, Section 4.2.4.

The function must be called before the initial handshake starts.

Parameters
othe SharkSslCon object returned by SharkSsl_createCon.
caLista CA list created by SharkSslCertStore_assemble or the SharkSSLParseCAList command-line tool. For this connection it overrides the list installed with SharkSsl_setCAList.
caListLenallocation length when caList is a serialized index list created by SharkSSLParseCAList. A nonzero length validates the complete list before accepting it. Lists created by SharkSslCertStore_assemble contain pointers and must use zero. Zero requires application-trusted data.
Returns
TRUE if the CA list is accepted. Returns FALSE if the handshake has already progressed too far or a length-aware validation fails.

◆ SharkSslCon_setCertVerifyHook()

SHARKSSL_API U8 SharkSslCon_setCertVerifyHook ( SharkSslCon *  o,
const SharkSslCertVerifyHook *  hook 
)

Install a certificate policy before the first I/O.

Requires SHARKSSL_ENABLE_CERT_VERIFY_HOOK.

Parameters
oConnection to configure.
hookBorrowed hook, or NULL to remove it. Keep both the hook and its argument alive until replacement or connection destruction.
Returns
One on success, zero on invalid input or after I/O has started.

◆ SharkSslCon_setSNI()

U8 SharkSslCon_setSNI ( SharkSslCon *  o,
const char *  name,
U16  length 
)

set Server Name Indication for TLS client connections

Parameters
oSharkSslCon *; required live connection created by SharkSsl_createCon. The caller serializes access.
nameconst char *; borrowed hostname bytes, required when length is nonzero. Keep them alive until ClientHello generation consumes them.
lengthU16; hostname length in bytes, at most 64; zero clears the name. No NUL terminator is counted.
Returns
U8: 1 if accepted for a client before handshake processing; 0 if the role/state/length is incompatible. This API stores the pointer rather than copying the name.

◆ SharkSslCon_trusted()

SHARKSSL_API SharkSslConTrust SharkSslCon_trusted ( SharkSslCon *  o,
const char *  name,
SharkSslCertInfo **  cPtr 
)

Return the peer's trust status and certificate.

This function extends SharkSslCon_trustedCA with reference-name and optional certificate-date checks. It accepts DNS names and canonical dotted-decimal IPv4 addresses; an IP address requires an iPAddress SAN.

Parameters
othe SharkSslCon object.
namethe NUL-terminated ASCII reference name used for the connection. Reject embedded NULs before converting length-delimited input to this C string. Supply the same canonical identity to the resolver and this function; SharkSSL does not inspect the connected socket address. IPv6 text is not supported by this comparison API. A NULL name skips identity matching and must not be used to authenticate a named endpoint.
cPtroptional output for the connection's SharkSslCertInfo object.
Returns
SharkSslConTrust.
See also
SharkSslConTrust and SharkSslCon_trustedCA.

◆ SharkSslCon_trustedCA()

U8 SharkSslCon_trustedCA ( SharkSslCon *  o)

Returns TRUE if the certificate is valid and is signed with a root certificate trusted by SharkSSL.

Root certificates can optionally be installed with parameter SharkSslCAList when calling SharkSsl_constructor.

Note, the function only validates the certificate and its chain. You must typically also validate the domain name and the certificate's date. Function SharkSslCon_trusted extends the certificate validation and also includes domain name validation and date check.

See also
SharkSslCon_trusted
Parameters
oSharkSslCon *; required live connection created by SharkSsl_createCon. The caller serializes access.
Returns
U8: 1 if the stored validation result trusts the peer chain, 0 otherwise. Apply expected-name and applicable date checks with SharkSslCon_trusted before trusting the endpoint.

◆ SubjectAltNameEnumerator_constructor()

void SubjectAltNameEnumerator_constructor ( SubjectAltNameEnumerator *  o,
U8 *  ptr,
SharkSslSANLen  len 
)

Initialize a cursor over encoded subject alternative names.

Parameters
oSubjectAltNameEnumerator *; Required caller-owned enumerator storage.
ptrU8 *; Required borrowed SAN content buffer, retained for enumeration.
lenSharkSslSANLen; SAN content length in bytes.

◆ SubjectAltNameEnumerator_getElement()

void SubjectAltNameEnumerator_getElement ( SubjectAltNameEnumerator *  o,
SubjectAltName *  s 
)

Return the next SAN and advance the cursor.

Parameters
oSubjectAltNameEnumerator *; Required initialized enumerator.
sSubjectAltName *; Required output view; ptr/len borrow the original buffer. ptr is NULL on end or parse failure; other fields are then not valid.