test_protocomm.c 33 KB

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