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