at_client.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #include <at_aes_crypt.h>
  13. #include <at_base64url.h>
  14. #include <at_client.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #define LOG_TAG "vtrust_at_client"
  19. char app_uuid[40] = {"186b54c8-8bd0-11ed-a8b1-cb652ea15773"};
  20. uint8_t app_request_id[4] = {0x12, 0x43, 0x55, 0x78};
  21. static uint8_t app_cek[16] = {0};
  22. struct iv iv_temp = {
  23. .byte = {1, 67, 43, 87, 46, 67, 34, 56, 78, 97, 56, 78, 24, 16, 45, 73},
  24. };
  25. struct key aes_key = {
  26. .byte = {20, 47, 89, 56, 23, 47, 89, 52, 12, 47, 89, 56, 123, 7, 85,
  27. 46},
  28. };
  29. static int at_send_recv(void* in, size_t in_size, void* out, size_t* out_size) {
  30. int rc;
  31. nbl_chn_t* chn;
  32. char* srv_name = AT_SRV_NAME;
  33. if ((!in) || (!out) || (in_size > MSG_SIZE_MAX)) {
  34. rc = NBL_ERR_INVALID_ARGS;
  35. goto failed;
  36. }
  37. rc = nbl_chn_open(srv_name, 0, &chn);
  38. if (rc != NBL_NO_ERROR) {
  39. NBL_LOGE("Connect to %s failed.\n", srv_name);
  40. rc = NBL_ERR_IO;
  41. goto connect_failed;
  42. }
  43. rc = nbl_chn_write_buf(chn, in, in_size);
  44. if (rc != in_size) {
  45. NBL_LOGE("Service B Client write buffer failed (%d).\n", rc);
  46. rc = NBL_ERR_IO;
  47. goto write_buf_failed;
  48. }
  49. rc = nbl_chn_read_buf(chn, out, *out_size, NBL_TIMEOUT_INFINITY);
  50. if (rc > *out_size) {
  51. rc = NBL_ERR_IO;
  52. goto read_buf_failed;
  53. }
  54. *out_size = rc;
  55. NBL_LOGI("buf size: %zu read buf:%s\n", *out_size, out);
  56. rc = NBL_NO_ERROR;
  57. read_buf_failed:
  58. write_buf_failed:
  59. nbl_chn_close(chn);
  60. connect_failed:
  61. failed:
  62. return rc;
  63. }
  64. static void get_cek(uint8_t* cek) {
  65. memcpy(cek, app_cek, 16);
  66. }
  67. static void get_requset_id(uint8_t* requset_id, size_t size) {
  68. memcpy(requset_id, app_request_id, size);
  69. }
  70. static void get_uuid(char* uuid, size_t size) {
  71. memcpy(uuid, app_uuid, size);
  72. }
  73. static void bytes_to_hexstring(uint8_t* in,
  74. size_t in_size,
  75. char* out,
  76. size_t out_size) {
  77. if (in == NULL || out == NULL || out_size < ((in_size * 2) + 1)) {
  78. return;
  79. }
  80. char* ptr = out;
  81. for (size_t i = 0; i < in_size; i++) {
  82. sprintf(ptr, "%02x", in[i]);
  83. ptr = ptr + 2;
  84. }
  85. }
  86. static void string_to_dec(char* in,
  87. size_t in_size,
  88. void* out,
  89. size_t out_size) {
  90. uint8_t flg = 0;
  91. int temp = 0;
  92. if (in == NULL || out == NULL) {
  93. return;
  94. }
  95. if (*in == '-') {
  96. in++;
  97. in_size--;
  98. flg = 1;
  99. }
  100. for (int i = 0; i < in_size; i++) {
  101. temp = temp * 10 + (*in - '0');
  102. in++;
  103. }
  104. if (flg) {
  105. temp = -1 * temp;
  106. }
  107. memset(out, 0, out_size);
  108. if (out_size >= sizeof(temp)) {
  109. memcpy(out, &temp, sizeof(temp));
  110. } else {
  111. memcpy(out, &temp, out_size);
  112. }
  113. }
  114. static int at_command_encrypt(char* in,
  115. size_t in_size,
  116. char* out,
  117. size_t* out_size) {
  118. int rc;
  119. char* cbase64url_out = NULL;
  120. size_t base64url_size;
  121. struct key cek;
  122. if ((in == NULL) || (out == NULL)) {
  123. *out_size = 0;
  124. return -1;
  125. }
  126. at_crypt_data crypt_in = {
  127. .data = in,
  128. .size = in_size,
  129. };
  130. at_crypt_data crypt_out;
  131. get_cek(cek.byte);
  132. rc = at_aes_hmac_encrypt(&cek, &crypt_in, &crypt_out, &iv_temp);
  133. if (rc < 0) {
  134. sprintf(out, "%s", "app aes encrypt error");
  135. *out_size = 0;
  136. return -1;
  137. }
  138. rc = at_base64url_encode((uint8_t*)crypt_out.data, crypt_out.size,
  139. &cbase64url_out, &base64url_size);
  140. free(crypt_out.data);
  141. if (rc < 0) {
  142. sprintf(out, "%s", "app aes encrypt error");
  143. *out_size = 0;
  144. return -1;
  145. }
  146. memcpy(out, cbase64url_out, base64url_size);
  147. *out_size = base64url_size;
  148. free(cbase64url_out);
  149. return 0;
  150. }
  151. static int at_command_decrypt(char* in,
  152. size_t in_size,
  153. char* out,
  154. size_t* out_size) {
  155. int rc;
  156. uint8_t* ubase64url_out = NULL;
  157. size_t base64url_size;
  158. struct key cek;
  159. if ((in == NULL) || (out == NULL)) {
  160. *out_size = 0;
  161. return -1;
  162. }
  163. rc = at_base64url_decode(in, in_size, &ubase64url_out, &base64url_size);
  164. if (rc < 0) {
  165. sprintf(out, "%s", "app aes decrypt error");
  166. *out_size = strlen(out);
  167. return -1;
  168. }
  169. at_crypt_data crypt_in = {
  170. .data = ubase64url_out,
  171. .size = base64url_size,
  172. };
  173. at_crypt_data crypt_out;
  174. get_cek(cek.byte);
  175. rc = at_aes_hmac_decrypt(&cek, &crypt_in, &crypt_out, &iv_temp);
  176. free(ubase64url_out);
  177. if (rc < 0) {
  178. sprintf(out, "%s", "app aes decrypt error");
  179. *out_size = strlen(out);
  180. return -1;
  181. }
  182. memcpy(out, crypt_out.data, crypt_out.size);
  183. free(crypt_out.data);
  184. return 0;
  185. }
  186. static int at_get_key(uint8_t* requset_id,
  187. char* uuid,
  188. uint8_t at_command,
  189. char* out,
  190. size_t* out_size) {
  191. char requset_id_string[8 + 1] = {0};
  192. if (*out_size > MSG_SIZE_MAX) {
  193. sprintf(out, "%s", "out size long");
  194. return -1;
  195. }
  196. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  197. memset(out, 0, *out_size);
  198. sprintf(out,
  199. "%s,"
  200. "%s,"
  201. "%d",
  202. requset_id_string, uuid, at_command);
  203. *out_size = strlen(out);
  204. return 0;
  205. }
  206. static int at_get_rand(uint8_t* requset_id,
  207. char* uuid,
  208. uint8_t at_command,
  209. size_t size,
  210. char* out,
  211. size_t* out_size) {
  212. int rc;
  213. char cry_in[10] = {0};
  214. char requset_id_string[8 + 1] = {0};
  215. size_t encrypt_out_size;
  216. if ((requset_id == NULL) | (uuid == NULL) || (out == NULL) ||
  217. (*out_size > MSG_SIZE_MAX)) {
  218. return -1;
  219. }
  220. memset(out, 0, *out_size);
  221. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  222. sprintf(out,
  223. "%s,"
  224. "%s,",
  225. requset_id_string, uuid);
  226. sprintf((char*)cry_in,
  227. "%d,"
  228. "%d",
  229. at_command, (int)size);
  230. rc = at_command_encrypt(cry_in, strlen(cry_in),
  231. out + strlen(requset_id_string) + strlen(uuid) + 2,
  232. &encrypt_out_size);
  233. if (rc < 0) {
  234. *out_size = encrypt_out_size;
  235. return -1;
  236. }
  237. *out_size = strlen(requset_id_string) + strlen(uuid) + 2 + encrypt_out_size;
  238. return 0;
  239. }
  240. static int at_get_device_id(uint8_t* requset_id,
  241. char* uuid,
  242. uint8_t at_command,
  243. char* out,
  244. size_t* out_size) {
  245. int rc;
  246. char cry_in[10] = {0};
  247. char requset_id_string[8 + 1] = {0};
  248. size_t encrypt_out_size;
  249. if ((requset_id == NULL) | (uuid == NULL) || (out == NULL) ||
  250. (*out_size > MSG_SIZE_MAX)) {
  251. return -1;
  252. }
  253. memset(out, 0, *out_size);
  254. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  255. sprintf(out,
  256. "%s,"
  257. "%s,",
  258. requset_id_string, uuid);
  259. sprintf((char*)cry_in, "%d", at_command);
  260. rc = at_command_encrypt(cry_in, strlen(cry_in),
  261. out + strlen(requset_id_string) + strlen(uuid) + 2,
  262. &encrypt_out_size);
  263. if (rc < 0) {
  264. *out_size = encrypt_out_size;
  265. return -1;
  266. }
  267. *out_size = strlen(requset_id_string) + strlen(uuid) + 2 + encrypt_out_size;
  268. return 0;
  269. }
  270. static int at_get_ree_time(uint8_t* requset_id,
  271. char* uuid,
  272. uint8_t at_command,
  273. char* out,
  274. size_t* out_size) {
  275. int rc;
  276. char cry_in[10] = {0};
  277. char requset_id_string[8 + 1] = {0};
  278. size_t encrypt_out_size;
  279. if ((requset_id == NULL) | (uuid == NULL) || (out == NULL) ||
  280. (*out_size > MSG_SIZE_MAX)) {
  281. return -1;
  282. }
  283. memset(out, 0, *out_size);
  284. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  285. sprintf(out,
  286. "%s,"
  287. "%s,",
  288. requset_id_string, uuid);
  289. sprintf((char*)cry_in, "%d", at_command);
  290. rc = at_command_encrypt(cry_in, strlen(cry_in),
  291. out + strlen(requset_id_string) + strlen(uuid) + 2,
  292. &encrypt_out_size);
  293. if (rc < 0) {
  294. *out_size = encrypt_out_size;
  295. return -1;
  296. }
  297. *out_size = strlen(requset_id_string) + strlen(uuid) + 2 + encrypt_out_size;
  298. return 0;
  299. }
  300. static int at_get_tee_time(uint8_t* requset_id,
  301. char* uuid,
  302. uint8_t at_command,
  303. char* out,
  304. size_t* out_size) {
  305. int rc;
  306. char cry_in[10] = {0};
  307. char requset_id_string[8 + 1] = {0};
  308. size_t encrypt_out_size;
  309. if ((requset_id == NULL) | (uuid == NULL) || (out == NULL) ||
  310. (*out_size > MSG_SIZE_MAX)) {
  311. return -1;
  312. }
  313. memset(out, 0, *out_size);
  314. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  315. sprintf(out,
  316. "%s,"
  317. "%s,",
  318. requset_id_string, uuid);
  319. sprintf((char*)cry_in, "%d", at_command);
  320. rc = at_command_encrypt(cry_in, strlen(cry_in),
  321. out + strlen(requset_id_string) + strlen(uuid) + 2,
  322. &encrypt_out_size);
  323. if (rc < 0) {
  324. *out_size = encrypt_out_size;
  325. return -1;
  326. }
  327. *out_size = strlen(requset_id_string) + strlen(uuid) + 2 + encrypt_out_size;
  328. return 0;
  329. }
  330. static int at_storge_write(uint8_t* requset_id,
  331. char* uuid,
  332. uint8_t at_command,
  333. char* name,
  334. size_t size,
  335. size_t offset,
  336. char* data,
  337. char* out,
  338. size_t* out_size) {
  339. int rc;
  340. char cry_in[1024] = {0};
  341. char requset_id_string[8 + 1] = {0};
  342. size_t encrypt_out_size;
  343. if ((requset_id == NULL) | (uuid == NULL) || (out == NULL) ||
  344. (*out_size > MSG_SIZE_MAX)) {
  345. return -1;
  346. }
  347. memset(out, 0, *out_size);
  348. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  349. sprintf(out,
  350. "%s,"
  351. "%s,",
  352. requset_id_string, uuid);
  353. sprintf((char*)cry_in,
  354. "%d,"
  355. "%s,"
  356. "%u,"
  357. "%d,"
  358. "%s",
  359. at_command, name, (unsigned int)size, (unsigned int)offset, data);
  360. rc = at_command_encrypt(cry_in, strlen(cry_in),
  361. out + strlen(requset_id_string) + strlen(uuid) + 2,
  362. &encrypt_out_size);
  363. if (rc < 0) {
  364. *out_size = encrypt_out_size;
  365. return -1;
  366. }
  367. *out_size = strlen(requset_id_string) + strlen(uuid) + 2 + encrypt_out_size;
  368. return 0;
  369. }
  370. static int at_storge_read(uint8_t* requset_id,
  371. char* uuid,
  372. uint8_t at_command,
  373. char* name,
  374. size_t size,
  375. size_t offset,
  376. char* out,
  377. size_t* out_size) {
  378. int rc;
  379. char cry_in[1024] = {0};
  380. char requset_id_string[8 + 1] = {0};
  381. size_t encrypt_out_size;
  382. if ((requset_id == NULL) | (uuid == NULL) || (out == NULL) ||
  383. (*out_size > MSG_SIZE_MAX)) {
  384. return -1;
  385. }
  386. memset(out, 0, *out_size);
  387. bytes_to_hexstring(requset_id, 4, requset_id_string, 9);
  388. sprintf(out,
  389. "%s,"
  390. "%s,",
  391. requset_id_string, uuid);
  392. sprintf((char*)cry_in,
  393. "%d,"
  394. "%s,"
  395. "%u,"
  396. "%d",
  397. at_command, name, (unsigned int)size, (unsigned int)offset);
  398. rc = at_command_encrypt(cry_in, strlen(cry_in),
  399. out + strlen(requset_id_string) + strlen(uuid) + 2,
  400. &encrypt_out_size);
  401. if (rc < 0) {
  402. *out_size = encrypt_out_size;
  403. return -1;
  404. }
  405. *out_size = strlen(requset_id_string) + strlen(uuid) + 2 + encrypt_out_size;
  406. return 0;
  407. }
  408. static int at_command_data(char* in,
  409. size_t in_size,
  410. void* out,
  411. size_t* out_size) {
  412. uint8_t at_command;
  413. char* paragma1 = NULL;
  414. char* paragma2 = NULL;
  415. char* paragma3 = NULL;
  416. size_t data_size = 0;
  417. paragma1 = strstr(in, ",");
  418. NBL_LOGI("at_command string:%s\n", in);
  419. if (!paragma1) {
  420. string_to_dec(in, in_size, &at_command, sizeof(at_command));
  421. } else {
  422. string_to_dec(in, paragma1 - in, &at_command, sizeof(at_command));
  423. }
  424. NBL_LOGI("at_command:%d\n", at_command);
  425. switch (at_command) {
  426. case AT_COMMAND_GET_KEY:
  427. if (paragma1) {
  428. uint8_t* ubase64url_out = NULL;
  429. size_t base64url_size;
  430. at_base64url_decode(paragma1 + 1, in + in_size - (paragma1 + 1),
  431. &ubase64url_out, &base64url_size);
  432. at_crypt_data cek_crypt_in = {
  433. .data = ubase64url_out,
  434. .size = base64url_size,
  435. };
  436. at_crypt_data cek_crypt_out;
  437. int rc = at_aes_hmac_decrypt(&aes_key, &cek_crypt_in,
  438. &cek_crypt_out, &iv_temp);
  439. free(ubase64url_out);
  440. if (rc < 0) {
  441. sprintf(out, "%s", "app aes decrypt error");
  442. return -1;
  443. }
  444. memcpy(&app_cek, cek_crypt_out.data, cek_crypt_out.size);
  445. memcpy(out, cek_crypt_out.data, cek_crypt_out.size);
  446. *out_size = cek_crypt_out.size;
  447. free(cek_crypt_out.data);
  448. }
  449. break;
  450. case AT_COMMAND_GET_RAND:
  451. paragma2 = strstr(paragma1 + 1, ",");
  452. if (paragma2) {
  453. string_to_dec(paragma1 + 1, paragma2 - (paragma1 + 1), &data_size,
  454. sizeof(data_size));
  455. memcpy(out, paragma2 + 1, data_size);
  456. *out_size = data_size;
  457. } else {
  458. return -1;
  459. }
  460. break;
  461. case AT_COMMAND_GET_DEVICE_ID:
  462. paragma2 = strstr(paragma1 + 1, ",");
  463. if (paragma2) {
  464. string_to_dec(paragma1 + 1, paragma2 - (paragma1 + 1), &data_size,
  465. sizeof(data_size));
  466. memcpy(out, paragma2 + 1, data_size);
  467. *out_size = data_size;
  468. } else {
  469. return -1;
  470. }
  471. break;
  472. case AT_COMMAND_GET_REE_TIME:
  473. paragma2 = strstr(paragma1 + 1, ",");
  474. if (paragma2) {
  475. string_to_dec(paragma1 + 1, paragma2 - (paragma1 + 1), &data_size,
  476. sizeof(data_size));
  477. memcpy(out, paragma2 + 1, data_size);
  478. nbl_time_t* ree_time = (nbl_time_t*)out;
  479. *out_size = data_size;
  480. } else {
  481. return -1;
  482. }
  483. break;
  484. case AT_COMMAND_GET_TEE_TIME:
  485. paragma2 = strstr(paragma1 + 1, ",");
  486. if (paragma2) {
  487. string_to_dec(paragma1 + 1, paragma2 - (paragma1 + 1), &data_size,
  488. sizeof(data_size));
  489. memcpy(out, paragma2 + 1, data_size);
  490. nbl_time_t* tee_time = (nbl_time_t*)out;
  491. *out_size = data_size;
  492. } else {
  493. return -1;
  494. }
  495. break;
  496. case AT_COMMAND_STORAGE_WRITE_FILE:
  497. *out_size = 0;
  498. break;
  499. case AT_COMMAND_STORAGE_READ_FILE:
  500. paragma2 = strstr(paragma1 + 1, ",");
  501. if (paragma2) {
  502. string_to_dec(paragma1 + 1, paragma2 - (paragma1 + 1), &data_size,
  503. sizeof(data_size));
  504. paragma3 = strstr(paragma2 + 1, ",");
  505. if (paragma3) {
  506. size_t file_size = 0;
  507. string_to_dec(paragma2 + 1, paragma3 - (paragma2 + 1),
  508. &file_size, sizeof(file_size));
  509. memcpy(out, &data_size, sizeof(data_size));
  510. memcpy(out + sizeof(data_size), &file_size, sizeof(file_size));
  511. memcpy(out + sizeof(data_size) + sizeof(file_size),
  512. paragma3 + 1, data_size);
  513. *out_size = sizeof(data_size) + sizeof(file_size) + data_size;
  514. } else {
  515. return -1;
  516. }
  517. } else {
  518. return -1;
  519. }
  520. break;
  521. default:
  522. sprintf(out, "%s", "no find at command");
  523. *out_size = strlen(out);
  524. return -1;
  525. break;
  526. }
  527. return 0;
  528. }
  529. static int at_data_process(char* recv_msg,
  530. size_t msg_size,
  531. void* out,
  532. size_t* out_size) {
  533. int rc;
  534. char* paragma1 = NULL;
  535. char* paragma2 = NULL;
  536. char* paragma3 = NULL;
  537. char res_request_id_string[8 + 1] = {0};
  538. uint8_t request_id[4];
  539. char requset_id_string[8 + 1] = {0};
  540. struct key cek;
  541. int8_t res;
  542. size_t paragma3_size;
  543. uint8_t* ubase64url_out = NULL;
  544. size_t base64url_size;
  545. paragma1 = strstr(recv_msg, ",");
  546. paragma2 = strstr(paragma1 + 1, ",");
  547. memset(out, 0, *out_size);
  548. if ((!paragma1) || (!paragma2)) {
  549. sprintf(out, "%s", "app recv paragma error");
  550. return -1;
  551. }
  552. memcpy(res_request_id_string, recv_msg, paragma1 - recv_msg);
  553. get_requset_id(request_id, sizeof(request_id));
  554. bytes_to_hexstring(request_id, 4, requset_id_string, 9);
  555. string_to_dec(paragma1 + 1, paragma2 - (paragma1 + 1), &res, sizeof(res));
  556. if (strcmp(requset_id_string, res_request_id_string) == 0) {
  557. if (res == 0) {
  558. paragma3 = strstr(paragma2 + 1, ",");
  559. if (paragma3) {
  560. paragma3_size = paragma3 - (paragma2 + 1);
  561. } else {
  562. paragma3_size = (char*)recv_msg + msg_size - (paragma2 + 1);
  563. }
  564. if (paragma3_size > 1) {
  565. at_base64url_decode(paragma2 + 1,
  566. recv_msg + msg_size - (paragma2 + 1),
  567. &ubase64url_out, &base64url_size);
  568. at_crypt_data crypt_in = {
  569. .data = ubase64url_out,
  570. .size = base64url_size,
  571. };
  572. at_crypt_data crypt_out;
  573. get_cek(cek.byte);
  574. rc = at_aes_hmac_decrypt(&cek, &crypt_in, &crypt_out, &iv_temp);
  575. free(ubase64url_out);
  576. if (rc < 0) {
  577. sprintf(out, "%s", "app aes decrypt error");
  578. *out_size = strlen(out);
  579. return -1;
  580. }
  581. rc = at_command_data(crypt_out.data, crypt_out.size, out,
  582. out_size);
  583. if (rc < 0) {
  584. return -1;
  585. }
  586. free(crypt_out.data);
  587. } else {
  588. rc = at_command_data(paragma2 + 1,
  589. recv_msg + msg_size - (paragma2 + 1), out,
  590. out_size);
  591. if (rc < 0) {
  592. return -1;
  593. }
  594. }
  595. } else {
  596. memcpy(out, paragma2 + 1, recv_msg + msg_size - (paragma2 + 1));
  597. *out_size = strlen(out);
  598. return -1;
  599. }
  600. } else {
  601. sprintf(out, "%s", "tee requset id error");
  602. return -1;
  603. }
  604. return 0;
  605. }
  606. int app_at_msg(uint8_t at_command,
  607. void* in,
  608. size_t in_size,
  609. void* out,
  610. size_t* out_size) {
  611. char uuid[40];
  612. uint8_t request_id[4];
  613. int rc = -1;
  614. if (*out_size > MSG_SIZE_MAX) {
  615. return -1;
  616. }
  617. memset(in, 0, in_size);
  618. memset(out, 0, *out_size);
  619. get_uuid(uuid, sizeof(uuid));
  620. get_requset_id(request_id, sizeof(request_id));
  621. switch (at_command) {
  622. case AT_COMMAND_GET_KEY:
  623. rc = at_get_key(request_id, uuid, AT_COMMAND_GET_KEY, in, &in_size);
  624. break;
  625. case AT_COMMAND_GET_RAND:
  626. rc = at_get_rand(request_id, uuid, AT_COMMAND_GET_RAND, 32, in,
  627. &in_size);
  628. break;
  629. case AT_COMMAND_GET_DEVICE_ID:
  630. rc = at_get_device_id(request_id, uuid, AT_COMMAND_GET_DEVICE_ID, in,
  631. &in_size);
  632. break;
  633. case AT_COMMAND_GET_REE_TIME:
  634. rc = at_get_ree_time(request_id, uuid, AT_COMMAND_GET_REE_TIME, in,
  635. &in_size);
  636. break;
  637. case AT_COMMAND_GET_TEE_TIME:
  638. rc = at_get_tee_time(request_id, uuid, AT_COMMAND_GET_TEE_TIME, in,
  639. &in_size);
  640. break;
  641. case AT_COMMAND_STORAGE_WRITE_FILE:
  642. rc = at_storge_write(request_id, uuid, AT_COMMAND_STORAGE_WRITE_FILE,
  643. "test", strlen("nihao"), 0, "nihao", in, &in_size);
  644. break;
  645. case AT_COMMAND_STORAGE_READ_FILE:
  646. rc = at_storge_read(request_id, uuid, AT_COMMAND_STORAGE_READ_FILE,
  647. "test", strlen("nihao"), 0, in, &in_size);
  648. break;
  649. default:
  650. sprintf(out, "%s", "app no find at command");
  651. *out_size = strlen(out);
  652. return -1;
  653. break;
  654. }
  655. if (rc < 0) {
  656. return rc;
  657. }
  658. NBL_LOGI("data size: %zu,in data:%s", in_size, in);
  659. char msg_recv[1024];
  660. size_t mgs_recv_size = 1024;
  661. memset(msg_recv, 0, mgs_recv_size);
  662. rc = at_send_recv(in, in_size, msg_recv, &mgs_recv_size);
  663. if (rc < 0) {
  664. sprintf(out, "%s", "at send recv falied");
  665. *out_size = strlen(out);
  666. return rc;
  667. }
  668. rc = at_data_process(msg_recv, mgs_recv_size, out, out_size);
  669. if (rc < 0) {
  670. return rc;
  671. }
  672. return rc;
  673. }
  674. char out[1024];
  675. char in[1024];
  676. int main(void) {
  677. int rc;
  678. int i;
  679. size_t in_size;
  680. size_t out_size;
  681. in_size = 1024;
  682. out_size = 1024;
  683. while (1) {
  684. rc = app_at_msg(AT_COMMAND_GET_KEY, in, in_size, out, &out_size);
  685. if (rc < 0) {
  686. NBL_LOGI("out:%s\n", out);
  687. } else {
  688. for (i = 0; i < out_size; i++) {
  689. NBL_LOGI("key:0x%02x\n", out[i]);
  690. }
  691. }
  692. rc = app_at_msg(AT_COMMAND_GET_RAND, in, in_size, out, &out_size);
  693. if (rc < 0) {
  694. NBL_LOGI("out:%s\n", out);
  695. } else {
  696. for (i = 0; i < out_size; i++) {
  697. NBL_LOGI("rand:%x\n", out[i]);
  698. }
  699. }
  700. rc = app_at_msg(AT_COMMAND_GET_RAND, in, in_size, out, &out_size);
  701. if (rc < 0) {
  702. NBL_LOGI("out:%s\n", out);
  703. } else {
  704. for (i = 0; i < out_size; i++) {
  705. NBL_LOGI("rand:%x\n", out[i]);
  706. }
  707. }
  708. rc = app_at_msg(AT_COMMAND_GET_DEVICE_ID, in, in_size, out, &out_size);
  709. if (rc < 0) {
  710. NBL_LOGI("out:%s\n", out);
  711. } else {
  712. for (i = 0; i < out_size; i++) {
  713. NBL_LOGI("device id:0x%02x\n", out[i]);
  714. }
  715. }
  716. rc = app_at_msg(AT_COMMAND_GET_REE_TIME, in, in_size, out, &out_size);
  717. if (rc < 0) {
  718. NBL_LOGI("out:%s\n", out);
  719. } else {
  720. nbl_time_t* ree_time = (nbl_time_t*)out;
  721. NBL_LOGI(" ree time: %d.%d\n", ree_time->seconds, ree_time->millis);
  722. }
  723. rc = app_at_msg(AT_COMMAND_GET_TEE_TIME, in, in_size, out, &out_size);
  724. if (rc < 0) {
  725. NBL_LOGI("out:%s\n", out);
  726. } else {
  727. nbl_time_t* tee_time = (nbl_time_t*)out;
  728. NBL_LOGI(" tee time: %d.%d\n", tee_time->seconds, tee_time->millis);
  729. }
  730. rc = app_at_msg(AT_COMMAND_STORAGE_WRITE_FILE, in, in_size, out,
  731. &out_size);
  732. if (rc < 0) {
  733. NBL_LOGI("out:%s\n", out);
  734. } else {
  735. NBL_LOGI("out:%s\n", out);
  736. }
  737. rc = app_at_msg(AT_COMMAND_STORAGE_READ_FILE, in, in_size, out,
  738. &out_size);
  739. if (rc < 0) {
  740. NBL_LOGI("%s\n", out);
  741. } else {
  742. size_t read_size = 0;
  743. size_t file_size = 0;
  744. memcpy(&read_size, out, sizeof(read_size));
  745. memcpy(&file_size, out, sizeof(file_size));
  746. NBL_LOGI("out size:%zu,read size:%zu,filesize:%zu,out:%s\n ",
  747. out_size, read_size, file_size,
  748. out + sizeof(read_size) + sizeof(file_size));
  749. }
  750. for (int i = 0; i < 5000; i++) {
  751. for (int j = 0; j < 65535; j++) {
  752. }
  753. }
  754. }
  755. return 0;
  756. }