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.comandsk-ssh-ed25519@openssh.com.
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#
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 bioTip
-k p-256-necreates a non-extractable key on the NIST P-256 curve, the only curve supported by the Secure Enclave.-t biorequires Touch ID to use the key.Write the key file for the credential. Run the command in
~/.sshas the files are written to the current working directory.cd ~/.ssh && ssh-keygen -w /usr/lib/ssh-keychain.dylib -KNote
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_rkandid_ecdsa_sk_rk.pub. Repeat the command with-Kwhenever you need the files again on another Mac with access to the same identity.Add the public key to the
authorized_keysin your~/.sshdirectory on the server running OpenSSH.ssh-copy-id -fi ~/.ssh/id_ecdsa_sk_rk.pub user@remotehostVerify 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@remotehostTip
Set the provider in
~/.ssh/configto omit the-ooption and the-ioption.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.dylibby default on macOS and do not require this directive.In the Bookmark or Connection window, in SSH Private Key select
id_ecdsa_sk_rkin your~/.sshdirectory. You do not need to set a value for Password.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#
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 residentTip
Use
-t ecdsa-skinstead for tokens without support for Ed25519 credentials, such as a YubiKey with a firmware version earlier than 5.2.3. Add-O verify-requiredto require the PIN of the token in addition to touching it. The option-O residentstores the credential on the token allowing to write the key files again on another machine withssh-keygen -K.Note
ssh-keygentalks 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.Add the public key to the
authorized_keysin your~/.sshdirectory on the server running OpenSSH.ssh-copy-id -fi ~/.ssh/id_ed25519_sk.pub user@remotehostRefer to the middleware library for the token in your OpenSSH configuration file
~/.ssh/config. The default/usr/lib/ssh-keychain.dylibset 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 optionssh.authentication.securitykey.providerto 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.
In the Bookmark or Connection window, in SSH Private Key select
id_ed25519_skin your~/.sshdirectory.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.
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_rkVerify the agent holds the key. The key type is printed as
ECDSA-SKorED25519-SK.ssh-add -lAdd 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.pubfor 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#
Cyberduck 9.7.0 or later is required for authentication with a security key without an SSH agent.