NanoSSL server integration

With TrustCore SDK NanoSSL server, devices may securely connect to remote peers.

Server integration process overview

This table describes the tasks for integrating a NanoSSL server into devices. Some tasks are optional and may not be relevant for a deployment. To determine whether to perform a particular task, read the beginning of each section.

TaskReference
Add the TrustCore SDK NanoSSL server software to the application development environment.Building Components
If 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
Specify which products (e.g., NanoSSL server), 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 servers into a system, define the following flags:
ENABLE_MOCANA_SSL_SERVER
ENABLE_MOCANA_SSL_ASYNC_SERVER_API
ENABLE_MOCANA_SSL_DUAL_MODE_APIBuilding TrustCore SDK Components
NanoSSL compilation flags
Create the object files and executable and verify that TrustCore SDK code may execute on the operating system.Building TrustCore SDK Components
Using TrustCore SDK NanoCrypto FIPS Binaries for Linux
Add 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.
When shutting down the SSL stack, the calling order should be:
SSL_releaseTables
SSL_shutdownStack (or SSL_shutdown in release 6.5 and older)
MOCANA_freeMocanaCommon TrustCore SDK initialization
Add calls within the application’s code to the TrustCore SDK functions for NanoSSL server setup and message processing, and optionally certificate management.Implement NanoSSL server
Optionally, 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.

Synchronous and asynchronous support

The source code distribution of NanoSSL server supports both synchronous and asynchronous message handling.

Synchronous server process flow

This figure shows the NanoSSL synchronous server process flow.

image

Asynchronous server process flow

This figure shows the NanoSSL asynchronous server process flow.

image

Build NanoSSL server sample code

To help with integration of a NanoSSL server 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 NanoSSL server and demonstrate its features. It comprises the following files:

  • ca_mgmt_example.c — Performs authentication with certificates by using the TrustCore SDK certificate management functions.
  • mocana_example.c — Initializes TrustCore SDK code.
  • ssl_example.c — Implements a secure, synchronous HTTP server.
  • ssl_example_async.c — Implements a secure, asynchronous HTTP server.
  • ssl_loopback_example.c — Implements asynchronous client-server loop-back communication.

The 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-NanoSSL server communication.

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

Generate a sample NanoSSL server

To generate a sample NanoSSL server:

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

Generate a sample TLS v1.3 NanoSSL server

To generate the TLS v1.3 sample NanoSSL server:

  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_server
    

Disable TLS v1.3 and features

By default, TLS v1.3 is enabled when the NanoSSL server 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 NanoCrypto library with TLS v1.3 features disabled:
    ./build.sh --clean --debug --suiteb ssl_server
    
    Where <disable-feature> may be:
    • --disable-0rtt: Disables the 0-RTT feature.
    • --disable-psk: Disables the PSK feature.
    • --disable-tls13: Disables TLS v1.3.

Run the NanoSSL server

To run the sample NanoSSL server:

  1. Run the following NanoSSL server sample code using the applicable command (i.e., using options for TLS or TLS v1.3).
    • For TLS:
      ./bin/ssl_server <options>
      
      where additional <options> include:
      • ? — Displays the help.
      • -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_keyblob <blob name> — Specifies the name of the client key BLOB file.
    • For TLS v1.3:
      ./bin/ssl_server <options>
      
      where additional <options> include:
      • -ssl_max_early_data_size <size> — Specifies the maximum early data size accepted when using the PSK for connection. This value is sent with the session ticket to the client.

Verify NanoSSL server functionality

To verify and run the NanoSSL server functionality with NanoSSL client, 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
    
  3. Run the NanoSSL client with the correct IP address, port number and server name to connect to NanoSSL server.

Verify NanoSSL server functionality with a web browser

To verify and run the NanoSSL Server functionality with a web browser:

  1. Start the NanoSSL server with the correct certificate and key. By default, the server IP address is 127.0.0.1 and listens on port 1440.
  2. Open a web browser, which acts as the SSL client, and navigate to https://127.0.0.1/a. The server terminal displays all the debug information, handshake messages, and the HTTPS request from the browser; the web browser displays the data sent by server.
  3. Optionally, request additional data from the server, use the following URLs in the browser:
    • To tell NanoSSL server to send 100,000 bytes in 1 KB blocks:
      https://127.0.0.1/ab
      
    • To tell NanoSSL server to send 10 million bytes in 1 KB blocks:
      https://127.0.0.1/ag
      
    • To tell NanoSSL server to send 100,000 bytes in 16 KB blocks:
      https://127.0.0.1/b
      
    • To tell NanoSSL client to shut down the NanoSSL server:
      https://127.0.0.1/q
      

