前人未踏の領域へ Androidアプリ開発編

Androidアプリ開発に関する調査メモ置き場。古い記事にはアプリ以外も含まれます。

Android 4.3 Hardware Credential Storage

Android4.3でサポートされたHardware Credential Storageの機能。
http://developer.android.com/intl/ja/about/versions/android-4.3.html#Security
ハードウェア側で保証された安全な領域にキーなどを保存してくれるようだが
どう使えばいいのか。
ドキュメントによると機能がサポートれされているかは
KeyChainクラスのisBoundKeyAlgorithm()を使えば分かるらしい。

http://developer.android.com/intl/ja/reference/android/security/KeyChain.html#isBoundKeyAlgorithm(java.lang.String)

if (KeyChain.isBoundKeyAlgorithm("RSA")) {
   //ここに処理を記述
}

引数のアルゴリズムに何を渡せばいいのかと思ったがソースを見る限りでは
どうやら"RSA"の一択らしい。

    /**
     * Returns {@code true} if the current device's {@code KeyChain} binds any
     * {@code PrivateKey} of the given {@code algorithm} to the device once
     * imported or generated. This can be used to tell if there is special
     * hardware support that can be used to bind keys to the device in a way
     * that makes it non-exportable.
     */
    public static boolean isBoundKeyAlgorithm(String algorithm) {
        if (!isKeyAlgorithmSupported(algorithm)) {
            return false;
        }

        return KeyStore.getInstance().isHardwareBacked();
    }

    /**
     * Returns {@code true} if the current device's {@code KeyChain} supports a
     * specific {@code PrivateKey} type indicated by {@code algorithm} (e.g.,
     * "RSA").
     */
    public static boolean isKeyAlgorithmSupported(String algorithm) {
        return "RSA".equals(algorithm);
    }

で、最新のNexus 7 2013で実行してみたらfalseが返ってきた。
サポートしてないのか。orz

と思いきや、OSをアップデータとしたらちゃんと対応してくれたようだ。

設定 > セキュリティ > 認証情報ストレージ > ストレージタイプ

の値が「ハードウェア式」に変わっている。