labunix's blog

labunixのラボUnix

debian bullseyeのsshで、Ed25519を使った公開鍵認証を設定する

■debian bullseyeのsshで、Ed25519を使った公開鍵認証を設定する

 今回はパスワード認証がコマンド実行確認の役割を果たしている一面もあるため、
 一部クライアント認証をパスワード認証のまま残すので、
 リモート側のsshd_configは変更しない

■対象となるクライアントもリモートも同じバージョン

$ lsb_release -d
Description:	Debian GNU/Linux 11 (bullseye)

■過去のssh証明書ログイン

 特定のホストからESXi(vSphere HyperVisor)にパスワードなしでログイン出来るようにする。
 https://labunix.hateblo.jp/entry/20180107/1515264031

■rsa以外の選択肢
 SSHではDSA鍵は1024ビットの物のみが正しい鍵なので、「dsa」オプションはレガシシステム用と考えて良い

 20211116日にはGitHubのDSAホストキーはサポートされなくなっている
 https://github.blog/security/application-security/improving-git-protocol-security-github/

$ ssh-keygen  --help 2>&1 | grep "\[-t"
                  [-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa]

■ハードウェアセキュリティキーの「ecdsa-sk | ed25519-sk」は今回使用しない
 「-sk」はSecurity Key、「FIDO」はFast Identity Online、「PIN」はPersonal Identification Number
 優先順としてはed25519-sk > ecdsa-sk

$ man ssh-keygen | lsec -sep "^     " "[a-z0-9]-sk" | awk '$0 !~ /^$/'
                [-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa] [-w provider]
             for ECDSA keys will fail.  ECDSA-SK, Ed25519 and Ed25519-SK keys have a fixed length and the -b flag will be ignored.
     -t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa
             Specifies the type of key to create.  The possible values are “dsa”, “ecdsa”, “ecdsa-sk”, “ed25519”, “ed25519-sk”, or “rsa”.
             option only makes sense for the FIDO authenticator algorithms ecdsa-sk and ed25519-sk.
             gorithms ecdsa-sk and ed25519-sk.  Currently PIN authentication is the only supported verification method, but other methods may be supported

$ w3m -dump https://docs.github.com/ja/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent | \
    lsec -sep " [0-9]." "ecdsa"
 3. 以下のテキストを貼り付け、例で使用されているメールを貴方のアカウントと関連
    付けられたメールアドレスGitHubに置き換えます。

    ssh-keygen -t ed25519-sk -C "your_email@example.com"

    ssh-keygen -t ed25519-sk -C "your_email@example.com"

    ssh-keygen -t ed25519-sk -C "your_email@example.com"

    注: コマンドが失敗し、エラー invalid format または feature not supported,
    を受け取る場合は、Ed25519 アルゴリズムをサポートしていないハードウェアセキ
    ュリティキーを使っている可能性があります。代わりに、次のコマンドを入力しま
    す。

     ssh-keygen -t ecdsa-sk -C "your_email@example.com"

■githubのデフォルトもed25519なので

$ w3m -dump https://docs.github.com/ja/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent | \
    lsec -sep " [0-9]." "Ed25519 アルゴリズムをサポートしていないレガシ"

 2. 以下のテキストを貼り付け、例で使用されているメールを GitHub メールアドレス
    に置き換えます。

    ssh-keygen -t ed25519 -C "your_email@example.com"

    注: Ed25519 アルゴリズムをサポートしていないレガシシステムを使っている場合
    は、以下を使います。

■ed25519は、エドワーズ曲線デジタル署名の実装の一つであり、ハッシュ関数としてSHA-512(SHA-2)を使い、曲線としてCurve25519を用いている
 RSAの3072ビット相当

$ w3m -dump https://dwango.github.io/articles/2024-02_security_level/ | lsec 鍵の扱いやすさ
RSA 暗号方式はオワコンですか? 今私は SSH の鍵として ssh-rsa の鍵を使っているの
ですが、新しく作り直した方がいいでしょうか?

いいえ。わざわざ新しく作り直す必要はありません。

