|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
Embedded applications can configure TLS directly from PEM bytes in flash, application storage or a provisioning response. Include SharkSslOsslPem.h and use the four SharkSSL-prefixed extensions below before creating SSL connections. The input does not need a terminating NUL byte.
These functions reuse the existing native PEM adapter in SharkSslOsslTlsCredentials.c. They are SharkSSL extensions, not standard OpenSSL entry points. They configure the TLS context directly and require no file access. For standalone key objects and signatures, use the PEM key APIs.
| Function | Operation |
|---|---|
SharkSslOssl_SSL_CTX_use_certificate_pem | Load the first certificate from the buffer |
SharkSslOssl_SSL_CTX_use_certificate_chain_pem | Validate and load the complete leaf-first certificate chain |
SharkSslOssl_SSL_CTX_use_PrivateKey_pem | Load an unencrypted private key |
SharkSslOssl_SSL_CTX_load_verify_pem | Append a CA bundle to the context's trust anchors |
All four functions have these arguments and return an int:
| Argument | Type | Contract |
|---|---|---|
ctx | SSL_CTX* | Required live context with no live SSL objects derived from it; serialize access |
pem | const void* | Required pointer to at least size readable bytes; borrowed only during the call and never modified |
size | size_t | Required byte count, 1..65,536 inclusive; exclude any terminating NUL |
Success returns 1. Failure returns 0, adds one diagnostic to the calling thread's ERR queue and preserves the previous configuration. Success preserves older diagnostics. An embedded NUL, including a NUL counted at the end, is rejected. Zero length does not clear a setting. Applications may overwrite or release their buffers immediately after the call.
Each call temporarily allocates size + 1 bytes to supply a terminated string to the existing native adapter. That temporary copy is cleared and freed on both success and failure. The context retains its own required PEM/native copies. Replacement construction also needs the existing adapter's temporary allocations; this API is not a zero-copy interface.
For a C string array, pass sizeof(array) - 1. For a binary byte array with no trailing NUL, pass sizeof(array). Each individual loader is transactional; the two-call example is not a transaction across both calls. Loading a different certificate clears an incompatible previous key, while a mismatched key load leaves the existing configuration intact. Certificates and keys can be loaded in either order. Trust-anchor loads accumulate.
The TLS PEM mapping and limits still apply: one identity slot, compiled native RSA/EC support, a 16,384-byte encoded certificate-body limit, 16 certificates per chain/CA input, and accumulated CA limits of 128 certificates and 262,144 text bytes. The first-certificate loader ignores later blocks, matching the existing file API. The chain and CA loaders reject a malformed later block as part of the complete input. The file and buffer loaders accept an unencrypted key with a text preamble. They recognize encrypted PKCS#8 labels and legacy PEM encryption headers. Encrypted keys, password callbacks and DER input remain outside this subset. RNG initialization remains application-owned.
Use the TLS source list with the portable BIO core and SharkSslOsslErr.c. The buffer functions are in SharkSslOsslTlsCredentials.c; there is no additional implementation source. Omit SharkSslOsslTlsFiles.c when file loading is not needed. The component mask hides file-loader declarations in openssl/ssl.h when pem_files is omitted; apply the same mask to sources and callers.
The TLS credential loaders map PEM certificates, keys and CA bundles to native SharkSSL functions. The wrapper performs framing, lifetime and state management; native SharkSSL performs decoding and cryptographic parsing.
| OpenSSL-style operation | Native operations |
|---|---|
| Load certificate/chain | sharkssl_PEM_extractPublicKey validates each certificate; retain PEM until its private key is available |
| Load private key | sharkssl_PEM(NULL, key, NULL, ...) parses an unencrypted key; public-component comparison checks an existing certificate |
| Install a complete identity | sharkssl_PEM(certificates, key, NULL, ...), then SharkSsl_addCertificateEx |
| Load trust anchors | SharkSslCertStore_add per validated PEM block, then SharkSslCertStore_assemble and SharkSsl_setCAListEx |
Each context has one identity slot, supporting compiled native RSA and EC types. Certificate and key may be loaded in either order. A mismatched key load fails without replacing the working identity. Loading a different certificate clears an incompatible old key. Failed parsing, allocation and native install leave the previous configuration intact. CA file loads accumulate trust anchors. One malformed block makes the whole load fail, rather than accepting a partially loaded native store. This transactional failure policy is a bounded subset, not a claim to reproduce every OpenSSL malformed-input side effect.
Configure a context before creating SSL objects. Credential/CA changes return failure while any SSL derived from the context exists, including before its handshake. OpenSSL permits a broader configuration lifecycle. The adapter constructs a complete native replacement before changing an idle context. No native list/coordinator structures are copied or modified internally.
The TLS identity loaders accept PEM with unencrypted RSA PKCS#1, EC SEC1 or PKCS#8 keys. They do not accept DER input, CA directories, X509/EVP_PKEY objects, password callbacks or encrypted private keys. Default trust requires an explicit embedded list or file configuration. No interactive password prompt occurs. The native encrypted PKCS#8 feature and AES-CBC are disabled in the current default configuration.
SharkSslOsslTlsFiles.c is an optional stdio file front end. The public PEM buffer loaders configure an application without it, using the same internal credential adapter. File loaders require seekable files of 1..65,536 bytes and reject embedded NUL bytes. Each encoded certificate body is limited to 16,384 bytes; chains and a single CA file to 16 certificates; accumulated CA text to 262,144 bytes and 128 certificates. A certificate file loader uses the first certificate; the chain loader validates all blocks. Unsupported PEM block types fail in certificate/CA chain inputs.
The context owns copies of PEM text and native identities/stores. Retained private-key text and successfully parsed native key blobs are cleared on release. Native parsing has its own temporary allocations and cleanup contract; do not assume these wrapper cleanup rules cover every native temporary.
Successful PEM loading does not guarantee that native TLS can select that identity. Native server selection supports slots for RSA-key/RSA-signature, EC-key/RSA-signature and EC-key/ECDSA-signature certificates. It has no slot for an RSA key whose certificate was signed by an ECDSA issuer. That combination loads but the native server rejects its handshake with handshake_failure. This is a native server identity-selection limit, separate from the PEM loader accepting the certificate and key.