Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/main/com/mysmartlogon/gidsApplet/CRTKeyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import javacard.framework.Util;
import javacard.security.CryptoException;
import javacard.security.KeyBuilder;
import javacard.security.KeyPair;
import javacard.security.RSAPrivateCrtKey;
import javacard.security.RSAPublicKey;

Expand All @@ -44,7 +43,8 @@ public class CRTKeyFile extends ElementaryFile {
private final short posCRT;
private final short lenCRT;

private KeyPair keyPair = null;
private RSAPublicKey rsaPublicKey = null;
private RSAPrivateCrtKey rsaPrivateKey = null;
private byte[] symmetricKey = null;

public CRTKeyFile(short fileID, byte[] fileControlInformation, short pos, short len) {
Expand All @@ -63,22 +63,31 @@ void clearContents() {
if (symmetricKey != null) {
symmetricKey = null;
}
if (keyPair != null) {
keyPair.getPrivate().clearKey();
keyPair = null;
if (rsaPublicKey != null) {
rsaPublicKey.clearKey();
rsaPublicKey = null;
}
if (rsaPrivateKey != null) {
rsaPrivateKey.clearKey();
rsaPrivateKey = null;
}
if(JCSystem.isObjectDeletionSupported()) {
JCSystem.requestObjectDeletion();
}
}

public void SaveKey(KeyPair kp) {
public void SaveKey(RSAPublicKey publicKey, RSAPrivateCrtKey privateKey) {
clearContents();
keyPair = kp;
rsaPublicKey = publicKey;
rsaPrivateKey = privateKey;
}

public RSAPublicKey GetPublicKey() {
return rsaPublicKey;
}

public KeyPair GetKey() {
return keyPair;
public RSAPrivateCrtKey GetPrivateKey() {
return rsaPrivateKey;
}

public void CheckUsage(byte operation, byte algRef) throws NotFoundException {
Expand Down Expand Up @@ -342,7 +351,8 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
// If the key is usable, it MUST NOT remain in buf.
Util.arrayFillNonAtomic(buffer, offset, length, (byte)0x00);
clearContents();
this.keyPair = new KeyPair(rsaPuKey, rsaPrKey);
this.rsaPublicKey = rsaPuKey;
this.rsaPrivateKey = rsaPrKey;
if(JCSystem.isObjectDeletionSupported()) {
JCSystem.requestObjectDeletion();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/com/mysmartlogon/gidsApplet/GidsApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private void processGetData(APDU apdu) throws ISOException {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
}
file.CheckPermission(pinManager, File.ACL_OP_KEY_GETPUBLICKEY);
PublicKey pk = file.GetKey().getPublic();
PublicKey pk = file.GetPublicKey();

// Return pubkey. See ISO7816-8 table 3.
try {
Expand Down Expand Up @@ -463,7 +463,7 @@ public void processGenerateAsymmetricKeypair(APDU apdu) throws ISOException {
}
ISOException.throwIt(ISO7816.SW_UNKNOWN);
}
file.SaveKey(kp);
file.SaveKey((RSAPublicKey) kp.getPublic(), (RSAPrivateCrtKey) kp.getPrivate());

// Return pubkey. See ISO7816-8 table 3.
try {
Expand Down Expand Up @@ -718,7 +718,7 @@ private void decipher(APDU apdu) {
// Get the key - it must be an RSA private key,
// checks have been done in MANAGE SECURITY ENVIRONMENT.
CRTKeyFile key = (CRTKeyFile) currentKey[0];
PrivateKey theKey = key.GetKey().getPrivate();
PrivateKey theKey = key.GetPrivateKey();

// Check the length of the cipher.
// Note: The first byte of the data field is the padding indicator
Expand Down Expand Up @@ -767,7 +767,7 @@ private void computeDigitalSignature(APDU apdu) throws ISOException {
lc = transmitManager.doChainingOrExtAPDU(apdu);

// RSA signature operation.
rsaKey = key.GetKey().getPrivate();
rsaKey = key.GetPrivateKey();

rsaRawCipher.init(rsaKey, Cipher.MODE_ENCRYPT);
sigLen = rsaRawCipher.doFinal(ram_buf, (short) 0, lc, ram_buf, (short)0);
Expand All @@ -782,7 +782,7 @@ private void computeDigitalSignature(APDU apdu) throws ISOException {
lc = apdu.setIncomingAndReceive();

// RSA signature operation.
rsaKey = key.GetKey().getPrivate();
rsaKey = key.GetPrivateKey();

if(lc > (short) 247) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
Expand Down