test_protocomm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include <esp_err.h>
  10. #include <esp_log.h>
  11. #include <esp_system.h>
  12. #include <sys/random.h>
  13. #include <unistd.h>
  14. #include <unity.h>
  15. /* ToDo - Remove this once appropriate solution is available.
  16. We need to define this for the file as ssl_misc.h uses private structures from mbedtls,
  17. which are undefined if the following flag is not defined */
  18. /* Many APIs in the file make use of this flag instead of `MBEDTLS_PRIVATE()` */
  19. /* ToDo - Replace them with proper getter-setter once they are added */
  20. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  21. #include <mbedtls/aes.h>
  22. #include <mbedtls/sha256.h>
  23. #include <mbedtls/entropy.h>
  24. #include <mbedtls/ctr_drbg.h>
  25. #include <mbedtls/ecdh.h>
  26. #include <mbedtls/error.h>
  27. #include <protocomm.h>
  28. #include <protocomm_security.h>
  29. #include <protocomm_security0.h>
  30. #include <protocomm_security1.h>
  31. #include "test_utils.h"
  32. #include "session.pb-c.h"
  33. #ifdef CONFIG_HEAP_TRACING
  34. #include <esp_heap_trace.h>
  35. #define NUM_RECORDS 100
  36. static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
  37. #endif
  38. #define PUBLIC_KEY_LEN 32
  39. #define SZ_RANDOM 16
  40. typedef struct {
  41. uint32_t id;
  42. uint8_t sec_ver;
  43. uint8_t weak;
  44. const protocomm_security1_params_t *pop;
  45. uint8_t device_pubkey[PUBLIC_KEY_LEN];
  46. uint8_t client_pubkey[PUBLIC_KEY_LEN];
  47. uint8_t sym_key[PUBLIC_KEY_LEN];
  48. uint8_t rand[SZ_RANDOM];
  49. /* mbedtls context data for Curve25519 */
  50. mbedtls_ecdh_context ctx_client;
  51. mbedtls_entropy_context entropy;
  52. mbedtls_ctr_drbg_context ctr_drbg;
  53. /* mbedtls context data for AES */
  54. mbedtls_aes_context ctx_aes;
  55. unsigned char stb[16];
  56. size_t nc_off;
  57. } session_t;
  58. static const char *TAG = "protocomm_test";
  59. static protocomm_t *test_pc = NULL;
  60. static const protocomm_security_t *test_sec = NULL;
  61. protocomm_security_handle_t sec_inst = NULL;
  62. static uint32_t test_priv_data = 1234;
  63. static void flip_endian(uint8_t *data, size_t len)
  64. {
  65. uint8_t swp_buf;
  66. for (int i = 0; i < len/2; i++) {
  67. swp_buf = data[i];
  68. data[i] = data[len - i - 1];
  69. data[len - i - 1] = swp_buf;
  70. }
  71. }
  72. static void hexdump(const char *msg, uint8_t *buf, int len)
  73. {
  74. ESP_LOGI(TAG, "%s:", msg);
  75. ESP_LOG_BUFFER_HEX(TAG, buf, len);
  76. }
  77. static esp_err_t prepare_command0(session_t *session, SessionData *req)
  78. {
  79. Sec1Payload *in = (Sec1Payload *) malloc(sizeof(Sec1Payload));
  80. if (in == NULL) {
  81. ESP_LOGE(TAG, "Error allocating memory for request");
  82. return ESP_ERR_NO_MEM;
  83. }
  84. SessionCmd0 *in_req = (SessionCmd0 *) malloc(sizeof(SessionCmd0));
  85. if (in_req == NULL) {
  86. ESP_LOGE(TAG, "Error allocating memory for request");
  87. free(in);
  88. return ESP_ERR_NO_MEM;
  89. }
  90. sec1_payload__init(in);
  91. session_cmd0__init(in_req);
  92. in_req->client_pubkey.data = session->client_pubkey;
  93. in_req->client_pubkey.len = PUBLIC_KEY_LEN;
  94. in->msg = SEC1_MSG_TYPE__Session_Command0;
  95. in->payload_case = SEC1_PAYLOAD__PAYLOAD_SC0;
  96. in->sc0 = in_req;
  97. req->proto_case = SESSION_DATA__PROTO_SEC1;
  98. req->sec_ver = protocomm_security1.ver;
  99. req->sec1 = in;
  100. return ESP_OK;
  101. }
  102. static void cleanup_command0(SessionData *req)
  103. {
  104. free(req->sec1->sc0);
  105. free(req->sec1);
  106. }
  107. static esp_err_t verify_response0(session_t *session, SessionData *resp)
  108. {
  109. if ((resp->proto_case != SESSION_DATA__PROTO_SEC1) ||
  110. (resp->sec1->msg != SEC1_MSG_TYPE__Session_Response0)) {
  111. ESP_LOGE(TAG, "Invalid response type");
  112. return ESP_ERR_INVALID_ARG;
  113. }
  114. int ret;
  115. Sec1Payload *in = (Sec1Payload *) resp->sec1;
  116. if (in->sr0->device_pubkey.len != PUBLIC_KEY_LEN) {
  117. ESP_LOGE(TAG, "Device public key length as not as expected");
  118. return ESP_FAIL;
  119. }
  120. if (in->sr0->device_random.len != SZ_RANDOM) {
  121. ESP_LOGE(TAG, "Device random data length is not as expected");
  122. return ESP_FAIL;
  123. }
  124. uint8_t *cli_pubkey = session->client_pubkey;
  125. uint8_t *dev_pubkey = session->device_pubkey;
  126. memcpy(session->device_pubkey, in->sr0->device_pubkey.data, in->sr0->device_pubkey.len);
  127. hexdump("Device pubkey", dev_pubkey, PUBLIC_KEY_LEN);
  128. hexdump("Client pubkey", cli_pubkey, PUBLIC_KEY_LEN);
  129. ret = mbedtls_mpi_lset(&session->ctx_client.ctx.mbed_ecdh.Qp.Z, 1);
  130. if (ret != 0) {
  131. ESP_LOGE(TAG, "Failed at mbedtls_mpi_lset with error code : %d", ret);
  132. return ESP_FAIL;
  133. }
  134. flip_endian(session->device_pubkey, PUBLIC_KEY_LEN);
  135. ret = mbedtls_mpi_read_binary(&session->ctx_client.ctx.mbed_ecdh.Qp.X, dev_pubkey, PUBLIC_KEY_LEN);
  136. flip_endian(session->device_pubkey, PUBLIC_KEY_LEN);
  137. if (ret != 0) {
  138. ESP_LOGE(TAG, "Failed at mbedtls_mpi_read_binary with error code : %d", ret);
  139. return ESP_FAIL;
  140. }
  141. ret = mbedtls_ecdh_compute_shared(&session->ctx_client.ctx.mbed_ecdh.grp,
  142. &session->ctx_client.ctx.mbed_ecdh.z,
  143. &session->ctx_client.ctx.mbed_ecdh.Qp,
  144. &session->ctx_client.ctx.mbed_ecdh.d,
  145. mbedtls_ctr_drbg_random,
  146. &session->ctr_drbg);
  147. if (ret != 0) {
  148. ESP_LOGE(TAG, "Failed at mbedtls_ecdh_compute_shared with error code : %d", ret);
  149. return ESP_FAIL;
  150. }
  151. ret = mbedtls_mpi_write_binary(&session->ctx_client.ctx.mbed_ecdh.z, session->sym_key, PUBLIC_KEY_LEN);
  152. if (ret != 0) {
  153. ESP_LOGE(TAG, "Failed at mbedtls_mpi_write_binary with error code : %d", ret);
  154. return ESP_FAIL;
  155. }
  156. flip_endian(session->sym_key, PUBLIC_KEY_LEN);
  157. const protocomm_security1_params_t *pop = session->pop;
  158. if (pop != NULL && pop->data != NULL && pop->len != 0) {
  159. ESP_LOGD(TAG, "Adding proof of possession");
  160. uint8_t sha_out[PUBLIC_KEY_LEN];
  161. ret = mbedtls_sha256((const unsigned char *) pop->data, pop->len, sha_out, 0);
  162. if (ret != 0) {
  163. ESP_LOGE(TAG, "Failed at mbedtls_sha256_ret with error code : %d", ret);
  164. return ESP_FAIL;
  165. }
  166. for (int i = 0; i < PUBLIC_KEY_LEN; i++) {
  167. session->sym_key[i] ^= sha_out[i];
  168. }
  169. }
  170. hexdump("Shared key", session->sym_key, PUBLIC_KEY_LEN);
  171. memcpy(session->rand, in->sr0->device_random.data, in->sr0->device_random.len);
  172. hexdump("Dev random", session->rand, sizeof(session->rand));
  173. return ESP_OK;
  174. }
  175. static esp_err_t prepare_command1(session_t *session, SessionData *req)
  176. {
  177. int ret;
  178. uint8_t *outbuf = (uint8_t *) malloc(PUBLIC_KEY_LEN);
  179. if (!outbuf) {
  180. ESP_LOGE(TAG, "Error allocating ciphertext buffer");
  181. return ESP_ERR_NO_MEM;
  182. }
  183. /* Initialise crypto context */
  184. mbedtls_aes_init(&session->ctx_aes);
  185. memset(session->stb, 0, sizeof(session->stb));
  186. session->nc_off = 0;
  187. ret = mbedtls_aes_setkey_enc(&session->ctx_aes, session->sym_key,
  188. sizeof(session->sym_key)*8);
  189. if (ret != 0) {
  190. ESP_LOGE(TAG, "Failed at mbedtls_aes_setkey_enc with erro code : %d", ret);
  191. free(outbuf);
  192. return ESP_FAIL;
  193. }
  194. ret = mbedtls_aes_crypt_ctr(&session->ctx_aes, PUBLIC_KEY_LEN,
  195. &session->nc_off, session->rand,
  196. session->stb, session->device_pubkey, outbuf);
  197. if (ret != 0) {
  198. ESP_LOGE(TAG, "Failed at mbedtls_aes_crypt_ctr with erro code : %d", ret);
  199. free(outbuf);
  200. return ESP_FAIL;
  201. }
  202. Sec1Payload *out = (Sec1Payload *) malloc(sizeof(Sec1Payload));
  203. if (!out) {
  204. ESP_LOGE(TAG, "Error allocating out buffer");
  205. free(outbuf);
  206. return ESP_ERR_NO_MEM;
  207. }
  208. sec1_payload__init(out);
  209. SessionCmd1 *out_req = (SessionCmd1 *) malloc(sizeof(SessionCmd1));
  210. if (!out_req) {
  211. ESP_LOGE(TAG, "Error allocating out_req buffer");
  212. free(outbuf);
  213. free(out);
  214. return ESP_ERR_NO_MEM;
  215. }
  216. session_cmd1__init(out_req);
  217. out_req->client_verify_data.data = outbuf;
  218. out_req->client_verify_data.len = PUBLIC_KEY_LEN;
  219. hexdump("Client verify data", outbuf, PUBLIC_KEY_LEN);
  220. out->msg = SEC1_MSG_TYPE__Session_Command1;
  221. out->payload_case = SEC1_PAYLOAD__PAYLOAD_SC1;
  222. out->sc1 = out_req;
  223. req->proto_case = SESSION_DATA__PROTO_SEC1;
  224. req->sec_ver = protocomm_security1.ver;
  225. req->sec1 = out;
  226. return ESP_OK;
  227. }
  228. static void cleanup_command1(SessionData *req)
  229. {
  230. free(req->sec1->sc1->client_verify_data.data);
  231. free(req->sec1->sc1);
  232. free(req->sec1);
  233. }
  234. static esp_err_t verify_response1(session_t *session, SessionData *resp)
  235. {
  236. uint8_t *cli_pubkey = session->client_pubkey;
  237. uint8_t *dev_pubkey = session->device_pubkey;
  238. hexdump("Device pubkey", dev_pubkey, PUBLIC_KEY_LEN);
  239. hexdump("Client pubkey", cli_pubkey, PUBLIC_KEY_LEN);
  240. if ((resp->proto_case != SESSION_DATA__PROTO_SEC1) ||
  241. (resp->sec1->msg != SEC1_MSG_TYPE__Session_Response1)) {
  242. ESP_LOGE(TAG, "Invalid response type");
  243. return ESP_ERR_INVALID_ARG;
  244. }
  245. uint8_t check_buf[PUBLIC_KEY_LEN];
  246. Sec1Payload *in = (Sec1Payload *) resp->sec1;
  247. int ret = mbedtls_aes_crypt_ctr(&session->ctx_aes, PUBLIC_KEY_LEN,
  248. &session->nc_off, session->rand, session->stb,
  249. in->sr1->device_verify_data.data, check_buf);
  250. if (ret != 0) {
  251. ESP_LOGE(TAG, "Failed at mbedtls_aes_crypt_ctr with erro code : %d", ret);
  252. return ESP_FAIL;
  253. }
  254. hexdump("Dec Device verifier", check_buf, sizeof(check_buf));
  255. if (memcmp(check_buf, session->client_pubkey, sizeof(session->client_pubkey)) != 0) {
  256. ESP_LOGE(TAG, "Key mismatch. Close connection");
  257. return ESP_FAIL;
  258. }
  259. return ESP_OK;
  260. }
  261. static esp_err_t test_new_session(session_t *session)
  262. {
  263. if (session->sec_ver == 0) {
  264. return ESP_OK;
  265. }
  266. if (!test_sec) {
  267. return ESP_ERR_INVALID_STATE;
  268. }
  269. if (test_sec->init && (test_sec->init(&sec_inst) != ESP_OK)) {
  270. return ESP_ERR_NO_MEM;
  271. }
  272. uint32_t session_id = session->id;
  273. if (test_sec->new_transport_session &&
  274. (test_sec->new_transport_session(sec_inst, session_id) != ESP_OK)) {
  275. ESP_LOGE(TAG, "Failed to launch new transport session");
  276. return ESP_FAIL;
  277. }
  278. if (protocomm_open_session(test_pc, session_id) != ESP_OK) {
  279. ESP_LOGE(TAG, "Failed to open new protocomm session");
  280. return ESP_FAIL;
  281. }
  282. return ESP_OK;
  283. }
  284. static esp_err_t test_delete_session(session_t *session)
  285. {
  286. if (!test_sec) {
  287. return ESP_ERR_INVALID_STATE;
  288. }
  289. if (test_sec->cleanup && (test_sec->cleanup(sec_inst) != ESP_OK)) {
  290. return ESP_FAIL;
  291. }
  292. return ESP_OK;
  293. }
  294. static esp_err_t test_sec_endpoint(session_t *session)
  295. {
  296. if (session->sec_ver == 0) {
  297. return ESP_OK;
  298. }
  299. uint32_t session_id = session->id;
  300. int ret = ESP_FAIL;
  301. SessionData req;
  302. SessionData *resp;
  303. ssize_t inlen = 0;
  304. uint8_t *inbuf = NULL;
  305. ssize_t outlen = 0;
  306. uint8_t *outbuf = NULL;
  307. mbedtls_ecdh_init(&session->ctx_client);
  308. mbedtls_ecdh_setup(&session->ctx_client, MBEDTLS_ECP_DP_CURVE25519);
  309. mbedtls_ctr_drbg_init(&session->ctr_drbg);
  310. mbedtls_entropy_init(&session->entropy);
  311. ret = mbedtls_ctr_drbg_seed(&session->ctr_drbg, mbedtls_entropy_func,
  312. &session->entropy, NULL, 0);
  313. if (ret != 0) {
  314. ESP_LOGE(TAG, "Failed at mbedtls_ctr_drbg_seed with error code : %d", ret);
  315. goto abort_test_sec_endpoint;
  316. }
  317. ret = mbedtls_ecp_group_load(&session->ctx_client.ctx.mbed_ecdh.grp, MBEDTLS_ECP_DP_CURVE25519);
  318. if (ret != 0) {
  319. ESP_LOGE(TAG, "Failed at mbedtls_ecp_group_load with error code : %d", ret);
  320. goto abort_test_sec_endpoint;
  321. }
  322. ret = mbedtls_ecdh_gen_public(&session->ctx_client.ctx.mbed_ecdh.grp,
  323. &session->ctx_client.ctx.mbed_ecdh.d,
  324. &session->ctx_client.ctx.mbed_ecdh.Q,
  325. mbedtls_ctr_drbg_random,
  326. &session->ctr_drbg);
  327. if (ret != 0) {
  328. ESP_LOGE(TAG, "Failed at mbedtls_ecdh_gen_public with error code : %d", ret);
  329. goto abort_test_sec_endpoint;
  330. }
  331. if (session->weak) {
  332. /* Read zero client public key */
  333. ret = mbedtls_mpi_read_binary(&session->ctx_client.ctx.mbed_ecdh.Q.X,
  334. session->client_pubkey,
  335. PUBLIC_KEY_LEN);
  336. if (ret != 0) {
  337. ESP_LOGE(TAG, "Failed at mbedtls_mpi_read_binary with error code : %d", ret);
  338. goto abort_test_sec_endpoint;
  339. }
  340. }
  341. ret = mbedtls_mpi_write_binary(&session->ctx_client.ctx.mbed_ecdh.Q.X,
  342. session->client_pubkey,
  343. PUBLIC_KEY_LEN);
  344. if (ret != 0) {
  345. ESP_LOGE(TAG, "Failed at mbedtls_mpi_write_binary with error code : %d", ret);
  346. goto abort_test_sec_endpoint;
  347. }
  348. flip_endian(session->client_pubkey, PUBLIC_KEY_LEN);
  349. /*********** Transaction0 = SessionCmd0 + SessionResp0 ****************/
  350. session_data__init(&req);
  351. if (prepare_command0(session, &req) != ESP_OK) {
  352. ESP_LOGE(TAG, "Failed in prepare_command0");
  353. goto abort_test_sec_endpoint;
  354. }
  355. inlen = session_data__get_packed_size(&req);
  356. inbuf = (uint8_t *) malloc(inlen);
  357. if (!inbuf) {
  358. ESP_LOGE(TAG, "Failed to allocate inbuf");
  359. goto abort_test_sec_endpoint;
  360. }
  361. session_data__pack(&req, inbuf);
  362. cleanup_command0(&req);
  363. outlen = 0;
  364. outbuf = NULL;
  365. ret = protocomm_req_handle(test_pc, "test-sec", session_id,
  366. inbuf, inlen, &outbuf, &outlen);
  367. free(inbuf);
  368. if (ret != ESP_OK) {
  369. ESP_LOGE(TAG, "test-sec handler failed");
  370. free(outbuf);
  371. goto abort_test_sec_endpoint;
  372. }
  373. resp = session_data__unpack(NULL, outlen, outbuf);
  374. free(outbuf);
  375. if (!resp) {
  376. ESP_LOGE(TAG, "Unable to unpack SessionResp0");
  377. goto abort_test_sec_endpoint;
  378. }
  379. if (verify_response0(session, resp) != ESP_OK) {
  380. ESP_LOGE(TAG, "Invalid response 0");
  381. session_data__free_unpacked(resp, NULL);
  382. goto abort_test_sec_endpoint;
  383. }
  384. session_data__free_unpacked(resp, NULL);
  385. /*********** Transaction1 = SessionCmd1 + SessionResp1 ****************/
  386. session_data__init(&req);
  387. if (prepare_command1(session, &req) != ESP_OK) {
  388. ESP_LOGE(TAG, "Failed in prepare_command1");
  389. goto abort_test_sec_endpoint;
  390. }
  391. inlen = session_data__get_packed_size(&req);
  392. inbuf = (uint8_t *) malloc(inlen);
  393. if (!inbuf) {
  394. ESP_LOGE(TAG, "Failed to allocate inbuf");
  395. goto abort_test_sec_endpoint;
  396. }
  397. session_data__pack(&req, inbuf);
  398. cleanup_command1(&req);
  399. outlen = 0;
  400. outbuf = NULL;
  401. ret = protocomm_req_handle(test_pc, "test-sec", session_id,
  402. inbuf, inlen, &outbuf, &outlen);
  403. free(inbuf);
  404. if (ret != ESP_OK) {
  405. ESP_LOGE(TAG, "test-sec handler failed");
  406. free(outbuf);
  407. goto abort_test_sec_endpoint;
  408. }
  409. resp = session_data__unpack(NULL, outlen, outbuf);
  410. free(outbuf);
  411. if (!resp) {
  412. ESP_LOGE(TAG, "Unable to unpack SessionResp0");
  413. goto abort_test_sec_endpoint;
  414. }
  415. if (verify_response1(session, resp) != ESP_OK) {
  416. ESP_LOGE(TAG, "Invalid response 1");
  417. session_data__free_unpacked(resp, NULL);
  418. goto abort_test_sec_endpoint;
  419. }
  420. session_data__free_unpacked(resp, NULL);
  421. mbedtls_ecdh_free(&session->ctx_client);
  422. mbedtls_ctr_drbg_free(&session->ctr_drbg);
  423. mbedtls_entropy_free(&session->entropy);
  424. return ESP_OK;
  425. abort_test_sec_endpoint:
  426. mbedtls_ecdh_free(&session->ctx_client);
  427. mbedtls_ctr_drbg_free(&session->ctr_drbg);
  428. mbedtls_entropy_free(&session->entropy);
  429. return ESP_FAIL;
  430. }
  431. #define TEST_VER_STR "<some version string>"
  432. static esp_err_t test_ver_endpoint(session_t *session)
  433. {
  434. ssize_t ver_data_len = 0;
  435. uint8_t *ver_data = NULL;
  436. esp_err_t ret = protocomm_req_handle(test_pc, "test-ver", session->id,
  437. NULL, 0, &ver_data, &ver_data_len);
  438. if (ret != ESP_OK) {
  439. ESP_LOGE(TAG, "test-ver handler failed");
  440. return ESP_FAIL;
  441. }
  442. if (ver_data_len != strlen(TEST_VER_STR) || memcmp(TEST_VER_STR, ver_data, ver_data_len)) {
  443. ESP_LOGE(TAG, "incorrect response data from test-ver");
  444. free(ver_data);
  445. return ESP_FAIL;
  446. }
  447. free(ver_data);
  448. return ESP_OK;
  449. }
  450. static esp_err_t test_req_endpoint(session_t *session)
  451. {
  452. uint32_t session_id = session->id;
  453. uint8_t rand_test_data[512], enc_test_data[512];
  454. getrandom(rand_test_data, sizeof(rand_test_data), 0);
  455. if (session->sec_ver == 0) {
  456. memcpy(enc_test_data, rand_test_data, sizeof(rand_test_data));
  457. }
  458. else if (session->sec_ver == 1) {
  459. #if !CONFIG_MBEDTLS_HARDWARE_AES
  460. // Check if the AES key is correctly set before calling the software encryption
  461. // API. Without this check, the code will crash, resulting in a test case failure.
  462. // For hardware AES, portability layer takes care of this.
  463. if (session->ctx_aes.rk != NULL && session->ctx_aes.nr > 0) {
  464. #endif
  465. mbedtls_aes_crypt_ctr(&session->ctx_aes, sizeof(rand_test_data), &session->nc_off,
  466. session->rand, session->stb, rand_test_data, enc_test_data);
  467. #if !CONFIG_MBEDTLS_HARDWARE_AES
  468. }
  469. #endif
  470. }
  471. ssize_t verify_data_len = 0;
  472. uint8_t *enc_verify_data = NULL;
  473. esp_err_t ret = protocomm_req_handle(test_pc, "test-ep", session_id,
  474. enc_test_data, sizeof(enc_test_data),
  475. &enc_verify_data, &verify_data_len);
  476. if (ret != ESP_OK || !verify_data_len) {
  477. ESP_LOGE(TAG, "test-ep handler failed");
  478. return ESP_FAIL;
  479. }
  480. uint8_t *verify_data = malloc(verify_data_len);
  481. if (!verify_data) {
  482. ESP_LOGE(TAG, "error allocating memory for decrypted data");
  483. free(enc_verify_data);
  484. return ESP_FAIL;
  485. }
  486. if (session->sec_ver == 0) {
  487. memcpy(verify_data, enc_verify_data, verify_data_len);
  488. }
  489. else if (session->sec_ver == 1) {
  490. mbedtls_aes_crypt_ctr(&session->ctx_aes, verify_data_len, &session->nc_off,
  491. session->rand, session->stb, enc_verify_data, verify_data);
  492. }
  493. free(enc_verify_data);
  494. hexdump("Sent data", rand_test_data, sizeof(rand_test_data));
  495. hexdump("Recv data", verify_data, verify_data_len);
  496. ESP_LOGI(TAG, "verify data len : %d", verify_data_len);
  497. ESP_LOGI(TAG, "expected data len : %d", sizeof(rand_test_data));
  498. if (verify_data_len != sizeof(rand_test_data)) {
  499. ESP_LOGE(TAG, "incorrect response length from test-ep");
  500. free(verify_data);
  501. return ESP_FAIL;
  502. }
  503. if (memcmp(rand_test_data, verify_data, verify_data_len)) {
  504. ESP_LOGE(TAG, "incorrect response data from test-ep");
  505. free(verify_data);
  506. return ESP_FAIL;
  507. }
  508. free(verify_data);
  509. return ESP_OK;
  510. }
  511. esp_err_t test_req_handler (uint32_t session_id,
  512. const uint8_t *inbuf, ssize_t inlen,
  513. uint8_t **outbuf, ssize_t *outlen,
  514. void *priv_data)
  515. {
  516. *outbuf = malloc(inlen);
  517. if (*outbuf) {
  518. *outlen = inlen;
  519. memcpy(*outbuf, inbuf, inlen);
  520. } else {
  521. ESP_LOGE(TAG, "Error allocating response outbuf");
  522. *outbuf = NULL;
  523. *outlen = 0;
  524. }
  525. uint32_t *priv = (uint32_t *) priv_data;
  526. if ((&test_priv_data != priv) || (test_priv_data != *priv)) {
  527. ESP_LOGE(TAG, "Handler private data doesn't match");
  528. return ESP_FAIL;
  529. }
  530. return ESP_OK;
  531. }
  532. static esp_err_t start_test_service(uint8_t sec_ver, const protocomm_security1_params_t *pop)
  533. {
  534. test_pc = protocomm_new();
  535. if (test_pc == NULL) {
  536. ESP_LOGE(TAG, "Failed to create new protocomm instance");
  537. return ESP_FAIL;
  538. }
  539. if (sec_ver == 0) {
  540. if (protocomm_set_security(test_pc, "test-sec", &protocomm_security0, NULL) != ESP_OK) {
  541. ESP_LOGE(TAG, "Failed to set Security0");
  542. return ESP_FAIL;
  543. }
  544. test_sec = &protocomm_security0;
  545. } else if (sec_ver == 1) {
  546. if (protocomm_set_security(test_pc, "test-sec", &protocomm_security1, pop) != ESP_OK) {
  547. ESP_LOGE(TAG, "Failed to set Security1");
  548. return ESP_FAIL;
  549. }
  550. test_sec = &protocomm_security1;
  551. }
  552. if (protocomm_set_version(test_pc, "test-ver", TEST_VER_STR) != ESP_OK) {
  553. ESP_LOGE(TAG, "Failed to set version");
  554. return ESP_FAIL;
  555. }
  556. if (protocomm_add_endpoint(test_pc, "test-ep",
  557. test_req_handler,
  558. (void *) &test_priv_data) != ESP_OK) {
  559. ESP_LOGE(TAG, "Failed to set test-ep endpoint handler");
  560. return ESP_FAIL;
  561. }
  562. return ESP_OK;
  563. }
  564. static void stop_test_service(void)
  565. {
  566. test_sec = NULL;
  567. protocomm_delete(test_pc);
  568. test_pc = NULL;
  569. }
  570. static esp_err_t test_security1_no_encryption (void)
  571. {
  572. ESP_LOGI(TAG, "Starting Security 1 no encryption test");
  573. const char *pop_data = "test pop";
  574. protocomm_security1_params_t pop = {
  575. .data = (const uint8_t *)pop_data,
  576. .len = strlen(pop_data)
  577. };
  578. session_t *session = calloc(1, sizeof(session_t));
  579. if (session == NULL) {
  580. ESP_LOGE(TAG, "Error allocating session");
  581. return ESP_ERR_NO_MEM;
  582. }
  583. session->id = 1;
  584. session->sec_ver = 1;
  585. session->pop = &pop;
  586. // Start protocomm service
  587. if (start_test_service(1, &pop) != ESP_OK) {
  588. ESP_LOGE(TAG, "Error starting test");
  589. free(session);
  590. return ESP_ERR_INVALID_STATE;
  591. }
  592. // Intialise protocomm session with zero public keys
  593. if (test_new_session(session) != ESP_OK) {
  594. ESP_LOGE(TAG, "Error creating new session");
  595. stop_test_service();
  596. free(session);
  597. return ESP_FAIL;
  598. }
  599. // Perform 25519 security handshake to set public keys
  600. if (test_sec_endpoint(session) != ESP_OK) {
  601. ESP_LOGE(TAG, "Error testing security endpoint");
  602. test_delete_session(session);
  603. stop_test_service();
  604. free(session);
  605. return ESP_FAIL;
  606. }
  607. // Force endpoint with un-encrypted data
  608. session->sec_ver = 0;
  609. // Send unencrypted request data to echo endpoint.
  610. // Response would be encrypted causing echoed back
  611. // data to not match that which was sent, hence failing.
  612. if (test_req_endpoint(session) == ESP_OK) {
  613. ESP_LOGE(TAG, "Error testing request endpoint");
  614. session->sec_ver = 1;
  615. test_delete_session(session);
  616. stop_test_service();
  617. free(session);
  618. return ESP_FAIL;
  619. }
  620. session->sec_ver = 1;
  621. test_delete_session(session);
  622. stop_test_service();
  623. free(session);
  624. ESP_LOGI(TAG, "Protocomm test successful");
  625. return ESP_OK;
  626. }
  627. static esp_err_t test_security1_session_overflow (void)
  628. {
  629. ESP_LOGI(TAG, "Starting Security 1 session overflow test");
  630. const char *pop_data = "test pop";
  631. protocomm_security1_params_t pop = {
  632. .data = (const uint8_t *)pop_data,
  633. .len = strlen(pop_data)
  634. };
  635. session_t *session1 = calloc(1, sizeof(session_t));
  636. if (session1 == NULL) {
  637. ESP_LOGE(TAG, "Error allocating session");
  638. return ESP_ERR_NO_MEM;
  639. }
  640. session1->id = 2;
  641. session1->sec_ver = 1;
  642. session1->pop = &pop;
  643. session_t *session2 = calloc(1, sizeof(session_t));
  644. if (session2 == NULL) {
  645. ESP_LOGE(TAG, "Error allocating session");
  646. free(session1);
  647. return ESP_ERR_NO_MEM;
  648. }
  649. session2->id = 3;
  650. session2->sec_ver = 1;
  651. session2->pop = NULL;
  652. // Start protocomm service
  653. if (start_test_service(1, &pop) != ESP_OK) {
  654. ESP_LOGE(TAG, "Error starting test");
  655. free(session1);
  656. free(session2);
  657. return ESP_FAIL;
  658. }
  659. // Intialise protocomm session with zero public keys
  660. if (test_new_session(session1) != ESP_OK) {
  661. ESP_LOGE(TAG, "Error creating new session");
  662. stop_test_service();
  663. free(session1);
  664. free(session2);
  665. return ESP_FAIL;
  666. }
  667. // Perform 25519 security handshake to set public keys
  668. if (test_sec_endpoint(session1) != ESP_OK) {
  669. ESP_LOGE(TAG, "Error testing security endpoint");
  670. test_delete_session(session1);
  671. stop_test_service();
  672. free(session1);
  673. free(session2);
  674. return ESP_FAIL;
  675. }
  676. // Try to perform security handshake again with different
  677. // session ID without registering new session, hence failing
  678. if (test_sec_endpoint(session2) == ESP_OK) {
  679. ESP_LOGE(TAG, "Error testing security endpoint");
  680. test_delete_session(session1);
  681. stop_test_service();
  682. free(session1);
  683. free(session2);
  684. return ESP_FAIL;
  685. }
  686. test_delete_session(session1);
  687. stop_test_service();
  688. free(session1);
  689. free(session2);
  690. ESP_LOGI(TAG, "Protocomm test successful");
  691. return ESP_OK;
  692. }
  693. static esp_err_t test_security1_wrong_pop (void)
  694. {
  695. ESP_LOGI(TAG, "Starting Security 1 wrong auth test");
  696. const char *pop_data = "test pop";
  697. protocomm_security1_params_t pop = {
  698. .data = (const uint8_t *)pop_data,
  699. .len = strlen(pop_data)
  700. };
  701. session_t *session = calloc(1, sizeof(session_t));
  702. if (session == NULL) {
  703. ESP_LOGE(TAG, "Error allocating session");
  704. return ESP_ERR_NO_MEM;
  705. }
  706. session->id = 4;
  707. session->sec_ver = 1;
  708. session->pop = &pop;
  709. // Start protocomm service
  710. if (start_test_service(1, &pop) != ESP_OK) {
  711. ESP_LOGE(TAG, "Error starting test");
  712. free(session);
  713. return ESP_FAIL;
  714. }
  715. // Intialise protocomm session with zero public keys
  716. if (test_new_session(session) != ESP_OK) {
  717. ESP_LOGE(TAG, "Error creating new session");
  718. stop_test_service();
  719. free(session);
  720. return ESP_FAIL;
  721. }
  722. const char *wrong_pop_data = "wrong pop";
  723. protocomm_security1_params_t wrong_pop = {
  724. .data = (const uint8_t *)wrong_pop_data,
  725. .len = strlen(wrong_pop_data)
  726. };
  727. // Force wrong pop during authentication
  728. session->pop = &wrong_pop;
  729. // Perform 25519 security handshake with
  730. // wrong pop, hence failing
  731. if (test_sec_endpoint(session) == ESP_OK) {
  732. ESP_LOGE(TAG, "Error testing security endpoint");
  733. test_delete_session(session);
  734. stop_test_service();
  735. free(session);
  736. return ESP_FAIL;
  737. }
  738. test_delete_session(session);
  739. stop_test_service();
  740. free(session);
  741. ESP_LOGI(TAG, "Protocomm test successful");
  742. return ESP_OK;
  743. }
  744. static esp_err_t test_security1_insecure_client (void)
  745. {
  746. ESP_LOGI(TAG, "Starting Security 1 insecure client test");
  747. const char *pop_data = "test pop";
  748. protocomm_security1_params_t pop = {
  749. .data = (const uint8_t *)pop_data,
  750. .len = strlen(pop_data)
  751. };
  752. session_t *session = calloc(1, sizeof(session_t));
  753. if (session == NULL) {
  754. ESP_LOGE(TAG, "Error allocating session");
  755. return ESP_ERR_NO_MEM;
  756. }
  757. session->id = 5;
  758. session->sec_ver = 1;
  759. session->pop = &pop;
  760. // Start protocomm service
  761. if (start_test_service(1, &pop) != ESP_OK) {
  762. ESP_LOGE(TAG, "Error starting test");
  763. free(session);
  764. return ESP_FAIL;
  765. }
  766. // Perform 25519 security handshake without
  767. // initialising session, hence failing
  768. if (test_sec_endpoint(session) == ESP_OK) {
  769. ESP_LOGE(TAG, "Error testing security endpoint");
  770. stop_test_service();
  771. free(session);
  772. return ESP_FAIL;
  773. }
  774. // Communicating with request endpoint without
  775. // initialising session, hence failing
  776. if (test_req_endpoint(session) == ESP_OK) {
  777. ESP_LOGE(TAG, "Error testing request endpoint");
  778. stop_test_service();
  779. free(session);
  780. return ESP_FAIL;
  781. }
  782. stop_test_service();
  783. free(session);
  784. ESP_LOGI(TAG, "Protocomm test successful");
  785. return ESP_OK;
  786. }
  787. static esp_err_t test_security1_weak_session (void)
  788. {
  789. ESP_LOGI(TAG, "Starting Security 1 weak session test");
  790. const char *pop_data = "test pop";
  791. protocomm_security1_params_t pop = {
  792. .data = (const uint8_t *)pop_data,
  793. .len = strlen(pop_data)
  794. };
  795. session_t *session = calloc(1, sizeof(session_t));
  796. if (session == NULL) {
  797. ESP_LOGE(TAG, "Error allocating session");
  798. return ESP_ERR_NO_MEM;
  799. }
  800. session->id = 6;
  801. session->sec_ver = 1;
  802. session->pop = &pop;
  803. session->weak = 1;
  804. // Start protocomm service
  805. if (start_test_service(1, &pop) != ESP_OK) {
  806. ESP_LOGE(TAG, "Error starting test");
  807. free(session);
  808. return ESP_FAIL;
  809. }
  810. // Intialise protocomm session with zero public keys
  811. if (test_new_session(session) != ESP_OK) {
  812. ESP_LOGE(TAG, "Error creating new session");
  813. stop_test_service();
  814. free(session);
  815. return ESP_FAIL;
  816. }
  817. // Perform 25519 security handshake with weak (zero)
  818. // client public key, hence failing
  819. if (test_sec_endpoint(session) == ESP_OK) {
  820. ESP_LOGE(TAG, "Error testing security endpoint");
  821. test_delete_session(session);
  822. stop_test_service();
  823. free(session);
  824. return ESP_FAIL;
  825. }
  826. // Sending request data to echo endpoint encrypted with zero
  827. // public keys on both client and server side should fail
  828. if (test_req_endpoint(session) == ESP_OK) {
  829. ESP_LOGE(TAG, "Error testing request endpoint");
  830. test_delete_session(session);
  831. stop_test_service();
  832. free(session);
  833. return ESP_FAIL;
  834. }
  835. test_delete_session(session);
  836. stop_test_service();
  837. free(session);
  838. ESP_LOGI(TAG, "Protocomm test successful");
  839. return ESP_OK;
  840. }
  841. static esp_err_t test_protocomm (session_t *session)
  842. {
  843. ESP_LOGI(TAG, "Starting Protocomm test");
  844. // Start protocomm service
  845. if (start_test_service(session->sec_ver, session->pop) != ESP_OK) {
  846. ESP_LOGE(TAG, "Error starting test");
  847. return ESP_FAIL;
  848. }
  849. // Check version endpoint
  850. if (test_ver_endpoint(session) != ESP_OK) {
  851. ESP_LOGE(TAG, "Error testing version endpoint");
  852. stop_test_service();
  853. return ESP_FAIL;
  854. }
  855. // Intialise protocomm session with zero public keys
  856. if (test_new_session(session) != ESP_OK) {
  857. ESP_LOGE(TAG, "Error creating new session");
  858. stop_test_service();
  859. return ESP_FAIL;
  860. }
  861. // Perform 25519 security handshake to set public keys
  862. if (test_sec_endpoint(session) != ESP_OK) {
  863. ESP_LOGE(TAG, "Error testing security endpoint");
  864. test_delete_session(session);
  865. stop_test_service();
  866. return ESP_FAIL;
  867. }
  868. // Send request data to echo endpoint encrypted with
  869. // the set public keys on both client and server side
  870. if (test_req_endpoint(session) != ESP_OK) {
  871. ESP_LOGE(TAG, "Error testing request endpoint");
  872. test_delete_session(session);
  873. stop_test_service();
  874. return ESP_FAIL;
  875. }
  876. // Stop protocomm service
  877. test_delete_session(session);
  878. stop_test_service();
  879. ESP_LOGI(TAG, "Protocomm test successful");
  880. return ESP_OK;
  881. }
  882. static esp_err_t test_security1 (void)
  883. {
  884. ESP_LOGI(TAG, "Starting Sec1 test");
  885. const char *pop_data = "test pop";
  886. protocomm_security1_params_t pop = {
  887. .data = (const uint8_t *)pop_data,
  888. .len = strlen(pop_data)
  889. };
  890. session_t *session = calloc(1, sizeof(session_t));
  891. if (session == NULL) {
  892. ESP_LOGE(TAG, "Error allocating session");
  893. return ESP_ERR_NO_MEM;
  894. }
  895. session->id = 7;
  896. session->sec_ver = 1;
  897. session->pop = &pop;
  898. if (test_protocomm (session) != ESP_OK) {
  899. ESP_LOGE(TAG, "Sec1 test failed");
  900. free(session);
  901. return ESP_FAIL;
  902. }
  903. ESP_LOGI(TAG, "Sec1 test successful");
  904. free(session);
  905. return ESP_OK;
  906. }
  907. static esp_err_t test_security0 (void)
  908. {
  909. ESP_LOGI(TAG, "Starting Sec0 test");
  910. session_t *session = calloc(1, sizeof(session_t));
  911. if (session == NULL) {
  912. ESP_LOGE(TAG, "Error allocating session");
  913. return ESP_ERR_NO_MEM;
  914. }
  915. session->id = 8;
  916. session->sec_ver = 0;
  917. session->pop = NULL;
  918. if (test_protocomm (session) != ESP_OK) {
  919. ESP_LOGE(TAG, "Sec0 test failed");
  920. free(session);
  921. return ESP_FAIL;
  922. }
  923. ESP_LOGI(TAG, "Sec0 test successful");
  924. free(session);
  925. return ESP_OK;
  926. }
  927. TEST_CASE("leak test", "[PROTOCOMM]")
  928. {
  929. #ifdef CONFIG_HEAP_TRACING
  930. heap_trace_init_standalone(trace_record, NUM_RECORDS);
  931. heap_trace_start(HEAP_TRACE_LEAKS);
  932. #endif
  933. /* Run basic tests for the first time to allow for internal long
  934. * time allocations to happen (not related to protocomm) */
  935. test_security0();
  936. test_security1();
  937. usleep(1000);
  938. #ifdef CONFIG_HEAP_TRACING
  939. heap_trace_stop();
  940. heap_trace_dump();
  941. #endif
  942. /* Run all tests passively. Any leaks due
  943. * to protocomm should show up now */
  944. unsigned pre_start_mem = esp_get_free_heap_size();
  945. test_security0();
  946. test_security1();
  947. test_security1_no_encryption();
  948. test_security1_session_overflow();
  949. test_security1_wrong_pop();
  950. test_security1_insecure_client();
  951. test_security1_weak_session();
  952. usleep(1000);
  953. unsigned post_stop_mem = esp_get_free_heap_size();
  954. if (pre_start_mem != post_stop_mem) {
  955. ESP_LOGE(TAG, "Mismatch in free heap size : %d bytes", post_stop_mem - pre_start_mem);
  956. }
  957. TEST_ASSERT(pre_start_mem == post_stop_mem);
  958. }
  959. TEST_CASE("security 0 basic test", "[PROTOCOMM]")
  960. {
  961. TEST_ASSERT(test_security0() == ESP_OK);
  962. }
  963. TEST_CASE("security 1 basic test", "[PROTOCOMM]")
  964. {
  965. TEST_ASSERT(test_security1() == ESP_OK);
  966. }
  967. TEST_CASE("security 1 no encryption test", "[PROTOCOMM]")
  968. {
  969. TEST_ASSERT(test_security1_no_encryption() == ESP_OK);
  970. }
  971. TEST_CASE("security 1 session overflow test", "[PROTOCOMM]")
  972. {
  973. TEST_ASSERT(test_security1_session_overflow() == ESP_OK);
  974. }
  975. TEST_CASE("security 1 wrong pop test", "[PROTOCOMM]")
  976. {
  977. TEST_ASSERT(test_security1_wrong_pop() == ESP_OK);
  978. }
  979. TEST_CASE("security 1 insecure client test", "[PROTOCOMM]")
  980. {
  981. TEST_ASSERT(test_security1_insecure_client() == ESP_OK);
  982. }
  983. TEST_CASE("security 1 weak session test", "[PROTOCOMM]")
  984. {
  985. TEST_ASSERT(test_security1_weak_session() == ESP_OK);
  986. }