Skip to content

Commit

Permalink
fix extended APDUs
Browse files Browse the repository at this point in the history
The extended APDU feature flag is still helpful to maintain
backwards compatibility in OpenSC.
Fix length check in UtilTLV: An extended APDU buffer can be
up to 65536 bytes long while the maximum positive value of
a short is 32767.
  • Loading branch information
swissbit-csteuer committed Nov 25, 2022
1 parent 4ac198b commit 6b5ba9b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/xyz/wendland/javacard/pki/isoapplet/IsoApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class IsoApplet extends Applet implements ExtendedLength {
private static final byte STATE_OPERATIONAL_DEACTIVATED = (byte) 0x04; // Applet usage is deactivated. (Unused at the moment.)
private static final byte STATE_TERMINATED = (byte) 0x0C; // Applet usage is terminated. (Unused at the moment.)

private static final byte API_FEATURE_EXT_APDU = (byte) 0x01;
private static final byte API_FEATURE_SECURE_RANDOM = (byte) 0x02;
private static final byte API_FEATURE_ECC = (byte) 0x04;
private static final byte API_FEATURE_RSA_SHA256_PSS = (byte) 0x08;
Expand Down Expand Up @@ -156,7 +157,7 @@ public static void install(byte[] bArray, short bOffset, byte bLength) {
* \brief Only this class's install method should create the applet object.
*/
protected IsoApplet() {
api_features = 0;
api_features = API_FEATURE_EXT_APDU;
pin = new OwnerPIN(PIN_MAX_TRIES, PIN_MAX_LENGTH);
fs = new IsoFileSystem();

Expand Down
3 changes: 1 addition & 2 deletions src/xyz/wendland/javacard/pki/isoapplet/UtilTLV.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public static short getLengthFieldLength(short length) throws InvalidArgumentsEx
public static short writeTagAndLen(short tag, short len, byte[] out, short outOffset) throws InvalidArgumentsException, NotEnoughSpaceException {
byte tagLen;
short pos = outOffset;
short outLen = (short)out.length;

if((short)(tag & (short)0xFF00) != 0) {
if((short)(tag & (short)0x1F00) != (short)0x1F00) {
Expand All @@ -197,7 +196,7 @@ 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)) {
if((int) tagLen + (int) getLengthFieldLength(len) > out.length - (int) outOffset) {
throw NotEnoughSpaceException.getInstance();
}

Expand Down

0 comments on commit 6b5ba9b

Please sign in to comment.