Page 1 of 1

Sample code to change ATR history bytes

Posted: Wed Oct 26, 2016 10:58 pm
by JCaberham
Share a sample code that used to change card ATR history bytes.

Code: Select all

package JC_setATRHistBytes;

import javacard.framework.*;
import org.globalplatform.*;

public class JC_setATRHistBytes_cls extends Applet {
     private static final byte[] newATRHistory  = { 
          (byte)'J', (byte)'C', (byte)'O', (byte)'P', (byte)'4', (byte)'1', (byte)'V', (byte)'2', (byte)'2', (byte)'1'
          } ;
     private static final byte[] newATRHistory1  = { 
          (byte)'J', (byte)'C', (byte)'O', (byte)'P', (byte)'4', (byte)'1', (byte)'V', (byte)'2', (byte)'2', (byte)'3'
          } ;
     public static void install(byte[] bArray, short bOffset, byte bLength) {
   
          new JC_setATRHistBytes_cls().register(bArray,
                    (short) (bOffset + 1), bArray[bOffset]);
     }

     public void process(APDU apdu) {
          if (selectingApplet()) {
               return;
          }

          byte[] buf = apdu.getBuffer();
       
          if (buf[ISO7816.OFFSET_CLA] != 0) ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
          switch (buf[ISO7816.OFFSET_INS]) {
          case (byte) 0x00:
               if (GPSystem.setATRHistBytes(newATRHistory, (short)0, (byte)newATRHistory1.length))
               {
                    return;
               }
               else
               {
                    ISOException.throwIt(ISO7816.SW_WARNING_STATE_UNCHANGED);
               }
          case (byte) 0x01:
               if (GPSystem.setATRHistBytes(newATRHistory1, (short)0, (byte)newATRHistory1.length))
               {
                    return;
               }
               else
               {
                    ISOException.throwIt(ISO7816.SW_WARNING_STATE_UNCHANGED);
               }               
          default:
             
               ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
          }
     }
}

Re: Sample code to change ATR history bytes

Posted: Thu Oct 27, 2016 3:21 am
by AmigoJack
Thank you for sharing!