LozCodes
LozCodes

Reputation: 119

How can I send my public key in an APDU (Javacard)?

I'm trying to send my public key from my key pair in an APDU so that a signature can be verified in another program. So far I've not been able to find anyway to get the key into the APDU buffer.

I have found solutions online that use RSAPublicKey.getExponent() to store the key in a byte array (then add it to the buffer) but as I'm not using an RSAPublicKey this has not been that helpful. Is this the only way to do it? Do I need to use RSAPublicKey instead of a key pair, or is there some way of putting the public key from my key pair into the APDU buffer?

This is my key generation code:

KeyPair key = new KeyPair(KeyPair.ALG_RSA, (short)5120);
key.genKeyPair(); 

Upvotes: 0

Views: 311

Answers (2)

Michal Gluchowski
Michal Gluchowski

Reputation: 1237

First, RSA public key consists of modulus and exponent. First you will need to use getPublic() and recover object having RSAPublicKey interface that exposes getExponent and getModulus methods that will get you byte arrays that you can put into APDU buffer. Public exponent will probably be something like 0x010001, but a modulus will be of bitlength you declare as your KeyPair parameter.

For such long modulus, you will need extended APDU or split retrieving this into several APDU exchanges. For compatibility I would normally recommend the second approach but if you're dealing with system where you are sure to handle extended APDU on both ends, you can try and do it in one go.

Upvotes: 1

Paul Bastian
Paul Bastian

Reputation: 2647

I don't have a valid JavaCard IDE in front of me but something along the lines should work:

KeyPair keyPair = new KeyPair(KeyPair.ALG_RSA, (short)xxx);
keyPair.genKeyPair();
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublicKey();
short len = pubkey.getExponent(apdu.getBuffer(), (short)0);

and continue from here...

Upvotes: 0

Related Questions