I have an issue with obtaining the RSA public key modulus with my Java Card applet implementation: The command SEND_PUB (see case statement case SEND_PUB
) for sending the public 512 bits RSA key returns the status word 6F 00. What could be wrong with my implementation?
public class crypto extends Applet {
private static final boolean NO_EXTERNAL_ACCESS = false;
private static byte[] file=new byte[128];
private static byte[] SignedFile=new byte[20];
private static RSAPublicKey p;
private static RSAPublicKey publicKey;
private static RSAPrivateKey privateKey;
private static KeyPair keyPair;
Signature sig;
private final static byte ALOC= 0x07; //vérifier le code PIN
private final static byte INS_PIN= 0x01; //vérifier le code PIN
private final static byte INS_PUK= 0x02; //vérifier le code PUK
private final static byte UPD_PIN= 0x03; //modifier le code PIN
private final static byte RCV_FILE= 0x04; //recvoir le fichier
private final static byte SIGNATURE= 0x05; //Récupérer la clé privée
private final static byte SEND_PUB= 0x06; //envoyer la la clé publique
public static OwnerPIN pin,puk;
public static void install(byte[] bArray, short bOffset, byte bLength) {
new crypto();
}
protected crypto() {
register();
puk = new OwnerPIN(nbre_tentative, length);
puk.update(code_puk, (short) 0, length);
pin = new OwnerPIN(nbre_tentative, length);
pin.update(code_pin, (short) 0, length);
// publicKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_512,true);
// keyPair = new KeyPair(KeyPair.ALG_RSA, (short) publicKey.getSize());
// publicKey = (RSAPublicKey) keyPair.getPublic();
KeyPair rsa_KeyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_512);
rsa_KeyPair.genKeyPair();
RSAPublicKey p = (RSAPublicKey) rsa_KeyPair.getPublic();
//RSAPrivateKey rsa_PrivateCrtKey 0= (RSAPrivateKey) rsa_KeyPair.getPrivate();
// cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
}
public void process(APDU apdu) {
byte[] buffer = apdu.getBuffer();
if(selectingApplet())
return;
if(buffer[ISO7816.OFFSET_CLA] != CLA)
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
switch (buffer[ISO7816.OFFSET_INS])
{
case SEND_PUB :
//this is to send the modulus
p.getModulus(buffer, ISO7816.OFFSET_CDATA);
apdu.setOutgoing();
apdu.setOutgoingLength((short) 64);
apdu.sendBytesLong(buffer, ISO7816.OFFSET_CDATA, (short) 64);
case SIGNATURE :
Signature s = Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1, false);
s.init(privateKey, Signature.MODE_SIGN);
short sigLen = s.sign(file,(short)0, (short)file.length,SignedFile, (short)0);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
}
You receive status word 6F 00
because you get a NullPointerException
when accessing p
on the line p.getModulus(...);
. The reason for this is that the instance field p
was never initialized (at least not with the code that you showed in your question above) and is, consequently, null
.
Note that the line
RSAPublicKey p = (RSAPublicKey) rsa_KeyPair.getPublic();
does not assign the public key object to the the instance field p
but to a local variable that is also named p
and, thus, hides the instance field.