Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

Got 0x8888 when transfering RSA public key outside

Algorithm School

Moderator: UNKNwYSHSA

Joevictor
Posts: 9
Joined: Tue Oct 20, 2015 6:05 am
Points :22
Contact:

Got 0x8888 when transfering RSA public key outside

Post by Joevictor » Wed Dec 09, 2015 5:44 am

The code below is to generate a RSA key pair and transfer the public key to outside in the APDU response:
But I came across a problem when I transfered RSA public key outside.

Code: Select all

public class RSAKEY extends Applet {

    private static final boolean NO_EXTERNAL_ACCESS = false;
    private static final byte GENERATE_KEY_PAIR = (byte) 0xC0;

    RSAPrivateKey thePrivateKey = (RSAPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE, KeyBuilder.LENGTH_RSA_512, NO_EXTERNAL_ACCESS);
    RSAPublicKey thePublickKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_512, NO_EXTERNAL_ACCESS);
    KeyPair theKeyPair = new KeyPair(thePublickKey, thePrivateKey);

    public static void install(byte[] bArray, short bOffset, byte bLength) {
        new RSAKEY();
    }

    protected RSAKEY() {
        register();
    }

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

        byte[] buffer = apdu.getBuffer();
        short privateKeySize = 0;
        short publicKeySize = 0;
        byte[] publicArray;
        byte[] privateArray;
        try {
            switch (buffer[ISO7816.OFFSET_INS]) {

                case GENERATE_KEY_PAIR:

                    theKeyPair.genKeyPair();

                    PrivateKey thePrivateKey = theKeyPair.getPrivate();
                    PublicKey thePublicKey = theKeyPair.getPublic();

                    publicKeySize = thePrivateKey.getSize();
                    privateKeySize = thePrivateKey.getSize();

                    byte[] publicKey = JCSystem.makeTransientByteArray((short) (publicKeySize), JCSystem.CLEAR_ON_DESELECT);
                    ((RSAPublicKey) thePrivateKey).getExponent(publicKey, (short) publicKeySize);

                    Util.arrayCopyNonAtomic(publicKey, (short) 0, buffer, (short) 0, (short) (publicKeySize ));
                    apdu.setOutgoingAndSend((short) 0, (short) (publicKeySize));
                    break;

                default:
                    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        } catch (Exception e) {
            if (e instanceof CryptoException) {
                short r = ((CryptoException) e).getReason();
                ISOException.throwIt(r);
            } else {
                ISOException.throwIt((short) 0x8888);
            }
        }

    }
}

When I send the APDU to the card, I got 8888,as shown below:

>> 00 A4 04 00 06 11 22 33 44 55 01
<< 90 00
>> 00 C0 00 00
<< 88 88
Could some body tell me the reason about this?

User avatar
mabel
Posts: 237
Joined: Mon May 18, 2015 3:09 am
Points :1705
Contact:

Re: Got 0x8888 when transfering RSA public key outside

Post by mabel » Wed Dec 16, 2015 2:01 am

1. getSize() returns the bit length of the key, not the byte length.

2. ((RSAPublicKey) thePrivateKey).getExponent(publicKey, (short) publicKeySize);
This won't work. You are asking for the exponent to be stored at offset publicKeySize in array publicKey -- that is, at the very end of the array, where there are precisely 0 bytes left to store it.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 11 guests

JavaCard OS : Disclaimer