Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Kryo NullPointerException when loading class KeyPair variable with ALG_EC_FP keys #213

Open
dempa opened this issue Dec 13, 2024 · 3 comments

Comments

@dempa
Copy link

dempa commented Dec 13, 2024

It looks like there is an issue when loading (deserializing) an installed applet with a KeyPair class variable that is initialized with ALG_EC_FP and has generated keys.

Using algorithm ALG_RSA does not trigger the crash.

public class OTPApplet extends Applet {
    private final KeyPair keyPair;
    OTPApplet(byte[] bArray, short bOffset, byte bLength) {
        //keyPair = new KeyPair(ALG_RSA, (short) 1024);  // works on both installation and on load
        keyPair = new KeyPair(ALG_EC_FP, (short) 256); // works on installation but crashes on load

        keyPair.genKeyPair(); // note: for the crash to occur the keys must be generated

The installation / instantiation of the applet works fine.
When the app is restarted it throws an exception in SimulatorRuntime::loadApplet()

Trying to load an instance of com.licel.globalplatform.GpSimulatorRuntime
Failed to load the instance! Will use the default SimulatorRuntime
com.licel.jcardsim.esotericsoftware.kryo.KryoException: java.lang.NullPointerException: Attempt to get length of null array
Serialization trace:
preCompTable (com.licel.jcardsim.bouncycastle.math.ec.ECPoint$Fp)
G (com.licel.jcardsim.bouncycastle.crypto.params.ECDomainParameters)
params (com.licel.jcardsim.bouncycastle.crypto.generators.ECKeyPairGenerator)
engine (com.licel.jcardsim.crypto.KeyPairImpl)
impl (javacard.security.KeyPair)
keyPair (com.licel.samples.applet.OTPApplet)

The issue was found when trying out the app https://github.com/licel/hotp-authenticator which is using jcardsim-3.0.5.jar

@agreenbhm
Copy link

I'm seeing this as well. Any workaround you found?

@dempa
Copy link
Author

dempa commented Jan 31, 2025

I'm seeing this as well. Any workaround you found?

I did something like this to avoid the issue:

public class TestApplet extends Applet {
    private final ECPublicKey publicKey;
    private final ECPrivateKey privateKey;

    TestApplet(byte[] bArray, short bOffset, byte bLength) {
        publicKey = (ECPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PUBLIC, KeyBuilder.LENGTH_EC_FP_256, false);
        privateKey = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, KeyBuilder.LENGTH_EC_FP_256, false);
        EC.setCurveParameters(publicKey);
        EC.setCurveParameters(privateKey);
    }

    private short someInitMethodCalledOnlyOnce() {
        KeyPair keyPair = new KeyPair(publicKey, privateKey);
        keyPair.genKeyPair();
    }
}

@agreenbhm
Copy link

privateKey = (ECPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, KeyBuilder.LENGTH_EC_FP_256, false);

Thanks for the reply. Would you mind sharing an example of where you are calling that "someInitMethodCalledOnlyOnce" method? Struggling to find the right place to put it.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants