Java Card 中的 AESKey LinkedList/Array [英] AESKey LinkedList/Array in Java Card

查看:23
本文介绍了Java Card 中的 AESKey LinkedList/Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章与我前几天问的一个问题有关:存储对称密钥在 Java 卡中

This post is related to a question I asked a few days ago: Store symmetric keys in Java Card

我想在 Java Card 中实现 LinkedList 来存储 AESKey.所以我这样写了一个KeyElement类:

I want to implement LinkedList in Java Card to store AESKey. So I wrote a class KeyElement this way:

package LinkedList;

import javacard.security.AESKey;
import javacard.security.KeyBuilder;


class KeyElement {
    private KeyElement next;
    private short id;
    private AESKey key;
    private boolean isUsed;

    /**
     * Constructor.
     * 
     * @param upperBound
     *            An upper bound to indicate of many elements it cans contain at
     *            maximum. It is essential to instanciate the structure this way
     *            to reserve all the necessary memory at the installation time.
     */
    public KeyElement(short upperBound) {
       this.id = (short) 0x0000;
       this.key = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES,
           KeyBuilder.LENGTH_AES_256, false);
       this.isUsed = false;
       this.next = null;
       for (short i = (short) 0x0001; i < upperBound; i++) {
           KeyElement e = new KeyElement();
           this.addKeyElement(e);
       }
    }

    public KeyElement() {
       this.id = (short) 0x0000;
       this.key = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES,
           KeyBuilder.LENGTH_AES_256, false);
       this.isUsed = false;
       this.next = null;
    }

    public KeyElement getNext() {
       return this.next;
    }

    public short getID() {
       return this.id;
    }

    public boolean isUsed() {
       return isUsed;
    }

    public void setNext(KeyElement e) {
       this.next = e;
    }

    public static boolean addKey(KeyElement main, final short id, byte[] key) {
       for (KeyElement p = main; p != null; p = p.getNext()) {
            if (!p.isUsed()) {
              p.isUsed = true;
              p.id = id;
              p.key.setKey(key, (short) 0x0000);
              return true;
           }
       }
       return false;
    }



    public void addKeyElement(KeyElement e) {
       e.setNext(this.getNext());
       this.setNext(e);
    }


    public static boolean getKey(final KeyElement e, final short id,
        byte[] key) {
       for (KeyElement p = e; p != null; p = p.getNext()) {
           if (p.id == id) {
              p.key.getKey(key, (short) 0x0000);
              return true;
           }
       }
       return false;
    }
}

我有一个小程序 LinkedListKey,它使用前面描述的 KeyElement 类.

And I have an applet LinkedListKey which uses the class KeyElement described before.

package LinkedList;

import javacard.framework.APDU;
import javacard.framework.APDUException;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.PINException;
import javacard.framework.SystemException;
import javacard.framework.TransactionException;
import javacard.framework.Util;
import javacard.framework.service.ServiceException;
import javacard.security.CryptoException;
import javacard.security.RandomData;

public class LinkedListKey extends Applet {

    byte[] rdm;
    RandomData rand;
    KeyElement e;

    private LinkedListKey(final byte[] aidArray, final short aidOffset,
        final byte aidLength, final byte[] dataArray, short dataOffset,
        final short dataLength) throws ISOException {

       e = new KeyElement((short) 0x0064);

       rdm = new byte[(short) 0x0020];

       KeyElement.addKey(e, (short) 0x7514, new byte[] { (byte) 0x75, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x75, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x14, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x75, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x75, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x14, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x75, (byte) 0x14,
        (byte) 0x75, (byte) 0x14, (byte) 0x75, (byte) 0x14,
        (byte) 0x75, (byte) 0x14 });

       register();
    }

    public static void install(final byte[] buffer, short offset,
        final byte length) throws ISOException {
       if (length == 0) {
          ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
       }

       short remainingBytes = makeShort(length);

       // Save instance AID.
       byte aidLength = buffer[offset++];
       short aidOffset = offset;

       // Skip instance AID.
       offset += makeShort(aidLength);

       remainingBytes--;
       remainingBytes -= makeShort(aidLength);

       if (remainingBytes <= (short) 0x0000) {
           ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
       }

       // Skip control data.
       byte infoLength = buffer[offset++];

       offset += makeShort(infoLength);

       remainingBytes--;
       remainingBytes -= makeShort(infoLength);

       if (remainingBytes <= (short) 0x0000) {
           ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
       }

       // Instantiate the application.
       // The length of the application data maybe greater than 127 bytes.
       short dataLength = makeShort(buffer[offset++]);

       remainingBytes--;

       if (remainingBytes != dataLength) {
           ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
       }

       new LinkedListKey(buffer, aidOffset, aidLength, buffer, offset,
           dataLength);
    }

