Configure Public Key Authentication for SFTP using a FIDO2 Security Key#

Public-key authentication with a FIDO2 security key allows you to connect to a remote server without a password. The private key never leaves the authenticator, which is either the Secure Enclave of your Mac unlocked with Touch ID or a hardware token such as a YubiKey you touch to confirm. OpenSSH calls these keys sk-ecdsa-sha2-nistp256@openssh.com and sk-ssh-ed25519@openssh.com.

Important

  • Cyberduck 9.7.0 or later required

  • Mountain Duck 6.0.0 or later required

The key file in ~/.ssh holds a reference to the credential on the authenticator but no private key. Signing a login is therefore delegated either to a security key provider library talking to the authenticator or to an SSH agent holding the key.

Note

A security key provider library is configured by default on macOS only, serving keys in the Secure Enclave. On Windows and Linux OpenSSH talks to a token with built-in USB HID support instead of a library, which Cyberduck cannot load. Leave the key to an SSH agent there, or set SecurityKeyProvider to a middleware library for your token on Linux. Loading a middleware library is not supported on Windows.

Important

The server must run OpenSSH 8.2 or later. Earlier versions do not know the sk-* key types and ignore the entry in authorized_keys. Verify the version of the server with ssh -v user@remotehost and look for the line remote software version.

Create a Key in the Secure Enclave unlocked with Touch ID#

  1. Create an identity in the Secure Enclave protected with Touch ID. The private key is generated on the device and cannot be extracted.

    sc_auth create-ctk-identity -l "MacBook Touch ID SSH" -k p-256-ne -t bio
    

    Tip

    -k p-256-ne creates a non-extractable key on the NIST P-256 curve, the only curve supported by the Secure Enclave. -t bio requires Touch ID to use the key.

  2. Write the key file for the credential. Run the command in ~/.ssh as the files are written to the current working directory.

    cd ~/.ssh && ssh-keygen -w /usr/lib/ssh-keychain.dylib -K
    

    Note

    When prompted with Enter PIN for authenticator:, press Enter to leave the PIN empty. A key in the Secure Enclave has no PIN and is protected by Touch ID instead. The prompt is shown by OpenSSH for any authenticator.

    This writes id_ecdsa_sk_rk and id_ecdsa_sk_rk.pub. Repeat the command with -K whenever you need the files again on another Mac with access to the same identity.

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

    ssh-copy-id -fi ~/.ssh/id_ecdsa_sk_rk.pub user@remotehost
    
  4. Verify the setup with OpenSSH before connecting with Cyberduck. Confirm the prompt with Touch ID.

    ssh -o SecurityKeyProvider=/usr/lib/ssh-keychain.dylib -i ~/.ssh/id_ecdsa_sk_rk user@remotehost
    

    Tip

    Set the provider in ~/.ssh/config to omit the -o option and the -i option.

    Host remotehost
        User user
        IdentityFile ~/.ssh/id_ecdsa_sk_rk
        SecurityKeyProvider /usr/lib/ssh-keychain.dylib
    

    Cyberduck and Mountain Duck use /usr/lib/ssh-keychain.dylib by default on macOS and do not require this directive.

  5. In the Bookmark or Connection window, in SSH Private Key select id_ecdsa_sk_rk in your ~/.ssh directory. You do not need to set a value for Password.

  6. Connect to the server and confirm the prompt with Touch ID to allow the Secure Enclave to sign the login.

Create a Key on a Hardware Token such as YubiKey#

  1. Insert the security key and create a credential on the token. Touch the token when it blinks.

    cd ~/.ssh && ssh-keygen -t ed25519-sk -O resident
    

    Tip

    Use -t ecdsa-sk instead for tokens without support for Ed25519 credentials, such as a YubiKey with a firmware version earlier than 5.2.3. Add -O verify-required to require the PIN of the token in addition to touching it. The option -O resident stores the credential on the token allowing to write the key files again on another machine with ssh-keygen -K.

    Note

    ssh-keygen talks to the token with the USB HID support built into OpenSSH. No middleware library is required to create the key. Pass -w <library> only to use a middleware library of your own instead.

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

    ssh-copy-id -fi ~/.ssh/id_ed25519_sk.pub user@remotehost
    
  3. Refer to the middleware library for the token in your OpenSSH configuration file ~/.ssh/config. The default /usr/lib/ssh-keychain.dylib set on macOS serves keys in the Secure Enclave only.

    Host *
        SecurityKeyProvider /path/to/middleware.so
    

    This configuration directive is supported by Cyberduck and Mountain Duck. A value beginning with $ is read from the environment variable of that name. Alternatively set the hidden configuration option ssh.authentication.securitykey.provider to the path of the library.

    Important

    The USB HID support built into OpenSSH is not a library and cannot be loaded. Unless the token comes with a middleware library implementing the OpenSSH security key API, leave the security key to an SSH agent instead, which works with any token supported by OpenSSH.

  4. In the Bookmark or Connection window, in SSH Private Key select id_ed25519_sk in your ~/.ssh directory.

  5. Connect to the server and touch the security key when it blinks. Enter the PIN of the token when prompted for a key created with -O verify-required.

Let an SSH Agent handle the Security Key#

Instead of talking to the authenticator, Cyberduck and Mountain Duck can leave the security key to an SSH agent. The agent prompts for the touch or the PIN and returns the signature.

  1. Add the security key to the SSH agent of OpenSSH. You are asked to confirm with Touch ID or to touch the token.

    ssh-add ~/.ssh/id_ecdsa_sk_rk
    
  2. Verify the agent holds the key. The key type is printed as ECDSA-SK or ED25519-SK.

    ssh-add -l
    
  3. Add a new Bookmark. No SSH Private Key needs to be selected because the key is offered by the agent.

    Tip

    The server may respond with Too many authentication failures when the agent holds many keys. Select the public key id_ecdsa_sk_rk.pub for SSH Private Key to only offer the matching identity from the agent.

Other SSH agents manage keys of their own instead of a credential on a security key, with the approval prompt of the application taking the place of the touch. Refer to the tutorials for the 1Password SSH Agent, the Bitwarden SSH Agent and yubikey-agent for the setup of the agent socket with IdentityAgent in ~/.ssh/config.

Troubleshooting#

Warning

When the connection fails with the message No security key provider configured, no middleware library is set for your platform. A library is configured by default on macOS only. Set SecurityKeyProvider in ~/.ssh/config or add the key to an SSH agent instead. A failure naming the library instead means it could not be loaded, in which case verify the path.

Warning

When the connection fails with the message Security key authentication is not supported on Windows, the security key selected in the bookmark is not held by an SSH agent. Loading a middleware library is not supported on Windows. Add the key to an SSH agent instead.

Warning

When the connection fails with the message No security key found, the hardware token holding the key is not connected. Insert the token and connect again.

Warning

When the server refuses the key without prompting for a touch, the public key is not accepted. Verify the entry in authorized_keys and that the server runs OpenSSH 8.2 or later.

References#