
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>YubiKey | monolog</title>
	<atom:link href="https://blog.monora.me/tag/yubikey/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.monora.me</link>
	<description>monora log #=&#62; ものろーぐ</description>
	<lastBuildDate>Sun, 24 May 2020 19:06:13 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.1</generator>
<site xmlns="com-wordpress:feed-additions:1">21279848</site>	<item>
		<title>YubiKey 5C を買ったので ECDSA鍵で ssh した</title>
		<link>https://blog.monora.me/2019/04/bought-yubikey-5c-and-ssh-using-ecdsa-key/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bought-yubikey-5c-and-ssh-using-ecdsa-key</link>
		
		<dc:creator><![CDATA[きょんたん]]></dc:creator>
		<pubDate>Thu, 18 Apr 2019 16:38:43 +0000</pubDate>
				<category><![CDATA[Infrastructure]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[macOS]]></category>
		<category><![CDATA[OpenSSH]]></category>
		<category><![CDATA[YubiKey]]></category>
		<guid isPermaLink="false">https://blog.monora.me/?p=1944</guid>

					<description><![CDATA[<p>こんにちは。唐突に YubiKey が欲しくなったので買いました。こんなことをやっている場合ではない…… 正確には、Amazon.co.jp を見たら異常に高くてそりゃ転売したら儲かるな、という気持ちになったので、適当に [&#8230;]</p>
The post <a href="https://blog.monora.me/2019/04/bought-yubikey-5c-and-ssh-using-ecdsa-key/">YubiKey 5C を買ったので ECDSA鍵で ssh した</a> first appeared on <a href="https://blog.monora.me">monolog</a>.]]></description>
										<content:encoded><![CDATA[<p>こんにちは。唐突に YubiKey が欲しくなったので買いました。こんなことをやっている場合ではない……</p>
<p>正確には、Amazon.co.jp を見たら異常に高くてそりゃ転売したら儲かるな、という気持ちになったので、適当に人を募って Amazon.com (US) で共同購入しました。<br />
関税や送料を足した結果、YubiKey 5 NFC が5500円, YubiKey 5C が6100円ぐらいで買えました。良かったですね。</p>
<p>YubiKey といえばそもそも OTP が出てくるキーボードとして認識されるデバイスですが、最近だと WebAuthn で使えたりしますね。あとは PKCS#11 の署名用や適当な鍵を登録できたりします。</p>
<p>雑に手元の macOS でssh するぞ、と思ったら地味にハマってしまったのでいろいろやった結果動くようになったのでメモ</p>
<p>手順だけ分かればおっけー！という方は Gist にパッチ等まとめたので、そちらを参照してください: <a href="https://gist.github.com/kyontan/763952e7be68a2e96d5c3f0ad0d3bce8">https://gist.github.com/kyontan/763952e7be68a2e96d5c3f0ad0d3bce8</a></p>
<p><span id="more-1944"></span></p>
<p>検証環境のバージョンは macOS Mojave 10.14.4 で、OpenSSH は 7.9p1, LibreSSL 2.7.3 が入っていました。<br />
ただ、今回は最終的に OpenSSH と OpenSSL は自前でビルドしたのであまり関係ありません。<br />
より重要そうな Homebrew でインストールしたパッケージのバージョンは、 OpenSSH 7.9p1, OpenSSL 1.0.2r (26 Feb 2019), OpenSC 0.19.0 です。</p>
<p>まず、下記の記事などを参考に鍵の生成を試みます。</p>
<blockquote><p>Putty CAC で SSH に YubiKey を使う(OpenSC編) &#8211; enjoy struggling<br />
<a href="https://blog.haniyama.com/2018/02/02/yubikey-ssh-opensc/"><span style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">https://blog.haniyama.com/2018/02/02/yubikey-ssh-opensc/</span></a></p></blockquote>
<p>GUI のツール (YubiKey PIV Manager) の場合は PIN を設定すると自動的に Authentication と Key Management 用の鍵を生成してくれます。現代だとデフォルトで <code>ecdsa-sha2-nistp256</code> の鍵が生成されるんですね。便利。</p>
<h2>公開鍵を取り出そうとした</h2>
<p>YubiKey へのアクセスや署名(でよいのでしょうか?) にはスマートカード等で使う OpenSC を使用するようです。今回は Homebrew でインストールしました。 (<code>brew install opensc</code>)</p>
<p>早速試してみます。まずは上の</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shellscript" data-enlighter-linenumbers="false"># ssh-keygen -D /usr/local/Cellar/opensc/0.19.0/lib/opensc-pkcs11.so
C_GetAttributeValue failed: 18
C_GetAttributeValue failed: 18
cannot read public key from pkcs11</pre>
<p>うまく行きませんね。(上手く行った場合はこの記事を読む必要はありません。最後のステップまで飛ばしてください。</p>
<h2>パッチを当てる</h2>
<p>エラー文でググると偉大な先達の記事が出てきます。今回この記事がなかったら即死していたでしょう。</p>
<blockquote><p>ssh with yubikey ECDSA keys &#8211; lithium03の物置<br />
<a href="https://lithium03.info/yubikey/index.html">https://lithium03.info/yubikey/index.html</a></p></blockquote>
<p>この記事を読むと、「RSA鍵なら問題なくいける」「OpenSSH が OpenSC を使うときに (具体的には PKCS#11 の署名等で) ECDSA鍵に対応できてない」ということが分かります。</p>
<p>親切にパッチや、そのパッチの元になったチケットまでリンクがあります。</p>
<blockquote><p>2474 – Enabling ECDSA in PKCS#11 support for ssh-agent<br />
<a href="https://bugzilla.mindrot.org/show_bug.cgi?id=2474">https://bugzilla.mindrot.org/show_bug.cgi?id=2474</a></p></blockquote>
<p>このチケットを見ると、Fedora 28 では既にこのパッチがバックポートされていること、upstream には OpenSSH 8.0 でマージされる予定であることが分かります。</p>
<p>ちなみにパッチは OpenSSH 7.6p1 用で、上の記事では 7.8p1 用に書き換えられたものが公開されていますが、現時点の Homebrew でインストール可能なのは 7.9p1 ということで、パッチを修正しました。</p>
<p>というわけでパッチです: <a href="https://gist.github.com/kyontan/763952e7be68a2e96d5c3f0ad0d3bce8">https://gist.github.com/kyontan/763952e7be68a2e96d5c3f0ad0d3bce8</a></p>
<h2>ビルドする</h2>
<p>Homebrew でパッチとかどうやるんだ……と思いましたが、どうやら <code>brew edit openssh</code> で行けるみたいです。こんなに簡単に当てられるなんて……便利だ……</p>
<blockquote><p>Homebrew: Patching an existing package<br />
<a href="https://www.ralfebert.de/snippets/brew-apply-patch-to-package-formula/">https://www.ralfebert.de/snippets/brew-apply-patch-to-package-formula/</a></p></blockquote>
<p>というわけで雑に既に書かれている <code>patch</code> の下に、以下のように追記してやるとパッチが当たります。</p>
<pre class="EnlighterJSRAW" data-enlighter-language="ruby" data-enlighter-linenumbers="false"># Add support ECDSA for PKCS11, ref: https://bugzilla.mindrot.org/show_bug.cgi?id=2474
patch do
  url "https://gist.githubusercontent.com/kyontan/763952e7be68a2e96d5c3f0ad0d3bce8/raw/2ad5f854eba7704fbb7af2182ac57ee82b9a8f61/openssh-7.9p1-pkcs11-ecdsa.patch"
  sha256 "93b4e48321db94d785833a9414b12467a4741156cec90fa3052a4092acec8938"
end</pre>
<p>&nbsp;</p>
<p>インストールは <code>brew install --build-from-source openssh</code> です。既にインストールしてあるものがある人は <code>install</code> を <code>reinstall</code> に読み替えてください。</p>
<p>ビルドできたら、ビルドした ssh や ssh-keygen のパスをよしなに通してやります。勝手に通ってるかもしれません。(<code>rehash</code> なりしてから <code>ssh -V</code> の結果を見るとかすれば分かると思います)</p>
<p>うおおおおお！！！！</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shellscript" data-enlighter-linenumbers="false"># ssh-keygen -D /usr/local/Cellar/opensc/{version}/lib/opensc-pkcs11.so
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDnIbZ4ANu...
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBv0e8HKnx...
</pre>
<p>というわけで公開鍵が見られました。2つ見えたのは、上で自動的に作られると行っていた Authentication と Key Management 用の鍵がどちらも見えてるからだと思います。普通は上の方を使えば良いはずです。</p>
<p>鍵を登録して、<code>ssh</code> していきます。 <code>ssh -I /usr/local/Cellar/opensc/{version}/lib/opensc-pkcs11.so host</code> です。</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shellscript" data-enlighter-linenumbers="false"># ssh -v -I /usr/local/Cellar/opensc/0.19.0/lib/opensc-pkcs11.so user@xxx.monora.me
OpenSSH_7.9p1, OpenSSL 1.0.2r  26 Feb 2019
...
debug1: Connection established.
debug1: provider /.../opensc-pkcs11.so: manufacturerID &lt;OpenSC Project&gt; cryptokiVersion 2.20 libraryDescription &lt;OpenSC smartcard framework&gt; libraryVersion 0.19
debug1: provider /.../opensc-pkcs11.so slot 0: label &lt;Yubico PIV Authentication&gt; manufacturerID &lt;piv_II&gt; model &lt;PKCS#15 emulate&gt; serial &lt;...&gt; flags 0x40d
debug1: have 1 keys
debug1: have 2 keys
...
debug1: Will attempt key: /.../opensc-pkcs11.so ECDSA SHA256:nr/MvmFI6cYmChV97dMVIBeE2Uq5UmhVvoIdIuayLvg token
debug1: Will attempt key: /.../opensc-pkcs11.so ECDSA SHA256:Z7dluZC4FCEsUAC0HYiITr5+I83bLtHUxOwdla81Rtc token
...
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /.../opensc-pkcs11.so ECDSA SHA256:nr/MvmFI6cYmChV97dMVIBeE2Uq5UmhVvoIdIuayLvg token
debug1: Server accepts key: /.../opensc-pkcs11.so ECDSA SHA256:nr/MvmFI6cYmChV97dMVIBeE2Uq5UmhVvoIdIuayLvg token
Enter PIN for 'Yubico PIV Authentication':
debug1: Authentication succeeded (publickey).
Authenticated to xxx.monora.me ([xxx.yyy.zzz.www]:22).
...
# sl
...
</pre>
<p>というわけで ssh 出来ました。嬉しいですね。</p>
<p>パッチを見て当ててみたらエラーが出た瞬間にやる気をなくして、うだうだやっていたら3時間ぐらい掛かってしまいましたが、なんとか動いてよかったです。</p>
<p>それでは皆さまもハッピーYubiKeyライフをお過ごしください！</p>The post <a href="https://blog.monora.me/2019/04/bought-yubikey-5c-and-ssh-using-ecdsa-key/">YubiKey 5C を買ったので ECDSA鍵で ssh した</a> first appeared on <a href="https://blog.monora.me">monolog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1944</post-id>	</item>
	</channel>
</rss>
