appAuth.c 8.4 KB

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