2014-11-05 72 views
0

我正在Eclipse上用JCDE開發JavaCard中的applet。JavaCard SW 6F 00帶類BigNumber(JavaCard API)的函數init()

我正在測試我的小程序與JCWDE和APDUTOOL,但我得到SW1 = 6F和SW2 = 00 ... 事情是,我正在使用JavaCard類的BigNumber類和函數init()不工作像我想要。

我的小應用程序的代碼:

import javacard.framework.APDU; 
import javacard.framework.APDUException; 
import javacard.framework.Applet; 
import javacard.framework.CardRuntimeException; 
import javacard.framework.ISO7816; 
import javacard.framework.ISOException; 
import javacard.framework.UserException; 
import javacard.framework.Util; 
import javacard.security.CryptoException; 
import javacardx.framework.math.BigNumber; 
import javacard.security.MessageDigest; 

public class SignatureGPS extends Applet { 

    public static final byte CLA = (byte) 0xB0; 
    public static final byte INS = (byte) 0x00; 

    private BigNumber s; 
    private BigNumber x; 

    private SignatureGPS() { 
     try { 
      s = new BigNumber((short)100); 
      x = new BigNumber((short)100); 
      byte[] tmp = {(byte) 0xc6, (byte) 0x85, (byte)0x8e, 0x06, (byte)0xb7, 0x04, 0x04, (byte)0xe9, (byte)0xcd, (byte)0x9e, 0x3e, (byte)0xcb, 0x66, 0x23, (byte)0x95, (byte)0xb4, 
        0x42, (byte)0x9c, 0x64, (byte)0x81, 0x39, 0x05, 0x3f, (byte)0xb5, 0x21, (byte)0xf8, 0x28, (byte)0xaf, 0x60, 0x6b, 0x4d, 0x3d, (byte)0xba, (byte)0xa1, 0x4b, 0x5e, 
        0x77, (byte)0xef, (byte)0xe7, 0x59, 0x28, (byte)0xfe, 0x1d, (byte)0xc1, 0x27, (byte)0xa2, (byte)0xff, (byte)0xa8, (byte)0xde, 0x33, 0x48, (byte)0xb3, (byte)0xc1, 
        (byte)0x85, 0x6a, 0x42, (byte)0x9b, (byte)0xf9, 0x7e, 0x7e, 0x31, (byte)0xc2, (byte)0xe5, (byte)0xbd, 0x66}; 
      x.init(tmp, (short) 0, (short) tmp.length, BigNumber.FORMAT_HEX); 
      h = MessageDigest.getInstance(MessageDigest.ALG_SHA_256, false); 
     } 
     catch(ArithmeticException e){} 
     catch(CryptoException e){CryptoException.throwIt(e.getReason());} 
     catch(NullPointerException e){} 
     catch(ArrayIndexOutOfBoundsException e){} 
    } 

    public static void install(byte bArray[], short bOffset, byte bLength) throws ISOException { 
     new SignatureGPS().register(); 
    } 

    public void process(APDU apdu) 
     throws ISOException, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, APDUException, { 
     byte[] buffer = apdu.getBuffer(); 

     if (this.selectingApplet()) return; 

     if (buffer[ISO7816.OFFSET_CLA] != CLA) 
      ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); 

     if (buffer[ISO7816.OFFSET_INS]!=0) 
      ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); 

     short LC = apdu.getIncomingLength(); 
     apdu.setIncomingAndReceive(); 
     s.init(buffer, (short)5, LC, BigNumber.FORMAT_HEX); 
    } 
} 

於是問題出現的行s.init(buffer, (short)5, LC, BigNumber.FORMAT_HEX);但奇怪的事情是,它似乎在構造函數中工作(x.init(tmp, (short) 0, (short) tmp.length, BigNumber.FORMAT_HEX);

有誰看到我究竟做錯了什麼?

下面是我從工具(apdutool)得到的迴應:

Java Card 2.2.2 APDU Tool, Version 1.3 
Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
Opening connection to localhost on port 9025. 
Connected. 
powerup; 
// Select the installer applet 
0x00 0xA4 0x04 0x00 0x09 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0x08 0x01 0x7F; 
// create SignatureGPS applet 
0x80 0xB8 0x00 0x00 0xd 0xb 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x01 0x00 0x7F;Received ATR = 0x3b 0xf0 0x11 0x00 0xff 0x00 
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: b8, P1: 00, P2: 00, Lc: 0d, 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, 00, Le: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, SW1: 90, SW2: 00 
// select SignatureGPS applet 
0x00 0xA4 0x04 0x00 0xb 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x01 0x7F; 
CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, Le: 00, SW1: 90, SW2: 00 
0xB0 0x00 0x00 0x00 0x02 0x03 0x04 0x7F; 
CLA: b0, INS: 00, P1: 00, P2: 00, Lc: 02, 03, 04, Le: 00, SW1: 6f, SW2: 00 
在APDU命令這個名單

所以,我創造我的小程序並選擇它,然後我送其中包含2個DATAS(0×03 0×04)和APDU與我的線s.init(buffer, (short)5, LC, BigNumber.FORMAT_HEX);我預計s會初始化在0x0304,但正如你所看到的,我得到SW 6F00 ... 非常感謝您的幫助!

+1

的區別的例外是x.init使用正持續字節數組,而s.init用途APDU緩衝區。我認爲後者是一個不能使用的特殊RAM部分。在調用s.init之前可能會嘗試將數據複製到常規字節[]? – Robert 2014-11-05 21:01:02

+0

謝謝你的回答,正如Michael Roland所說的,它看起來像's.init(buffer,(short)5,LC,BigNumber.FORMAT_HEX);'是返回一個'ArithmeticException'。所以我通過嘗試調用'init'來獲得'NullPointerException' ...但我不知道爲什麼它會在構造函數中拋出ArithmeticException:( – Raoul722 2014-11-06 21:22:39

回答

1

正如你已經在你的previous question on that issue中發現的那樣,試圖分配s = new BigNumber((short)100);會拋出一個ArithmethicException

  • 這意味着s永遠不會在您的代碼中被初始化,因此是null
  • 因此,試圖調用initnull參考

    s.init(buffer, (short)5, LC, BigNumber.FORMAT_HEX); 
    

    將導致NullPointerException,其在響應狀態字6F00轉的結果。

注意,在你的構造x.init(...);永遠被稱爲由於上線

s = new BigNumber((short)100); 
+0

謝謝你的回答,但是如果我把行代碼's.init(buffer,(short)5,LC,BigNumber.FORMAT_HEX);'在我的'process(APDU apdu)'函數中註釋,無論如何我發送給我的卡的APDU,我總是得到一個SW爲9000 ,所以我在想我的構造函數是好的,而不是我捕捉到一個異常...所以我必須明白,我不會初始化我的BigNumber正確的權利...你看到爲什麼嗎? 我認爲它是's = new BigNumber((short)100)的好方法;',我只是遵循JavaCard2.2.2 API :( )再次感謝您的幫助! – Raoul722 2014-11-06 21:12:36

+1

@ Raoul722在你的另一個問題上看到我關於'BigNumber'實例化的問題的答案。 – 2014-11-06 23:45:50