1

我想在我的 Java Card 上签署一些数据(MESSAGE 字节数组),然后在响应 APDU 中返回签名。我的代码在没有apdu.sendBytes(BAS, sSignLen)行的情况下工作正常(或者至少我认为它可以正常工作并且它返回 9000),但是当我取消注释它时,我得到一个未知错误(0xC000002B (Unknown error.))。

当我尝试在响应 APDU 中发送其他数据时,它可以完美运行。

apdu.setIncomingAndReceive();
Util.arrayCopyNonAtomic(MESSAGE, (short) 0, buffer, (short) 0, (short) MESSAGE.length);
apdu.setOutgoingAndSend((short) 0, (short) MESSAGE.length);

这是我的代码。我做错了什么或错过了什么?谢谢!

public class TestApplet extends Applet {

    ...

    private final static byte SIGN = (byte) 0x01;

    ...

    private final static byte[] MESSAGE = new byte[] { 'M', 'e', 's', 's', 'a', 'g', 'e' };

    final static short BAS = 0;

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

        byte buffer[] = apdu.getBuffer();

        ...

        switch (buffer[ISO7816.OFFSET_INS]) {
        case SIGN:
            try {
                ECDSAKeyPair = Secp256k1Domain.getKeyPairParameter();
                ECDSAKeyPair.genKeyPair();

                ECDSAPublicKey = (ECPublicKey) ECDSAKeyPair.getPublic();
                ECDSAPrivateKey = (ECPrivateKey) ECDSAKeyPair.getPrivate();

                ECDSASignature = Signature.getInstance(Signature.ALG_ECDSA_SHA, false);

                short signLen = 0;

                byte[] signatureArray = new byte[70];

                ECDSASignature.init(ECDSAPrivateKey, Signature.MODE_SIGN);
                signLen = ECDSASignature.sign(MESSAGE, BAS, (short) MESSAGE.length, signatureArray, BAS);

                apdu.setIncomingAndReceive();
                Util.arrayCopyNonAtomic(signatureArray, (short) 0, buffer, (short) 0, (short) signatureArray.length);
                apdu.setOutgoingAndSend((short) 0, (short) signatureArray.length);
            } catch (CryptoException c) {
                short reason = c.getReason();
                ISOException.throwIt((short) ((short) (0x9C00) | reason));
            }

            break;

        ...

        return;
    }
}
4

1 回答 1

2

它可能signLen大于 Ne 值(Le在 JavaCard 规范中被错误地调用)。顺便说一句,您也在滥用Le价值(short) MESSAGE.length。Ne 表示期望被发回的最大字节数。

于 2015-05-08T17:40:16.060 回答