クマは森で用を足しますか?

アウトプットは重要です。

Pixel 7 のログから逆引きして学び始める eSIM MEP

AppleiPhone 13 から、Google は Pixel 7 から eSIM MEP (Multiple Enabled Profiles) をサポートしています。一般市民が触って遊べるようなものなのかどうかは疑問なところですが、どんな仕組みで動いているのか知っておいても良いかなと考えています。ということで、今回は ETSI TS 102 221 を読んでゆきます。

source.android.com

UICC が MEP をサポートしているかどうか

Pixel 7 のバグレポートを見ると、UiccSlot やその近辺のログに "..MultipleEnabledProfiles=true" といった情報を見つけることができます。

      UiccSlot:
       mActive=true
       mIsEuicc=true
       isEuiccSupportsMultipleEnabledProfiles=true
       mIsRemovable=false
       mLastRadioState={0=1, 1=2}
       mIccIds={0=898130005[****], 1=}
       mPortIdxToPhoneId={0=1, 1=-1}
       mEid=[****]
       mCardState=CARDSTATE_PRESENT
       mUiccCard=com.android.internal.telephony.uicc.euicc.EuiccCard@224a7c5
      UiccCard:
       mCardState=CARDSTATE_PRESENT
       mCardId=[****]
       mNumberOfPorts=1
      mIsSupportsMultipleEnabledProfiles=true

この情報がどこから来ているんだろうとソースコードを追ってみると、AnswerToReset.java による ATR の確認結果であることがわかります。

    private void checkEuiccSupportedCapabilities() {
        // eUICC is supported only if the b8 and b2 of the first tB after T=15 are set to 1.
        // MEP is supported only if the b8 and b1 of the first tB after T=15 are set to 1.
        for (int i = 0; i < mInterfaceBytes.size() - 1; i++) {
            if (mInterfaceBytes.get(i).getTD() != null
                    && (mInterfaceBytes.get(i).getTD() & T_MASK) == T_VALUE_FOR_GLOBAL_INTERFACE
                    && mInterfaceBytes.get(i + 1).getTB() != null) {
                if ((mInterfaceBytes.get(i + 1).getTB() & B8_MASK) != 0
                        && (mInterfaceBytes.get(i + 1).getTB() & B2_MASK) != 0) {
                    mIsEuiccSupported = true;
                }
                if ((mInterfaceBytes.get(i + 1).getTB() & B8_MASK) != 0
                        && (mInterfaceBytes.get(i + 1).getTB() & B1_MASK) != 0) {
                    mIsMultipleEnabledProfilesSupported = true;
                }
                return;
            }
        }
    }

TD3 の下位ニブル 'F' (Protocol T=15) の次のセットが Global Interface Bytes です。ETSI TS 102 221 によると、そのセットの TB の b8 および b1 がセットされていれば UICC が LSI (Logical SE Interface) をサポートしていることを示しているとのこと。上記のソースコードにあるコメントもそのように説明していますね。

Table 6.7: Coding of the first tB i (i > 2) after T = 15 of the ATR (ETSI TS 102 221)

Pixel 7 の ATR もログから拾ってきてみました。TD=3F (Protocol T=15) となっている 3 つめのセットの次のセットが Global Interface Bytes で、その TB=83 (b8/b1 ともに 1) が LSI のサポートを意味しているということですね。

D/AnswerToReset( 2191): Successfully parsed the ATR string
    3B9F97C00AB1FE453FC6838031E073FE211F65D002341569810F21 into AnswerToReset:{
        mConventionByte=3B,
        mFormatByte=9F,
        mInterfaceBytes={
            {TA=97,TB=null,TC=null,TD=C0}
            {TA=null,TB=null,TC=0A,TD=B1}
            {TA=FE,TB=45,TC=null,TD=3F}
            {TA=C6,TB=83,TC=null,TD=null}
        },
        mHistoricalBytes={80,31,E0,73,FE,21,1F,65,D0,02,34,15,69,81,0F,},
        mCheckByte=21
    }

LSI (Logical SE Interface) とその選択

LSI とは、ISO 7816 の物理インターフェースを複数の論理インターフェースへと多重化してアクセスする仕組みのこと。主に T = 0 プロトコルの端末の場合、MANAGE LSI コマンドを使って後続の APDU コマンドの論理インターフェースを選択したり、特定の論理インターフェースのみをリセットしたりするようです。論理インターフェースの、0 から 31 までの 32 個まで取り扱うことができますよと。

