@@ -377,11 +377,11 @@ public final class FIDO2Applet extends Applet implements ExtendedLength {
377377 /**
378378 * How many resident key slots are filled
379379 */
380- private byte numResidentCredentials ;
380+ private short numResidentCredentials ;
381381 /**
382382 * How many distinct RPs are present across all resident keys
383383 */
384- private byte numResidentRPs ;
384+ private short numResidentRPs ;
385385 /**
386386 * Storage for the largeBlobs extension
387387 */
@@ -1050,7 +1050,7 @@ private void makeCredential(APDU apdu, short lc, byte[] buffer) {
10501050 boolean uniqueRP = false ;
10511051 if (!foundMatchingRK ) {
10521052 // We're filling an empty slot
1053- numResidentCredentials ++ ;
1053+ numResidentCredentials = ( short )( numResidentCredentials + 1 ) ;
10541054 if (!foundRPMatchInRKs ) {
10551055 uniqueRP = true ;
10561056 }
@@ -1556,7 +1556,7 @@ private short checkIfPubKeyBlockSupported(APDU apdu, byte[] buffer, short readId
15561556 sendErrorByte (apdu , FIDOConstants .CTAP2_ERR_CBOR_UNEXPECTED_TYPE );
15571557 }
15581558 short valLen = (short )(typeB - 0x60 );
1559- readIdx += valLen + 1 ;
1559+ readIdx += ( short )( valLen + 1 ) ;
15601560 if (readIdx >= lc ) {
15611561 sendErrorByte (apdu , FIDOConstants .CTAP2_ERR_INVALID_CBOR );
15621562 }
@@ -1574,7 +1574,7 @@ private short checkIfPubKeyBlockSupported(APDU apdu, byte[] buffer, short readId
15741574 // Not of type "public-key", although same length
15751575 transientStorage .readyStoredVars ();
15761576 }
1577- readIdx += typeValLen + 1 ;
1577+ readIdx += ( short )( typeValLen + 1 ) ;
15781578 if (readIdx >= lc ) {
15791579 sendErrorByte (apdu , FIDOConstants .CTAP2_ERR_INVALID_CBOR );
15801580 }
@@ -1941,7 +1941,7 @@ private void getAssertion(final APDU apdu, final short lc, final byte[] buffer,
19411941 boolean acceptedMatch = false ;
19421942
19431943 short hmacSecretBytes = 0 ;
1944- byte numMatchesThisRP = 0 ;
1944+ short numMatchesThisRP = 0 ;
19451945 short rkMatch = -1 ;
19461946 short allowListLength = 0 ;
19471947
@@ -2964,11 +2964,7 @@ private boolean checkCredential(APDU apdu, byte[] credentialBuffer, short creden
29642964 }
29652965 }
29662966
2967- if (!matches ) {
2968- return false ;
2969- }
2970-
2971- return true ;
2967+ return matches ;
29722968 }
29732969
29742970 /**
@@ -4988,10 +4984,11 @@ private void handleDeleteCred(APDU apdu, byte[] buffer, short readOffset, short
49884984 } else {
49894985 residentKeys [rpHavingSameRP ].setUniqueRP (true );
49904986 }
4991- for (short j = i ; j < (short )(numResidentCredentials - 1 ); j ++) {
4987+ numResidentCredentials = (short )(numResidentCredentials - 1 );
4988+ for (short j = i ; j < numResidentCredentials ; j ++) {
49924989 residentKeys [j ] = residentKeys [(short )(j + 1 )];
49934990 }
4994- residentKeys [-- numResidentCredentials ] = null ;
4991+ residentKeys [numResidentCredentials ] = null ;
49954992 ok = true ;
49964993 } finally {
49974994 if (ok ) {
@@ -5004,10 +5001,11 @@ private void handleDeleteCred(APDU apdu, byte[] buffer, short readOffset, short
50045001 JCSystem .beginTransaction ();
50055002 boolean ok = false ;
50065003 try {
5007- for (short j = i ; j < (short )(numResidentCredentials - 1 ); j ++) {
5004+ numResidentCredentials = (short )(numResidentCredentials - 1 );
5005+ for (short j = i ; j < numResidentCredentials ; j ++) {
50085006 residentKeys [j ] = residentKeys [(short )(j + 1 )];
50095007 }
5010- residentKeys [-- numResidentCredentials ] = null ;
5008+ residentKeys [numResidentCredentials ] = null ;
50115009 ok = true ;
50125010 } finally {
50135011 if (ok ) {
@@ -5039,7 +5037,7 @@ private void handleDeleteCred(APDU apdu, byte[] buffer, short readOffset, short
50395037 * If zero, we're starting a new iteration
50405038 * @param lc Length of the incoming request, as sent by the platform
50415039 */
5042- private void handleEnumerateCreds (APDU apdu , byte [] buffer , short bufferIdx , short startCredIdx , short lc ) {
5040+ private void handleEnumerateCreds (APDU apdu , byte [] buffer , short bufferIdx , final short startCredIdx , short lc ) {
50435041 transientStorage .clearIterationPointers ();
50445042
50455043 if (startCredIdx > (short ) residentKeys .length ) { // intentional > instead of >=
@@ -5099,7 +5097,7 @@ private void handleEnumerateCreds(APDU apdu, byte[] buffer, short bufferIdx, sho
50995097 rpIdHashBuf , credIdIdx , (byte ) 3 )) {
51005098 // Cred is for this RP ID, yay.
51015099
5102- byte matchingCount = 1 ; // remember to count THIS cred as a match
5100+ short matchingCount = 1 ; // remember to count THIS cred as a match
51035101 if (startCredIdx == 0 ) {
51045102 // Unfortunately, we need to scan forward through all remaining credentials
51055103 // we're not storing a list of which creds share an RP, so this is the only way to get
@@ -5119,7 +5117,7 @@ private void handleEnumerateCreds(APDU apdu, byte[] buffer, short bufferIdx, sho
51195117 }
51205118 }
51215119 }
5122- transientStorage .setCredIterationPointer ((byte )(rkIndex + 1 )); // resume iteration from beyond this one
5120+ transientStorage .setCredIterationPointer ((short )(rkIndex + 1 )); // resume iteration from beyond this one
51235121
51245122 byte [] outBuf = bufferMem ;
51255123
@@ -5231,15 +5229,15 @@ private short packCredentialId(byte[] credBuffer, short credOffset, byte[] write
52315229 * This is, unfortunately, O(N^2) in the number of unique RPs.
52325230 */
52335231 private void updateRKStatekeeping (APDU apdu ) {
5234- short rp2Handle = bufferManager .allocate (apdu , CREDENTIAL_ID_LEN , BufferManager .ANYWHERE );
5235- byte [] rp2Buffer = bufferManager .getBufferForHandle (apdu , rp2Handle );
5236- short rp2Offset = bufferManager .getOffsetForHandle (rp2Handle );
5232+ final short rp2Handle = bufferManager .allocate (apdu , CREDENTIAL_ID_LEN , BufferManager .ANYWHERE );
5233+ final byte [] rp2Buffer = bufferManager .getBufferForHandle (apdu , rp2Handle );
5234+ final short rp2Offset = bufferManager .getOffsetForHandle (rp2Handle );
52375235
5238- short rp1Handle = bufferManager .allocate (apdu , RP_HASH_LEN , BufferManager .ANYWHERE );
5239- byte [] rp1Buffer = bufferManager .getBufferForHandle (apdu , rp1Handle );
5240- short rp1Offset = bufferManager .getOffsetForHandle (rp1Handle );
5236+ final short rp1Handle = bufferManager .allocate (apdu , RP_HASH_LEN , BufferManager .ANYWHERE );
5237+ final byte [] rp1Buffer = bufferManager .getBufferForHandle (apdu , rp1Handle );
5238+ final short rp1Offset = bufferManager .getOffsetForHandle (rp1Handle );
52415239
5242- byte numUniqueRPsFound = 0 ;
5240+ short numUniqueRPsFound = 0 ;
52435241
52445242 for (short rkIndex1 = 0 ; rkIndex1 < (short ) residentKeys .length ; rkIndex1 ++) {
52455243 if (residentKeys [rkIndex1 ] == null ) {
@@ -5328,7 +5326,7 @@ private void handleEnumerateRPs(APDU apdu, short startOffset) {
53285326
53295327 outBuf [writeOffset ++] = FIDOConstants .CTAP2_OK ;
53305328
5331- transientStorage .setRPIterationPointer ((byte )(rkIndex + 1 ));
5329+ transientStorage .setRPIterationPointer ((short )(rkIndex + 1 ));
53325330
53335331 outBuf [writeOffset ++] = isContinuation ? (byte ) 0xA2 : (byte ) 0xA3 ; // map with two or three keys
53345332 outBuf [writeOffset ++] = 0x03 ; // map key: rp
@@ -5407,12 +5405,15 @@ private void handleCredentialManagementGetCredsMetadata(APDU apdu) {
54075405 *
54085406 * @return New write offset into given buffer
54095407 */
5410- private short encodeIntTo (byte [] outBuf , short writeOffset , byte v ) {
5408+ private short encodeIntTo (byte [] outBuf , short writeOffset , short v ) {
54115409 if (v < 24 ) {
5412- outBuf [writeOffset ++] = v ;
5413- } else {
5410+ outBuf [writeOffset ++] = ( byte ) v ;
5411+ } else if ( v < 256 ) {
54145412 outBuf [writeOffset ++] = 0x18 ; // Integer stored in one byte
5415- outBuf [writeOffset ++] = v ;
5413+ outBuf [writeOffset ++] = (byte ) v ;
5414+ } else {
5415+ outBuf [writeOffset ++] = 0x19 ; // Integer stored in two bytes
5416+ writeOffset = Util .setShort (outBuf , writeOffset , v );
54165417 }
54175418 return writeOffset ;
54185419 }
@@ -6421,7 +6422,7 @@ private short consumeKeyAgreement(APDU apdu, byte[] buffer, short readIdx, byte
64216422 CannedCBOR .PUBLIC_KEY_DH_ALG_PREAMBLE , (short ) 0 , (short ) CannedCBOR .PUBLIC_KEY_DH_ALG_PREAMBLE .length ) != 0 ) {
64226423 sendErrorByte (apdu , FIDOConstants .CTAP2_ERR_UNSUPPORTED_ALGORITHM );
64236424 }
6424- readIdx += CannedCBOR .PUBLIC_KEY_DH_ALG_PREAMBLE .length ;
6425+ readIdx += ( short ) CannedCBOR .PUBLIC_KEY_DH_ALG_PREAMBLE .length ;
64256426
64266427 short xIdx = readIdx ;
64276428 readIdx += KEY_POINT_LENGTH ;
0 commit comments