auth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: auth.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2016-04-21
  7. 功能说明: 装置授权管理
  8. 其它说明:
  9. 修改记录:
  10. */
  11. /*------------------------------- 头文件 --------------------------------------
  12. */
  13. #include "head.h"
  14. #include "base64.h"
  15. /*------------------------------- 宏定义 --------------------------------------
  16. */
  17. #define AUTH_BUF_LEN 64
  18. /*------------------------------ 类型结构 -------------------------------------
  19. */
  20. /*------------------------------ 全局变量 -------------------------------------
  21. */
  22. u8 g_auth_id[AUTH_ID_LEN];
  23. /*------------------------------ 函数声明 -------------------------------------
  24. */
  25. int read_unique_id(unsigned char *id);
  26. /*------------------------------ 外部函数 -------------------------------------
  27. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  28. */
  29. int auth_verify(u8 *data, int len)
  30. {
  31. return 0;
  32. }
  33. int auth_get_status(void)
  34. {
  35. return 0;
  36. }
  37. int auth_master_index(void)
  38. {
  39. return 0;
  40. }
  41. int auth_encrypt(u8 *id, u8 *id0, u8 *data, int len)
  42. {
  43. return 0;
  44. }
  45. int auth_get_encrypt_id(u8 *id, int len)
  46. {
  47. #define ID_ENCRPT_LEN 32
  48. return ID_ENCRPT_LEN;
  49. }
  50. int auth_init(void)
  51. {
  52. int ret = 0;
  53. #ifndef CPU_FUXI
  54. printk("HZ = %d\n", HZ);
  55. #endif
  56. ret = read_unique_id(g_auth_id);
  57. if (ret != 0)
  58. {
  59. return -1;
  60. }
  61. if (auth_master_index() >= 0)
  62. {
  63. return 0;
  64. }
  65. ret = auth_check();
  66. if (ret != 0)
  67. {
  68. u8 buf[AUTH_LICENSE_LEN];
  69. char sz[AUTH_BUF_LEN];
  70. auth_get_encrypt_id(buf, AUTH_LICENSE_LEN);
  71. encode_base64(sz, buf, AUTH_LICENSE_LEN);
  72. // print_mem("id:",buf,32);
  73. rt_printf("授权标识码:%s\r\n", sz);
  74. }
  75. return ret;
  76. }
  77. int auth_exit(void)
  78. {
  79. return 0;
  80. }
  81. // int auth_get_encrypt_id(u8 *id,
  82. #define BASE64_LENGTH(len) (4 * ((len + 2) / 3))
  83. int auth_save(u8 *buf, u32 len)
  84. {
  85. char sz[AUTH_BUF_LEN];
  86. int ret;
  87. ret = auth_verify(buf, AUTH_LICENSE_LEN);
  88. if (ret != 0)
  89. {
  90. rt_err_set(ERR_CODE_AUTH, 0);
  91. rt_printf("auth_verify:ret=%d.\r\n", ret);
  92. return -1;
  93. }
  94. else
  95. {
  96. rt_err_clr(ERR_CODE_AUTH, 0);
  97. }
  98. if (BASE64_LENGTH(len) >= AUTH_BUF_LEN)
  99. {
  100. return -1;
  101. }
  102. print_mem("auth_save:", buf, len);
  103. encode_base64(sz, buf, len);
  104. env_setenv("auth", sz);
  105. return 0;
  106. }
  107. int auth_check(void)
  108. {
  109. char *sz;
  110. char buf[64], *p;
  111. int ret;
  112. sz = env_get_info(ENV_AUTH);
  113. if (sz == NULL)
  114. {
  115. rt_err_set(ERR_CODE_AUTH, 0);
  116. rt_printf("没有授权信息!\r\n");
  117. return -11;
  118. }
  119. p = buf;
  120. decode_base64(&p, sz);
  121. // print_mem("auth_check:",buf,32);
  122. ret = auth_verify(buf, AUTH_LICENSE_LEN);
  123. if (ret != 0)
  124. {
  125. rt_err_set(ERR_CODE_AUTH, 0);
  126. rt_printf("auth_verify:ret=%d.\r\n", ret);
  127. }
  128. else
  129. {
  130. rt_printf("装置通过授权检查!\r\n");
  131. }
  132. return ret;
  133. }
  134. int auth_license(u8 *id, struct rtc_time_t *p_tm, u8 *license)
  135. {
  136. char log[128];
  137. u8 id0[32];
  138. int len, i, ret;
  139. struct rtc_time_t tm;
  140. // 如果不是授权机,返回
  141. if (auth_master_index() < 0)
  142. {
  143. return -1;
  144. }
  145. // 如果授权文件已满,停止授权
  146. if (log_file_is_overrun(LOG_AUTH))
  147. {
  148. return -2;
  149. }
  150. // 授权
  151. // print_mem(" id:",id,32);
  152. ret = auth_encrypt(id, id0, license, 32);
  153. if (ret < 0)
  154. {
  155. rt_printf("auth_encrypt error:ret=%d.\r\n", ret);
  156. print_mem(" id:", id, 32);
  157. print_mem(" id0:", id0, 32);
  158. print_mem("license:", license, 32);
  159. return ret;
  160. }
  161. // print_mem("license:",license,32);
  162. // 如果授权成功,记录授权数据
  163. memcpy(&tm, p_tm, sizeof(tm));
  164. sprintf(log, "20%02hu-%02hu-%02hu %02hu:%02hu:%02hu.%06u 授权成功[%02d]:", tm.year, tm.month, tm.day, tm.hour, tm.min, tm.ms / 1000, tm.ms % 1000 * 1000, ret);
  165. len = strlen(log);
  166. for (i = 0; i < 8; i++)
  167. {
  168. sprintf(log + len + i * 3, "%02x-", id0[i]);
  169. }
  170. len = strlen(log);
  171. log[len - 1] = 0;
  172. log_str(LOG_AUTH, log);
  173. return 0;
  174. }
  175. int auth_test(void)
  176. {
  177. u8 id[32], id0[32], license[32];
  178. int ret;
  179. auth_get_encrypt_id(id, 32);
  180. print_mem(" id:", id, 24);
  181. ret = auth_encrypt(id, id0, license, 32);
  182. rt_printf("auth_encrypt:ret=%d.\r\n", ret);
  183. print_mem(" id:", id, 24);
  184. print_mem(" id0:", id0, 24);
  185. print_mem("license:", license, 32);
  186. ret = auth_verify(license, 32);
  187. rt_printf("auth_verify:ret=%d.\r\n", ret);
  188. return 0;
  189. }
  190. extern char *gCmdArg[32];
  191. extern int gCmdArgCount;
  192. int auth_info_printf(void)
  193. {
  194. int i, ret;
  195. u8 id[64], license[64];
  196. char *p;
  197. char sz[64];
  198. struct timespec ts;
  199. struct rtc_time_t rt;
  200. if (gCmdArgCount == 2)
  201. {
  202. p = (char *)id;
  203. // rt_printf("sz=%s\r\n",gCmdArg[1]);
  204. decode_base64(&p, gCmdArg[1]);
  205. clk_time_get(&ts);
  206. timespec_to_rtc(ts, &rt, 1);
  207. ret = auth_license(id, &rt, license);
  208. if (ret == 0)
  209. {
  210. encode_base64(sz, license, AUTH_LICENSE_LEN);
  211. rt_printf("授权成功:auth=%s\r\n", sz);
  212. }
  213. else
  214. {
  215. rt_printf("授权失败:ret=%d\r\n", ret);
  216. }
  217. }
  218. else
  219. {
  220. rt_printf(" 授权状态:%d.\r\n", auth_get_status());
  221. rt_printf("授权标识码:");
  222. for (i = 0; i < 8; i++)
  223. {
  224. rt_printf("%02x", g_auth_id[i]);
  225. if (i < 7)
  226. {
  227. rt_printf("-");
  228. }
  229. }
  230. rt_printf("\r\n授权机索引:%d.\r\n", auth_master_index());
  231. }
  232. // auth_test();
  233. return 0;
  234. }
  235. /*------------------------------ 内部函数 -------------------------------------
  236. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  237. */
  238. /*------------------------------ 测试函数 -------------------------------------
  239. 一个实体文件必须带一个本模块的测试函数来进行单元测试,如果的确不方便在本模块中
  240. 进行单元测试,必须在此注明实际的测试位置(例如在哪个实体文件中使用哪个测试函数).
  241. */
  242. /*------------------------------ 文件结束 -------------------------------------
  243. */