Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Isoapplet v1 #29

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
send EC public key in chunks and remove buffer length check
  • Loading branch information
swissbit-csteuer committed Dec 1, 2022
commit 4ad8ffac7b84536e68987657212e5ce4870a6e72
26 changes: 23 additions & 3 deletions src/xyz/wendland/javacard/pki/isoapplet/IsoApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -879,12 +879,23 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
short len, r;
byte[] buf = apdu.getBuffer();

short le = apdu.setOutgoing();

// Return pubkey. See ISO7816-8 table 3.
len = (short)(7 // We have: 7 tags,
+ (key.getSize() >= LENGTH_EC_FP_512 ? 9 : 7) // 7 length fields, of which 2 are 2 byte fields when using 521 bit curves,
+ 8 * field_bytes + 4); // 4 * field_len + 2 * 2 field_len + cofactor (2 bytes) + 2 * uncompressed tag

pos += UtilTLV.writeTagAndLen((short)0x7F49, len, buf, pos);

// Total size = sizeof(Start-TLV) + len = pos + len
short toSend = (short)(len + pos);
if (toSend < le) {
apdu.setOutgoingLength(toSend);
} else if (toSend > le) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}

// Prime - "P"
len = field_bytes;
pos += UtilTLV.writeTagAndLen((short)0x81, len, buf, pos);
Expand All @@ -897,7 +908,10 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
} else if (r > len) {
throw InvalidArgumentsException.getInstance();
}
// Send the data after each field to avoid buffer overflows
pos += len;
apdu.sendBytes((short)0, pos);
pos = (short)0;

// First coefficient - "A"
len = field_bytes;
Expand All @@ -910,6 +924,8 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
throw InvalidArgumentsException.getInstance();
}
pos += len;
apdu.sendBytes((short)0, pos);
pos = (short)0;

// Second coefficient - "B"
len = field_bytes;
Expand All @@ -922,6 +938,8 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
throw InvalidArgumentsException.getInstance();
}
pos += len;
apdu.sendBytes((short)0, pos);
pos = (short)0;

// Generator - "PB"
len = (short)(1 + 2 * field_bytes);
Expand All @@ -934,6 +952,8 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
throw InvalidArgumentsException.getInstance();
}
pos += len;
apdu.sendBytes((short)0, pos);
pos = (short)0;

// Order - "Q"
len = field_bytes;
Expand All @@ -946,6 +966,8 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
throw InvalidArgumentsException.getInstance();
}
pos += len;
apdu.sendBytes((short)0, pos);
pos = (short)0;

// Public key - "PP"
len = (short)(1 + 2 * field_bytes);
Expand All @@ -964,9 +986,7 @@ private void sendECPublicKey(APDU apdu, ECPublicKey key) throws InvalidArguments
pos += UtilTLV.writeTagAndLen((short)0x87, len, buf, pos);
Util.setShort(buf, pos, key.getK());
pos += 2;

// buf now contains the complete public key.
apdu.setOutgoingAndSend((short)0, pos);
apdu.sendBytes((short)0, pos);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/xyz/wendland/javacard/pki/isoapplet/UtilTLV.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ public static short writeTagAndLen(short tag, short len, byte[] out, short outOf
if(len < 0) {
throw InvalidArgumentsException.getInstance();
}
if((short)(tagLen + getLengthFieldLength(len)) > (short)(outLen - outOffset)) {
throw NotEnoughSpaceException.getInstance();
}

if(tagLen == 1) {
out[pos] = (byte)(tag & (short)0x00FF);
Expand Down