Page 1 of 1

store and access the certificates with different applets

Posted: Tue Aug 02, 2016 8:40 am
by Brewling
Hello!

I need to store the certificates within the admin applet and access with user applet. So I wonder if it is feasible to store the certificate on the java card with one applet and access it with the other one.

I appreciate any answer.

B

Re: store and access the certificates with different applets

Posted: Wed Aug 03, 2016 3:11 am
by Sajaki
Yes. you can do that with two applets. If your applets are in the same package, it will be easier. If the applets are in different packages, you need to pay attention to the applet firewall and use SOI.

Re: store and access the certificates with different applets

Posted: Wed Aug 03, 2016 10:06 pm
by Brewling
Hi, Thank you very much for your help.

My applets are in the same package. Do you have any sample that I can take as a reference?

Re: store and access the certificates with different applets

Posted: Thu Aug 04, 2016 5:28 am
by Sajaki
For your reference,

User

Code: Select all

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class Client extends Applet {

    public static void install(byte[] bArray, short bOffset, byte bLength) {
   
        new Client().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
    }

    public void process(APDU apdu) {
        // Good practice: Return 9000 on SELECT
        if (selectingApplet()) {
            return;
        }

        byte[] buf = apdu.getBuffer();
        switch (buf[ISO7816.OFFSET_INS]) {
            case (byte) 0x00:
                byte[] cert = Admin.getInstance().getCertificate();
                short certLen = Admin.getInstance().getCertLength();
                Util.arrayCopyNonAtomic(cert, (short) 0, buf, (short) 0, certLen);
                apdu.setOutgoingAndSend((short) 0, certLen);
                break;
            default:
                // good practice: If you don't know the INStruction, say so:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

}




Admin

Code: Select all

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.AppletEvent;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class Admin extends Applet implements AppletEvent {
    private final static short MAX_CERT_SIZE = 1024;
    private static Admin _instance;
    private byte[] certificate = new byte[MAX_CERT_SIZE];
    private short certLength;

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

        _instance = new Admin();
        _instance.register(bArray, (short) (bOffset + 1), bArray[bOffset]);
    }

    public void process(APDU apdu) {
        // Good practice: Return 9000 on SELECT
        if (selectingApplet()) {
            return;
        }

        byte[] buf = apdu.getBuffer();

        apdu.setIncomingAndReceive();

        switch (buf[ISO7816.OFFSET_INS]) {
            case (byte) 0x00:
                certLength = buf[ISO7816.OFFSET_LC];
                Util.arrayCopy(buf, ISO7816.OFFSET_CDATA, certificate, (short) 0, certLength);
                break;
            default:
                // good practice: If you don't know the INStruction, say so:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

    public byte[] getCertificate() {
        return certificate;
    }

    /**
     * @return the certLength
     */
    public short getCertLength() {
        return certLength;
    }

    public static Admin getInstance() {
        return _instance;
    }

    public void uninstall() {
        _instance = null;
    }

}

Re: store and access the certificates with different applets

Posted: Thu Aug 04, 2016 10:58 pm
by Brewling
Thanks a ton. Its very helpful to me.