JCWDE模拟器运行:来自调用的install()方法的异常

时间:2014-10-03 08:17:43

标签: java javacard apdu

我正在尝试使用hello world applet运行jwcde模拟。我用的是JC221。

这是小程序:

package hello;



import javacard.framework.APDU;

import javacard.framework.Applet;

import javacard.framework.ISO7816;

import javacard.framework.ISOException;

import javacard.framework.Util;



public class HelloWorldApplet extends Applet {

         private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};



         private static final byte HW_CLA = (byte)0x80;

         private static final byte HW_INS = (byte)0x00;



         public static void install(byte[] bArray, short bOffset, byte bLength) {

             new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);

         }



         public void process(APDU apdu) {



             if (selectingApplet()) {

             return;

         }



         byte[] buffer = apdu.getBuffer();

         byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);

         byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);



         if (CLA != HW_CLA)

        {

            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

        }



          switch ( INS ) {

             case HW_INS:

               getHelloWorld( apdu );

               break;

            default:

               ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);

         }

   }



  private void getHelloWorld( APDU apdu)

  {

      byte[] buffer = apdu.getBuffer();

      short length = (short) helloWorld.length;



      Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);



      apdu.setOutgoingAndSend((short)0, length);

  }

}

我设法用这个命令编译它:

javac -g -d hello -classpath “.;%JC_HOME%\lib\api.jar;%JC_HOME%\lib\javacardframework.jar” HelloWorldApplet.java

我检查了编译后的类文件是否在路径上。

这是jcwde使用的jcwde.app文件:

//+

// Copyright © 2003 Sun Microsystems, Inc. All rights reserved.

// SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

//-



//+

// Workfile:@(#)jcwde.app    1.4

// Version:1.4

// Date:07/16/03

//-



// applet                                        AID

com.sun.javacard.installer.InstallerApplet       0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0x8:0x1

com.sun.javacard.samples.JavaPurse.JavaPurse     0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x2:0x1

com.sun.javacard.samples.JavaLoyalty.JavaLoyalty 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x5:0x1

com.sun.javacard.samples.wallet.Wallet           0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x6:0x1

hello.HelloWorldApplet                           0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08:0x09:0x00:0x01 

我在命令行窗口(Windows7)中使用此命令启动jcwde:

jcwde jcwde.app

然后我启动一个新的命令行窗口并在那里启动apdutool:

apdutool -nobanner -noatr HelloWorldApplet.scr > hello.scr.jcwde.out

这是.scr脚本文件:     通电;

// Select the installer applet
0x00 0xA4 0x04 0x00 0x09 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0x08 0x01 0x7F;
// 90 00 = SW_NO_ERROR

// create Applet
0x80 0xB8 0x00 0x00 0x0d 0x0b 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x01 0x00 0x7F;

// Select Applet
0x00 0xa4 0x04 0x00 0x0b 0x01 0x02 0x03 0x04 0x05 0x6 0x07 0x08 0x09 0x00 0x01 0x7F;

// Send APDU command to get a Hello World message
0x80 0x00 0x00 0x00 0x00 0x00;

powerdown;

这是结果(来自jcwde命令窗口):

D:\jc221\samples\src\demo\jcwde>jcwde jcwde.app

Java Card 2.2.1 Workstation Development Environment, Version 1.3

Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

jcwde is listening for T=0 Apdu's on TCP/IP port 9á025.

Exception from the invoked install() method:public static void hello.HelloWorldApplet.install(byte[],short,byte)

jcwde exiting on receipt of power down command.



D:\jc221\samples\src\demo\jcwde>

这是hello.scr.jcwde.out文件中的结果:

CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 09, a0, 00, 00, 00, 62, 03, 01, 08, 01, Le: 00, SW1: 90, SW2: 00

CLA: 80, INS: b0, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 90, SW2: 00

CLA: 80, INS: b8, P1: 00, P2: 00, Lc: 0d, 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, 00, Le: 00, SW1: 64, SW2: 44

CLA: 80, INS: ba, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 90, SW2: 00

CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, Le: 00, SW1: 6d, SW2: 00

CLA: 80, INS: 00, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 6d, SW2: 00

所以问题是它为什么会抛出异常:来自调用的install()方法的异常:public static void hello.HelloWorldApplet.install(byte [],short,byte)

为什么结果SW在第三行SW1:64 SW2:44而不是90 00应该是什么?

1 个答案:

答案 0 :(得分:0)

此问题的根源似乎是JCWDE中的错误(?)。虽然Java Card规范要求install参数至少包含长度字节(即使是指示零长度),但JCWDE似乎使用它通过脚本获得的任何安装参数。那么,如果您没有明确地传递安装参数,则字节数组bArray将具有零长度。因此,访问bArray[bOffset]会导致IndexOutOfBounds例外。因此,检查bArray / bLength的长度并调用

可能是个好主意
register(bArray, (short) (bOffset + 1), bArray[bOffset]);

register();

相应。