NanoSSL client integration

Client integration process overview

This table describes the tasks for integrating NanoSSL client into an application.

TaskDescriptionRefer to
1Add the TrustCore SDK NanoSSL client software to the application development environment.Building TrustCore SDK Components
2If there is no pre-configured TrustCore SDK port for the operating system, edit the appropriate abstraction files to port the code to the operating system.Porting TrustCore SDK Code
3Specify which products (e.g., synchronous NanoSSL client), functions (e.g., debugging and examples), and features will be included in the TrustCore SDK executable by setting the appropriate compilation flags.
For example, to incorporate synchronous and asynchronous NanoSSL clients into a system, define the following flags:
Building TrustCore SDK Components
NanoSSL compilation flags
4Create the object files and executable.Building TrustCore SDK Components
Using TrustCore SDK NanoCrypto FIPS Binaries for Linux
5Add calls within the application’s code to the TrustCore SDK functions for initialization and shutdown, and then set up threads for any required TrustCore SDK components.Common TrustCore SDK initialization
6Add calls within the application’s code to the TrustCore SDK functions for NanoSSL client setup and message processing, and (optionally) certificate management.Implement NanoSSL client
7Optionally, if certificate management functions do not already exist within the application, add calls to the TrustCore SDK certificate management functions for performing certificate validation.CERT_STORE_* functions, in the NanoSSL API Reference.

NanoSSL client process flow

This diagram shows the NanoSSL client process flow. For more information about the NanoSSL client process flow, see Implement NanoSSL client.

image

Build TrustCore SDK client sample code

To help with integration of NanoSSL client into devices, a suite of sample code is included in the source distribution (in the ${MSS_SRC_PKG}/src/examples directory), to quickly build a client and demonstrate its features using the provided NanoSSL cmake project and build scripts. The following files are provided:

  • ssl_client_example.c — Implements a secure, synchronous HTTP client.
  • ssl_example_async_client.c — Implements a secure, asynchronous HTTP client.
  • ssl_loopback_example.c — Implements an asynchronous client-server loop-back communication.

Sample code should be used as a reference and modified as required for inclusion into an application’s source code. The sample code may also be used to verify NanoSSL client-server communication.

The following sections describe tasks that may be performed to generate and run sample NanoSSL Client code:

Generate a sample NanoSSL client

To generate the sample NanoSSL client:

  1. Change to the projects/nanossl directory:
    cd projects/nanossl
    
  2. Run the following command:
    ./build.sh --clean --debug --suiteb ssl_client
    

Generate a TLS v1.3 sample NanoSSL client

To generate the TLS v1.3 sample NanoSSL client:

  1. Change to the projects/nanossl directory:
    cd projects/nanossl
    
  2. Run the following command to build the NanoSSL library with support for TLS v1.3.
    ./build.sh --clean --debug --suiteb ssl_client
    

Disable TLS v1.3 and features

By default, TLS v1.3 is enabled when the NanoSSL client is generated.

To disable TLS v1.3 and features:

  1. Change to the projects/nanossl directory:
    cd projects/nanossl
    
  2. Run the following command to build the NanoSSL library with TLS v1.3 features disabled:
    ./build.sh --clean --debug --suiteb ssl_client <disable-feature>
    
    Where <disable-feature> may be:
    • --disable-0rtt: Disables the 0-RTT feature.
    • --disable-psk: Disables the PSK feature.
    • --disable-tls13: Disables TLS v1.3.

Verify and run the sample NanoSSL client

If hosting an SSL server, we recommend verifying basic NanoSSL client communications with the SSL server before beginning implementations for custom code using the NanoSSL client sample code.

To verify and run the NanoSSL client sample code:

  1. Start the SSL server.
  2. Open a command shell and start the NanoSSL client.
  3. Run the sample NanoSSL client using the applicable command (i.e., using options for TLS or TLS v1.3) to securely connect via HTTPS to the SSL server, dump debug information to a temporary command prompt window, and then automatically terminate.
    • For TLS:
      ./bin/ssl_client <options>
      
      where <options> may be:
      • ? — Displays the help.
      • -ssl_ip <IP> — Specifies the IP address of the SSL server.
      • -ssl_port <ssl port> — Specifies the port number of the SSL server.
      • -ssl_servername <ssl server name> — Specifies the SSL server’s name.
      • -ssl_certpath <path to files> — Specifies the directory path to the certificate files.
      • -ssl_server_cert <cert name> — Specifies the name of the server certificate.
      • -ssl_client_cert <cert name> — Specifies the name of the client certificate.
      • -ssl_client_keyblob <blob name> — Specifies the name of the client key BLOB file.
    • For TLS v1.3:
      ./bin/ssl_client <options>
      
      where additional <options> include:
      • -ssl_external_psk — Specifies to use an external PSK for TLS v1.3.
      • -ssl_early_data <early_data> — Specifies the early data content to be sent.

Implement NanoSSL client

To integrate a NanoSSL client into an application, add calls to TrustCore SDK functions for NanoSSL client initialization, socket and connection management, message processing, and NanoSSL client shutdown, as shown in the ${MSS_SRC_PKG}/src/examples/ssl_client_example.c sample module.

