Extended APDUs and T=0/1 communication protocols
Question: I have a JCOP V2.4.2 R3 java card that it is mentioned in its datasheet "The
card support both `T=1` and `T=0` communication protocols"

I have also an ACR38 smart card reader that it support both T=0 and T=1
protocols. (I have T=0 communication with one card successfully and T=1
communication with this card successfully.)

I wrote the below program and upload it on the card to send and receive
extended APDUs:

    
    
    package extAPDU;
    
    import javacard.framework.APDU;
    import javacard.framework.Applet;
    import javacard.framework.ISOException;
    import javacardx.apdu.ExtendedLength;
    
    public class ExAPDU extends Applet implements ExtendedLength {
    
        private ExAPDU() {
        }
    
    
        public static void install(byte bArray[], short bOffset, byte bLength)
                throws ISOException {
            new ExAPDU().register();
        }
    
        public void process(APDU arg0) throws ISOException {
            short number = arg0.setIncomingAndReceive();
            arg0.setOutgoingAndSend((short)0, (short)(number+7));
        }
    
    }
    

In the CAD-side I used python scripts to send different APDUs to card. The
questions is:

**1- Why I can't start communication with T=0 protocol(While it is mentioned
that the card support this protocol):**

The python script:

    
    
    from smartcard.scard import *
    import smartcard.util
    from smartcard.System import readers
    from smartcard.CardConnection import CardConnection
    
    r=readers()
    print r
    connection =r[0].createConnection()
    connection.connect(CardConnection.T0_protocol)
    
    normalCommand=[0x00,0xa4,0x04,0x00,0x06,0x01,0x02,0x03,0x04,0x05,0x06]
    data,sw1,sw2=connection.transmit(normalCommand)
    print "SW for Normal Command:"
    print data,hex(sw1),hex(sw2)
    

Output:

    
    
    >>> ================================ RESTART ================================
    >>> 
    ['ACS CCID USB Reader 0']
    
    Traceback (most recent call last):
      File "C:\extAPDU.py", line 13, in <module>
        connection.connect(CardConnection.T0_protocol)
      File "D:\PythonX\Lib\site-packages\smartcard\CardConnectionDecorator.py", line 54, in connect
        self.component.connect(protocol, mode, disposition)
      File "D:\PythonX\Lib\site-packages\smartcard\pcsc\PCSCCardConnection.py", line 118, in connect
        raise CardConnectionException('Unable to connect with protocol: ' + dictProtocol[pcscprotocol] + '. ' + SCardGetErrorMessage(hresult))
    CardConnectionException: Unable to connect with protocol: T0. The requested protocols are incompatible with the protocol currently in use with the smart card. 
    >>> 
    

**2- Why the card doesn't work fine with the Select APDU commands in the
extended form on the T=1 protocol :**

The python script:

    
    
    from smartcard.scard import *
    import smartcard.util
    from smartcard.CardConnection import CardConnection
    from smartcard.System import readers
    
    r=readers()
    print r
    connection =r[0].createConnection()
    connection.connect(CardConnection.T1_protocol)
    
    normalCommand=[0x00,0xa4,0x04,0x00,0x00,0x00,0x06,0x01,0x02,0x03,0x04,0x05,0x06]
    data,sw1,sw2=connection.transmit(normalCommand)
    print "SW for Normal Command:"
    print data,hex(sw1),hex(sw2)
    

Output :

    
    
    >>> ================================ RESTART ================================
    >>> 
    ['ACS CCID USB Reader 0']
    SW for Normal Command:
    [] 0x67 0x0
    >>> 
    

I think I misunderstood this concept and I mixed up Extended APDUs with `T=1`
and `T=0` protocols!

Every `T=1` compatible smart card, can send and receive Extended APDUs? and we
can't send and receive extended APDUs over `T=0` protocols? If we want to send
Extended SELECT APDU commands to the Security Domain, the SD must implement
`ExtendedLength` interface?

For an Extended APDU transmission what are the requirements?

  1. A T=1 compatible card reader
  2. A T=1 compatible smart card
  3. An applet that implemented `ExtendedLength` interface

Is it right?

I am really confused about Extended compatibility and `T=0/1` compatibility in
smart cards. Any light will appreciated.

Note that, I can successfully send Extended APDUs to the above applet with
`T=1` protocol!


Answer: **Q1:** Changing Protocol is possible. Information which protocols are
supported by hte card is transceived via ATR/ATS. The terminal then can decide
which one to use. So it is dependend from your Terminal shell if protocols are
selectable or not. For JCOP Shell this is `/change-protocol`. However is do
not recommend T=0 in general.

**Q2:** If you start the card via sending ATR/ATS the Card Manager is active
which only supports Global Platform commands. Global Platform does not support
Extended Length what so ever. By sending a Select command(which must be simple
length because of that) the applet gets selected and that actual Select
commands is also forwarded into your Applet's `process()` method (and can be
detected by the `selectingApplet()` method). Now that you are in your Applet
you can send as many Extended Length commands as you want. You can bypass the
initial Non-Extended-Length-Select by installing your applet as default
selected.

