📄 myapplet.java
字号:
// get rnd
if (!r.isvaild()) ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
r.getRndValue(ai.ucTemp16);
r.revokeRnd();
usDOLLength = r.sizeOfRnd();
rc = mm.getByte(currentObject.acl_offset,currentObject.acl_length,(short)2);
action = (byte)(rc&0x0f);
if ( action == (byte)0 ) // key blocked
ISOException.throwIt(constdef.SW_E_KEYBLOCKED);
// load key
mm.memcpyOUT(currentObject.value_offset,currentObject.value_length,(short)0,ai.ucTemp32,(short)0,currentObject.value_length);
c.tripledes(ai.ucTemp32,ai.ucTemp16,(short)0,usDOLLength,ai.ucTemp128,(short)0,Cipher.MODE_ENCRYPT);
if (Util.arrayCompare(ai.pdata,usPos,ai.ucTemp128,(short)0,subtotal) != (byte)0 ) {
if (action!=(byte)0x0F) {
action--;
rc = (byte)(rc&0xF0); // clear
rc = (byte)(rc|action); // set
mm.putByte(currentObject.acl_offset,currentObject.acl_length,(short)2,rc);
}
ISOException.throwIt((short)(0x63c0 + action));
} else {
// set reference flag for SM
set_ref_flag(currentObject.oid);
}
// end of ext-auth
} else if (currentObject.ko_canRef == (byte)0x02) { // verify pin
if ( subtotal != currentObject.value_length )
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
rc = mm.getByte(currentObject.acl_offset,currentObject.acl_length,(short)2);
action = (byte)(rc&0x0f);
if ( action == (byte)0 ) // key blocked
ISOException.throwIt(constdef.SW_E_KEYBLOCKED);
mm.memcpyOUT(currentObject.value_offset,currentObject.value_length,(short)0,ai.ucTemp32,(short)0,currentObject.value_length);
if (Util.arrayCompare(ai.pdata,usPos,ai.ucTemp32,(short)0,subtotal) != (byte)0 ) {
if (action!=(byte)0x0F) {
action--;
rc = (byte)(rc&0xF0); // clear
rc = (byte)(rc|action); // set
mm.putByte(currentObject.acl_offset,currentObject.acl_length,(short)2,rc);
}
ISOException.throwIt((short)(0x63c0 + action));
} else {
// set reference flag for SM
set_ref_flag(currentObject.oid);
}
} else ISOException.throwIt(constdef.SW_E_INTERNAL);
ai.le = (short)0;
break;
case (byte)0x3: // read : OID + DOL
if ( ai.pdata[usPos++] != UAPP_TAG_DOL ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
subtotal = (short)(ai.pdata[usPos++]&0x0ff);
if ( ai.lc != (short)(dl+subtotal+4) )
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
if(currentObject.attrib1!=(byte)0x0) // not a data object
ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
Util.arrayCopyNonAtomic(ai.pdata,usPos,ai.ucTemp128,(short)0,subtotal);
mm.memcpyOUT(currentObject.acl_offset,currentObject.acl_length,(short)0,ai.ucTemp256,(short)0,currentObject.acl_length);
ai.le = (short)0;
// Load and Check AC
for(usi=(short)0; usi<subtotal; ) {
// Load Tag to Read
if ((ai.ucTemp128[usi]&0x01F) == 0x1F ) {
aTag = Util.makeShort(ai.ucTemp128[usi],ai.ucTemp128[(short)(usi+1)]);
usi++;
usi++;
} else {
aTag = (short)(ai.ucTemp128[usi]&0x0FF);
usi++;
}
// query Tag for AC and Data;
qTag = (short)0;
for(usj=(short)0; usj<currentObject.acl_length; ) {
qTag = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
usj ++;
usj ++;
if ( aTag == qTag ) { // found
rc = check_ac(ai.ucTemp256[usj]);
if(rc==(byte)0x0ff) {
ai.le = (short)0;
ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
}
if ( rc==(byte)1) {// sm key
// check CLA byte
if (ai.cla != (byte)0x84) {
ai.le = (short)0;
ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
}
if (bInSecuMessage) { // duplicate sm key
ai.le = (short)0;
ISOException.throwIt(constdef.SW_E_DUPSMKEY);
}
bInSecuMessage = true;
ucSecuteObject = ai.ucTemp256[usj];
}
// else is allowed and free, load data to buffer
usj ++;
usj ++;
qOff = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
usj ++;
usj ++;
qLen = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
mm.memcpyOUT(currentObject.value_offset,currentObject.value_length,qOff,ai.pdata,ai.le, qLen);
ai.le += qLen;
break;
} // end of found
usj ++;
usj ++;
usj ++;
usj ++;
usj ++;
usj ++;
} // end of qTag
if ( aTag != qTag ) {
ai.le = (short)0;
ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);
}
} // end of read
break;
case (byte)0x4: // update: OID + DOL + RAWDATA
if ( ai.pdata[usPos++] != UAPP_TAG_DOL ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
usDOLLength = (short)(ai.pdata[usPos++]&0x0ff);
usDOLStarted = usPos;
if(currentObject.attrib1!=(byte)0x0) // not a data object
ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
// Copy DOL to ucTemp128
Util.arrayCopyNonAtomic(ai.pdata,usPos,ai.ucTemp128,(short)0,usDOLLength);
mm.memcpyOUT(currentObject.acl_offset,currentObject.acl_length,(short)0,ai.ucTemp256,(short)0,currentObject.acl_length);
usPos += usDOLLength;
dl += usDOLLength;
dl +=(short)4; // OID + DOL
// BEGIN calculate all DOL length for check and find sm key is necesary
ucSecuteObject = (byte)0xFF;
qOff = (short)0;
for(usi=(short)0; usi<usDOLLength; ) {
// Load Tag to Read Length
if ((ai.ucTemp128[usi]&0x01F) == 0x1F ) {
aTag = Util.makeShort(ai.ucTemp128[usi],ai.ucTemp128[(short)(usi+1)]);
usi++;
usi++;
} else {
aTag = (short)(ai.ucTemp128[usi]&0x0FF);
usi++;
}
// Query Length by Tag
qTag = (short)0;
for(usj=(short)0; usj<currentObject.acl_length; ) {
qTag = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
usj ++;
usj ++;
if ( aTag == qTag ) { // found
rc = check_ac(ai.ucTemp256[(short)(usj+1)]);
if(rc==(byte)0x0ff) {
ai.le = (short)0;
ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
}
if ( rc==(byte)1) {// sm key
// check CLA byte
if (ai.cla != (byte)0x84) {
ai.le = (short)0;
ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
}
if (bInSecuMessage) { // duplicate sm key
ai.le = (short)0;
ISOException.throwIt(constdef.SW_E_DUPSMKEY);
}
bInSecuMessage = true;
ucSecuteObject = ai.ucTemp256[(short)(usj+1)];
}
// else is allowed and free, load data to buffer
usj ++;
usj ++;
usj ++;
usj ++;
qLen = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
qOff += qLen;
break;
} // end of found
usj ++;
usj ++;
usj ++;
usj ++;
usj ++;
usj ++;
} // end of qTag
if ( aTag != qTag ) ISOException.throwIt((short)0x6766);//ISO7816.SW_FILE_NOT_FOUND);
} // end of for usi
// END for calculate all DOL length for check...
// SM mode ?
if( ai.cla == (byte)0x84) {
if(bInSecuMessage) {
UnwarpApdu(ucSecuteObject,usPos);
// Restore buffer
Util.arrayCopyNonAtomic(ai.pdata,usDOLStarted,ai.ucTemp128,(short)0,usDOLLength);
mm.memcpyOUT(currentObject.acl_offset,currentObject.acl_length,(short)0,ai.ucTemp256,(short)0,currentObject.acl_length);
} else ISOException.throwIt((short)0x6788);//ISO7816.SW_FILE_NOT_FOUND); // sm key not found
} else {
if(bInSecuMessage) ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
}
//--------------------- moved from up lines --------------------------------------------
if ( ai.pdata[usPos++] != UAPP_TAG_RAWDATA ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
subtotal = (short)(ai.pdata[usPos++]&0x0ff);
// check LC and data is match???
if ( ai.lc != (short)(dl+subtotal+2) )
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//-----------------------------------------------------------------------------
if ( qOff != subtotal ) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
// Update and Check AC
for(usi=(short)0; usi<usDOLLength; ) {
// Load Tag to write
if ((ai.ucTemp128[usi]&0x01F) == 0x1F ) {
aTag = Util.makeShort(ai.ucTemp128[usi],ai.ucTemp128[(short)(usi+1)]);
usi++;
usi++;
} else {
aTag = (short)(ai.ucTemp128[usi]&0x0FF);
usi++;
}
// query Tag for AC and Data;
qTag = (short)0;
for(usj=(short)0; usj<currentObject.acl_length; ) {
qTag = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
usj ++;
usj ++;
if ( aTag == qTag ) { // found
// BECAUSE AC is checked first, so...
usj ++;
usj ++;
qOff = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
usj ++;
usj ++;
qLen = Util.makeShort(ai.ucTemp256[usj],ai.ucTemp256[(short)(usj+1)]);
// Update Data
mm.memcpyIN(currentObject.value_offset,currentObject.value_length,qOff,ai.pdata,usPos,qLen);
usPos += qLen;
break;
} // end of found
usj ++;
usj ++;
usj ++;
usj ++;
usj ++;
usj ++;
} // end of qTag
if ( aTag != qTag ) ISOException.throwIt(constdef.SW_E_INTERNAL);
} // end of write
ai.le = (short)0;
break;
case (byte)0x5: // increase
// debug content
if ( ai.pdata[usPos++] != UAPP_TAG_DOL ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
subtotal = (short)(ai.pdata[usPos++]&0x0ff);
if ( ai.lc != (short)(dl+subtotal+4) )
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
// dump object for debug
mm.memcpyOUT(currentObject.value_offset,currentObject.value_length,(short)0,ai.pdata,(short)0,currentObject.value_length);
ai.le = currentObject.value_length;
break;
case (byte)0x6: // decrease
// debug AC
if ( ai.pdata[usPos++] != UAPP_TAG_DOL ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
subtotal = (short)(ai.pdata[usPos++]&0x0ff);
if ( ai.lc != (short)(dl+subtotal+4) )
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
// dump object ACL for debug
//mm.memcpyOUT(currentObject.acl_offset,currentObject.acl_length,(short)0,ai.pdata,(short)0,currentObject.acl_length);
//ai.le = currentObject.acl_length;
// dump SM for debug
ai.le = (short)(5*oidCurrentApp.usSMCount);
Util.arrayCopyNonAtomic(oidCurrentApp.statemcachine,(short)0,ai.pdata,(short)0,ai.le);
break;
}
return true;
}
//------------------------------------------------
public boolean debug_debug() throws ISOException
{
// dump data
ai.le = ai.usTempShort;
Util.arrayCopyNonAtomic(ai.ucTemp256,(short)0,ai.pdata,(short)0,ai.le);
return true;
}
//------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -