Skip to content

Commit

Permalink
Thanks to United Access GmbH added support for oldest JCOP 2.4.1 and …
Browse files Browse the repository at this point in the history
…2.4.2 cards
  • Loading branch information
vletoux committed Sep 17, 2019
1 parent 7a8626c commit 88d70a2
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/com/mysmartlogon/gidsApplet/CRTKeyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void importKey(byte[] buffer, short offset, short length) throws InvalidA
// focused on A5 tag
short innerPos = 0, innerLen = 0;
short pos = 0, len = 0;
// keytype is missing for symetric key
// keytype is missing for symmetric key
byte keytype = 1;
byte keyref = 0;
if (buffer[offset] != (byte) 0xA5) {
Expand All @@ -140,7 +140,7 @@ public void importKey(byte[] buffer, short offset, short length) throws InvalidA
}
keytype = buffer[(short) (pos+2)];
} catch (NotFoundException e) {
// optional tag: default = symetric key
// optional tag: default = symmetric key
} catch (InvalidArgumentsException e) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
}
Expand All @@ -167,7 +167,7 @@ public void importKey(byte[] buffer, short offset, short length) throws InvalidA

pos += 1 + UtilTLV.getLengthFieldLength(buffer, (short)(pos+1));
if (keytype == 1) {
importSymetricKey(buffer, pos, len);
importSymmetricKey(buffer, pos, len);
} else if (keytype == 2) {
importRsaKey(buffer, pos, len);
} else {
Expand Down Expand Up @@ -247,11 +247,17 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
}
len = UtilTLV.decodeLengthField(buffer, (short)(pos+1));
pos += 1 + UtilTLV.getLengthFieldLength(buffer, (short)(pos+1));
// the minidriver may prepend a 00 before (len = len+1) and the javacard don't like it => remove the 00
// the minidriver may prepend a 00 before (len = len+1) and the javacards don't like it => remove the 00
if ((len & 0x0F) == (byte) 1 && buffer[pos] == 0) {
len -= 1;
pos++;
}
// one byte too small?
if ((len & 0x0F) == (byte) 0x0F) {
len += 1;
pos--;
buffer[pos] = (byte) 0x00;
}
try {
rsaPrKey.setP(buffer, pos, len);
} catch(CryptoException e) {
Expand All @@ -271,6 +277,11 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
len -= 1;
pos++;
}
if ((len & 0x0F) == (byte) 0x0F) {
len += 1;
pos--;
buffer[pos] = (byte) 0x00;
}
rsaPrKey.setQ(buffer, pos, len);
pos += len;
// d mod p-1
Expand All @@ -279,10 +290,15 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
}
len = UtilTLV.decodeLengthField(buffer, (short)(pos+1));
pos += 1 + UtilTLV.getLengthFieldLength(buffer, (short)(pos+1));
if ((len & 0x0F) == (byte) 1 && buffer[pos] == 0) {
if (((len & 0x0F) == (byte) 0x01) && (buffer[pos] == 0)) {
len -= 1;
pos++;
}
if ((len & 0x0F) == (byte) 0x0F) {
len += 1;
pos--;
buffer[pos] = (byte) 0x00;
}
rsaPrKey.setDP1(buffer, pos, len);
pos += len;
// d mod q-1
Expand All @@ -295,6 +311,11 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
len -= 1;
pos++;
}
if ((len & 0x0F) == (byte) 0x0F) {
len += 1;
pos--;
buffer[pos] = (byte) 0x00;
}
rsaPrKey.setDQ1(buffer, pos, len);
pos += len;
// q-1 mod p
Expand All @@ -307,6 +328,11 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
len -= 1;
pos++;
}
if ((len & 0x0F) == (byte) 0x0F) {
len += 1;
pos--;
buffer[pos] = (byte) 0x00;
}
rsaPrKey.setPQ(buffer, pos, len);
pos += len;

Expand All @@ -324,7 +350,7 @@ private void importRsaKey(byte[] buffer, short offset, short length) throws Inva
}
}

private void importSymetricKey(byte[] buffer, short offset, short length) {
private void importSymmetricKey(byte[] buffer, short offset, short length) {
clearContents();
byte[] key = new byte[length];
Util.arrayCopyNonAtomic(buffer, offset, key, (short) 0, length);
Expand Down

0 comments on commit 88d70a2

Please sign in to comment.