Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Tunnel
oleobject loo_Json
integer li_SshPort
integer li_WaitForThreadExit

li_Success = 0

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

loo_Tunnel = create oleobject
li_rc = loo_Tunnel.ConnectToNewObject("Chilkat.SshTunnel")
if li_rc < 0 then
    destroy loo_Tunnel
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  List the allowed algorithms for each category, in order of preference.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("kex","curve25519-sha256@libssh.org,ecdh-sha2-nistp256")
loo_Json.UpdateString("hostKey","ssh-ed25519,ecdsa-sha2-nistp256")
loo_Json.UpdateString("cipher","chacha20-poly1305@openssh.com,aes256-ctr")
loo_Json.UpdateString("mac","hmac-sha2-256,hmac-sha2-512")

//  Apply the allow-list before connecting.
li_Success = loo_Tunnel.SetAllowedAlgorithms(loo_Json)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    destroy loo_Json
    return
end if

loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 5432

li_SshPort = 22
li_Success = loo_Tunnel.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    destroy loo_Json
    return
end if

Write-Debug "Connected."

li_WaitForThreadExit = 1
li_Success = loo_Tunnel.CloseTunnel(li_WaitForThreadExit)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    destroy loo_Json
    return
end if



destroy loo_Tunnel
destroy loo_Json