Skip to main content

Use case examples

This section describes typical NanoSSH Client use cases and explains how to build applicable TrustCore SDK example code:

The NanoSSH client shell provides a secured communication channel between two networked devices. The typical usage is to log in to a remote machine and execute commands, as shown below.

Figure 1.
nanossh-client-guide-figures_nanossh-client-shell-architecture.png

NanoSSH client shell architecture


Before you begin

  • Add the NanoSSH client shell example code and supporting software (such as TrustCore SDK code) to the application’s development environment. Make sure to include the sshc_shell_example.c file. See Add TrustCore SDK code to your development environment for more details.

    Note

    If there is no pre-configured TrustCore SDK port for your operating system, your can edit the appropriate abstraction files to port the code. For more information, contact your DigiCert​​®​​ account representative.

Build the shell access example code

Verify communication between NanoSSH client shell and NanoSSH server

After building the NanoSSH code, verify that it executes in the operating environment. Assuming that the NanoSSH client and NanoSSH server (and the example code) have been enabled, the loopback interface may be used to confirm that the client and server are able to communicate within the operating environment.

If using only the NanoSSH client but not the NanoSSH server, see Verifying NanoSSH client Shell Communication with Any SSH server.

To verify communication between the client shell and the NanoSSH server:

Verify NanoSSH client shell communication with any SSH server

It is recommended to verify basic NanoSSH client communications with an SSH server before beginning custom code implementations. This process is easily done using the NanoSSH client shell or SFTP example code, as follows.

The following procedure assumes that Cygwin is used to interface to a UNIX/Linux-like development system. For other environments, modify the commands accordingly.

To verify NanoSSH client shell communication with any SSH server:

Client shell integration flowchart

The following flowchart shows the NanoSSH client shell integration. For detailed steps to customizing the NanoSSH client shell implementation, see Customizing a NanoSSH client implementation.

Figure 2.
nanossh-client-shell-flowchart-01.png

NanoSSH client shell flowchart


The NanoSSH SFTP client enables secure writing (PUT) and retrieval (GET) of files to/from a remote machine; for example, to retrieve an updated image file from a server and to write a log file to the server, as shown below.

Figure 3.
nanossh-client-guide-figures_nanossh-client-secure-file-transfer-architecture.png

NanoSSH client secure file transfer architecture


Build SFTP example code

Verifying NanoSSH client SFTP communication with any SSH server

Be sure to verify basic NanoSSH SFTP client communications with the SSH server before beginning custom code implementations. This process is easily done using the NanoSSH client shell or SFTP example code, as follows:

  1. Open Cygwin and change to the directory where the TrustCore SDK distribution package ZIP file was extracted.

  2. If any source code or makefiles have been modified since the sshc target was last built, rebuild the executable:

    • ./script/nanossh/ssh_client/build_ssh_client_ncrypto.sh

  3. Run the example code executable:

    • ./bin/ssh_client

NanoSSH SFTP client integration flowchart

The following flowchart shows the NanoSSH SFTP client integration. For detailed steps to customizing the NanoSSH SFTP client implementation, see Customize a NanoSSH client Implementation.

Figure 4.
nanossh-sftp-client-process-flow-01.png

NanoSSH client Secure File Transfer Protocol (SFTP) flowchart


NanoSSH client port forwarding encrypts and decrypts TCP/IP traffic. A typical application, as shown in Figure 6 below, is for proprietary applications that are operating on distributed machines to securely communicate over any network or the Internet using TCP/IP.

Figure 5.
nanossh-client-guide-figures_nanossh-client-port-forwarding-architecture.png

NanoSSH client port forwarding architecture


Building the port forwarding example code

  1. Add the NanoSSH port forwarding example code and supporting software (such as TrustCore SDK code) to the application development environment.

    • Be sure to include the sshc_pf_example.c file.

  2. 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.

  3. In the sshc_pf_example.c example code file, change the following values to match the environment’s:

    • CONNECT_HOST

    • sshc_exampleUserName[]

    • sshc_examplePassword[]

    • sshc_exampleIPAddress

  4. Create the object files and executable. The commands are:

    • make -f make/Makefile.<os> clean

    • make -f make/Makefile.<os> sshc

  5. Verify that the NanoSSH client example code executes in the environment.

  6. Repeat step 5 and step 6 as needed to add features one at a time.

