1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.telephony.uicc.euicc.apdu; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 /** 23 * A helper class to build a request for sending APDU commands. It contains a few convenient methods 24 * to add different APDU commands to the request. 25 * 26 * @hide 27 */ 28 public class RequestBuilder { 29 // The maximum bytes of the data of an APDU command. If more bytes need to be sent than this 30 // limit, we need to separate them into multiple commands. 31 private static final int MAX_APDU_DATA_LEN = 0xFF; 32 // The maximum bytes of the data of an extended APDU command. If more bytes need to be sent than 33 // this limit, we need to separate them into multiple commands. 34 private static final int MAX_EXT_APDU_DATA_LEN = 0xFFFF; 35 36 // Parameters used by STORE DATA command sent to ISD-R, defined by SGP.22 v2.0. 37 private static final int CLA_STORE_DATA = 0x80; 38 private static final int INS_STORE_DATA = 0xE2; 39 private static final int P1_STORE_DATA_INTERM = 0x11; 40 private static final int P1_STORE_DATA_END = 0x91; 41 42 private final int mChannel; 43 private final int mMaxApduDataLen; 44 private final List<ApduCommand> mCommands = new ArrayList<>(); 45 46 /** 47 * Adds an APDU command by specifying every parts. The parameters are defined as in 48 * GlobalPlatform Card Specification v.2.3. 49 */ addApdu(int cla, int ins, int p1, int p2, int p3, String cmdHex)50 public void addApdu(int cla, int ins, int p1, int p2, int p3, String cmdHex) { 51 mCommands.add(new ApduCommand(mChannel, cla, ins, p1, p2, p3, cmdHex)); 52 } 53 54 /** 55 * Adds an APDU command with given command data. P3 will be the length of the command data 56 * bytes. The parameters are defined as in GlobalPlatform Card Specification v.2.3. 57 */ addApdu(int cla, int ins, int p1, int p2, String cmdHex)58 public void addApdu(int cla, int ins, int p1, int p2, String cmdHex) { 59 mCommands.add(new ApduCommand(mChannel, cla, ins, p1, p2, cmdHex.length() / 2, cmdHex)); 60 } 61 62 /** 63 * Adds an APDU command with empty command data. The parameters are defined as in GlobalPlatform 64 * Card Specification v.2.3. 65 */ addApdu(int cla, int ins, int p1, int p2)66 public void addApdu(int cla, int ins, int p1, int p2) { 67 mCommands.add(new ApduCommand(mChannel, cla, ins, p1, p2, 0, "")); 68 } 69 70 /** 71 * Adds a STORE DATA command. Long command length of which is larger than {@link 72 * #mMaxApduDataLen} will be automatically split into multiple ones. 73 * 74 * @param cmdHex The STORE DATA command in a hex string as defined in GlobalPlatform Card 75 * Specification v.2.3. 76 */ addStoreData(String cmdHex)77 public void addStoreData(String cmdHex) { 78 final int cmdLen = mMaxApduDataLen * 2; 79 int startPos = 0; 80 int totalLen = cmdHex.length() / 2; 81 int totalSubCmds = totalLen == 0 ? 1 : (totalLen + mMaxApduDataLen - 1) / mMaxApduDataLen; 82 for (int i = 1; i < totalSubCmds; ++i) { 83 String data = cmdHex.substring(startPos, startPos + cmdLen); 84 addApdu(CLA_STORE_DATA, INS_STORE_DATA, P1_STORE_DATA_INTERM, i - 1, data); 85 startPos += cmdLen; 86 } 87 String data = cmdHex.substring(startPos); 88 addApdu(CLA_STORE_DATA, INS_STORE_DATA, P1_STORE_DATA_END, totalSubCmds - 1, data); 89 } 90 getCommands()91 List<ApduCommand> getCommands() { 92 return mCommands; 93 } 94 RequestBuilder(int channel, boolean supportExtendedApdu)95 RequestBuilder(int channel, boolean supportExtendedApdu) { 96 mChannel = channel; 97 mMaxApduDataLen = supportExtendedApdu ? MAX_EXT_APDU_DATA_LEN : MAX_APDU_DATA_LEN; 98 } 99 } 100