    public void process(final APDU apdu) throws ISOException {
       byte[] apduBuffer = apdu.getBuffer();

       if (selectingApplet()) {
           apdu.setOutgoingAndSend((short) 0x0000, (short) 0x0019);

           return;
        }

    // Send result.
    try {
        if (apduBuffer[ISO7816.OFFSET_INS] == (byte) 0x00) {
           for (short i = 0x0000; i < (short) 0x0008; i++) {
               rand.generateData(rdm, (short) 0x0000, (short) rdm.length);
               KeyElement.addKey(e, i, rdm);
           }
           KeyElement.addKey(e, (short) 0x7503, new byte[] { (byte) 0x75, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x75, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x14, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x75, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x75, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x14, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x75, (byte) 0x03,
            (byte) 0x75, (byte) 0x03, (byte) 0x75, (byte) 0x03,
            (byte) 0x75, (byte) 0x03 });
        }
        else if (apduBuffer[ISO7816.OFFSET_INS] == (byte) 0x01){
           KeyElement.getKey(e, (short) 0x7503, apduBuffer);
           setOutgoingAndSend((short) 0x0000, (short) 0x0020);
        }
        else {
           KeyElement.getKey(e, (short) 0x7514, apduBuffer);
           setOutgoingAndSend((short) 0x0000, (short) 0x0020);
        }
    } catch (ArithmeticException e) {
        ISOException.throwIt((short) 0x0100);
    } catch (ArrayStoreException e) {
        ISOException.throwIt((short) 0x0200);
    } catch (APDUException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x03,
            (byte) e.getReason()));
    } catch (CryptoException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x04,
            (byte) e.getReason()));
    } catch (ISOException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x05,
            (byte) e.getReason()));
    } catch (PINException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x06,
            (byte) e.getReason()));
    } catch (ServiceException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x07,
            (byte) e.getReason()));
    } catch (SystemException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x08,
            (byte) e.getReason()));
    } catch (TransactionException e) {
        ISOException.throwIt(Util.makeShort((byte) 0x09,
            (byte) e.getReason()));
    } catch (ClassCastException e) {
        ISOException.throwIt((short) 0x0A00);
    } catch (IndexOutOfBoundsException e) {
        ISOException.throwIt((short) 0x0B00);
    } catch (NegativeArraySizeException e) {
        ISOException.throwIt((short) 0x0C00);
    } catch (NullPointerException e) {
        ISOException.throwIt((short) 0x0D00);
    } catch (SecurityException e) {
        ISOException.throwIt((short) 0x0E00);
    } catch (RuntimeException e) {
    }
 }


    static private short makeShort(final byte value) {
    return Util.makeShort((byte) 0x00, value);
    }

    private void setOutgoingAndSend(final short dataOffset, short dataLength) throws ISOException {
    APDU.getCurrentAPDU().setOutgoingAndSend(dataOffset, dataLength);
    }
}

所以我写了 if/else 条件来计算需要多少时间:

So I wrote if/else conditions to bench how many time it takes to:

  • 填写列表(INS = 0x00)
  • 获取列表的最后一个元素(当INS = 0x01)
  • 获取列表的第一个元素(当INS = 0x02)
  • Fill the list (INS = 0x00)
  • get the last element of the list (when INS = 0x01)
  • get the first element of the list (when INS = 0x02)

因此,使用上面给出的代码,我可以毫无问题地在 Java 卡上安装我的小程序.

So with the code given above, I can install my applet on a Java Card without problems.

但是当我运行我的小程序时,我得到一个异常 0x0D00,它引用了一个 NullPointerException.

But when I run my applet, I get an exception 0x0D00 which makes reference to a NullPointerException.

所以呢?我是否必须得出结论,我的 KeyElement 对象实例化失败了?但是,由于它是在安装时制作的,我认为它应该在安装过程中返回一个错误,但它没有.

So, what? Do I have to conclude that my KeyElement object instanciation failed? But, as it's made at the installation time, I thought it should return me an error during the installation but it didn't.

更准确地说,它在步骤失败:

More precisely it fails at step:

// Send result.
 try {
   if (apduBuffer[ISO7816.OFFSET_INS] == (byte) 0x00) {
      for (short i = 0x0000; i < (short) 0x0008; i++) {
         rand.generateData(rdm, (short) 0x0000, (short) rdm.length);
         KeyElement.addKey(e, i, rdm);
      }
...

更准确地说,在 KeyElement.addKey(e, i, rdm); 这就是为什么 e 似乎引发了 NullPointerException 甚至安装成功...

And more precisely at KeyElement.addKey(e, i, rdm); that's why it seems that e raises the NullPointerException even the installation was sucessfull...

推荐答案

您尚未创建 rand 的实例,该实例会生成 NullPointerException.您需要使用:

You have not create the instance of rand, which generates the NullPointerException. You need to use:

RandomData.getInstance(byte algorithm) 

有算法

public static final byte    ALG_PSEUDO_RANDOM   1
public static final byte    ALG_SECURE_RANDOM   2

这篇关于Java Card 中的 AESKey LinkedList/Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