appAuth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "includes.h"
  2. #define AUTH_FILE "m5700_info"
  3. #define ID_VALID_BYTE 12
  4. char *modemImei=NULL;
  5. char id_imei[ID_VALID_BYTE+1]={0};
  6. void saveAuthEncode(char *writebuf,int length){
  7. int fd;
  8. MSG_INFO(1,"%s save start",AUTH_FILE);
  9. fd=LSAPI_FS_Open(AUTH_FILE, LSAPI_FS_O_RDWR |LSAPI_FS_O_CREAT |LSAPI_FS_O_TRUNC,0);
  10. if(fd<0){
  11. MSG_ERR(1,"%s open failed",AUTH_FILE);
  12. return;
  13. }
  14. //淇濆瓨
  15. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  16. LSAPI_FS_Write(fd, writebuf, length);
  17. //nwy_sdk_fsync(fd);
  18. LSAPI_FS_Close(fd);
  19. MSG_INFO(1,"%s save done", AUTH_FILE);
  20. }
  21. bool readAuthEncode(char *readbuf,int length){
  22. int fd;
  23. fd=LSAPI_FS_Open(AUTH_FILE, LSAPI_FS_O_RDWR,0x0);
  24. if(fd<0){
  25. MSG_ERR(1,"%s open read failed", AUTH_FILE);
  26. return false;
  27. }
  28. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  29. LSAPI_FS_Read(fd, readbuf,length);
  30. LSAPI_FS_Close(fd);
  31. return true;
  32. }
  33. /*
  34. DecodeImeiBytes
  35. 授权码加密
  36. */
  37. void DecodeImeiBytes(unsigned char *imei){
  38. unsigned char i,j,seeds='w';
  39. j='j';
  40. for(i=0;i<IMEI_SIZE;i++){
  41. imei[i] ^= seeds;
  42. seeds += j;
  43. j += 'l';
  44. }
  45. }
  46. /*
  47. saveAuth
  48. */
  49. void saveAuth(char *imei){
  50. unsigned char writebuf[64],tmp[10];
  51. unsigned short length,i;
  52. char tempImei[IMEI_SIZE];
  53. memcpy(tempImei, imei,IMEI_SIZE );
  54. DecodeImeiBytes(tempImei);
  55. length=0;
  56. for(i=0;i<IMEI_SIZE;i++){
  57. snprintf(tmp,sizeof(tmp),"%02x",tempImei[i]);
  58. writebuf[length++]=tmp[0];
  59. //MSG_INFO(0,"%02x",tmp[0]);
  60. writebuf[length++]=tmp[1];
  61. //MSG_INFO(0,"%02x", tmp[1]);
  62. }
  63. //save code bellow
  64. saveAuthEncode(writebuf,length);
  65. }
  66. /*
  67. DecodeImei
  68. */
  69. static bool DecodeImei(char *enCodeimei, char *sourceimei){
  70. char thisimei[IMEI_SIZE+1];
  71. char i;
  72. /*
  73. MSG_INFO(1,"enCodeimei====");
  74. for(i=0;i<sizeof(thisimei)*2;i++){
  75. MSG_INFO(0,"%02x", enCodeimei[i]);
  76. }
  77. MSG_INFO(1,"over");
  78. */
  79. restoreDataFormatByHex(enCodeimei, IMEI_SIZE*2);
  80. DecodeImeiBytes(enCodeimei);
  81. memcpy(thisimei, enCodeimei,sizeof(thisimei));
  82. // thisimei[IMEI_SIZE]=0;//HYL
  83. if(0==memcmp(sourceimei, thisimei,IMEI_SIZE)) return true;
  84. else return false;
  85. }
  86. void shortImei(char *srcImei){
  87. int i=0,j=0,w;
  88. int len=strlen(srcImei);
  89. if(len>ID_VALID_BYTE){
  90. w=(len-ID_VALID_BYTE)*2;
  91. for(i=0;i<w;i+=2){
  92. id_imei[j++]=charStrToHex(srcImei[i],srcImei[i+1]);
  93. }
  94. memcpy(id_imei+j, srcImei+w, len-w);
  95. }else{
  96. for(i=0;i<ID_VALID_BYTE-len;i++){
  97. id_imei[j++]=0;
  98. }
  99. for(i=ID_VALID_BYTE-len;i<ID_VALID_BYTE;i++) id_imei[j++]=srcImei[i];
  100. }
  101. id_imei[ID_VALID_BYTE]=0;
  102. }
  103. bool localAuthOk(void){
  104. unsigned char enCodeImei[(IMEI_SIZE+1)*2];
  105. //鏈夐壌鏉冩枃浠讹紝鍒欒�IMEI杩涜�瀵规瘮
  106. if(modemImei==NULL){
  107. modemImei=ohpocGetIMEI();
  108. if(modemImei==NULL) return false;
  109. if(modemImei[0]=='\0'){
  110. modemImei=NULL;
  111. return false;
  112. }
  113. shortImei(modemImei);
  114. }
  115. //璇诲彇鏄�惁鏈夐壌鏉冩枃浠?
  116. if(false==readAuthEncode(enCodeImei, sizeof(enCodeImei))) return false;
  117. //濡傛灉涓嶪MEI涓€鑷达紝鍒欓壌鏉冮€氳繃
  118. return DecodeImei(enCodeImei, modemImei);
  119. return 1;
  120. }
  121. /////////////////////////////鎺堟潈閮ㄥ垎///////////////////////////////
  122. #define PRO_HEADER 0x89
  123. #define TX_RX_SEED 0x86
  124. #define PRO_AUTH_RESPONS 0x30 //鍚戞湇鍔″櫒璇锋眰鎻℃墜
  125. #define PRO_AUTH_ACK 0x31 //鏈嶅姟鍝嶅簲鎻℃墜
  126. #define PRO_AUTH_QUERY 0X32 //鏈嶅姟鍣ㄥ悜鎴戠储瑕?鍙傛暟璇锋眰
  127. #define PRO_AUTH_REQ 0x34 //缁堢�璇锋眰閴存潈
  128. #define PRO_AUTH_OK 0x35 //缁堢�搴旂瓟閴存潈缁撴灉涓€鏍?
  129. #define PRO_AUTH_SUC 0x36 //閴存潈宸ュ叿閫氱煡閴存潈鎴愬姛 杩斿洖閴存潈鐮?
  130. #define PRO_AUTH_FAI 0x37 //缁堢�閫氱煡閿�壌鏉冪粨鏋滀笉涓€鑷?
  131. typedef struct
  132. {
  133. unsigned char noUseStart[31];//涓嶄娇鐢ㄦ�
  134. unsigned int authCode;//涓嶉€傜敤绗�竴涓�瓧鑺?
  135. unsigned char SlaveNum;//浠庢満鍙�敤涓�暟
  136. unsigned char noUseEnd[17];
  137. } __attribute__((packed)) AUTH_INFO;
  138. void authSendCmd(unsigned char cmd, unsigned char *data, int len){
  139. //鍗忚�鏍煎紡
  140. //head cmd len data
  141. //data:liushui[2] id[12] 鍧囩粡杩嘽md鍜岄暱搴﹀姞瀵?
  142. static unsigned short liushui=0;
  143. unsigned char sendbuf[128];
  144. unsigned char i,k;
  145. unsigned char *dataPtr=sendbuf+3;
  146. unsigned char datalen=2+len+1;//?贸茅?sum
  147. unsigned char temp[3],sum;
  148. i=0;
  149. sendbuf[i++]=PRO_HEADER;
  150. sendbuf[i++]=cmd;
  151. sendbuf[i++]=datalen;
  152. sendbuf[i++]=liushui>>8; //4-5
  153. sendbuf[i++]=liushui;
  154. for(k=0;k<len;k++)
  155. sendbuf[i++]=data[k];
  156. temp[0]=3+datalen;//脳眉3陇?猫锟?掳眉脿篓sum //47+3
  157. temp[1]=sendbuf[0]; //0x89
  158. temp[2]=sendbuf[1]; //0x34
  159. for(k=0;k<datalen-1;k++){
  160. dataPtr[k] ^= temp[k % 3];
  161. dataPtr[k] ^= TX_RX_SEED;
  162. }
  163. sum=0;
  164. for(k=0;k<i;k++) sum ^= sendbuf[k];
  165. sendbuf[i++]=sum;
  166. usbOutPut(sendbuf, i);
  167. }
  168. bool handshake=false;
  169. void localAuthNow(void){
  170. static char showFlag=0;
  171. static unsigned int cnt=0;
  172. sutApp.authReady=0;
  173. #ifndef AUTH_SKIP
  174. for(;;){
  175. if(true==localAuthOk()) break;
  176. if(showFlag==0){
  177. guiShowMessageBox("未授权");
  178. showFlag=1;
  179. }
  180. if(++cnt>=10){
  181. if(handshake==false) authSendCmd(PRO_AUTH_RESPONS,0,0);
  182. cnt=0;
  183. }
  184. LSAPI_OSI_ThreadSleep(100);
  185. }
  186. if(showFlag) guiShowMessageBox("授权通过");
  187. #endif
  188. sutApp.authReady=1;
  189. }
  190. #define FACTORY_NAME "YT"
  191. //#define FACTORY_NAME "RTL"
  192. void EncodeKeyWithFactorAndMode(unsigned char *code)
  193. {
  194. int i,j;
  195. char *FN=FACTORY_NAME;
  196. char *MN=OHPOC_APP_NAME;
  197. j=strlen(FACTORY_NAME);
  198. for(i=0;i<j;i++) code[i%4] ^= FN[i];
  199. j=strlen(OHPOC_APP_NAME);
  200. for(i=0;i<j;i++) code[i%4] ^= MN[i];
  201. }
  202. unsigned int encodeWithImei(unsigned char *id, unsigned char idlen){
  203. const unsigned char sucRandTable[256]={
  204. 0x68,0x59,0x63,0x64,0xc8,0xcc,0xaa,0xa7,0xa2,0xab,0xa5,0x6e,0x6a,0x56,0x8a,0x57,
  205. 0xf4,0xbb,0xfe,0xfa,0x26,0x21,0x2d,0x29,0x0d,0x09,0x3e,0x3a,0x37,0x32,0x0a,0x07,
  206. 0xf7,0xf2,0xfb,0xf5,0xff,0xf6,0x1c,0x18,0x13,0x14,0xf1,0xfd,0xf9,0x0c,0x9f,0x08,
  207. 0x9a,0x97,0x92,0x9b,0x95,0x9f,0x12,0x1b,0x15,0x2c,0x28,0x23,0xf9,0x2a,0x27,0x22,
  208. 0x24,0x2e,0x67,0x25,0x2f,0x98,0xfc,0x75,0xf8,0xf3,0x3c,0x38,0x33,0x34,0x03,0x04,
  209. 0x05,0x0f,0x06,0x01,0x7a,0x77,0x72,0x62,0x6b,0x83,0x84,0x8e,0xc4,0xcb,0x87,0x82,
  210. 0x35,0x3f,0x36,0x4f,0x3d,0x39,0x9c,0x98,0x93,0x94,0x42,0x4f,0x46,0x41,0x81,0x8d,
  211. 0x4d,0x49,0x71,0x7d,0x79,0x96,0x91,0x9d,0x99,0xc3,0xc4,0x1d,0xce,0xca,0x7f,0x76,
  212. 0x7c,0x78,0x73,0x74,0x7e,0x7b,0x75,0x1e,0x1a,0x17,0x1f,0x16,0x11,0x1d,0x47,0x19,
  213. 0x5d,0xc7,0xc2,0xcb,0xc5,0xcf,0xd1,0xdd,0xd9,0x4c,0x48,0xc6,0xc1,0xcd,0x61,0xc9,
  214. 0xd7,0x8c,0xfd,0x88,0xa1,0xad,0xa9,0xb1,0xbd,0xb9,0x8b,0x85,0x8f,0x4b,0x45,0x86,
  215. 0xdc,0xd8,0xd3,0xd4,0xde,0xd2,0xdb,0xd5,0xdf,0xd6,0x43,0x44,0x4e,0x4a,0xe7,0x47,
  216. 0xac,0xa8,0xa3,0xa4,0xae,0xba,0x0d,0xb7,0xb2,0xaf,0xa6,0xe4,0xee,0xea,0xe7,0xe2,
  217. 0xe1,0xed,0x0f,0xe9,0x66,0x61,0x6d,0x69,0xbb,0xb5,0xbf,0xb6,0xec,0xe8,0x0b,0xe3,
  218. 0x52,0x5c,0x65,0x6f,0x58,0x53,0x54,0x0c,0x5e,0x5b,0x55,0x5f,0x31,0x2f,0x9a,0x0b,
  219. 0x0b,0x5a,0x2b,0x9e,0x02,0x89,0x59,0x3b,0x0e,0x51,0x6c,0xeb,0xda,0xe6,0x31,0xf6
  220. };
  221. unsigned char i;
  222. unsigned int ret;
  223. unsigned char temp1,temp2;
  224. unsigned char idtemp[64];
  225. temp1=0;
  226. for(i=0;i<idlen;i++){
  227. temp1 ^= id[i];
  228. idtemp[i]=id[i];
  229. }
  230. temp1 ^= idlen;
  231. temp2=sucRandTable[temp1];
  232. //脩颅禄路虏茅卤铆脪矛禄貌脭脣脣茫拢卢脡煤鲁脡脨脗id
  233. for(i=0;i<idlen;i++){
  234. temp1=idtemp[i]^temp2;
  235. idtemp[i]=i^temp1;
  236. temp2=sucRandTable[temp1];
  237. }
  238. //脳卯潞贸脙驴3脳脰陆脷脭脵脪矛禄貌碌脙碌陆4脳脰陆脷脳梅脦陋脢盲鲁枚
  239. for(i=0;i<4;i++){
  240. temp1=idtemp[3*i] ^ idtemp[3*i+1];
  241. idtemp[i]=temp1 ^ idtemp[3*i+2];
  242. }
  243. ret=idtemp[0];ret <<= 8;ret &= 0xFFFFFF00;
  244. ret |= idtemp[1];ret <<= 8;ret &= 0xFFFFFF00;
  245. ret |= idtemp[2];ret <<= 8;ret &= 0xFFFFFF00;
  246. ret |= idtemp[3];
  247. return ret;
  248. }
  249. char authDataPro(const char *data, unsigned int len){
  250. unsigned char i;
  251. unsigned char sum=0;
  252. unsigned char temp[3];
  253. unsigned char *dataPtr=data+3;
  254. unsigned int codeData;
  255. if(data[0] != PRO_HEADER) return 1;
  256. if((data[2]+3) != len) return 2;
  257. for(i=0;i<len-1;i++) sum ^= data[i];
  258. if(sum != data[len-1]) return 3;
  259. temp[0]=len;
  260. temp[1]=data[0];
  261. temp[2]=data[1];
  262. for(i=0;i<data[1]-1;i++){
  263. dataPtr[i] ^= temp[i % 3];
  264. dataPtr[i] ^= TX_RX_SEED;
  265. }
  266. if(data[1]==PRO_AUTH_ACK){
  267. return 5;
  268. }else if(data[1]==PRO_AUTH_QUERY){
  269. return 6;
  270. }
  271. if(data[1]!=PRO_AUTH_SUC) return 4;
  272. //鏀跺埌DTU閫氱煡閴存潈鎴愬姛锛岃幏鍙栭壌鏉冪爜
  273. //璺宠繃娴佹按鍙?
  274. dataPtr += 2;
  275. EncodeKeyWithFactorAndMode(dataPtr);
  276. codeData=dataPtr[0];codeData <<= 8;codeData &= 0xFFFFFF00;
  277. codeData |= dataPtr[1];codeData <<= 8;codeData &= 0xFFFFFF00;
  278. codeData |= dataPtr[2];codeData <<= 8;codeData &= 0xFFFFFF00;
  279. codeData |= dataPtr[3];
  280. #if 0
  281. dataPtr += 4;
  282. //SlaveNum=*dataPtr;
  283. authInfo->SlaveNum=*dataPtr;
  284. //write new auth
  285. authInfo->authCode=codeData;
  286. //make lier data
  287. for(i=0;i<sizeof(authInfo->noUseStart);i++){
  288. authInfo->noUseStart[i]=*dataPtr++^data[6];
  289. if(dataPtr>=&data[len]) dataPtr=data;
  290. }
  291. for(i=0;i<sizeof(authInfo->noUseEnd);i++){
  292. authInfo->noUseEnd[i]=*dataPtr++^data[4];
  293. if(dataPtr>=&data[len]) dataPtr=data;
  294. }
  295. #endif
  296. if(codeData == encodeWithImei(id_imei, ID_VALID_BYTE)){
  297. saveAuth(modemImei);
  298. sutApp.authReady=1;
  299. return 0;
  300. }else return 4;
  301. }
  302. char authRecvPro(const char *data, unsigned int length){
  303. unsigned char senddata[ID_VALID_BYTE+16+16];
  304. char ret=authDataPro(data, length);
  305. switch(ret){
  306. case 0:
  307. authSendCmd(PRO_AUTH_OK,id_imei,ID_VALID_BYTE);
  308. break;
  309. case 4://閴存潈鍚椾笉姝g‘
  310. authSendCmd(PRO_AUTH_FAI, id_imei, ID_VALID_BYTE);
  311. break;
  312. case 5://鎻℃墜鎴愬姛
  313. handshake=true;
  314. break;
  315. case 6://璇锋眰鍩烘湰鏁版嵁
  316. memset(senddata, 0, sizeof(senddata));
  317. memcpy(senddata, id_imei, ID_VALID_BYTE);
  318. strcpy(senddata+ID_VALID_BYTE, FACTORY_NAME);
  319. strcpy(senddata+ID_VALID_BYTE+16, OHPOC_APP_NAME);
  320. authSendCmd(PRO_AUTH_REQ,senddata,ID_VALID_BYTE+16+16);
  321. break;
  322. }
  323. return ret;
  324. }