DataFlex
DataFlex
SSH Authenticate using a Smart Card Private Key
See more SSH Examples
Demonstrates using a private key stored on an HSM (smart card or token) for SSH public-key authentication. A JSON template locates the private and public key objects by class, key type, and label, and the returned handles are bound to an SshKey object.
Background: PKCS#11 objects are found by attribute rather than by name alone, which is why a template describing the class, key type, and label is used. An important practical detail is that the returned handles are valid only for the lifetime of the PKCS#11 session — they cannot be cached and reused later, so a subsequent session must locate the objects again. Logging out and closing the session when finished releases the device for other applications.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vPkcs11
Handle hoPkcs11
String sPin
Integer iUserType
Variant vJsonTemplate
Handle hoJsonTemplate
UInteger iPrivKeyHandle
UInteger iPubKeyHandle
Key Handle hoSshKey
Handle hoSsh
Integer iPort
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates using a private key stored on an HSM (smart card or token) for SSH public-key
// authentication. Public-key authentication means the client uses the private key, while the
// corresponding public key is installed on the server under the SSH account.
//
// Note: Chilkat's PKCS#11 implementation runs on Windows, Linux, macOS, and other supported
// operating systems.
Get Create (RefClass(cComChilkatPkcs11)) To hoPkcs11
If (Not(IsComObjectCreated(hoPkcs11))) Begin
Send CreateComObject of hoPkcs11
End
// Use the PKCS#11 driver (.dll, .so, or .dylib) for your particular HSM.
Set ComSharedLibPath Of hoPkcs11 To "C:/Program Files (x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS11.dll"
// The HSM PIN should come from a secure source rather than being hard-coded.
Move "0000" To sPin
// Normal user = 1
Move 1 To iUserType
Get ComQuickSession Of hoPkcs11 iUserType sPin To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPkcs11 To sTemp1
Showln sTemp1
Procedure_Return
End
// Provide a template describing the PKCS#11 object to find: an RSA private key labeled
// "MySshKey". For how such a key is originally imported, see
// PKCS11 Import SSH Key
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonTemplate
If (Not(IsComObjectCreated(hoJsonTemplate))) Begin
Send CreateComObject of hoJsonTemplate
End
Get ComUpdateString Of hoJsonTemplate "class" "private_key" To iSuccess
Get ComUpdateString Of hoJsonTemplate "key_type" "rsa" To iSuccess
Get ComUpdateString Of hoJsonTemplate "label" "MySshKey" To iSuccess
Get pvComObject of hoJsonTemplate to vJsonTemplate
Get ComFindObject Of hoPkcs11 vJsonTemplate To iPrivKeyHandle
If (iPrivKeyHandle = 0) Begin
Get ComLastErrorText Of hoPkcs11 To sTemp1
Showln sTemp1
Procedure_Return
End
// The handle is only valid for the duration of this PKCS#11 session. To use the key in a
// later session, find it again.
Showln "private key handle: " iPrivKeyHandle
// Find the corresponding public key by changing the class in the same template.
Get ComUpdateString Of hoJsonTemplate "class" "public_key" To iSuccess
Get pvComObject of hoJsonTemplate to vJsonTemplate
Get ComFindObject Of hoPkcs11 vJsonTemplate To iPubKeyHandle
If (iPubKeyHandle = 0) Begin
Get ComLastErrorText Of hoPkcs11 To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "public key handle: " iPubKeyHandle
// Create an empty SSH key object and tell it to use the PKCS#11 handles, indicating the key type.
Get Create (RefClass(cComChilkatSshKey)) To hoSshKey
If (Not(IsComObjectCreated(hoSshKey))) Begin
Send CreateComObject of hoSshKey
End
Get pvComObject of hoPkcs11 to vPkcs11
Get ComUsePkcs11 Of hoSshKey vPkcs11 iPrivKeyHandle iPubKeyHandle "rsa" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSshKey To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iPort
Get ComConnect Of hoSsh "ssh.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// Authentication uses the existing PKCS#11 session. The signing happens on the smart card --
// the private key never leaves the device.
Get pvComObject of hoSshKey to vSshKey
Get ComAuthenticatePk Of hoSsh "mySshLogin" vSshKey To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Public-key authentication successful."
// ... use the authenticated SSH session ...
Send ComDisconnect To hoSsh
Get ComLogout Of hoPkcs11 To iSuccess
Get ComCloseSession Of hoPkcs11 To iSuccess
End_Procedure