I'm trying to send a public key from a java program to the javacard. I
do this by sending both the exponent and the modulus of the public key
to the card. However, I have some trouble sending the (large) modulus:
I then send an APDU to the card: new CommandAPDU(CLA, INS, (byte) 0x00, (byte) 0x00, getModulus()));
privatevoid setModulus(APDU apdu) {
byte buf[] = apdu.getBuffer();
short len = (short)((short) buf[(short)ISO7816.OFFSET_LC] & (short)0x00FF);
short numBytes = apdu.setIncomingAndReceive();
while (numBytes < len)
numBytes += apdu.receiveBytes((short)((short)ISO7816.OFFSET_CDATA+numBytes)); // APS- copy more incoming bytes to APDU buffer starting at specific buffer offset
Util.arrayCopy(buf, ISO7816.OFFSET_CDATA, MYMODULUS, (short)(MYMODULUS.length-numBytes), numBytes);
// Send results
apdu.setOutgoing();
apdu.setOutgoingLength((short)numBytes );
apdu.sendBytesLong(buf, (short) ISO7816.OFFSET_CDATA, (short)numBytes );
}
This seems to go okay (the above '//send results' part shows the correct
modulus when I print it), however when I try to set the publickey right
after that it seems not to be possible:
Re: Sending RSA public key modulus to card
03.12.2009 17:32
(reply 2
of 30) (In reply to
#1 )
Hi,
Can you include the APDU's traffic to and from the card?
Also, try moving short numBytes = apdu.setIncomingAndReceive(); to the
start of the method before accessing the buffer. This method starts the
transfer process and calls getBytes to get the first lot of data from
the buffer. Make sure this method is only called once. A good place to
do this is in the applet process method.
Re: Sending RSA public key modulus to card
03.12.2009 17:45
(reply 3
of 30) (In reply to
#2 )
I'm not exactly sure what you mean. The APDU when loading the applet
to the card? I have no trouble installing. I can't see the APDU's when
running from the Java program.
What I want to do is transfer a RSA public key from a Java application
to a card. My initial code is to large to post here (could mail it if
you wish), but I'm quite stuck. I've tried sending the exponent and the
modulus to the card. The exponent is short and works okay, the modulus
doesn't. I need two store in fact 2 public keys on the card. If I store 2
keys, 2 exponents, and 2 modulo, then the memory will be full right?..
Re: Sending RSA public key modulus to card
03.12.2009 17:54
(reply 4
of 30) (In reply to
#3 )
Hi,
What I was looking for was the APDU's that your Java application is sending, not the install APDU's.
What is the RSA key size you are using? If you are using RSA 2048 bit
keys, the modulus will be 256 bytes that you have to send to the card.
Also if the modulus works out to be a negative number, Java will pad it
with a 0x00 byte automatically making 257 bytes. This will cause you
problems since you can only send 255 bytes in a single standard length
APDU. You may need to implement APDU chaining.
Re: Sending RSA public key modulus to card
03.12.2009 17:58
(reply 5
of 30) (In reply to
#4 )
1024 bit keys. I don't know if I need chaining, it's a 128 bytes
modulus then right? Is sending the modulus/exponent to the card the way
to go? Can't I just sent the key in one step? If yes, how?
Re: Sending RSA public key modulus to card
03.12.2009 18:40
(reply 6
of 30) (In reply to
#5 )
That should be fine.
You can indeed send the key in one go. You would simply send the DER
encoded key object (PKCS#1 defines the ASN.1 structure of the key). Once
you send the key to the card, you will have to parse the DER-TLV. Since
the key is a SEQUENCE of INTEGERS, you can extract the modulus and
exponent from the stream. You still need to call setModulus and
setExponent on the key object but you can do this in one APDU so it
simplifies the communication with the card. You would then have some
code like the following:
case SET_KEY:
key.clearKey();
key.setModulus(buf, modOff, modLen);
key.setExponent(buf, expOff, expLen);
There are a few things to be aware of with RSA keys. If the modulus is
negative and Java has padded the key object for you, you will need to
remove the padding byte before setting the key component on card.
With your previous error, it would seem that either the modulus length
does not match the key size of the key you created or your card does not
support the algorithm. As a guess, I would say that the former is more
likely (padded modulus?).
Re: Sending RSA public key modulus to card
03.12.2009 18:49
(reply 7
of 30) (In reply to
#6 )
'You would simply send the DER encoded key object (PKCS#1 defines the
ASN.1 structure of the key). Once you send the key to the card, you will
have to parse the DER-TLV.'
Uff, this is all waaay too technical for me my friend. Surely someone
has done this before right, and surely there's a simple way of doing
this. I just want an example on how to do this, not an understanding of
all the details.. I hope that isn't necessary to get it to work. RSA on a
smartcard,... it must be possible in a non-super-technical way.
Re: Sending RSA public key modulus to card
03.12.2009 18:55
(reply 8
of 30) (In reply to
#7 )
Helpful
To get the DER encoded key you just have to do the following:
pubKey.getEncoded(); assuming that pubKey is an instance of an
RSAPublicKey. This would be what you send as you data in the APDU.
Re: Sending RSA public key modulus to card
03.12.2009 19:33
(reply 13
of 30) (In reply to
#12 )
That is a good place to start. Just remember that you applet will only
do encryption. The host application (J2SE) will do the decryption with
the private key.
Re: Sending RSA public key modulus to card
03.12.2009 19:47
(reply 14
of 30) (In reply to
#13 )
Right. I'm having my card communicate with a host application (host1),
and also another application (host2). I want secure connections between
the card and host1, and between host1 and host2 (card and host2 do not
directly communicate).
I'm not sure why the applet should only do encryption?
This topic has
30
replies
on
3
pages.
1
|
2
|
3
|
Next »
Oracle Forums - On Friday November 5th, the Sun Forums will be redirected to Oracle Forums. Please use the new forums at
http://forums.oracle.com. For more information on the migration, read the Forums Migration FAQ
Forums Statistics
About Sun forums
Sun Forums is a large collection of user generated
discussions. It is here to help you ask questions, find answers, and
participate in discussions.
Check out our guide on Getting
started with Sun Forums for a full walkthrough of how to best
leverage the benefits of this community.