Use externally generated private keys with NanoSSL

When generating the certificate, ensure that the Common Name matches the -ssl_servername option.

For example, to use a generated key and certificate when starting the NanoSSL server:

./ssl_server -ssl_port 1440 -ssl_certpath keystore -ssl_servername www.sslexample.com -ssl_server_cert dsa_cert.der -ssl_server_keyblob dsa_key.pem

To generate a private key for use with NanoSSL:

  1. Generate the RSA key and certificate. For example:
    openssl genpkey -out rsa_key.pem -algorithm RSA -outform PEM -pkeyopt rsa_keygen_bits:2048
    
    openssl req -x509 -new -days 3650 -key rsa_key.pem -outform DER -out rsa_cert.der
    
  2. Generate the DS keypair and certificate. For example:
    openssl genpkey -genparam -algorithm DSA -out dsap.pem -pkeyopt dsa_paramgen_bits:2048
    
    openssl genpkey -paramfile dsap.pem -out dsa_key.pem
    
    openssl req -x509 -new -days 3650 -key dsa_key.pem -outform DER -out dsa_cert.der
    
  3. Generate the ECDSA keypair and certificate. For example:
    openssl ecparam -out ec_key.pem -name prime256v1 -genkey
    
    openssl req -new -key ec_key.pem -x509 -nodes -days 365 -outform DER -out ecdsa_cert.der
    
  4. Generate the p12 file.
    openssl pkcs12 -export -inkey [private key filename] -in [certificate filename] -out cert.p12
    

Implement NanoSSL server

This table lists NanoSSL products and features that may be enabled, the flags required to enable them, and the required callback functions that must be customized and registered. (Both NanoSSL client and server are listed because they share a common code base.)

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

In particular, the functions and tasks described in below should be performed in the order shown, making calls to either the synchronous or asynchronous methods as appropriate.

TaskSynchronous referenceAsynchronous reference
Initialize NanoSSL server’s session manager.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.
Initialize NanoSSL server’s internal structures:SSL_initServerCertSSL_ASYNC_initServer
SSL_initServerCert
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 methods.The application’s existing methods.
Establish a connection between the device and a web browser.SSL_acceptConnectionSSL_ASYNC_acceptConnection
Negotiate SSL handshaking.SSL_negotiateConnectionNot applicable for asynchronous communication.
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
Reclaim device and SSL/TLS server resources.SSL_closeConnectionSSL_ASYNC_closeConnection
Close TCP socket connection to NanoSSL server.The application’s existing methods.The application’s existing methods.
Optionally, shut down the SSL stack to enable SSL/TLS server to listen to a different port during its next session.SSL_shutdownStackNot applicable for asynchronous communication.

Optional NanoSSL server functions

This table lists the optional functions that NanoSSL may also perform.

Optional functionDescription
SSL_enableCiphersLimit which ciphers to use (for performance or security reasons).
SSL_getCipherInfoRetrieve the connection’s cipher and eccCurves.
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_Settings_IoctlDynamically enable and/or disable global settings for NanoSSL Server.
SSL_isSessionSSLDetermine whether a connection instance represents an SSL/TLS server, an SSL/TLS client, or an unrecognized connection (for example, SSH).
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_setDHParametersInject pre-generated DH prime and group parameters into a server session (accept and async_accept).
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_setServerSavePSKCallbackSave PSK generated and send to client.
SSL_setServerLookupPSKCallbackLook up for a PSK based on the identity sent by the client.
SSL_setMaxEarlyDataSizeSet the maximum early data size to accepted with a particular PSK in 0RTT flow.
SSL_setRecvEarlyDataSizeSet the maximum size of the early data that may be received by server.
SSL_sendPosthandshakeAuthCertificateRequestInitiate the flow to authenticate client after handshake is completed.
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.