しかし個人的には、もし鍵を新規で作るのであれば ssh-rsa ではなく ssh-ed25519 な
どの鍵を作ることを推奨します。その理由は以下の通りです。

  • 鍵の扱いやすさ: 128 ビットセキュリティーを達成するために、RSA だと 3072 ビ
    ットの鍵が必要ですが、ssh-ed25519 であれば 256 ビットで済みます。12 倍の差
    があります。(これは公開鍵の話であり、秘密鍵だとさらに差が広がります。)
  • 実装の簡単さ: RSA を安全に実装するのは難しいです。多くの実装は演算のために
    多倍長整数を使っていますが、これは Go 言語で問題とみなされ修正されました。
    最近も脆弱な実装が見つかりました。RSA はその単純さから多くの場所で教えられ
    ていますが、その有用性が過大評価されていると感じます。

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

 1. 実際には \(x^2\) の項があっても問題なく、そういう曲線も存在します。 ↩︎

 2. 厳密には Ed25519 は楕円曲線ではなく twisted Edwards curve とよばれる曲線を
    使っていますが、ほとんど楕円曲線と同値です。 ↩︎

■将来的にはEd448の方が良さそう。ファイルの圧縮・解凍を行うツールであるXZ Utilsにバックドアが仕込まれていた問題(CVE-2024-3094)で見かけた。

 https://www.dnsops.jp/event/20180627/20180627-ed25519.pdf
 https://github.com/amlweems/xzbot

■「NIST SP 800-186」では「Curve25519」「Edwards25519」として登場し、セキュリティ強度は128bit
 ※「Curve448, Edwards448」のキュリティ強度は224bit

 https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf

■セキュリティ強度128bitでRSA3072よりも計算コストの低い、Ed25519を選択することとした。
 ※加えて、githubやawsのデフォルトの鍵でもあるので。

$ ssh -V
OpenSSH_9.2p1 Debian-2+deb12u3, OpenSSL 3.0.13 30 Jan 2024

$ cd .ssh

■上書きされて困るようなバックアップ対象もないので

~/.ssh$ ls id* 2>/dev/null |  wc -l
0

■公開鍵と秘密鍵の作成

~/.ssh$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/labunix/.ssh/id_ed25519): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/labunix/.ssh/id_ed25519
Your public key has been saved in /home/labunix/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:MT44INO8F7J4IzdwIx2T13j3LsFOfJJFsTf+pS2DVOE labunix@ibm-debian
The key's randomart image is:
+--[ED25519 256]--+
|    o. o   .o..  |
|   +.oo o . .o . |
|  = O...o+ +. E  |
|   B * + oB o+ . |
|  o B + So =. . .|
|   + + . .o... +.|
|           .. + o|
|               o |
|                 |
+----[SHA256]-----+

~/.ssh$ ls id_ed25519*
id_ed25519  id_ed25519.pub

■リモート側に公開鍵をauthorized_keysとして追記登録(なければ作成)する

~/.ssh$ scp id_ed25519.pub labunix@remote:~/.ssh/id_ed25519-$(hostname).pub

~/.ssh$ cd ..
$ ssh -i id_ed25519.pub  remote
remote$ chmod 600 .ssh/id_ed25519-*.pub
remote$ ls -l .ssh/* | awk '/ed25519/{print $1}' 
-rw-------
remote$ cat .ssh/id_ed25519-*.pub >> .ssh/authorized_keys
remote$ exit

■毎回「-i」で指定するのは面倒なのでconfigを書く

$ cat .ssh/config 
Host remote
    HostName remote
    User labunix
    Port 22
    IdentityFile ~/.ssh/id_ed25519

■接続時のログ

$ ssh -v remote 2>&1 | grep -B 2 Authenticated
debug1: Offering public key: /home/labunix/.ssh/id_ed25519 ED25519 SHA256:MT44INO8F7XXXzdwIx2T13j3LsFOfJJFsTf+pS2DVOE explicit agent
debug1: Server accepts key: /home/labunix/.ssh/id_ed25519 ED25519 SHA256:MT44INO8F7XXXzdwIx2T13j3LsFOfJJFsTf+pS2DVOE explicit agent
Authenticated to remote ([10.0.0.1]:22) using "publickey".