123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- #include "includes.h"
- #define AUTH_FILE "m5700_info"
- #define ID_VALID_BYTE 12
- char *modemImei=NULL;
- char id_imei[ID_VALID_BYTE+1]={0};
- void saveAuthEncode(char *writebuf,int length){
- int fd;
- MSG_INFO(1,"%s save start",AUTH_FILE);
- fd=LSAPI_FS_Open(AUTH_FILE, LSAPI_FS_O_RDWR |LSAPI_FS_O_CREAT |LSAPI_FS_O_TRUNC,0);
- if(fd<0){
- MSG_ERR(1,"%s open failed",AUTH_FILE);
- return;
- }
- //淇濆瓨
- LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
- LSAPI_FS_Write(fd, writebuf, length);
- //nwy_sdk_fsync(fd);
- LSAPI_FS_Close(fd);
- MSG_INFO(1,"%s save done", AUTH_FILE);
- }
- bool readAuthEncode(char *readbuf,int length){
- int fd;
- fd=LSAPI_FS_Open(AUTH_FILE, LSAPI_FS_O_RDWR,0x0);
-
- if(fd<0){
- MSG_ERR(1,"%s open read failed", AUTH_FILE);
- return false;
- }
- LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
- LSAPI_FS_Read(fd, readbuf,length);
- LSAPI_FS_Close(fd);
- return true;
- }
- /*
- DecodeImeiBytes
- 授权码加密
- */
- void DecodeImeiBytes(unsigned char *imei){
- unsigned char i,j,seeds='w';
-
- j='j';
- for(i=0;i<IMEI_SIZE;i++){
- imei[i] ^= seeds;
- seeds += j;
- j += 'l';
- }
- }
- /*
- saveAuth
- */
- void saveAuth(char *imei){
- unsigned char writebuf[64],tmp[10];
- unsigned short length,i;
- char tempImei[IMEI_SIZE];
- memcpy(tempImei, imei,IMEI_SIZE );
-
- DecodeImeiBytes(tempImei);
- length=0;
- for(i=0;i<IMEI_SIZE;i++){
- snprintf(tmp,sizeof(tmp),"%02x",tempImei[i]);
- writebuf[length++]=tmp[0];
- //MSG_INFO(0,"%02x",tmp[0]);
- writebuf[length++]=tmp[1];
- //MSG_INFO(0,"%02x", tmp[1]);
- }
- //save code bellow
- saveAuthEncode(writebuf,length);
- }
- /*
- DecodeImei
- */
- static bool DecodeImei(char *enCodeimei, char *sourceimei){
- char thisimei[IMEI_SIZE+1];
- char i;
- /*
- MSG_INFO(1,"enCodeimei====");
- for(i=0;i<sizeof(thisimei)*2;i++){
- MSG_INFO(0,"%02x", enCodeimei[i]);
- }
- MSG_INFO(1,"over");
- */
- restoreDataFormatByHex(enCodeimei, IMEI_SIZE*2);
-
- DecodeImeiBytes(enCodeimei);
-
- memcpy(thisimei, enCodeimei,sizeof(thisimei));
-
- // thisimei[IMEI_SIZE]=0;//HYL
- if(0==memcmp(sourceimei, thisimei,IMEI_SIZE)) return true;
-
- else return false;
-
- }
- void shortImei(char *srcImei){
- int i=0,j=0,w;
- int len=strlen(srcImei);
- if(len>ID_VALID_BYTE){
- w=(len-ID_VALID_BYTE)*2;
- for(i=0;i<w;i+=2){
- id_imei[j++]=charStrToHex(srcImei[i],srcImei[i+1]);
- }
- memcpy(id_imei+j, srcImei+w, len-w);
- }else{
- for(i=0;i<ID_VALID_BYTE-len;i++){
- id_imei[j++]=0;
- }
- for(i=ID_VALID_BYTE-len;i<ID_VALID_BYTE;i++) id_imei[j++]=srcImei[i];
- }
- id_imei[ID_VALID_BYTE]=0;
- }
- bool localAuthOk(void){
- unsigned char enCodeImei[(IMEI_SIZE+1)*2];
- //鏈夐壌鏉冩枃浠讹紝鍒欒�IMEI杩涜�瀵规瘮
- if(modemImei==NULL){
- modemImei=ohpocGetIMEI();
- if(modemImei==NULL) return false;
- if(modemImei[0]=='\0'){
- modemImei=NULL;
- return false;
- }
- shortImei(modemImei);
- }
- //璇诲彇鏄�惁鏈夐壌鏉冩枃浠?
- if(false==readAuthEncode(enCodeImei, sizeof(enCodeImei))) return false;
- //濡傛灉涓嶪MEI涓€鑷达紝鍒欓壌鏉冮€氳繃
- return DecodeImei(enCodeImei, modemImei);
-
- return 1;
- }
- /////////////////////////////鎺堟潈閮ㄥ垎///////////////////////////////
- #define PRO_HEADER 0x89
- #define TX_RX_SEED 0x86
- #define PRO_AUTH_RESPONS 0x30 //鍚戞湇鍔″櫒璇锋眰鎻℃墜
- #define PRO_AUTH_ACK 0x31 //鏈嶅姟鍝嶅簲鎻℃墜
- #define PRO_AUTH_QUERY 0X32 //鏈嶅姟鍣ㄥ悜鎴戠储瑕?鍙傛暟璇锋眰
- #define PRO_AUTH_REQ 0x34 //缁堢�璇锋眰閴存潈
- #define PRO_AUTH_OK 0x35 //缁堢�搴旂瓟閴存潈缁撴灉涓€鏍?
- #define PRO_AUTH_SUC 0x36 //閴存潈宸ュ叿閫氱煡閴存潈鎴愬姛 杩斿洖閴存潈鐮?
- #define PRO_AUTH_FAI 0x37 //缁堢�閫氱煡閿�壌鏉冪粨鏋滀笉涓€鑷?
- typedef struct
- {
- unsigned char noUseStart[31];//涓嶄娇鐢ㄦ�
- unsigned int authCode;//涓嶉€傜敤绗�竴涓�瓧鑺?
- unsigned char SlaveNum;//浠庢満鍙�敤涓�暟
- unsigned char noUseEnd[17];
- } __attribute__((packed)) AUTH_INFO;
- void authSendCmd(unsigned char cmd, unsigned char *data, int len){
- //鍗忚�鏍煎紡
- //head cmd len data
- //data:liushui[2] id[12] 鍧囩粡杩嘽md鍜岄暱搴﹀姞瀵?
- static unsigned short liushui=0;
- unsigned char sendbuf[128];
- unsigned char i,k;
- unsigned char *dataPtr=sendbuf+3;
- unsigned char datalen=2+len+1;//?贸茅?sum
- unsigned char temp[3],sum;
-
- i=0;
- sendbuf[i++]=PRO_HEADER;
- sendbuf[i++]=cmd;
- sendbuf[i++]=datalen;
- sendbuf[i++]=liushui>>8; //4-5
- sendbuf[i++]=liushui;
-
- for(k=0;k<len;k++)
- sendbuf[i++]=data[k];
-
- temp[0]=3+datalen;//脳眉3陇?猫锟?掳眉脿篓sum //47+3
- temp[1]=sendbuf[0]; //0x89
- temp[2]=sendbuf[1]; //0x34
-
- for(k=0;k<datalen-1;k++){
- dataPtr[k] ^= temp[k % 3];
- dataPtr[k] ^= TX_RX_SEED;
- }
- sum=0;
- for(k=0;k<i;k++) sum ^= sendbuf[k];
- sendbuf[i++]=sum;
- usbOutPut(sendbuf, i);
- }
- bool handshake=false;
- void localAuthNow(void){
- static char showFlag=0;
- static unsigned int cnt=0;
- sutApp.authReady=0;
-
- #ifndef AUTH_SKIP
-
- for(;;){
- if(true==localAuthOk()) break;
- if(showFlag==0){
- guiShowMessageBox("未授权");
- showFlag=1;
- }
- if(++cnt>=10){
- if(handshake==false) authSendCmd(PRO_AUTH_RESPONS,0,0);
- cnt=0;
- }
- LSAPI_OSI_ThreadSleep(100);
- }
- if(showFlag) guiShowMessageBox("授权通过");
- #endif
-
- sutApp.authReady=1;
- }
- #define FACTORY_NAME "YT"
- //#define FACTORY_NAME "RTL"
- void EncodeKeyWithFactorAndMode(unsigned char *code)
- {
- int i,j;
- char *FN=FACTORY_NAME;
- char *MN=OHPOC_APP_NAME;
-
- j=strlen(FACTORY_NAME);
- for(i=0;i<j;i++) code[i%4] ^= FN[i];
-
- j=strlen(OHPOC_APP_NAME);
- for(i=0;i<j;i++) code[i%4] ^= MN[i];
- }
- unsigned int encodeWithImei(unsigned char *id, unsigned char idlen){
- const unsigned char sucRandTable[256]={
- 0x68,0x59,0x63,0x64,0xc8,0xcc,0xaa,0xa7,0xa2,0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x52,0x5c,0x65,0x6f,0x58,0x53,0x54,0x0c,0x5e,0x5b,0x55,0x5f,0x31,0x2f,0x9a,0x0b,
- 0x0b,0x5a,0x2b,0x9e,0x02,0x89,0x59,0x3b,0x0e,0x51,0x6c,0xeb,0xda,0xe6,0x31,0xf6
- };
- unsigned char i;
- unsigned int ret;
- unsigned char temp1,temp2;
- unsigned char idtemp[64];
- temp1=0;
-
- for(i=0;i<idlen;i++){
- temp1 ^= id[i];
- idtemp[i]=id[i];
- }
-
- temp1 ^= idlen;
- temp2=sucRandTable[temp1];
- //脩颅禄路虏茅卤铆脪矛禄貌脭脣脣茫拢卢脡煤鲁脡脨脗id
- for(i=0;i<idlen;i++){
- temp1=idtemp[i]^temp2;
- idtemp[i]=i^temp1;
- temp2=sucRandTable[temp1];
- }
-
- //脳卯潞贸脙驴3脳脰陆脷脭脵脪矛禄貌碌脙碌陆4脳脰陆脷脳梅脦陋脢盲鲁枚
- for(i=0;i<4;i++){
- temp1=idtemp[3*i] ^ idtemp[3*i+1];
- idtemp[i]=temp1 ^ idtemp[3*i+2];
- }
- ret=idtemp[0];ret <<= 8;ret &= 0xFFFFFF00;
- ret |= idtemp[1];ret <<= 8;ret &= 0xFFFFFF00;
- ret |= idtemp[2];ret <<= 8;ret &= 0xFFFFFF00;
- ret |= idtemp[3];
- return ret;
- }
- char authDataPro(const char *data, unsigned int len){
- unsigned char i;
- unsigned char sum=0;
- unsigned char temp[3];
- unsigned char *dataPtr=data+3;
- unsigned int codeData;
- if(data[0] != PRO_HEADER) return 1;
- if((data[2]+3) != len) return 2;
- for(i=0;i<len-1;i++) sum ^= data[i];
- if(sum != data[len-1]) return 3;
-
- temp[0]=len;
- temp[1]=data[0];
- temp[2]=data[1];
-
- for(i=0;i<data[1]-1;i++){
- dataPtr[i] ^= temp[i % 3];
- dataPtr[i] ^= TX_RX_SEED;
- }
-
- if(data[1]==PRO_AUTH_ACK){
- return 5;
- }else if(data[1]==PRO_AUTH_QUERY){
- return 6;
- }
-
- if(data[1]!=PRO_AUTH_SUC) return 4;
- //鏀跺埌DTU閫氱煡閴存潈鎴愬姛锛岃幏鍙栭壌鏉冪爜
- //璺宠繃娴佹按鍙?
- dataPtr += 2;
- EncodeKeyWithFactorAndMode(dataPtr);
- codeData=dataPtr[0];codeData <<= 8;codeData &= 0xFFFFFF00;
- codeData |= dataPtr[1];codeData <<= 8;codeData &= 0xFFFFFF00;
- codeData |= dataPtr[2];codeData <<= 8;codeData &= 0xFFFFFF00;
- codeData |= dataPtr[3];
- #if 0
- dataPtr += 4;
- //SlaveNum=*dataPtr;
- authInfo->SlaveNum=*dataPtr;
- //write new auth
- authInfo->authCode=codeData;
- //make lier data
- for(i=0;i<sizeof(authInfo->noUseStart);i++){
- authInfo->noUseStart[i]=*dataPtr++^data[6];
- if(dataPtr>=&data[len]) dataPtr=data;
- }
- for(i=0;i<sizeof(authInfo->noUseEnd);i++){
- authInfo->noUseEnd[i]=*dataPtr++^data[4];
- if(dataPtr>=&data[len]) dataPtr=data;
- }
- #endif
- if(codeData == encodeWithImei(id_imei, ID_VALID_BYTE)){
- saveAuth(modemImei);
- sutApp.authReady=1;
- return 0;
- }else return 4;
- }
- char authRecvPro(const char *data, unsigned int length){
- unsigned char senddata[ID_VALID_BYTE+16+16];
- char ret=authDataPro(data, length);
- switch(ret){
- case 0:
- authSendCmd(PRO_AUTH_OK,id_imei,ID_VALID_BYTE);
- break;
- case 4://閴存潈鍚椾笉姝g‘
- authSendCmd(PRO_AUTH_FAI, id_imei, ID_VALID_BYTE);
- break;
- case 5://鎻℃墜鎴愬姛
- handshake=true;
- break;
- case 6://璇锋眰鍩烘湰鏁版嵁
- memset(senddata, 0, sizeof(senddata));
- memcpy(senddata, id_imei, ID_VALID_BYTE);
- strcpy(senddata+ID_VALID_BYTE, FACTORY_NAME);
- strcpy(senddata+ID_VALID_BYTE+16, OHPOC_APP_NAME);
- authSendCmd(PRO_AUTH_REQ,senddata,ID_VALID_BYTE+16+16);
- break;
- }
- return ret;
- }
|