7.5 Logical secure element Interfaces
A UICC may support LSIs. If supported by the UICC, this shall be indicated in the ATR. LSIs provide a multiplexing mechanism which allows the terminal to communicate with multiple LSEs over one physical interface. LSIs supported by the UICC and the terminal shall be identified by consecutive numbers starting from zero. The maximum value for the LSI is '1F'. Management of LSIs is done via a dedicated command MANAGE LSI which allows:

  • selection of an LSI for subsequent APDUs (mandatory for protocol T = 0, optional for protocol T = 1);
  • resetting an LSE without affecting other LSEs; and
  • setup of the LSI configuration to be used during the card session

各論理インターフェースを通してアクセスする LSE については、下記のように説明されています。eUICC SE の場合、eSIM プロファイル (もしくはそれが入っている箱のようなもの) がこの LSE に相当するものとして読んで不都合なさそうです。なるほど、活性化されている複数の eSIM プロファイルのうちのひとつから REFRESH コマンドを受けたとき、従来の方法で eUICC をリセットすると eUICC 全体を (=全ての eSIM プロファイルを) リセットしてしまいますもんね。複数の論理インターフェースで多重化されているときには、MANAGE LSI コマンドを使って特定の論理インターフェースをリセットしたい旨を伝える必要があるみたいです。

8.10 Logical secure elements
A UICC may support several LSEs on the same physical element. Each LSE acts and is handled by the terminal like a separate secure element. The terminal communicates with the different LSEs via LSIs. Each LSE operates logically independently from the others. As seen on the interface, each LSE has its own file system, its own applications, its own AIDs, its own independent security status, its own CAT session (which means that several of them can run interleaved in parallel on the different LSIs), and its own runtime environment.
The terminal can switch between the LSEs by sending MANAGE LSI (select LSI) commands or by selecting the LSI via the NAD byte when using T = 1. The MANAGE LSI command also allows resetting an LSE. Such a reset shall be equivalent to a warm reset of the interface for a UICC not supporting LSEs. A cold reset or a warm reset of the physical interface results in resetting all LSEs. When the terminal receives a REFRESH proactive command with mode "UICC reset" on an LSI, the terminal shall reset only the corresponding LSE via a MANAGE LSI command

先ほど MANAGE LSI コマンドによる論理インターフェース選択について「主に T = 0 プロトコルの端末の場合」と書きましたが、T = 1 プロトコルであれば MANAGE LSI コマンドではなく NAD (Node ADdress byte) バイトを使って論理インターフェースを指定することができます。NAD バイトは、T = 1 のブロック構造のプロローグフィールド内に存在します。

Table 7.4. Node address byte (ETSI TS 102 221)

この NAD バイトにある DAD/SAD を使ってブロックの送受信の対象とする論理インターフェースを指定することになりますが、AT コマンドや PC/SC インターフェース経由で一般市民があれこれやれそうな気がしていないので、それについて言及するのはこのあたりまでにしておこうと思います。

Table 7.4a: LSI number encoding

端末と eUICC の LSI 利用に関する同意確認

UICC は ATR を使って LSI のサポートを端末に対して示しますが、それをただ見ているだけでは端末は LSI を利用することができません。

7.5 Logical secure element Interfaces
Terminal and UICC shall agree on using LSIs as follows:

  • The UICC indicates support for LSIs in the ATR.
  • If indicated by the UICC, the terminal shall indicate in PPS2 if LSIs shall be used for the card session.
  • If LSIs are used for the card session, LSI 0 is selected after the PPS.
  • Before sending a command on an LSI different from 0 or sending any other MANAGE LSI command, the terminal shall perform the setup of the LSI configuration by sending a MANAGE LSI (configure LSIs) APDU to the UICC.

端末側もまた、PPS2 を使って LSI を利用する能力と意思があることを UICC に対して示す必要があると説明されています。

6.4 PPS procedure
PPS2 shall only be used if the first tB i (i > 2) after T = 15 is present in the ATR. The coding for PPS2 is identical to that of the first tBi (i > 2) after T = 15. The value selected depends upon the features supported by the terminal.

MANAGE LSI コマンドの仕様

MANAGE LSI コマンドの CLA および INS は、下表のように規定されています。CLA は '8X' ではなく '80' なので、論理チャネル 0 (基本チャネル) に対してのみ送信可能なコマンドなんですね。

Command CLA INS
MANAGE LSI '80' '7C'

MANAGE LSI コマンドについて整理するには ETSI TS 102 221 から複数の表を引用する必要がありますので、あらためて別の投稿でそれを行うことにしようと思います。