LoginSignup
4

More than 3 years have passed since last update.

posted at

updated at

MacOS で opensc-pkcs11.so がうまくいかないとき

MacOS を再インストールして Yubikey が使えなくなってハマったのでメモ

brew で opensc をインストール

brew install opensc

opensc をインストールして Yubikey を読み込みSSHをしようとして下記のようなエラーがでた

# ssh-agent に登録
$ ssh-add -s /usr/local/lib/opensc-pkcs11.so
Enter passphrase for PKCS#11:
Could not add card "/usr/local/lib/opensc-pkcs11.so": agent refused operation

これは、 pkcs モジュールの読み込み先ホワイトリストに入ってないから拒否されているようだ。
下記の手順で無事に使えるようになったので残しとく

手順

  • 再起動して
  • Ctrl + R でリカバリーモードにする
  • 上の方のメニューバーより「Utilitys」=> 「Terminal」を起動
  • コマンドを実行

    csrutil disable
    
    
  • 再起動

  • ファイルを編集
    sudo nano /System/Library/LaunchAgents/com.openssh.ssh-agent.plist

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
            <key>Label</key>
            <string>com.openssh.ssh-agent</string>
            <key>ProgramArguments</key>
            <array>
                    <string>/usr/bin/ssh-agent</string>
                    <string>-l</string>
                    <string>-P</string>
                    <string>/usr/lib/*,/usr/local/lib/*,/usr/local/Cellar/opensc/*/lib/*</string>
            </array>
            <key>Sockets</key>
            <dict>
                    <key>Listeners</key>
                    <dict>
                            <key>SecureSocketWithKey</key>
                            <string>SSH_AUTH_SOCK</string>
                    </dict>
            </dict>
            <key>EnableTransactions</key>
            <true/>
    </dict>
    </plist>
    
    

ここの部分を追記した

                    <string>-P</string>
                    <string>/usr/lib/*,/usr/local/lib/*,/usr/local/Cellar/opensc/*/lib/*</string>
  • 再起動
  • ssh-add してちゃんと認識するか確認する

確認後

  • 再起動
  • Ctrl + R でリカバリーモードにする
  • 上の方のメニューバーより「Utilitys」=> 「Terminal」を起動
  • コマンドを実行

    csrutil enable
    
    
  • 完了

別のやり方。

上記のやり方では、 MacOS を更新するたびに実施する必要があって、なかなかしんどい思いをしてたので別解を探したら下記が見つかった。

こちらのほうが、良さそうなので追記

https://gist.github.com/gerbsen/5fd8aa0fde87ac7a2cae を参考に改造したらよい感じになりました。

##- -----------------------------------------------------------------
##- ssh-agent
##- -----------------------------------------------------------------
# Ref: https://gist.github.com/gerbsen/5fd8aa0fde87ac7a2cae
setenv SSH_ENV $HOME/.ssh/environment

function start_agent
    echo "Initializing new SSH agent ..."
    ssh-agent -c -P "/usr/lib/*,/usr/local/lib/*,/usr/local/Cellar/opensc/*/lib/*" | sed 's/^echo/#echo/' > $SSH_ENV
    echo "succeeded"
    chmod 600 $SSH_ENV
    . $SSH_ENV > /dev/null
    ssh-add -s /usr/local/lib/opensc-pkcs11.so
end

function test_identities
    ssh-add -l | grep "The agent has no identities" > /dev/null
    if [ $status -eq 0 ]
        ssh-add
        if [ $status -eq 2 ]
            start_agent
        end
    end
end

if [ -n "$SSH_AGENT_PID" ]
    ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
    if [ $status -eq 0 ]
        test_identities
    end
else
    if [ -f $SSH_ENV ]
        . $SSH_ENV > /dev/null
    end
    ps -ef | grep $SSH_AGENT_PID | grep -v grep | grep ssh-agent > /dev/null
    if [ $status -eq 0 ]
        test_identities
    else
        start_agent
    end
end

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
4