Client port forwarding integration flowchart

The following flowchart displays the NanoSSH client port forwarding integration.

Figure 6.
nanossh-client-port-forwarding-process-flowchart-01.png

NanoSSH client port forwarding process flowchart


SSH reverse port forwarding lets you reach a device that can only make outbound connections (because it sits behind a firewall or NAT) by routing traffic through a public “relay” host.

Table 1. Reverse port forwarding components at a glance

Role

Runs on

Responsibility

Isolated device

Private network

Opens an outbound SSH session that requests reverse port forwarding.

Relay host

Publicly reachable VM/server

SSH server that accepts reverse-forward requests and relays traffic.

Remote user/device

Any location

Opens a normal SSH session to the relay to reach the isolated device.


Why and when to use it

  • Inbound blocked, outbound allowed. Your target device lives on a locked-down network but can open outbound TCP connections.

  • No firewall changes. You avoid reconfiguring NAT rules or requesting new ports from network admins.

  • Ad-hoc or permanent access. Keep the tunnel up permanently (service/watchdog) or start it only when remote access is required.

How the workflow unfolds

  1. Isolated device dials out. It initiates an SSH session to the relay and asks the relay to listen on a specific port.

  2. Relay binds the port. It exposes that port externally and links it to the tunnel.

  3. Remote user connects. They SSH (or otherwise TCP-connect) to the relay’s exposed port. The relay pipes that traffic through the tunnel to the isolated device.

  4. Bi-directional exchange. From here, traffic flows both ways—remote ↔ relay ↔ isolated—as if there were a direct connection.

Implementation reminders

  • Isolated-side client still hooks standard SSH port-forward callbacks plus the events for registering/canceling reverse forwards.

  • Relay server needs AllowTcpForwarding yes (and often GatewayPorts yes) and handlers for reverse-forward requests.

  • Remote-side client behaves like a regular port-forwarding SSH client; no extras required.

Building the reverse port forwarding example code

  1. Add the NanoSSH reverse port forwarding example code and supporting software (such as TrustCore SDK code) to the application development environment.

    • Be sure to include the src/examples/sshc_rpf_example.c file.

  2. 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.

  3. In the sshc_rpf_example.c example code file, change the following values to match the environment’s:

    • CONNECT_HOST

    • sshc_exampleUserName[]

    • sshc_examplePassword[]

    • sshc_exampleIPAddress

  4. Use the sshc_rpf_example.c sample code’s implementation as-is or modify it as desired.

  5. Create the object files and executable. The commands are:

    • make -f make/Makefile.<os> clean

    • make -f make/Makefile.<os> sshc

  6. Verify that the NanoSSH client example code executes in the environment.

  7. Repeat step 4 to step 6 as needed to add features one at a time.

Implementing Reverse Port Forwarding for a NanoSSH client

  1. (Optional) Set up environment-specific thread support.

    • The example client creates a thread for each channel opened in the port-forwarding tunnel, and the code sets up lpfTable to track information about the open channels. To avoid thread conflicts, a mutex is used for access control.

  2. Initialize the NanoSSH client’s session manager, internal structures, and NanoSSH client-specific settings.

  3. Customize and register upcalls (callbacks) for all desired products and features:

    • Replace stub functions with the corresponding custom routines, modeling them after the NanoSSH client example code.

    • Register the callbacks as required.

  4. Set up management for authentication keys.

  5. Set up a TCP/IP socket connection with the remote SSH server (NanoSSH or any SSH server).

  6. Initialize the SSH session context.

  7. (Optional) Customize the session establishment, such as to override the default choice of the strongest available cipher and specify a particular cipher suite instead.

  8. Set up a secure and authenticated SSH session with the remote server.

  9. If using the NanoSSH SFTP client, open an SFTP session and negotiate SFTP version support.

  10. Send a remote port forwarding request to the SSH server to establish the client-server link.

    • The tunnel with a remote device is not established yet and not created until after the server uses the established client-server link to forward a connection request from a remote device.

  11. Verify that the client-server link was successfully established.

  12. Allocate the resources that the application needs for receiving data on the connection.

  13. Loop continuously to process messages from the SSH server and from local ports.

  14. Close the connection.

  15. Shut down the NanoSSH client and reclaim device and NanoSSH client resources.