Configure Public Key Authentication for SFTP using a PKCS#11 Smartcard#

Public-key authentication with a smartcard allows you to connect to a remote server without a password. The private key is kept on the card and never leaves it. A PKCS#11 module, the native library shipped with the driver of your card, is used to read the public keys from the card and to sign the login with the corresponding private key protected by a PIN. This is the equivalent of ssh -I /path/to/module.so.

Important

  • Cyberduck 9.7.0 or later required

  • Mountain Duck 6.0.0 or later required

Note

Only keys with a matching certificate on the card are found. Cards holding a key without a certificate, such as a key generated with pkcs11-tool --keypairgen only, cannot be used.

Setup#

  1. Install the PKCS#11 module for your card. OpenSC supports many cards including PIV, CAC and OpenPGP cards and hardware tokens such as a YubiKey with the PIV application.

    brew install opensc
    

    Tip

    Cyberduck already bundles OpenSC and uses it by default. The installation is only required for the command line tools used in the following steps to read the public key from the card and to verify the setup with OpenSSH.

    Warning

    The module /usr/lib/ssh-keychain.dylib included with macOS for a smartcard paired with sc_auth pair can only be loaded by ssh and is not supported in Cyberduck and Mountain Duck.

  2. Verify the module can read the certificates from the card. Insert the card first.

    pkcs11-tool --module /opt/homebrew/lib/opensc-pkcs11.so --list-objects --type cert
    
  3. Print the public keys on the card and save them to a file.

    ssh-keygen -D /opt/homebrew/lib/opensc-pkcs11.so > ~/.ssh/smartcard.pub
    

    The command prints a line for every key on the card. Edit the file to only keep the line with the key to authenticate with.

  4. Add the public key to the authorized_keys in your ~/.ssh directory on the server running OpenSSH.

    ssh-copy-id -fi ~/.ssh/smartcard.pub user@remotehost
    
  5. Verify the setup with OpenSSH before connecting with Cyberduck. You are prompted for the PIN of the card.

    ssh -I /opt/homebrew/lib/opensc-pkcs11.so user@remotehost
    
  6. Add a new Bookmark in Cyberduck or Mountain Duck. Enter the alias from your OpenSSH configuration or the hostname in Server. You do not need to set a value for Password because the keys are read from the card.

  7. In SSH Private Key select the public key smartcard.pub saved in step 3 to only offer the matching key on the card to the server.

    Tip

    With no key selected, every key on the card is offered to the server in turn. The server may respond with Too many authentication failures when the card holds several keys.

  8. Connect to the server and enter the PIN of the card when prompted. The PIN is requested once and the card remains unlocked until you quit the application.

Use a Different Module#

The bundled OpenSC module is used by default on macOS. To use the module from the vendor of your card instead, refer to it in your OpenSSH configuration file ~/.ssh/config. On Windows, set the path of the module with the hidden configuration option as described above.

Host *
    PKCS11Provider /path/to/module.so

This configuration directive is supported by Cyberduck and Mountain Duck. You can restrict the setting to a single alias in the configuration file instead of matching it for all connections with *. Alternatively set the hidden configuration option connection.keystore.pkcs11.library to the absolute path of the module.

Limit Connections Using the Card#

With a card inserted, the keys on the card are attempted for every connection before a private key file or the prompt for a password. You are prompted for the PIN of the card including for servers you do not log in to with a key on the card, unless a password is saved for the bookmark or the login succeeds with an identity from the SSH agent.

  • Cancel the prompt for the PIN to skip the card. The login continues with the private key file selected in the bookmark or the prompt for a password.

  • Select a private key file for SSH Private Key in the Bookmark. The card is not used and there is no prompt for the PIN.

  • Set PKCS11Provider none for the host in ~/.ssh/config to not use the card.

  • Set IdentitiesOnly yes for the host in ~/.ssh/config. Only the key on the card matching the public key file set with IdentityFile is offered and the card is not used when IdentityFile is missing or set to a private key file.

    Host remotehost
        IdentityFile ~/.ssh/smartcard.pub
        IdentitiesOnly yes
    
  • Set the hidden configuration option ssh.authentication.pkcs11.enable to false to disable authentication with a smartcard for all connections.

Troubleshooting#

Warning

When no key from the card is offered, verify the card holds a certificate for the key with pkcs11-tool --module <module> --list-objects --type cert. A key without a certificate on the card is not found. When a public key file is selected in the bookmark, verify it matches a key on the card.

Warning

When the connection continues with a prompt for a password with no prompt for the PIN before, no card was found or the module could not be loaded. Verify the card is inserted and that a path set with PKCS11Provider works with ssh -I. On Windows, verify the path set with connection.keystore.pkcs11.library.

Note

Removing the card while connected fails the session. Reconnect with the card inserted.

References#