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

📄 myapplet.java

📁 智能卡多应用一直是业界的重要课题
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                 mm.memcpyIN(dAddress, dSize, __us_dOffset,pdata,usPos,eatlen);
             }

             __us_dOffset += eatlen;

             // tag counter
             ai.usTempCount++;

          } else {
             if(!expandTLVObject(dAddress, dSize, pdata, usPos, eatlen,defaultAC)) return false;
          }
          length -=eatlen;
          usPos += eatlen;
       }

       if ( length == 0 ) return true;
       return false;
    }
    //------------------------------------------------
    private boolean selectApplication() throws ISOException
    {
       if( ai.cla != (byte)0x80 )
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

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

       if( ai.lc > (short)16 ) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

       if ( !findEntryByOid(ai.pdata,ai.lc) )
            ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

       ai.le = (short)0;
       return true;
    }
    //------------------------------------------------
    private void set_ref_flag(byte oid )
    {
        short  i,offset;

        if (oidCurrentApp==null) return;

        offset = (short)0;
        for(i=(short)0; i<oidCurrentApp.usSMCount;i++) {

           if ( oidCurrentApp.statemcachine[offset] == oid ) { // found object
              if (oidCurrentApp.statemcachine[(short)(offset+3)] > (byte)0x0 )   // canRef?
                 oidCurrentApp.statemcachine[(short)(offset+4)] = 0x0;  // free
              return;
           }
           offset++;
           offset++;
           offset++;
           offset++;
           offset++;
        }
    }
    //------------------------------------------------
    private byte check_ac(byte oid )
    {
        short  i,offset;
        byte   rac;

        if (oidCurrentApp==null) return (byte)0xFF;

        if ( oid == (byte)0x0 ) return oid;   // free
        if ( oid == (byte)0xFF ) return oid;  // never

        offset = (short)0;
        for(i=(short)0; i<oidCurrentApp.usSMCount;i++) {

           if ( oidCurrentApp.statemcachine[offset] == oid ) { // found object

              rac = oidCurrentApp.statemcachine[(short)(offset+2)];

              while(rac > (byte)0x0) {
                 if (rac == (byte)0x0ff ) return rac;
                 rac = check_ac(rac);
              }

              if (oidCurrentApp.statemcachine[(short)(offset+3)] > (byte)0x0 ) { // canRef?
                 return oidCurrentApp.statemcachine[(short)(offset+4)];
              } else { // sm key
                 return (byte)1;
              }

           } // end of found
           offset++;
           offset++;
           offset++;
           offset++;
           offset++;
        }
        return (byte)0xff;
    }
    //------------------------------------------------
    private short load_securemessage_key(byte oid, byte[] pBuff)
    {
        short  i,offset;
        tAObject keyObject;

        if (oidCurrentApp==null) return (short)-1;

        offset = (short)0;
        for(i=(short)0; i<oidCurrentApp.usSMCount;i++) {

           if ( oidCurrentApp.statemcachine[offset] == oid ) { // found object
              i = oidCurrentApp.statemcachine[(short)(offset+1)];
              keyObject = oidCurrentApp.mObjectList[i];
              if ( keyObject.attrib1 == (byte)0x0 ) return (short)-1;
              offset = (short)(keyObject.attrib2&0x07);
              if ( offset <(short)2) return (short)-1;
              if ( offset >(short)3) return (short)-1;
              mm.memcpyOUT(keyObject.value_offset,keyObject.value_length,(short)0,pBuff,(short)0,keyObject.value_length);
              return offset;
           }
           offset++;
           offset++;
           offset++;
           offset++;
           offset++;
        }
       return (short)-1;
    }
    //------------------------------------------------
    private void WarpApdu() throws ISOException
    {
 /*
        if( pcore.apduin.le == (short)0) return;

        pcore.apduin.le = pcore.c.huaweiPadding(pcore.apduin.pdata,pcore.apduin.le);
        pcore.c.tripledes(seskey,pcore.apduin.pdata,(short)0, pcore.apduin.le,pcore.apduin.ucTemp128,(short)0,Cipher.MODE_ENCRYPT);
        pcore.c.huaweiMac(mackey,pcore.apduin.ucTemp128,pcore.apduin.le, pcore.apduin.ucTemp8);
        Util.arrayCopyNonAtomic(pcore.apduin.ucTemp128,(short)0,pcore.apduin.pdata,(short)0,pcore.apduin.le);
        Util.arrayCopyNonAtomic(pcore.apduin.ucTemp8, (short)0,pcore.apduin.pdata,pcore.apduin.le,(short)4);

        pcore.apduin.le +=(short)4;
*/
    }
    //------------------------------------------------
    private void UnwarpApdu(byte kOid,short ePos) throws ISOException
    {
        short offset,mode;

        if (kOid == (byte)0xFF )
            ISOException.throwIt(constdef.SW_E_INTERNAL);

        offset = (short)(ai.lc - ePos - 4);

        if ( (short)(offset%8) !=(short)0)
           ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        // check rnd
        if (!r.isvaild()) ISOException.throwIt((short)0x6712);//ISO7816.SW_CONDITIONS_NOT_SATISFIED);
        r.getRndValue(ai.ucTemp256);
        r.revokeRnd();
        mode = r.sizeOfRnd();
        if ( mode !=(short)8 )
           Util.arrayFillNonAtomic(ai.ucTemp256,mode,(short)4,(byte)0x0);

        mode = load_securemessage_key(kOid,ai.ucTemp32);
        if(mode<(short)0)
           ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);

        Util.arrayCopyNonAtomic(ai.pdata, (short)(offset+ePos), ai.ucTemp16,(short)0,(short)4);

        ai.ucTemp128[0] = ai.cla;
        ai.ucTemp128[1] = ai.ins;
        ai.ucTemp128[2] = ai.p1;
        ai.ucTemp128[3] = ai.p2;
        ai.ucTemp128[4] = (byte)ai.lc;

        Util.arrayCopyNonAtomic(ai.pdata,(short)0, ai.ucTemp128, (short)5, (short)(ePos + offset));

        c.gmac4(penCipher.ALG_3DES,ai.ucTemp32,ai.ucTemp128, (short)(5+ePos+offset), ai.ucTemp8,ai.ucTemp256);

