Sample code for 30+ languages & platforms
DataFlex

SSH Authentication using X.509 Certificates

See more SSH Examples

Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509 certificate. The certificate and key are loaded from a .pfx, the private key is exported as PEM, and that key is used for public-key authentication. See X.509v3 Certificates for SSH Authentication for more information.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: This is not X.509 certificate authentication in the SSH-protocol sense — the server still performs ordinary public-key authentication. The certificate is simply a convenient container for a key pair your organization already manages through its PKI, so the same credential issued for other purposes can be reused for SSH. What the server needs installed is the corresponding public key, exactly as with any other key. Chilkat can also load such certificates from smart cards and USB tokens rather than a file.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    String sHostname
    Integer iPort
    String sPfxPassword
    Handle hoCert
    String sPrivKeyPem
    Variant vKey
    Handle hoKey
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    //  Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509
    //  certificate.

    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 = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the certificate and its private key from a .pfx file.  The PFX password should come
    //  from a secure source rather than being hard-coded.
    //  Note: Chilkat can load certificates and private keys from many sources, including smart
    //  cards and USB tokens (HSMs).
    Move "myPfxPassword" To sPfxPassword
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/pfx/example.pfx" sPfxPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get the certificate's private key as PEM, to be used for SSH authentication.  The
    //  corresponding public key is installed on the server.
    Get ComGetPrivateKeyPem Of hoCert To sPrivKeyPem
    Get ComLastMethodSuccess Of hoCert To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatSshKey)) To hoKey
    If (Not(IsComObjectCreated(hoKey))) Begin
        Send CreateComObject of hoKey
    End
    Get ComFromOpenSshPrivateKey Of hoKey sPrivKeyPem To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoKey to vKey
    Get ComAuthenticatePk Of hoSsh "mySshLogin" vKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Public-key authentication successful."

    Send ComDisconnect To hoSsh


End_Procedure