Sample code for 30+ languages & platforms
Swift

SSH HSM Public Key Authentication

See more SSH Examples

Demonstrates SSH public-key authentication using a private key stored on an HSM — a USB token or smart card — accessed through PKCS#11. A session is opened with the vendor's driver, the key handles are located, and an SshKey object is bound to them with UsePkcs11.

Background: The point of an HSM is that the private key is generated on the device and cannot be exported: the signing operation happens on the hardware, so the key material never reaches your application's memory or disk. Even a fully compromised host cannot yield a copy of the key. PKCS#11 is the vendor-neutral interface to such devices, which is why the driver path and the object-finding template are the only vendor-specific parts of this example.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    //  Demonstrates SSH public-key authentication using a private key stored on an HSM (a USB token
    //  or smart card) accessed through PKCS#11.
    //  
    //  Note: Chilkat's PKCS#11 implementation runs on Windows, Linux, macOS, and other supported
    //  operating systems.

    let pkcs11 = CkoPkcs11()!

    //  The PKCS#11 driver supplied by your HSM vendor: a .dll on Windows, a .so on Linux, or a
    //  .dylib on macOS.
    pkcs11.sharedLibPath = "C:/Program Files (x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS1164.dll"

    //  The PIN should come from a secure source rather than being hard-coded.
    var pin: String? = "0000"

    //  Normal user = 1
    var userType: Int = 1

    success = pkcs11.quickSession(userType: userType, pin: pin)
    if success == false {
        print("\(pkcs11.lastErrorText!)")
        return
    }

    //  Describe the private key object to be located on the HSM.
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "class", value: "private_key")
    json.updateString(jsonPath: "label", value: "MySshKey")

    var priv_handle: UInt32 = pkcs11.find(jsonTemplate: json)
    if priv_handle == 0 {
        print("\(pkcs11.lastErrorText!)")
        return
    }

    //  Find the corresponding public key by changing the class in the same template.
    json.updateString(jsonPath: "class", value: "public_key")

    var pub_handle: UInt32 = pkcs11.find(jsonTemplate: json)
    if pub_handle == 0 {
        print("\(pkcs11.lastErrorText!)")
        return
    }

    //  Create an SSH key object that uses the HSM handles.  The key type may be "rsa" or "ec".
    let key = CkoSshKey()!
    var keyType: String? = "rsa"
    success = key.usePkcs11(session: pkcs11, privKeyHandle: priv_handle, pubKeyHandle: pub_handle, keyType: keyType)
    if success == false {
        print("\(key.lastErrorText!)")
        return
    }

    let ssh = CkoSsh()!

    var port: Int = 22
    success = ssh.connect(hostname: "ssh.example.com", port: port)
    if success == false {
        print("\(ssh.lastErrorText!)")
        return
    }

    //  The corresponding public key must already be installed on the SSH server for the account.
    //  The signing operation happens on the HSM -- the private key never leaves the device.
    success = ssh.authenticatePk(username: "mySshLogin", privateKey: key)
    if success == false {
        print("\(ssh.lastErrorText!)")
        return
    }

    print("Public-key authentication successful.")

    ssh.disconnect()

    pkcs11.logout()
    pkcs11.closeSession()

}