//  debug
/*
        // rnd
        ai.usTempShort =  (short)8;
        // apdu
        Util.arrayCopyNonAtomic(ai.ucTemp128,(short)0,ai.ucTemp256,ai.usTempShort,dl);
        ai.usTempShort +=  dl;
        // key
        Util.arrayCopyNonAtomic(ai.ucTemp32,(short)0,ai.ucTemp256,ai.usTempShort,(short)16);
        ai.usTempShort += (short)16;
        // mac java
        Util.arrayCopyNonAtomic(ai.ucTemp8,(short)0,ai.ucTemp256,ai.usTempShort,(short)4);
        ai.usTempShort += (short)4;
        // mac apdu
        Util.arrayCopyNonAtomic(ai.ucTemp16,(short)0,ai.ucTemp256,ai.usTempShort,(short)4);
        ai.usTempShort += (short)4;
*/
//  debug

        if ( Util.arrayCompare(ai.ucTemp16,(short)0,ai.ucTemp8,(short)0,(short)4)!= (byte)0)
           ISOException.throwIt(constdef.SW_E_SECUREMSG);

        // cut mac length
        ai.lc--;
        ai.lc--;
        ai.lc--;
        ai.lc--;
// debug
//        ai.usTempShort =  ai.lc;
//        Util.arrayCopyNonAtomic(ai.pdata,(short)0,ai.ucTemp256,(short)0,ai.lc);
// debug
        if((mode==(short)3) && (offset>(short)0)) {

           offset = c.PBDecrypt(penCipher.ALG_3DES,ai.ucTemp32,ai.pdata,ePos,offset,ai.ucTemp128);

           ai.lc = (short)(ePos+offset); // new length
           Util.arrayCopyNonAtomic(ai.ucTemp128,(short)0,ai.pdata,(short)ePos,offset);

           offset = ai.lc;
           offset--;
           if( ai.pdata[offset] != (byte)0x80 && ai.pdata[offset] != (byte)0x0) return;

           //passed
           if( ai.pdata[offset] == (byte)0x80 ) {
              offset--;
              ai.lc = offset;
           } else {
              for(;; offset--) {
                if (offset >(short)0 ) {
                   if (ai.pdata[offset]!=(byte)0 ) {//added
                      if (ai.pdata[offset] == (byte)0x80) break;
                      else ISOException.throwIt(constdef.SW_E_DATAINVAILD); // padding error
                   } //added
                } else break;
              } // end of for

              if(offset <= ePos )
                ISOException.throwIt(constdef.SW_E_DATAINVAILD); // padding error
              ai.lc = offset;
           } // end of else
        } // end SM case 3
    }
    //------------------------------------------------
    public boolean insertObject() throws ISOException
    {
       short    dl,usPos,i,ustempforSM;
       short    subtotal, sublen;
       tAObject currentObject;
       byte     objectRAC;
       byte     testChar, ucKeyInstallMethod,tempchar;
       boolean  bKeyObject;

       if( ai.cla != (byte)0x80 && ai.cla != (byte)0x84)
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
//       if( ai.cla == (byte)0x84) UnwarpApdu();
       if( ai.lc < (short)16 )  ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
       if( ai.p2 != (byte)0x0 ) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);

       if( ai.p1 == (byte)0x40 ) { // authentic data

          // check oid
          if ( ai.pdata[0] != UAPP_TAG_OID ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
          dl = (short)(ai.pdata[1]&0x0ff);
          if ( dl > (short)16 ) ISOException.throwIt(constdef.SW_E_DATAINVAILD);

          // retrieve oid hex as factor
          Util.arrayCopyNonAtomic(ai.pdata,(short)2,ai.ucTemp8,(short)0,(short)8);

          if ( dl < (short)8 ) Util.arrayFillNonAtomic(ai.ucTemp8,dl,(short)(8-dl),(byte)0xFF);

          // get dxk
          c.diversify(uapp_def_mck,ai.ucTemp8,ai.ucTemp16);

          // check cryptomsg
          if ( ai.pdata[(short)(dl+2)] != UAPP_TAG_RAWDATA) ISOException.throwIt(constdef.SW_E_DATAINVAILD);
          Util.arrayCopyNonAtomic(ai.pdata,(short)(dl+4),ai.ucTemp8,(short)0,(short)8);

          dl += (short)(ai.pdata[(short)(dl+3)]&0x0ff) +(short)4;
          if ( ai.lc != dl ) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

          if (!r.isvaild()) ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);

          // get sxk
          c.diversify(ai.ucTemp16,oiduid,ai.ucTemp32);
          r.getRndValue(ai.ucTemp16);
          r.revokeRnd();

          c.tripledes(ai.ucTemp32,ai.ucTemp16,(short)0,(short)8,ai.ucTemp256,(byte)0,Cipher.MODE_ENCRYPT);

          if (Util.arrayCompare(ai.ucTemp256,(byte)0,ai.ucTemp8,(byte)0,(byte)8)!=(byte)0 )
             ISOException.throwIt(ISO7816.SW_DATA_INVALID);

          // select container or create it
          dl = (short)(ai.pdata[1]&0x0ff);
          Util.arrayCopyNonAtomic(ai.pdata,(short)2,ai.ucTemp32,(short)0,dl);

          // create or select
          if ( !findEntryByOid(ai.ucTemp32,dl) ) { // not found

              // search empty entry...
              if (!findFreeEntry()) {
                 // if no empty entry, we create it
                 if ( oidcount >= constdef.ct_oid_entrys )
                    ISOException.throwIt(constdef.SW_E_OUTMEMORY);

                 oidArray[oidcount] = new tjoidEntry(ai.ucTemp32,dl);
                 if ( oidArray[oidcount] == null ) ISOException.throwIt(constdef.SW_E_OUTMEMORY);
                 oidCurrentApp = (tjoidEntry)oidArray[oidcount];  // set as default
                 oidcount++;
              } else { // found a empty entry, reuse it
                 oidCurrentApp.setOid(ai.ucTemp32,dl);
              }
          }

          oidbAllowInsert = true;
          ai.le = (short)0;

       } else { // insert object
           // check P1
           testChar  = (byte)((ai.p1 & 0xC0)>>6); // insert data object only
           if ( testChar!=(byte)0x02) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);

           if (!oidbAllowInsert) ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);

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

⌨️ 快捷键说明

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