⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myapplet.java

📁 智能卡多应用一直是业界的重要课题
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

           // search or create a cell for object
           for( i=(short)0; i<constdef.ct_objects_num; i++ ) {
              if ( oidCurrentApp.mObjectList[i] != null ) {
                 if ( !oidCurrentApp.mObjectList[i].used) break;
              } else {  // create
                 oidCurrentApp.mObjectList[i] = new tAObject();
                 break;
              }
           }
           if (i >=constdef.ct_objects_num) ISOException.throwIt(constdef.SW_E_OUTMEMORY);

           currentObject = oidCurrentApp.mObjectList[i];

// parse object B
           ucKeyInstallMethod = (byte)((ai.p1 & 0x30)>>4);

           usPos = (short)0;
           // Object Tag ?
           if ( ai.pdata[usPos++] != UAPP_TAG_OBJECT ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           dl = (short)(ai.pdata[usPos++]&0x0ff);

           //oid?
           if (ai.pdata[usPos++] != UAPP_TAG_OID ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           subtotal = (short)(ai.pdata[usPos++]&0x0ff);
           Util.arrayCopyNonAtomic(ai.pdata,usPos,ai.ucTemp32,(short)0,subtotal);
           usPos += subtotal;

           // check same class by oid ???
           if(!oidCurrentApp.isMyObject(ai.ucTemp32)) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           currentObject.oid = ai.ucTemp32[(short)(subtotal-1)];
           // check same order ??
           if(oidCurrentApp.isExistObject(currentObject.oid)!=null)
              ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);

           //attrib ?
           if (ai.pdata[usPos++] != UAPP_TAG_ATTRIB ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           if (ai.pdata[usPos++] != (byte)0x02) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           currentObject.attrib1 = ai.pdata[usPos++];
	   currentObject.attrib2 = ai.pdata[usPos++];
           subtotal++;
           subtotal++;

           // oac ?
           if (ai.pdata[usPos++] != UAPP_TAG_OAC ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           if (ai.pdata[usPos++] != (byte)0x02) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           //currentObject.rac = ai.pdata[usPos++];
           objectRAC = ai.pdata[usPos++];
           currentObject.dac = ai.pdata[usPos++];
           subtotal++;
           subtotal++;

           // parse attrib
           testChar =(byte)0x0;
           bKeyObject = false;
           if ( currentObject.attrib1 != (byte)0x0 ) bKeyObject = true;

           if ( bKeyObject ) { // not data object
               testChar = (byte)((currentObject.attrib1 & 0xc0)>>6);
               if ( testChar !=(byte)0x02)  // should be authentic object or APDU object
                  ISOException.throwIt(constdef.SW_E_DATAINVAILD);
               // this version not support APDU Object

//               ucKeyInstallMethod = (byte)((ai.p1 & 0x30)>>4);
               if ((ucKeyInstallMethod != (byte)0x2)&&(ucKeyInstallMethod != (byte)0x1))  // install, update
                  ISOException.throwIt(ISO7816.SW_WRONG_P1P2);

               // Check Algo
               testChar = (byte)((currentObject.attrib1 & 0x0c)>>2);
               if (testChar==(byte)0x03) { // RSA algo
                  currentObject.ko_length = (short)(currentObject.attrib2 & 0xf8); // >>3
                  currentObject.ko_length = (short)(currentObject.ko_length<<1);
                  // debug ???
               } else if (testChar==(byte)0x02) { // 3DES algo
                  currentObject.ko_length = (short)16;
               } else /*if (testChar==(byte)0x01)*/ { // DES algo and other like PIN
                  currentObject.ko_length = (short)8;
               }

               if ( ucKeyInstallMethod == (byte)0x1) // update key object
                  return insertObject_update_key();

           } else
               if (ucKeyInstallMethod != (byte)0x0)
                 ISOException.throwIt(ISO7816.SW_WRONG_P1P2);

           // content ?
           if (ai.pdata[usPos++] != UAPP_TAG_CONTENT ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           sublen = (short)(ai.pdata[usPos]&0x0ff);
           subtotal += sublen;

           // check object length
           subtotal += (short)8;
           if  ( dl != subtotal ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);

           // parse content template
           dl = (short)(ai.pdata[usPos++]&0x0ff);

           // ACL ?
           if (ai.pdata[usPos++] != UAPP_TAG_ACL ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
           sublen = (short)(ai.pdata[usPos++]&0x0ff);

           // Store ACL to tempbuf
           Util.arrayCopyNonAtomic(ai.pdata,usPos,ai.ucTemp128,(short)0, sublen);
           ai.usTempShort = (byte)sublen;

           usPos += sublen;
           dl -= (short)(sublen+2);

           // Alloc space for object value
           // if rsa key then allocl specific space
           if (bKeyObject) { // auto calculated
               //if (ucKeyInstallMethod != (byte)0x10) ISOException.throwIt(constdef.SW_E_INTERNAL);
              currentObject.value_length = currentObject.ko_length;
           } else { // use content length with tag as SPACE LENGTH
              currentObject.value_length = dl;
           }

           currentObject.value_offset = mm.JMalloc(currentObject.value_length);

           if ( currentObject.value_offset <(short)0) {
              mm.JFree(currentObject.acl_offset,currentObject.acl_length);
              ISOException.throwIt(constdef.SW_E_OUTMEMORY);
           }

           ai.usTempCount = (short)0;
           __us_aOffset = (short)0;
           __us_dOffset = (short)0;
           if ( dl > (short)0 ) {
               if(!expandTLVObject(currentObject.value_offset,currentObject.value_length,/*(short)0,*/
                                   ai.pdata, usPos, dl,constdef.ct_default_ac))
                  ISOException.throwIt(constdef.SW_E_DATAINVAILD);
               if ( currentObject.attrib1 != (byte)0x0 )
                   currentObject.ko_virgin = (byte)0x01;

               // reduce space for content as possible as we can for data object
               if (!bKeyObject) {
                  sublen = (short)(ai.usTempCount<<1);
                  currentObject.value_length = mm.JReduce(currentObject.value_offset,currentObject.value_length,sublen);
               }
           } else { // no content, install key header?
               if (ucKeyInstallMethod != (byte)0x2) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
               // if is install key with header, store ACL and return true;
           }

           // alloc space for ACL
           sublen = (short)(ai.usTempCount<<3/**8*/);
           currentObject.acl_length = sublen;
           currentObject.acl_offset = mm.JMalloc(sublen);
           if (currentObject.acl_offset < (short)0 )
              ISOException.throwIt(constdef.SW_E_OUTMEMORY);

           // store ACL
           mm.memcpyIN(currentObject.acl_offset,currentObject.acl_length, (short)0,
                       ai.ucTemp256,(short)0,sublen);

           // Maintain StateMachine here
           if ( bKeyObject ) { // not data object
               if (oidCurrentApp.usSMCount >= (short)15 ) //constdef.ct_keyobjects
                  ISOException.throwIt(constdef.SW_E_OUTMEMORY);

               ustempforSM = (short)(5 * oidCurrentApp.usSMCount);

               oidCurrentApp.statemcachine[ustempforSM++] = currentObject.oid;
               oidCurrentApp.statemcachine[ustempforSM++] = (byte)i;
               oidCurrentApp.statemcachine[ustempforSM++] = objectRAC;   // currentObject.rac;
               oidCurrentApp.statemcachine[ustempforSM] = (byte)0x0;     // can't ref

               tempchar = (byte)((currentObject.attrib1&0x30)>>4);
               if (tempchar == (byte)0x1) oidCurrentApp.statemcachine[ustempforSM] = (byte)0x01;  // external-auth
               else {
                  tempchar = (byte)((currentObject.attrib2&0x07));
                  if (tempchar==(byte)0x1) oidCurrentApp.statemcachine[ustempforSM] = (byte)0x02; // verify
               }
               currentObject.ko_canRef = oidCurrentApp.statemcachine[ustempforSM];

               ustempforSM++;
               oidCurrentApp.statemcachine[ustempforSM]=(byte)0xff; // set default for never
               oidCurrentApp.usSMCount++;
           }

           // set ready flag
           currentObject.used = true;
           oidCurrentApp.usCount++;

           // debug Content
           //ai.le = currentObject.value_length;
           //mm.memcpyOUT(currentObject.value_offset,currentObject.value_length,(short)0,ai.pdata,(short)0,ai.le);
// parse object E
       }

       ai.le = (short)0;
       return true;
    }
    //------------------------------------------------
    private boolean insertObject_update_key() throws ISOException
    {
         // OID + ATTRIB + CONTENT
         return false;
    }
    //------------------------------------------------
    public boolean accessObject() throws ISOException
    {
       byte  action,rc;
       short usPos,dl,subtotal,usDOLStarted,usDOLLength;
       tAObject  currentObject;

       // inerchage used;
       short  usi, usj, aTag,qTag;
       short  qOff, qLen;

       // secure message setting
       boolean  bInSecuMessage;
       byte     ucSecuteObject;

       if( ai.cla != (byte)0x80 && ai.cla != (byte)0x84)
           ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

       if ( ai.p2 != (byte)0 )
           ISOException.throwIt(ISO7816.SW_WRONG_P1P2);

       if ( ai.p1 < (byte)0x80 ) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);

       // app selected ?
       if (oidCurrentApp==null) ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);

       // parse action
       action = (byte)((ai.p1&0x70)>>4);
       if (action<(byte)1||action>(byte)6)
          ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);

       // parse data
       usPos = (short)0;

       // OID Tag ?
       if ( ai.pdata[usPos++] != UAPP_TAG_OID ) ISOException.throwIt(ISO7816.SW_DATA_INVALID);
       subtotal = (short)(ai.pdata[usPos++]&0x0ff);
       Util.arrayCopyNonAtomic(ai.pdata,usPos,ai.ucTemp32,(short)0,subtotal);

       // check same class by oid ?
       if(!oidCurrentApp.isMyObject(ai.ucTemp32)) ISOException.throwIt(ISO7816.SW_DATA_INVALID);

       // exist this object ?
       currentObject = oidCurrentApp.isExistObject(ai.ucTemp32[(short)(subtotal-1)]);
       if(currentObject==null)
           ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

       usPos += subtotal;
       dl = subtotal;

       bInSecuMessage = false;
       switch(action) {
          case (byte)0x2: // delete

                if( ai.cla == (byte)0x84) UnwarpApdu(currentObject.dac,usPos);

                if ( ai.lc != (short)(dl+2) )
                   ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

                // check AC
                rc = check_ac(currentObject.dac);

                if(rc==(byte)0x0ff) {
                   ai.le = (short)0;
                   ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
                }

                if ( rc==(byte)1)  {// sm mode
                   if( ai.cla != (byte)0x84) ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
                   //bInSecuMessage = true;
                   //ucSecuteObject = currentObject.dac;
                }

                // free memory
                mm.JFree(currentObject.acl_offset,currentObject.acl_length);
                mm.JFree(currentObject.value_offset,currentObject.value_length);
                // set flag
                currentObject.used = false;
                oidCurrentApp.usCount--;

                // clean SM entry if is key object
                if ( currentObject.attrib1 != (byte)0x0 ) {
                   rc = (short)0;
                   for(usPos=(short)0; usPos<oidCurrentApp.usSMCount;usPos++) {
                      if ( oidCurrentApp.statemcachine[rc] == currentObject.oid ) {
                         subtotal = (short)(5*usPos);
                         dl = (short)(oidCurrentApp.usSMCount-usPos-1);
                         dl = (short)(5*dl);
                         if ( dl > (short)0) // memmove
                            Util.arrayCopy(oidCurrentApp.statemcachine,(short)(subtotal+5),
                                           oidCurrentApp.statemcachine,subtotal,dl);
                         oidCurrentApp.usSMCount--;
                         break;
                      }
                      rc++;
                      rc++;
                      rc++;
                      rc++;
                      rc++;
                   } // end of search
                } // end of key object

                if ( oidCurrentApp.usCount <(short)1) oidCurrentApp.used = false;
                ai.le = (short)0;
                break;
          case (byte)0x1: // reference PIN or External Authentic
                if ( ai.pdata[usPos++] != UAPP_TAG_RAWDATA ) 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);

                // data object?
                if (currentObject.attrib1 == (byte)0)
                   ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);

                if (currentObject.ko_canRef == (byte)0) // can't ref
                   ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);

                if (currentObject.ko_canRef == (byte)0x01) {// external auth
                    if ( subtotal != (short)8 )
                       ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -