Sample code for 30+ languages & platforms
Unicode C

SSH Tunnel Set Allowed SSH Algorithms

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.SetAllowedAlgorithms method, which restricts the SSH connection to a specific set of algorithms. A JsonObject supplies comma-separated allow-lists for the kex, hostKey, cipher, and mac categories. It must be called before Connect.

Important: You typically should not set allowed algorithms explicitly. By default Chilkat orders algorithms according to best practices and accounts for known vulnerabilities.

Background: SSH negotiates a mutually supported algorithm from each category at connection time, and pinning that choice makes an application brittle: if the server later drops a weak algorithm, the two sides may fail to agree and the tunnel stops working. The legitimate reasons to override are a security policy mandating specific algorithms or a compatibility problem with an old server; otherwise let the library's defaults evolve with current guidance.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSshTunnelW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSshTunnelW tunnel;
    HCkJsonObjectW json;
    int sshPort;
    BOOL waitForThreadExit;

    success = FALSE;

    //  Demonstrates the SshTunnel.SetAllowedAlgorithms method, which restricts the SSH connection to a
    //  specific set of algorithms.  The only argument is a JsonObject.  It must be called before
    //  Connect.
    //  
    //  Note: You typically should NOT set allowed algorithms explicitly.  By default Chilkat orders
    //  algorithms according to best practices and accounts for known vulnerabilities.  Hard-coding
    //  them can make an application brittle if a server later changes its allowed algorithms.

    tunnel = CkSshTunnelW_Create();

    //  List the allowed algorithms for each category, in order of preference.
    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"kex",L"curve25519-sha256@libssh.org,ecdh-sha2-nistp256");
    CkJsonObjectW_UpdateString(json,L"hostKey",L"ssh-ed25519,ecdsa-sha2-nistp256");
    CkJsonObjectW_UpdateString(json,L"cipher",L"chacha20-poly1305@openssh.com,aes256-ctr");
    CkJsonObjectW_UpdateString(json,L"mac",L"hmac-sha2-256,hmac-sha2-512");

    //  Apply the allow-list before connecting.
    success = CkSshTunnelW_SetAllowedAlgorithms(tunnel,json);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshTunnelW_lastErrorText(tunnel));
        CkSshTunnelW_Dispose(tunnel);
        CkJsonObjectW_Dispose(json);
        return;
    }

    CkSshTunnelW_putDestHostname(tunnel,L"db.internal.example.com");
    CkSshTunnelW_putDestPort(tunnel,5432);

    sshPort = 22;
    success = CkSshTunnelW_Connect(tunnel,L"ssh.example.com",sshPort);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshTunnelW_lastErrorText(tunnel));
        CkSshTunnelW_Dispose(tunnel);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"Connected.\n");

    waitForThreadExit = TRUE;
    success = CkSshTunnelW_CloseTunnel(tunnel,waitForThreadExit);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSshTunnelW_lastErrorText(tunnel));
        CkSshTunnelW_Dispose(tunnel);
        CkJsonObjectW_Dispose(json);
        return;
    }



    CkSshTunnelW_Dispose(tunnel);
    CkJsonObjectW_Dispose(json);

    }