test_protocomm.c 34 KB

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