appAuth.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #include "includes.h"
  2. #define AUTH_FILE "hauth"
  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. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  15. LSAPI_FS_Write(fd, writebuf, length);
  16. LSAPI_FS_Close(fd);
  17. MSG_INFO(1,"%s save done", AUTH_FILE);
  18. }
  19. bool readAuthEncode(char *readbuf,int length){
  20. int fd;
  21. fd=LSAPI_FS_Open(AUTH_FILE, LSAPI_FS_O_RDWR,0x0);
  22. if(fd<0){
  23. MSG_ERR(1,"%s open read failed", AUTH_FILE);
  24. return false;
  25. }
  26. LSAPI_FS_Seek(fd, 0L, LSAPI_FS_SEEK_SET);
  27. LSAPI_FS_Read(fd, readbuf,length);
  28. LSAPI_FS_Close(fd);
  29. return true;
  30. }
  31. /*
  32. DecodeImeiBytes
  33. 授权码加?
  34. */
  35. void DecodeImeiBytes(unsigned char *imei){
  36. unsigned char i,j,seeds='w';
  37. j='j';
  38. for(i=0;i<IMEI_SIZE;i++){
  39. imei[i] ^= seeds;
  40. seeds += j;
  41. j += 'l';
  42. }
  43. }
  44. /*
  45. saveAuth
  46. */
  47. /*
  48. saveAuth
  49. 保存授权?
  50. */
  51. void saveAuth(char *imei){
  52. unsigned char writebuf[64],tmp[10];
  53. unsigned short length,i;
  54. char tempImei[IMEI_SIZE];
  55. memcpy(tempImei, imei,IMEI_SIZE );
  56. DecodeImeiBytes(tempImei);
  57. length=0;
  58. for(i=0;i<IMEI_SIZE;i++){
  59. snprintf(tmp,sizeof(tmp),"%02x",tempImei[i]);
  60. writebuf[length++]=tmp[0];
  61. writebuf[length++]=tmp[1];
  62. }
  63. //save code bellow
  64. saveAuthEncode(writebuf,length);
  65. }
  66. /*
  67. DecodeImei
  68. 授权码解?
  69. */
  70. static bool DecodeImei(char *enCodeimei, char *sourceimei){
  71. char thisimei[IMEI_SIZE+1];
  72. char i;
  73. restoreDataFormatByHex(enCodeimei, IMEI_SIZE*2);
  74. DecodeImeiBytes(enCodeimei);
  75. memcpy(thisimei, enCodeimei,sizeof(thisimei));
  76. // thisimei[IMEI_SIZE]=0;//HYL
  77. if(0==memcmp(sourceimei, thisimei,IMEI_SIZE)) return true;
  78. else return false;
  79. }
  80. void shortImei(char *srcImei){
  81. int i=0,j=0,w;
  82. int len=strlen(srcImei);
  83. if(len>ID_VALID_BYTE){
  84. w=(len-ID_VALID_BYTE)*2;
  85. for(i=0;i<w;i+=2){
  86. id_imei[j++]=charStrToHex(srcImei[i],srcImei[i+1]);
  87. }
  88. memcpy(id_imei+j, srcImei+w, len-w);
  89. }else{
  90. for(i=0;i<ID_VALID_BYTE-len;i++){
  91. id_imei[j++]=0;
  92. }
  93. for(i=ID_VALID_BYTE-len;i<ID_VALID_BYTE;i++) id_imei[j++]=srcImei[i];
  94. }
  95. id_imei[ID_VALID_BYTE]=0;
  96. }
  97. bool localAuthOk(void){
  98. unsigned char enCodeImei[(IMEI_SIZE+1)*2];
  99. //有鉴权文件,则读IMEI进行对比
  100. if(modemImei==NULL){
  101. modemImei=ohpocGetIMEI();
  102. if(modemImei==NULL) return false;
  103. if(modemImei[0]=='\0'){
  104. modemImei=NULL;
  105. return false;
  106. }
  107. shortImei(modemImei);
  108. }
  109. //读取是否有鉴权文?
  110. if(false==readAuthEncode(enCodeImei, sizeof(enCodeImei))) return false;
  111. //如果与IMEI一致,则鉴权通过
  112. return DecodeImei(enCodeImei, modemImei);
  113. }
  114. /////////////////////////////授权部分///////////////////////////////
  115. #define PRO_HEADER 0x89
  116. #define TX_RX_SEED 0x86
  117. #define PRO_AUTH_RESPONS 0x30 //向服务器请求握手
  118. #define PRO_AUTH_ACK 0x31 //服务响应握手
  119. #define PRO_AUTH_QUERY 0X32 //服务器向我索?参数请求
  120. #define PRO_AUTH_REQ 0x34 //终端请求鉴权
  121. #define PRO_AUTH_OK 0x35 //终端应答鉴权结果一?
  122. #define PRO_AUTH_SUC 0x36 //鉴权工具通知鉴权成功 返回鉴权?
  123. #define PRO_AUTH_FAI 0x37 //终端通知键鉴权结果不一?
  124. typedef struct
  125. {
  126. unsigned char noUseStart[31];//不使用段
  127. unsigned int authCode;//不适用第一个字?
  128. unsigned char SlaveNum;//从机可用个数
  129. unsigned char noUseEnd[17];
  130. } __attribute__((packed)) AUTH_INFO;
  131. void authSendCmd(unsigned char cmd, unsigned char *data, int len){
  132. //协议格式
  133. //head cmd len data
  134. //data:liushui[2] id[12] 均经过cmd和长度加?
  135. static unsigned short liushui=0;
  136. unsigned char sendbuf[128];
  137. unsigned char i,k;
  138. unsigned char *dataPtr=sendbuf+3;
  139. unsigned char datalen=2+len+1;//?óé?sum
  140. unsigned char temp[3],sum;
  141. i=0;
  142. sendbuf[i++]=PRO_HEADER;
  143. sendbuf[i++]=cmd;
  144. sendbuf[i++]=datalen;
  145. sendbuf[i++]=liushui>>8; //4-5
  146. sendbuf[i++]=liushui;
  147. for(k=0;k<len;k++)
  148. sendbuf[i++]=data[k];
  149. temp[0]=3+datalen;//×ü3¤?è?°üà¨sum //47+3
  150. temp[1]=sendbuf[0]; //0x89
  151. temp[2]=sendbuf[1]; //0x34
  152. for(k=0;k<datalen-1;k++){
  153. dataPtr[k] ^= temp[k % 3];
  154. dataPtr[k] ^= TX_RX_SEED;
  155. }
  156. sum=0;
  157. for(k=0;k<i;k++) sum ^= sendbuf[k];
  158. sendbuf[i++]=sum;
  159. usbOutPut(sendbuf, i);
  160. }
  161. bool handshake=false;
  162. void localAuthNow(void){
  163. static char showFlag=0;
  164. static unsigned int cnt=0;
  165. static unsigned char dlyCnt=0;
  166. #ifdef DEVELOP_MODE
  167. sutApp.authReady=1;
  168. return;
  169. #else
  170. sutApp.authReady=0;
  171. #endif
  172. for(;;){
  173. if(true==localAuthOk()) break;
  174. if(dlyCnt==10){
  175. if(showFlag==0){
  176. guiShowMessageBox("δȨ");
  177. showFlag=1;
  178. }
  179. }else dlyCnt++;
  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. sutApp.authReady=1;
  188. }
  189. #define FACTORY_NAME "ZT"
  190. //#define FACTORY_NAME "RTL"
  191. void EncodeKeyWithFactorAndMode(unsigned char *code)
  192. {
  193. int i,j;
  194. char *FN=FACTORY_NAME;
  195. char *MN=OHPOC_APP_NAME;
  196. j=strlen(FACTORY_NAME);
  197. for(i=0;i<j;i++) code[i%4] ^= FN[i];
  198. j=strlen(OHPOC_APP_NAME);
  199. for(i=0;i<j;i++) code[i%4] ^= MN[i];
  200. }
  201. unsigned int encodeWithImei(unsigned char *id, unsigned char idlen){
  202. const unsigned char sucRandTable[256]={
  203. 0x68,0x59,0x63,0x64,0xc8,0xcc,0xaa,0xa7,0xa2,0xab,0xa5,0x6e,0x6a,0x56,0x8a,0x57,
  204. 0xf4,0xbb,0xfe,0xfa,0x26,0x21,0x2d,0x29,0x0d,0x09,0x3e,0x3a,0x37,0x32,0x0a,0x07,
  205. 0xf7,0xf2,0xfb,0xf5,0xff,0xf6,0x1c,0x18,0x13,0x14,0xf1,0xfd,0xf9,0x0c,0x9f,0x08,
  206. 0x9a,0x97,0x92,0x9b,0x95,0x9f,0x12,0x1b,0x15,0x2c,0x28,0x23,0xf9,0x2a,0x27,0x22,
  207. 0x24,0x2e,0x67,0x25,0x2f,0x98,0xfc,0x75,0xf8,0xf3,0x3c,0x38,0x33,0x34,0x03,0x04,
  208. 0x05,0x0f,0x06,0x01,0x7a,0x77,0x72,0x62,0x6b,0x83,0x84,0x8e,0xc4,0xcb,0x87,0x82,
  209. 0x35,0x3f,0x36,0x4f,0x3d,0x39,0x9c,0x98,0x93,0x94,0x42,0x4f,0x46,0x41,0x81,0x8d,
  210. 0x4d,0x49,0x71,0x7d,0x79,0x96,0x91,0x9d,0x99,0xc3,0xc4,0x1d,0xce,0xca,0x7f,0x76,
  211. 0x7c,0x78,0x73,0x74,0x7e,0x7b,0x75,0x1e,0x1a,0x17,0x1f,0x16,0x11,0x1d,0x47,0x19,
  212. 0x5d,0xc7,0xc2,0xcb,0xc5,0xcf,0xd1,0xdd,0xd9,0x4c,0x48,0xc6,0xc1,0xcd,0x61,0xc9,
  213. 0xd7,0x8c,0xfd,0x88,0xa1,0xad,0xa9,0xb1,0xbd,0xb9,0x8b,0x85,0x8f,0x4b,0x45,0x86,
  214. 0xdc,0xd8,0xd3,0xd4,0xde,0xd2,0xdb,0xd5,0xdf,0xd6,0x43,0x44,0x4e,0x4a,0xe7,0x47,
  215. 0xac,0xa8,0xa3,0xa4,0xae,0xba,0x0d,0xb7,0xb2,0xaf,0xa6,0xe4,0xee,0xea,0xe7,0xe2,
  216. 0xe1,0xed,0x0f,0xe9,0x66,0x61,0x6d,0x69,0xbb,0xb5,0xbf,0xb6,0xec,0xe8,0x0b,0xe3,
  217. 0x52,0x5c,0x65,0x6f,0x58,0x53,0x54,0x0c,0x5e,0x5b,0x55,0x5f,0x31,0x2f,0x9a,0x0b,
  218. 0x0b,0x5a,0x2b,0x9e,0x02,0x89,0x59,0x3b,0x0e,0x51,0x6c,0xeb,0xda,0xe6,0x31,0xf6
  219. };
  220. unsigned char i;
  221. unsigned int ret;
  222. unsigned char temp1,temp2;
  223. unsigned char idtemp[64];
  224. temp1=0;
  225. for(i=0;i<idlen;i++){
  226. temp1 ^= id[i];
  227. idtemp[i]=id[i];
  228. }
  229. temp1 ^= idlen;
  230. temp2=sucRandTable[temp1];
  231. //Ñ­»·²é±íÒì»òÔËË㣬Éú³ÉÐÂid
  232. for(i=0;i<idlen;i++){
  233. temp1=idtemp[i]^temp2;
  234. idtemp[i]=i^temp1;
  235. temp2=sucRandTable[temp1];
  236. }
  237. //×îºóÿ3×Ö½ÚÔÙÒì»òµÃµ½4×Ö½Ú×÷ΪÊä³ö
  238. for(i=0;i<4;i++){
  239. temp1=idtemp[3*i] ^ idtemp[3*i+1];
  240. idtemp[i]=temp1 ^ idtemp[3*i+2];
  241. }
  242. ret=idtemp[0];ret <<= 8;ret &= 0xFFFFFF00;
  243. ret |= idtemp[1];ret <<= 8;ret &= 0xFFFFFF00;
  244. ret |= idtemp[2];ret <<= 8;ret &= 0xFFFFFF00;
  245. ret |= idtemp[3];
  246. return ret;
  247. }
  248. char authDataPro(const char *data, unsigned int len){
  249. unsigned char i;
  250. unsigned char sum=0;
  251. unsigned char temp[3];
  252. unsigned char *dataPtr=data+3;
  253. unsigned int codeData;
  254. if(data[0] != PRO_HEADER) return 1;
  255. if((data[2]+3) != len) return 2;
  256. for(i=0;i<len-1;i++) sum ^= data[i];
  257. if(sum != data[len-1]) return 3;
  258. temp[0]=len;
  259. temp[1]=data[0];
  260. temp[2]=data[1];
  261. for(i=0;i<data[1]-1;i++){
  262. dataPtr[i] ^= temp[i % 3];
  263. dataPtr[i] ^= TX_RX_SEED;
  264. }
  265. if(data[1]==PRO_AUTH_ACK){
  266. return 5;
  267. }else if(data[1]==PRO_AUTH_QUERY){
  268. return 6;
  269. }
  270. if(data[1]!=PRO_AUTH_SUC) return 4;
  271. //收到DTU通知鉴权成功,获取鉴权码
  272. //跳过流水?
  273. dataPtr += 2;
  274. EncodeKeyWithFactorAndMode(dataPtr);
  275. codeData=dataPtr[0];codeData <<= 8;codeData &= 0xFFFFFF00;
  276. codeData |= dataPtr[1];codeData <<= 8;codeData &= 0xFFFFFF00;
  277. codeData |= dataPtr[2];codeData <<= 8;codeData &= 0xFFFFFF00;
  278. codeData |= dataPtr[3];
  279. #if 0
  280. dataPtr += 4;
  281. //SlaveNum=*dataPtr;
  282. authInfo->SlaveNum=*dataPtr;
  283. //write new auth
  284. authInfo->authCode=codeData;
  285. //make lier data
  286. for(i=0;i<sizeof(authInfo->noUseStart);i++){
  287. authInfo->noUseStart[i]=*dataPtr++^data[6];
  288. if(dataPtr>=&data[len]) dataPtr=data;
  289. }
  290. for(i=0;i<sizeof(authInfo->noUseEnd);i++){
  291. authInfo->noUseEnd[i]=*dataPtr++^data[4];
  292. if(dataPtr>=&data[len]) dataPtr=data;
  293. }
  294. #endif
  295. if(codeData == encodeWithImei(id_imei, ID_VALID_BYTE)){
  296. saveAuth(modemImei);
  297. sutApp.authReady=1;
  298. return 0;
  299. }else return 4;
  300. }
  301. char authRecvPro(const char *data, unsigned int length){
  302. unsigned char senddata[ID_VALID_BYTE+16+16];
  303. char ret=authDataPro(data, length);
  304. switch(ret){
  305. case 0:
  306. authSendCmd(PRO_AUTH_OK,id_imei,ID_VALID_BYTE);
  307. break;
  308. case 4://鉴权吗不正确
  309. authSendCmd(PRO_AUTH_FAI, id_imei, ID_VALID_BYTE);
  310. break;
  311. case 5://握手成功
  312. handshake=true;
  313. break;
  314. case 6://请求基本数据
  315. memset(senddata, 0, sizeof(senddata));
  316. memcpy(senddata, id_imei, ID_VALID_BYTE);
  317. strcpy(senddata+ID_VALID_BYTE, FACTORY_NAME);
  318. strcpy(senddata+ID_VALID_BYTE+16, OHPOC_APP_NAME);
  319. authSendCmd(PRO_AUTH_REQ,senddata,ID_VALID_BYTE+16+16);
  320. break;
  321. }
  322. return ret;
  323. }