macOS を Ventura にアップデートした後に SSH 接続が失敗する問題

macOSSSH

手元のマシンの macOS のバージョンを Ventura ( 13.2 )にあげてから SSH が接続できなくなりました。

問題

ひとつ前の Monterey で成功していた SSH 接続が Ventura にあげた後から失敗するようになりました。

ssh user@example.com
user@example.com: Permission denied (publickey).

-v オプションを付けたときのメッセージの一部:

debug1: Offering public key: /Users/<user>/.ssh/id_rsa RSA xxx explicit agent
debug1: send_pubkey_test: no mutual signature algorithm
debug1: No more authentication methods to try.

環境

❯ sw_vers
ProductName:    macOS
ProductVersion:   13.2.1
BuildVersion:   22D68
ssh -V
OpenSSH_9.0p1, LibreSSL 3.3.6

原因

原因は Ventura で OpenSSH ( ssh コマンド)のバージョンが 9.0 にあがったことです。 具体的には、 OpenSSH 8.8 から SHA-1 を使った RSA 署名がデフォルトで無効になったため、オプションを付けないと使えなくなりました。

OpenSSH 8.8 のリリースノートの説明:

Potentially-incompatible changes
================================

This release disables RSA signatures using the SHA-1 hash algorithm
by default. This change has been made as the SHA-1 hash algorithm is
cryptographically broken, and it is possible to create chosen-prefix
hash collisions for <USD$50K [1]

For most users, this change should be invisible and there is
no need to replace ssh-rsa keys. OpenSSH has supported RFC8332
RSA/SHA-256/512 signatures since release 7.2 and existing ssh-rsa keys
will automatically use the stronger algorithm where possible.

Incompatibility is more likely when connecting to older SSH
implementations that have not been upgraded or have not closely tracked
improvements in the SSH protocol. For these cases, it may be necessary
to selectively re-enable RSA/SHA1 to allow connection and/or user
authentication via the HostkeyAlgorithms and PubkeyAcceptedAlgorithms
options. For example, the following stanza in ~/.ssh/config will enable
RSA/SHA1 for host and user authentication for a single destination host:

    Host old-host
        HostkeyAlgorithms +ssh-rsa
        PubkeyAcceptedAlgorithms +ssh-rsa

We recommend enabling RSA/SHA1 only as a stopgap measure until legacy
implementations can be upgraded or reconfigured with another key type
(such as ECDSA or Ed25519).

[1] "SHA-1 is a Shambles: First Chosen-Prefix Collision on SHA-1 and
    Application to the PGP Web of Trust" Leurent, G and Peyrin, T
    (2020) https://eprint.iacr.org/2020/014.pdf

OpenSSH: Release Notes

ポイント:

  • OpenSSH 8.8 で SHA-1 を使用した RSA 署名がデフォルトで無効になった
  • 変更の理由は SHA-1 が暗号学的にダメになったから
  • ほとんどの場合この変更のユーザーへの影響は無いが、 SSH が古いサーバーに接続するときは RSA/SHA1 を再度有効にする必要がある
  • RSA/SHA1 を有効にするにはオプション HostkeyAlgorithmsPubkeyAcceptedAlgorithms を使う必要がある

対処方法

大きく 2 通りの方法があります。

  • A) 設定で SHA-1 ハッシュアルゴリズムを有効にする
  • B) 別のアルゴリズムに切り替える

A) 設定で SHA-1 ハッシュアルゴリズムを有効にする

オプション PubkeyAcceptedAlgorithmsssh-rsa にセットすれば SHA-1 ハッシュアルゴリズムを使い続けられます。

一度かぎりならコマンドのオプション -o で指定できますが、繰り返し使用するなら設定ファイルに設定するのがよいです。

コマンドで設定する:

ssh -i ~/.ssh/id_rsa user@example.com -o 'PubkeyAcceptedAlgorithms=ssh-rsa'

設定ファイルで設定する:

~/.ssh/config:

Host example.com
  User user
  PubkeyAcceptedAlgorithms ssh-rsa

リリースノートでは HostkeyAlgorithms にも言及されていますが、私の環境では HostkeyAlgorithms はセットしなくても問題が解消しました。

ちなみに、この問題についてウェブ上で検索すると、次 ↓ のように Host * を使ってすべてのホストに対して設定してある例を見かけますが、こうするよりも問題が起こるホスト・サーバーに対して個別に設定するのがよいと思います。

Host *
  PubkeyAcceptedAlgorithms +ssh-rsa

B) 別のアルゴリズムに切り替える

ED25519 など別のアルゴリズムに移行する手もあります。

A) よりも B) の方が安全なので、 B) が選べる状況なら B) にすべきと思います。

参考


アバター
後藤隼人 ( ごとうはやと )

ソフトウェア開発やマーケティング支援などをしています。詳しくはこちら