/** * Constructs a CommandAPDU from the four header bytes and command data. * This is case 3 in ISO 7816, command data present and Ne absent. The * value Nc is taken as data.length. If <code>data</code> is null or * its length is 0, the APDU is encoded as ISO 7816 case 1. * * <p>Note that the data bytes are copied to protect against * subsequent modification. * * @param cla the class byte CLA * @param ins the instruction byte INS * @param p1 the parameter byte P1 * @param p2 the parameter byte P2 * @param data the byte array containing the data bytes of the command body * * @throws IllegalArgumentException if data.length is greater than 65535 */ public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data) { this(cla, ins, p1, p2, data, 0, arrayLength(data), 0); }
/** * Constructs a CommandAPDU from a byte array containing the complete * APDU contents (header and body). The APDU starts at the index * <code>apduOffset</code> in the byte array and is <code>apduLength</code> * bytes long. * * <p>Note that the apdu bytes are copied to protect against * subsequent modification. * * @param apdu the complete command APDU * @param apduOffset the offset in the byte array at which the apdu * data begins * @param apduLength the length of the APDU * * @throws NullPointerException if apdu is null * @throws IllegalArgumentException if apduOffset or apduLength are * negative or if apduOffset + apduLength are greater than apdu.length, * or if the specified bytes are not a valid APDU */ public CommandAPDU(byte[] apdu, int apduOffset, int apduLength) { checkArrayBounds(apdu, apduOffset, apduLength); this.apdu = new byte[apduLength]; System.arraycopy(apdu, apduOffset, this.apdu, 0, apduLength); parse(); }
checkArrayBounds(data, dataOffset, dataLength); if (dataLength > 65535) { throw new IllegalArgumentException("dataLength is too large"); setHeader(cla, ins, p1, p2); } else { setHeader(cla, ins, p1, p2); this.apdu[4] = len; } else { setHeader(cla, ins, p1, p2); this.apdu[5] = l1; this.apdu[6] = l2; setHeader(cla, ins, p1, p2); apdu[4] = (byte)dataLength; this.dataOffset = 5; setHeader(cla, ins, p1, p2); apdu[4] = 0; apdu[5] = (byte)(dataLength >> 8); setHeader(cla, ins, p1, p2); apdu[4] = (byte)dataLength; this.dataOffset = 5; setHeader(cla, ins, p1, p2); apdu[4] = 0;
@Override public byte[] processCommandApdu(byte[] buffer, Bundle extras) { Log.d(TAG, "Process command Apdu " + toHexString(buffer)); CommandAPDU command = new CommandAPDU(buffer); int ins = command.getINS(); switch (ins) { case ISO_SELECT_APPLICATION: { Log.d(TAG, "ISO Select Application"); broadcast.broadcast(Broadcast.HOST_CARD_EMULATION_SERVICE_STARTED); return send(OPERATION_OK); } case SELECT_APPLICATION: { String application = toHexString(command.getData(), 1, 3); Log.d(TAG, "Selected application " + application); broadcast.broadcast(Broadcast.HOST_CARD_EMULATION_APPLICATION_SELECTED, Broadcast.KEY_APPLICATION_ID, application); return send(OPERATION_OK); } default : { Log.d(TAG, "Receieved unknown command " + Integer.toHexString(ins)); } } return send(APPL_INTEGRITY_ERROR); }
/** * Creates a CommandAPDU from the ByteBuffer containing the complete APDU * contents (header and body). * The buffer's <code>position</code> must be set to the start of the APDU, * its <code>limit</code> to the end of the APDU. Upon return, the buffer's * <code>position</code> is equal to its limit; its limit remains unchanged. * * <p>Note that the data in the ByteBuffer is copied to protect against * subsequent modification. * * @param apdu the ByteBuffer containing the complete APDU * * @throws NullPointerException if apdu is null * @throws IllegalArgumentException if apdu does not contain a valid * command APDU */ public CommandAPDU(ByteBuffer apdu) { this.apdu = new byte[apdu.remaining()]; apdu.get(this.apdu); parse(); }
CommandAPDU command = new CommandAPDU(0x00, 0xA4, 0x04, 00, key); Log.d(TAG, "Send request " + toHexString(command.getBytes()) ); byte[] responseBytes = isoDep.transceive(command.getBytes());
checkArrayBounds(data, dataOffset, dataLength); if (dataLength > 65535) { throw new IllegalArgumentException("dataLength is too large"); setHeader(cla, ins, p1, p2); } else { setHeader(cla, ins, p1, p2); this.apdu[4] = len; } else { setHeader(cla, ins, p1, p2); this.apdu[5] = l1; this.apdu[6] = l2; setHeader(cla, ins, p1, p2); apdu[4] = (byte)dataLength; this.dataOffset = 5; setHeader(cla, ins, p1, p2); apdu[4] = 0; apdu[5] = (byte)(dataLength >> 8); setHeader(cla, ins, p1, p2); apdu[4] = (byte)dataLength; this.dataOffset = 5; setHeader(cla, ins, p1, p2); apdu[4] = 0;
/** * Creates a CommandAPDU from the ByteBuffer containing the complete APDU * contents (header and body). * The buffer's <code>position</code> must be set to the start of the APDU, * its <code>limit</code> to the end of the APDU. Upon return, the buffer's * <code>position</code> is equal to its limit; its limit remains unchanged. * * <p>Note that the data in the ByteBuffer is copied to protect against * subsequent modification. * * @param apdu the ByteBuffer containing the complete APDU * * @throws NullPointerException if apdu is null * @throws IllegalArgumentException if apdu does not contain a valid * command APDU */ public CommandAPDU(ByteBuffer apdu) { this.apdu = new byte[apdu.remaining()]; apdu.get(this.apdu); parse(); }
/** * Constructs a CommandAPDU from a byte array containing the complete * APDU contents (header and body). The APDU starts at the index * <code>apduOffset</code> in the byte array and is <code>apduLength</code> * bytes long. * * <p>Note that the apdu bytes are copied to protect against * subsequent modification. * * @param apdu the complete command APDU * @param apduOffset the offset in the byte array at which the apdu * data begins * @param apduLength the length of the APDU * * @throws NullPointerException if apdu is null * @throws IllegalArgumentException if apduOffset or apduLength are * negative or if apduOffset + apduLength are greater than apdu.length, * or if the specified bytes are not a valid APDU */ public CommandAPDU(byte[] apdu, int apduOffset, int apduLength) { checkArrayBounds(apdu, apduOffset, apduLength); this.apdu = new byte[apduLength]; System.arraycopy(apdu, apduOffset, this.apdu, 0, apduLength); parse(); }
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { apdu = (byte[])in.readUnshared(); // initialize transient fields parse(); }
/** * Constructs a CommandAPDU from the four header bytes and command data. * This is case 3 in ISO 7816, command data present and Ne absent. The * value Nc is taken as data.length. If <code>data</code> is null or * its length is 0, the APDU is encoded as ISO 7816 case 1. * * <p>Note that the data bytes are copied to protect against * subsequent modification. * * @param cla the class byte CLA * @param ins the instruction byte INS * @param p1 the parameter byte P1 * @param p2 the parameter byte P2 * @param data the byte array containing the data bytes of the command body * * @throws IllegalArgumentException if data.length is greater than 65535 */ public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data) { this(cla, ins, p1, p2, data, 0, arrayLength(data), 0); }
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { apdu = (byte[])in.readUnshared(); // initialize transient fields parse(); }
/** * Constructs a CommandAPDU from the four header bytes, command data, * and expected response data length. This is case 4 in ISO 7816, * command data and Ne present. The value Nc is taken as data.length * if <code>data</code> is non-null and as 0 otherwise. If Ne or Nc * are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816. * * <p>Note that the data bytes are copied to protect against * subsequent modification. * * @param cla the class byte CLA * @param ins the instruction byte INS * @param p1 the parameter byte P1 * @param p2 the parameter byte P2 * @param data the byte array containing the data bytes of the command body * @param ne the maximum number of expected data bytes in a response APDU * * @throws IllegalArgumentException if data.length is greater than 65535 * or if ne is negative or greater than 65536 */ public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne) { this(cla, ins, p1, p2, data, 0, arrayLength(data), ne); }
/** * Constructs a CommandAPDU from a byte array containing the complete * APDU contents (header and body). * * <p>Note that the apdu bytes are copied to protect against * subsequent modification. * * @param apdu the complete command APDU * * @throws NullPointerException if apdu is null * @throws IllegalArgumentException if apdu does not contain a valid * command APDU */ public CommandAPDU(byte[] apdu) { this.apdu = apdu.clone(); parse(); }
/** * Constructs a CommandAPDU from the four header bytes, command data, * and expected response data length. This is case 4 in ISO 7816, * command data and Ne present. The value Nc is taken as data.length * if <code>data</code> is non-null and as 0 otherwise. If Ne or Nc * are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816. * * <p>Note that the data bytes are copied to protect against * subsequent modification. * * @param cla the class byte CLA * @param ins the instruction byte INS * @param p1 the parameter byte P1 * @param p2 the parameter byte P2 * @param data the byte array containing the data bytes of the command body * @param ne the maximum number of expected data bytes in a response APDU * * @throws IllegalArgumentException if data.length is greater than 65535 * or if ne is negative or greater than 65536 */ public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne) { this(cla, ins, p1, p2, data, 0, arrayLength(data), ne); }
/** * Constructs a CommandAPDU from a byte array containing the complete * APDU contents (header and body). * * <p>Note that the apdu bytes are copied to protect against * subsequent modification. * * @param apdu the complete command APDU * * @throws NullPointerException if apdu is null * @throws IllegalArgumentException if apdu does not contain a valid * command APDU */ public CommandAPDU(byte[] apdu) { this.apdu = apdu.clone(); parse(); }