|
SharkSSL™ Embedded SSL/TLS Stack
|
The transport agnostic SharkSSL API.
The SharkSSL API provides a unique design that enables anything from bare-metal embedded devices to high end non embedded systems to include a very fast and compact secure socket layer. The core API documentation includes information on the SharkSSL transport agnostic API.
Most of the SharkSSL's core API is used internally by the SharkSSL socket example lib (selib). It is easier to use selib, however, You must use the core functions if you are working with a nontraditional TCP/IP stack or if you provide encryption for another type of networking than TCP/IP.
Macros | |
| #define | SHARKSSL_KEY_UPDATE_NOT_REQUESTED 0 |
| Updates the local write keys without requesting a reciprocal update of the peer's write keys. More... | |
| #define | SHARKSSL_KEY_UPDATE_REQUESTED 1 |
| Updates the local write keys and requests a reciprocal update of the peer's write keys. More... | |
| #define | SharkSslCon_getAlertData(o) SharkSslCon_getEncData(o) |
| This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt. More... | |
| #define | SharkSslCon_getAlertDataLen(o) SharkSslCon_getEncDataLen(o) |
| This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt. More... | |
Enumerations | |
| enum | SharkSslCon_RetVal { SharkSslCon_Error = 1 , SharkSslCon_AllocationError , SharkSslCon_Decrypted , SharkSslCon_Handshake , SharkSslCon_NeedMoreData , SharkSslCon_AlertSend , SharkSslCon_AlertRecv , SharkSslCon_Encrypted , SharkSslCon_HandshakeNotComplete , SharkSslCon_CertificateError } |
| The SharkSslCon_decrypt and SharkSslCon_encrypt return values. More... | |
Functions | |
| SharkSslCon_RetVal | SharkSslCon_decrypt (SharkSslCon *o, U16 readLen) |
| Decrypt the received data copied into the SharkSslCon receive buffer (the buffer returned by SharkSslCon_getBuf). More... | |
| SharkSslCon_RetVal | SharkSslCon_encrypt (SharkSslCon *o, U8 *buf, U16 maxLen) |
| Encrypt the data provided by parameter 'buf' or encrypt data in the SharkSslCon send buffer. More... | |
| SHARKSSL_API U8 | SharkSslCon_keyUpdate (SharkSslCon *o, U8 requestUpdate) |
| Creates a TLS 1.3 KeyUpdate message and advances the local write keys. More... | |
| U8 | SharkSslCon_isHandshakeComplete (SharkSslCon *o) |
| Returns the following values: More... | |
| U8 | SharkSslCon_encryptMore (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCon_encrypt. More... | |
| U8 | SharkSslCon_decryptMore (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_Decrypted returned by function SharkSslCon_decrypt. More... | |
| U8 * | SharkSslCon_getBuf (SharkSslCon *o) |
| Returns a pointer to the SharkSslCon input/receive buffer. More... | |
| U16 | SharkSslCon_getBufLen (SharkSslCon *o) |
| Returns the number of bytes currently available at the pointer returned by SharkSslCon_getBuf. More... | |
| U16 | SharkSslCon_copyDecData (SharkSslCon *o, U8 *buf, U16 maxLen) |
| Copy decoded data to 'buf'. More... | |
| SHARKSSL_API U16 | SharkSslCon_getDecData (SharkSslCon *o, U8 **bufPtr) |
| Get a pointer to the decoded data. More... | |
| U8 * | SharkSslCon_getHandshakeData (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_Handshake returned by function SharkSslCon_encrypt. More... | |
| U16 | SharkSslCon_getHandshakeDataLen (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_Handshake returned by function SharkSslCon_decrypt. More... | |
| U16 | SharkSslCon_setHandshakeDataSent (SharkSslCon *o, U16 length) |
| This function is used in conjunction with SharkSslCon_getHandshakeData. More... | |
| U8 * | SharkSslCon_getEncData (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCon_encrypt. More... | |
| U16 | SharkSslCon_getEncDataLen (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCon_encrypt. More... | |
| U8 * | SharkSslCon_getEncBufPtr (SharkSslCon *o) |
| This function is used in conjunction with SharkSslCon_encrypt when using the zero copy send API. More... | |
| U16 | SharkSslCon_getEncBufSize (SharkSslCon *o) |
| Returns the length of the buffer returned by SharkSslCon_getEncBufPtr. | |
| U8 | SharkSslCon_getAlertLevel (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt or in conjunction with state SharkSslCon_AlertRecv returned by function SharkSslCon_decrypt. More... | |
| U8 | SharkSslCon_getAlertDescription (SharkSslCon *o) |
| This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt or in conjunction with state SharkSslCon_AlertRecv returned by function SharkSslCon_decrypt. More... | |
| #define SHARKSSL_KEY_UPDATE_NOT_REQUESTED 0 |
Updates the local write keys without requesting a reciprocal update of the peer's write keys.
The peer still updates its read keys when it receives the KeyUpdate. This value is also used when replying to a peer that sent SHARKSSL_KEY_UPDATE_REQUESTED, preventing an endless KeyUpdate exchange.
| #define SHARKSSL_KEY_UPDATE_REQUESTED 1 |
Updates the local write keys and requests a reciprocal update of the peer's write keys.
The peer must reply with a KeyUpdate containing SHARKSSL_KEY_UPDATE_NOT_REQUESTED before sending its next application data.
| #define SharkSslCon_getAlertData | ( | o | ) | SharkSslCon_getEncData(o) |
This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt.
The function returns a pointer to the alert data that must be sent to the peer side.
| #define SharkSslCon_getAlertDataLen | ( | o | ) | SharkSslCon_getEncDataLen(o) |
This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt.
The function returns the length of the alert data that must be sent to the peer side.
| enum SharkSslCon_RetVal |
The SharkSslCon_decrypt and SharkSslCon_encrypt return values.
| Enumerator | |
|---|---|
| SharkSslCon_Error | Indicates general errors, including configuration errors. |
| SharkSslCon_AllocationError | The memory pool is too small. |
| SharkSslCon_Decrypted | Returned when a block of received data has been successfully decrypted. selib.c code snippet: case SharkSslCon_Decrypted:
@ SharkSslCon_Decrypted Returned when a block of received data has been successfully decrypted. Definition: SharkSSL.h:640 baAssert(buf);
readLen = SharkSslCon_getDecData(s, buf);
if (SharkSslCon_decryptMore(s) && (readLen == 0))
{
break; /* decrypt next record */
}
return readLen;
U8 SharkSslCon_decryptMore(SharkSslCon *o) This function is used in conjunction with state SharkSslCon_Decrypted returned by function SharkSslCo... SHARKSSL_API U16 SharkSslCon_getDecData(SharkSslCon *o, U8 **bufPtr) Get a pointer to the decoded data.
|
| SharkSslCon_Handshake | Returned when an SSL/TLS handshake message has been received or is to be sent. During this phase, one must call SharkSslCon_getHandshakeData and SharkSslCon_getHandshakeDataLen, send the handshake data to the peer, and report the accepted length using SharkSslCon_setHandshakeDataSent. selib.c code snippet: case SharkSslCon_Handshake:
@ SharkSslCon_Handshake Returned when an SSL/TLS handshake message has been received or is to be sent. Definition: SharkSSL.h:654 |
| SharkSslCon_NeedMoreData | Returned when SharkSSL holds an incomplete SSL/TLS record or when the connection is initially established and the readLen parameter is zero. selib.c code snippet: case SharkSslCon_NeedMoreData:
@ SharkSslCon_NeedMoreData Returned when SharkSSL holds an incomplete SSL/TLS record or when the connection is initially establi... Definition: SharkSSL.h:667 SharkSslCon_getBufLen(s), timeout);
if (readLen <= 0)
return readLen; /* 0 (no data) or -1 (socket error) */
if (NULL == buf) /* seSec_handshake */
{
NTD xprintf(("received %d handshake bytes\n", readLen));
}
break; /* decrypt next record */
U16 SharkSslCon_getBufLen(SharkSslCon *o) Returns the number of bytes currently available at the pointer returned by SharkSslCon_getBuf. U8 * SharkSslCon_getBuf(SharkSslCon *o) Returns a pointer to the SharkSslCon input/receive buffer. #define xprintf(data) The macro xprintf expands to function _xprintf if the code is compiled with XPRINTF set to 1. Definition: selib.h:271
|
| SharkSslCon_AlertSend | Returned when an SSL/TLS alert message must be sent to the peer side. The error code, which must be sent to the peer, indicates that SharkSSL was unable to decode the data sent from the peer. selib.c code snippet: @ SharkSslCon_AlertSend Returned when an SSL/TLS alert message must be sent to the peer side. Definition: SharkSSL.h:681 /* One can retrieve the alert level and description through:
* SharkSslCon_getAlertLevel(s)
* SharkSslCon_getAlertDescription(s)
*/
xprintf(("SharkSSL : Sent alert message, level %d, description %d\n",
baAssert(SharkSslCon_getAlertDataLen(s));
break;
/* One can retrieve the alert level & description through:
* SharkSslCon_getAlertLevel(s)
* SharkSslCon_getAlertDescription(s)
*/
{
xprintf(("SharkSSL : Received closure alert \n"));
}
else
{
xprintf(("SharkSSL : Received alert, level %d, description %d\n",
}
break;
case SharkSslCon_Error:
xprintf(("SharkSSL : Internal failure\n"));
break;
xprintf(("SharkSSL : Certificate error\n"));
break;
xprintf(("SharkSSL : Allocation error\n"));
break;
default:
baAssert(0);
return -1;
#define SHARKSSL_ALERT_CLOSE_NOTIFY SHARKSSL_ALERT_CLOSE_NOTIFY. Definition: SharkSSL.h:131 #define SHARKSSL_ALERT_LEVEL_WARNING SSL/TLS warning message. Definition: SharkSSL.h:114 U8 SharkSslCon_getAlertLevel(SharkSslCon *o) This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCo... #define SharkSslCon_getAlertDataLen(o) This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCo... Definition: SharkSSL.h:1442 U8 SharkSslCon_getAlertDescription(SharkSslCon *o) This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCo... #define SharkSslCon_getAlertData(o) This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCo... Definition: SharkSSL.h:1430 @ SharkSslCon_AllocationError The memory pool is too small. Definition: SharkSSL.h:629 @ SharkSslCon_Error Indicates general errors, including configuration errors. Definition: SharkSSL.h:625 @ SharkSslCon_CertificateError Unrecognized format of a provided certificate. Definition: SharkSSL.h:712 @ SharkSslCon_AlertRecv SharkSSL received an SSL/TLS alert message from the peer, which means that the peer either failed to ... Definition: SharkSSL.h:690
|
| SharkSslCon_AlertRecv | SharkSSL received an SSL/TLS alert message from the peer, which means that the peer either failed to decode the message from SharkSSL or the peer entered an exception state. |
| SharkSslCon_Encrypted | SharkSSL has successfully decrypted a chunk of data. The data is available in the SharkSSL receive buffer, which is returned by function SharkSslCon_getEncData. The length is returned by function SharkSslCon_getEncDataLen. selib.c code snippet: case SharkSslCon_Encrypted:
@ SharkSslCon_Encrypted SharkSSL has successfully decrypted a chunk of data. Definition: SharkSSL.h:702 nb = SharkSslCon_getEncDataLen(s);
{
return -1;
}
/*
* Returns true if the unencrypted buffer is larger than
* what fits into the SharkSSL output buffer. SharkSSL
* solves this by breaking the buffer into multiple chunks.
*/
if (SharkSslCon_encryptMore(s))
{
break;
}
return maxLen; /* All data encrypted and sent. */
U8 * SharkSslCon_getEncData(SharkSslCon *o) This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCo... U16 SharkSslCon_getEncDataLen(SharkSslCon *o) This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCo... U8 SharkSslCon_encryptMore(SharkSslCon *o) This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCo... |
| SharkSslCon_HandshakeNotComplete | An error condition returned by function SharkSslCon_encrypt if the SSL handshake is not completed. You cannot send encrypted data before completing the handshake phase. |
| SharkSslCon_CertificateError | Unrecognized format of a provided certificate. |
| U16 SharkSslCon_copyDecData | ( | SharkSslCon * | o, |
| U8 * | buf, | ||
| U16 | maxLen | ||
| ) |
Copy decoded data to 'buf'.
Provides an alternative to using the zero copy API and the buffer returned by SharkSslCon_getDecData.
| o | the SharkSslCon object returned by function SharkSsl_createCon. |
| buf | a buffer provided by the caller |
| maxLen | the size of parameter 'buf'. SharkSSL will copy at most maxLen bytes to 'buf'. |
Example:
| SharkSslCon_RetVal SharkSslCon_decrypt | ( | SharkSslCon * | o, |
| U16 | readLen | ||
| ) |
Decrypt the received data copied into the SharkSslCon receive buffer (the buffer returned by SharkSslCon_getBuf).
See SharkSslCon_RetVal for example code.
The SharkSslCon_decrypt function keeps decrypt state information in the SharkSslCon object and the function can therefore be used in event driven systems.
| o | the SharkSslCon object returned by function SharkSsl_createCon. |
| readLen | the length of the received network data copied into the buffer returned by SharkSslCon_getBuf. |
| U8 SharkSslCon_decryptMore | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_Decrypted returned by function SharkSslCon_decrypt.
The function returns TRUE if a complete SSL frame has been decrypted and is ready for consumption. See SharkSslCon_Decrypted for example code.
| SharkSslCon_RetVal SharkSslCon_encrypt | ( | SharkSslCon * | o, |
| U8 * | buf, | ||
| U16 | maxLen | ||
| ) |
Encrypt the data provided by parameter 'buf' or encrypt data in the SharkSslCon send buffer.
SharkSSL provides a zero copy API and you can optionally copy data directly to the SharkSslCon send buffer. The send buffer is returned by calling function SharkSslCon_getEncBufPtr.
The SharkSslCon_encrypt function keeps decrypt state information in the SharkSslCon object and the function can therefore be used in event driven systems.
| o | the SharkSslCon object returned by function SharkSsl_createCon. |
| buf | the data to be encrypted or NULL when encrypting data copied directly to the SharkSslCon send buffer i.e. when copying the data to the pointer returned by function SharkSslCon_getEncBufPtr. |
| maxLen | is the length of 'buf' or the length of the data data copied directly to the SharkSslCon send buffer. |
When SHARKSSL_ENABLE_KEY_UPDATE is enabled and the copy API is used, this function can return SharkSslCon_Handshake before consuming any application bytes in order to send a proactive AES-GCM KeyUpdate. Send and acknowledge the handshake data, then call this function again with the same buf and maxLen values. The zero-copy API cannot update before encrypting plaintext already placed in its output buffer; it therefore fails closed at the key-usage limit if the application has not explicitly updated sooner.
| U8 SharkSslCon_encryptMore | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCon_encrypt.
The function returns TRUE if the SharkSslCon send buffer is full and must be flushed out.
The maximum size of the output buffer is set with parameter 'outBufSize' when calling the SharkSsl_constructor. When the unencrypted buffer is larger than what fits into the internal output buffer, SharkSSL breaks the unencrypted data into multiple chunks and SharkSslCon_encryptMore lets the application know when the buffer must be flushed.
See SharkSslCon_Encrypted for example code.
| U8 SharkSslCon_getAlertDescription | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt or in conjunction with state SharkSslCon_AlertRecv returned by function SharkSslCon_decrypt.
returns the SSL/TLS alert message
| U8 SharkSslCon_getAlertLevel | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_AlertSend returned by function SharkSslCon_encrypt or in conjunction with state SharkSslCon_AlertRecv returned by function SharkSslCon_decrypt.
| U8 * SharkSslCon_getBuf | ( | SharkSslCon * | o | ) |
Returns a pointer to the SharkSslCon input/receive buffer.
The pointer returned by SharkSslCon_getBuf is where you copy the encrypted data you receive from the network.
Example code:
| U16 SharkSslCon_getBufLen | ( | SharkSslCon * | o | ) |
Returns the number of bytes currently available at the pointer returned by SharkSslCon_getBuf.
The SharkSslCon input-buffer start size is set with parameter inBufStartSize when calling the SharkSsl_constructor. If #SHARKSSL_DISABLE_INBUF_EXPANSION is zero, SharkSSL may grow this buffer to receive a larger record or reassemble a fragmented handshake message. The configured SHARKSSL_MAX_HANDSHAKE_LENGTH and the internal record limit bound this growth. This U16 API exposes at most 65535 bytes at a time.
Many ciphers are block based and the receiving end must read the complete block before it can be decrypted. The SSL/TLS specification enables blocks up to 16K in size. Note, the block will not be larger than the size of the packet sent by the peer side.
| SHARKSSL_API U16 SharkSslCon_getDecData | ( | SharkSslCon * | o, |
| U8 ** | bufPtr | ||
| ) |
Get a pointer to the decoded data.
See SharkSslCon_Decrypted for example code.
| o | the SharkSslCon object returned by function SharkSsl_createCon. |
| bufPtr | is set to the beginning of the decoded data. |
| U8 * SharkSslCon_getEncBufPtr | ( | SharkSslCon * | o | ) |
This function is used in conjunction with SharkSslCon_encrypt when using the zero copy send API.
| U8 * SharkSslCon_getEncData | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCon_encrypt.
The function returns a pointer to the encoded data that must be sent to the peer side.
| U16 SharkSslCon_getEncDataLen | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_Encrypted returned by function SharkSslCon_encrypt.
The function returns the length of the encoded data that must be sent to the peer side.
See SharkSslCon_Encrypted for example code.
| U8 * SharkSslCon_getHandshakeData | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_Handshake returned by function SharkSslCon_encrypt.
The function returns the handshake data that must be sent to the peer side, if any.
After each send attempt, report the number of bytes accepted by the transport using SharkSslCon_setHandshakeDataSent.
See SharkSslCon_Handshake for example code.
| U16 SharkSslCon_getHandshakeDataLen | ( | SharkSslCon * | o | ) |
This function is used in conjunction with state SharkSslCon_Handshake returned by function SharkSslCon_decrypt.
The function returns the length of the handshake data that must be sent to the peer side, if any. The function returns zero if no handshake data should be sent.
See SharkSslCon_Handshake for example code.
| U8 SharkSslCon_isHandshakeComplete | ( | SharkSslCon * | o | ) |
Returns the following values:
| SHARKSSL_API U8 SharkSslCon_keyUpdate | ( | SharkSslCon * | o, |
| U8 | requestUpdate | ||
| ) |
Creates a TLS 1.3 KeyUpdate message and advances the local write keys.
Every KeyUpdate rotates the sending direction from the caller to the peer. SHARKSSL_KEY_UPDATE_NOT_REQUESTED rotates only caller-to-peer traffic and requires no response. SHARKSSL_KEY_UPDATE_REQUESTED also asks the peer to respond with SHARKSSL_KEY_UPDATE_NOT_REQUESTED, thereby rotating both traffic directions. After a successful SHARKSSL_KEY_UPDATE_REQUESTED, another requested update is rejected until any subsequent peer KeyUpdate is successfully processed. SHARKSSL_KEY_UPDATE_NOT_REQUESTED remains available while a requested update is outstanding, subject to the normal connection and output checks.
Retrieve the encrypted handshake data using SharkSslCon_getHandshakeData, send it, and report the number of bytes accepted by the transport using SharkSslCon_setHandshakeDataSent. This acknowledgement is required after every send attempt, including a complete send or a zero-byte result.
| o | the SharkSslCon object returned by function SharkSsl_createCon. |
| requestUpdate | must be SHARKSSL_KEY_UPDATE_NOT_REQUESTED to rotate only the local sending direction, or SHARKSSL_KEY_UPDATE_REQUESTED to ask the peer to rotate its sending direction as well. Both values rotate the local write keys. |
| U16 SharkSslCon_setHandshakeDataSent | ( | SharkSslCon * | o, |
| U16 | length | ||
| ) |
This function is used in conjunction with SharkSslCon_getHandshakeData.
See SharkSslCon_Handshake for example code.
The function returns the residual length of the handshake data that must be sent to the peer side, if any. The function returns zero if no handshake data should be sent.
Call this function after every attempt to send handshake data, including when the transport accepts the complete buffer or zero bytes. Do not continue connection processing until the function returns zero. A zero-copy transport must acknowledge only bytes for which it no longer references the buffer returned by SharkSslCon_getHandshakeData.
| o | the SharkSslCon object returned by function SharkSsl_createCon. |
| length | is the number of handshake bytes consumed or copied by the transport (usually TCP/IP); it may be zero. |