In particular, the functions and tasks described in the table below should be performed in the order shown, making calls to either the synchronous or asynchronous methods as appropriate. For additional NanoSSL Client process flow information, see NanoSSL client process flow.

TaskSynchronous referenceAsynchronous reference
Initialize NanoSSL client’s session manager and internal structures.SSL_initSSL_ASYNC_init
Set up certificates: create a certificate store, add identities and CA certificates to the store, and associate the certificate with the SSL connection.CERT_STORE_* functions, in the NanoSSL API Reference.CERT_STORE_* functions, in the NanoSSL API Reference.
Customize and register upcalls (callbacks) for all desired products and features.Enabling NanoSSL and NanoDTLS products and features
SSL_sslSettingsEnabling NanoSSL and NanoDTLS products and features
SSL_sslSettings
Open a TCP socket connection to the NanoSSL server.The application’s existing methodsThe application’s existing methods.
Create a connection instance and establish session context.SSL_connectSSL_ASYNC_connect
Negotiate SSL handshaking.SSL_negotiateConnectionSSL_ASYNC_start
Loop to continuously process packets.SSL_send
SSL_sendPending
SSL_recv
SSL_recvPendingSSL_ASYNC_sendMessage
SSL_ASYNC_sendMessagePending
TCP read of data via SSL_ASYNC_recvMessage
SSL_ASYNC_recvMessage2
SSL_ASYNC_getRecvBuffer
SSL_ASYNC_getSendBuffer
Invoke the asynchronous upcall to begin application communications
7. a) Send flowSSL_send: Sends application data to the connected client/server, then returns the number of bytes sent.
SSL_sendPending: Gives the number of pending bytes. If SSL_send was not able to send all the bytes, SSL_sendPending should be called in a loop until all the data is sent
7. b) Receive flowSSL_recv: Reads application data from the connected client/server, then returns the total number of bytes received.
SSL_recvPending: Indicates if there is any data to be read. SSL-recv should be invoked in a loop until SSL_recvPending indicates there is no data to be read.
Optionally, save the session context for later reconnection.SSL_getClientSessionInfo
SSL_getSessionFlagsSSL_getClientSessionInfo
SSL_getSessionFlags
Reclaim device and NanoSSL client resources.SSL_closeConnectionSSL_ASYNC_closeConnection
Close TCP socket connection to NanoSSL server.The application’s existing methodsThe application’s existing methods

Optional NanoSSL client functions

This table lists optional functions that may also be performed:

Optional functionDescription
SSL_enableCiphersLimit which ciphers to use (for performance or security reasons).
SSL_getCipherInfoRetrieve the connection’s cipher and eccCurves.
SSL_setDNSNamesSpecify a list of DNS names that when matched to the certificate subject name will enable a connection.
SSL_setServerNameListSpecify a list of server names acceptable to the client.
SSL_getInstanceFromSocketRetrieve the connection instance for a socket identifier.
SSL_lookupAlert and SSL_sendAlertRetrieve the SSL alert code for a TrustCore SDK error.
SSL_getSocketIdRetrieve the socket identifier for a connection instance.
SSL_ioctl (also see SSL_ioctl)Dynamically enable and/or disable selected features for an SSL session’s connection instance.
SSL_isSessionSSLDetermine whether a connection instance represents an SSL/TLS server, an SSL/TLS client, or an unrecognized connection (for example, SSH).
SSL_initiateRehandshakeRenegotiate an SSL/TLSsession.
SSL_setSessionFlagsSet session flags.
SSL_getSessionStatusGet session’s connection status.
SSL_setVersionCallbackLog the TLS version during negotiation (using the __ENABLE_MOCANA_SSL_VERSION_LOG_CALLBACK__ compilation flag).
SSL_setCookie and SSL_getCookieStore and retrieve custom information for a connection instance’s context.
SSL_releaseTables and SSL_shutdownStackShutdown sequence must be as follows:
SSL_setCipherAlgorithmSet ciphers, supported groups, signature algorithms, and certificate signature algorithms to be used during TLS v1.3 negotiation.
SSL_sendKeyUpdateRequestInvoke the flow to update the keys used to encrypt/decrypt application data.
SSL_setClientSavePSKCallbackSet a callback to save PSK sent by server.
SSL_CLIENT_setRetrievePSKCallbackSend a callback to retrieve the PSK to use to establish a new connection.
SSL_setEarlyDataSet application data to be sent as early data in the 0-RTT flow.
SSL_serializePSKSerialize PSK data.
SSL_deserializePSKDe-serialize PSK data.
SSL_setMinRSAKeySizeSet the minimum size of the RSA key used in the connection.
SSL_setSha1SigAlgEnable/disable the SHA-1 signature algorithm.
SSL_setFIPSEnabledEnable/disable FIPS.
SSL_setClientCAListSet the list of accepted CAs on the server. This CA list is sent in the Certificate Request message.
SSL_populateMutualAuthCertStorePopulate the certificate store for mutual authentication per session.
SSL_enableSrtpProfilesSet the SRTP profiles to be used by the DTLS SRTP feature.
SSL_enableHeartbeatSupportEnable heartbeat support for the connection.
SSL_sendHeartbeatMessageSend the heartbeat message.