macOS 上基于原生安全隔离区 (Secure Enclave) 的 SSH 密钥
Native Secure Enclave backed SSH keys on macOS

原始链接: https://gist.github.com/arianvp/5f59f1783e3eaf1a2d4cd8e952bb4acf

## MacOS 安全隔离区 SSH 密钥 近期的 MacOS 版本现在原生支持生成和使用由安全隔离区支持的 SSH 密钥,无需再使用像 `secretive` 这样的第三方工具。这是通过 `/usr/lib/ssh-keychain.dylib` 库实现的,该库现在实现了 `SecurityKeyProvider` – 与 YubiKey 等 FIDO2 设备使用的相同接口。 您可以使用 `sc_auth create-ctk-identity` 创建生物识别保护的密钥,并使用 `sc_auth list-ctk-identities` 和 `sc_auth delete-ctk-identity` 进行管理。密钥可以使用 `ssh-keygen -w /usr/lib/ssh-keychain.dylib -K -N ""` “下载”(实际上是创建了对隔离区凭据的引用)。 为了简化使用,请在您的 shell 配置文件中设置环境变量 `SSH_SK_PROVIDER=/usr/lib/ssh-keychain.dylib`。这将允许标准的 SSH 命令,如 `ssh-add -K` 和 `ssh-keygen -K` 无缝地利用安全隔离区进行密钥存储和身份验证。使用此方法可以提高安全性,将您的 SSH 密钥保存在 Mac 的硬件安全中。

最近的 Hacker News 讨论集中在一个新的方法上,该方法利用 macOS 的原生安全隔离区来备份 SSH 密钥,详情见 Gist。这一进展为 Secretive 等第三方解决方案和物理安全密钥(YubiKey)提供了一种由 Apple 构建的替代方案。 一些用户称赞这种方法带来的便利性和安全性,而另一些用户则指出 Secretive 等工具已经存在一段时间了,它提供了详细的密钥使用日志。有些人觉得 Secretive 在多台机器上使用起来很麻烦,因此转向 1Password 等密钥管理工具。 主要优点是避免了安装和信任第三方软件带来的摩擦和安全问题。然而,一位评论员指出,潜在的复杂性可能会 Ironically 导致用户回到不太安全的做法,例如以明文形式存储密钥。
相关文章

原文

It turns out that MacOS Tahoe can generate and use secure-enclave backed SSH keys! This replaces projects like https://github.com/maxgoedjen/secretive

There is a shared library /usr/lib/ssh-keychain.dylib that traditionally has been used to add smartcard support to ssh by implementing PKCS11Provider interface. However since recently it also implements SecurityKeyProivder which supports loading keys directly from the secure enclave! SecurityKeyProvider is what is normally used to talk to FIDO2 devices (e.g. libfido2 can be used to talk to your Yubikey). However you can now use it to talk to your Secure Enclave instead!

recording.mov

See man sc_auth and man ssh-keychain for all the options

To create a Secure Enclave backed key that requires biometrics, run the following command and press TouchID:

% sc_auth create-ctk-identity -l ssh -k p-256-ne -t bio

You can confirm that the key was create with the list-ctk-identities command:

arian@Mac ssh-keychain % sc_auth  list-ctk-identities       
Key Type Public Key Hash                          Prot Label Common Name Email Address Valid To        Valid 
p-256-ne A71277F0BC5825A7B3576D014F31282A866EF3BC bio  ssh   ssh                       23.11.26, 17:09 YES

It also supports listing the ssh key fingerprints instead:

% sc_auth  list-ctk-identities -t ssh
Key Type Public Key Hash                                    Prot Label Common Name Email Address Valid To        Valid 
p-256-ne SHA256:vs4ByYo+T9M3V8iiDYONMSvx2k5Fj2ujVBWt1j6yzis bio  ssh   ssh                       23.11.26, 17:09 YES 

Keys can be deleted with

% sc_auth delete-ctk-identity -h <Public Key Hash>

You can "download" the public / private keypair from the secure enclave using the following command:

% ssh-keygen -w /usr/lib/ssh-keychain.dylib -K -N ""
Enter PIN for authenticator: 
You may need to touch your authenticator to authorize key download.
Saved ECDSA-SK key to id_ecdsa_sk_rk
% cat id_ecdsa_sk_rk.pub 
[email protected] AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBKiHAiAZhcsZ95n85dkNGs9GnbDt0aNOia2gnuknYV2wKL3y0u+d3QrE9cFkmWXIymHZMglL+uJA+6mShY8SeykAAAAEc3NoOg== ssh:

You can just use the empty string for PIN. For some reason openssh always asks for it even if the authenticator in question does not use a PIN but a biometric. Note that the "private" key here is just a reference to the FIDO credential. It does not contain any secret key material. Hence I'm specifiyng -N "" to skip an encryption passphrase.

Now if you copy this public key to your authorized keys file, it should work!

% ssh-copy-id -i id_ecdsa_sk_rk localhost
% ssh -o SecurityKeyProvider=/usr/lib/ssh-keychain.dylib localhost

Instead of downloading the public/private keypair to a file you can also directly make the keys available to ssh-agent. For this you can use the following command:

% ssh-add -K -S /usr/lib/ssh-keychain.dylib
Enter PIN for authenticator: 
Resident identity added: ECDSA-SK SHA256:vs4ByYo+T9M3V8iiDYONMSvx2k5Fj2ujVBWt1j6yzis
% ssh-add -L
[email protected] AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBKiHAiAZhcsZ95n85dkNGs9GnbDt0aNOia2gnuknYV2wKL3y0u+d3QrE9cFkmWXIymHZMglL+uJA+6mShY8SeykAAAAEc3NoOg== 
% ssh-copy-id localhost
% ssh -o SecurityKeyProvider=/usr/lib/ssh-keychain.dylib localhost

Using the SecurityKeyProvider by default

SecurityKeyProvider can be configured in .ssh/config but I recommend setting export SSH_SK_PROVIDER=/usr/lib/ssh-keychain.dylib in your .zprofile instead as that environment variable gets picked up by ssh, ssh-add and ssh-keygen.

This means you can just do:

ssh-add -K
ssh my-server

or

ssh-keygen -K
ssh -i id_ecdsa_rk_sk my-server

to ssh into your server

联系我们 contact @ memedata.com