Page 1 of 1

Failed to generate RSA Key

Posted: Fri Feb 19, 2016 5:36 am
by Sajaki
I am trying to deploy an application on a CREF emulated card which generates an RSA KeyPair and sends the public key over to the host RMI Client application.

Here is my applet code:

Code: Select all

package sid2;
 
import java.rmi.RemoteException;
 
import javacard.framework.UserException;
import javacard.framework.service.CardRemoteObject;
import javacard.security.*;
import javacardx.crypto.Cipher;
 
public class CompteurImpl extends CardRemoteObject implements ICompteur {
   
    private byte compteur = 120;
    RSAPrivateKey  rsa_PrivateKey;
    RSAPublicKey rsa_PublicKey;
    KeyPair rsa_KeyPair;
    Cipher cipherRSA;
   
    public void setPub(byte[] expo,byte[] mod){
       
        rsa_PublicKey.setExponent(expo, (short)0, (short)expo.length);
        rsa_PublicKey.setModulus(mod, (short)0, (short)mod.length);
    }
   
    public byte[] getPub(){
        byte[] ret = null;
        rsa_PublicKey.getModulus(ret, (short)0);
        rsa_PublicKey.getExponent(ret, (short)(ret.length+1));
       
        return ret;
    }
   
 
    public void initialiser(byte v) throws RemoteException, UserException {
 
        rsa_KeyPair = new KeyPair( KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048);
        rsa_KeyPair.genKeyPair();
        rsa_PublicKey = (RSAPublicKey) rsa_KeyPair.getPublic();
        rsa_PrivateKey = (RSAPrivateKey) rsa_KeyPair.getPrivate();
        compteur = v;
       
    }
   
}


But when I run the init method from the client application, I get the exception below. Could anyone help me to let me know what is wrong? Thanks

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.javacard.impl.NativeMethods.getCurrentContext()B
at com.sun.javacard.impl.NativeMethods.getCurrentContext(Native Method)
at com.sun.javacard.impl.PrivAccess.getCurrentAppID(PrivAccess.java:454)
at javacard.framework.CardRuntimeException.<init>(CardRuntimeException.java:46)
at javacard.security.CryptoException.<init>(DashoA10*..:25)
at com.sun.javacard.javax.smartcard.rmiclient.CardObjectFactory.throwIt(Unknown Source)
at com.sun.javacard.javax.smartcard.rmiclient.CardObjectFactory.throwException(Unknown Source)
at com.sun.javacard.javax.smartcard.rmiclient.CardObjectFactory.getObject(Unknown Source)
at com.sun.javacard.rmiclientlib.JCRemoteRefImpl.parseAPDU(Unknown Source)
at com.sun.javacard.rmiclientlib.JCRemoteRefImpl.invoke(Unknown Source)
at sid2.CompteurImpl_Stub.initialiser(Unknown Source)
at sid2.ClientRmi.main(ClientRmi.java:36)

Re: Failed to generate RSA Key

Posted: Sun Feb 21, 2016 12:57 am
by UNKNwYSHSA
How you run the init method?

Re: Failed to generate RSA Key

Posted: Mon Feb 22, 2016 3:48 am
by btwtiger
You may need to check the documentation, but from memory the CREF emulator only supports 512 bit keys.

Re: Failed to generate RSA Key

Posted: Mon Feb 22, 2016 3:51 am
by btwtiger
The below code also has the issue that you are trying to copy into a null buffer. The method does not allocate memory for you. You need to allocate a buffer, or use a previously allocated buffer.

Code: Select all

 public byte[] getPub(){
        byte[] ret = null;
        rsa_PublicKey.getModulus(ret, (short)0);
        rsa_PublicKey.getExponent(ret, (short)(ret.length+1));
       
        return ret;
    }