Sample code for 30+ languages & platforms
DataFlex

SSH Authentication using an SSH Certificate

See more SSH Examples

Demonstrates how to authenticate using an SSH certificate.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbSshCert
    Handle hoSbPrivKey
    Variant vKey
    Handle hoKey
    Handle hoSsh
    String sHostname
    Integer iPort
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbSshCert
    If (Not(IsComObjectCreated(hoSbSshCert))) Begin
        Send CreateComObject of hoSbSshCert
    End
    Get ComLoadFile Of hoSbSshCert "qa_data/sshCert/user_ecdsa_key-cert.pub" "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to load user_ecdsa_key-cert.pub"
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPrivKey
    If (Not(IsComObjectCreated(hoSbPrivKey))) Begin
        Send CreateComObject of hoSbPrivKey
    End
    Get ComLoadFile Of hoSbPrivKey "qa_data/sshKeys/user_ecdsa_key" "utf-8" To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to load user_ecdsa_key"
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatSshKey)) To hoKey
    If (Not(IsComObjectCreated(hoKey))) Begin
        Send CreateComObject of hoKey
    End
    // Provide the password if the user_ecdsa_key is stored in an encrypted format.
    Set ComPassword Of hoKey To "secret"
    Get ComGetAsString Of hoSbPrivKey To sTemp1
    Get ComFromOpenSshPrivateKey Of hoKey sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Indicate that the SSH certificate is to be used for authentication.
    // The UseSshCertificate method was added in Chilkat v11.0.0
    Get ComGetAsString Of hoSbSshCert To sTemp1
    Get ComUseSshCertificate Of hoKey sTemp1 To iSuccess

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    Move "ssh.example.com" To sHostname
    Move 22 To iPort
    Get ComConnect Of hoSsh sHostname iPort To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoKey to vKey
    Get ComAuthenticatePk Of hoSsh "myLogin" vKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Public-Key Authentication using an SSH Certificate was Successful!"


End_Procedure