test_protocomm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. mbedtls_aes_crypt_ctr(&session->ctx_aes, sizeof(rand_test_data), &session->nc_off,
  461. session->rand, session->stb, rand_test_data, enc_test_data);
  462. }
  463. ssize_t verify_data_len = 0;
  464. uint8_t *enc_verify_data = NULL;
  465. esp_err_t ret = protocomm_req_handle(test_pc, "test-ep", session_id,
  466. enc_test_data, sizeof(enc_test_data),
  467. &enc_verify_data, &verify_data_len);
  468. if (ret != ESP_OK || !verify_data_len) {
  469. ESP_LOGE(TAG, "test-ep handler failed");
  470. return ESP_FAIL;
  471. }
  472. uint8_t *verify_data = malloc(verify_data_len);
  473. if (!verify_data) {
  474. ESP_LOGE(TAG, "error allocating memory for decrypted data");
  475. free(enc_verify_data);
  476. return ESP_FAIL;
  477. }
  478. if (session->sec_ver == 0) {
  479. memcpy(verify_data, enc_verify_data, verify_data_len);
  480. }
  481. else if (session->sec_ver == 1) {
  482. mbedtls_aes_crypt_ctr(&session->ctx_aes, verify_data_len, &session->nc_off,
  483. session->rand, session->stb, enc_verify_data, verify_data);
  484. }
  485. free(enc_verify_data);
  486. hexdump("Sent data", rand_test_data, sizeof(rand_test_data));
  487. hexdump("Recv data", verify_data, verify_data_len);
  488. ESP_LOGI(TAG, "verify data len : %d", verify_data_len);
  489. ESP_LOGI(TAG, "expected data len : %d", sizeof(rand_test_data));
  490. if (verify_data_len != sizeof(rand_test_data)) {
  491. ESP_LOGE(TAG, "incorrect response length from test-ep");
  492. free(verify_data);
  493. return ESP_FAIL;
  494. }
  495. if (memcmp(rand_test_data, verify_data, verify_data_len)) {
  496. ESP_LOGE(TAG, "incorrect response data from test-ep");
  497. free(verify_data);
  498. return ESP_FAIL;
  499. }
  500. free(verify_data);
  501. return ESP_OK;
  502. }
  503. esp_err_t test_req_handler (uint32_t session_id,
  504. const uint8_t *inbuf, ssize_t inlen,
  505. uint8_t **outbuf, ssize_t *outlen,
  506. void *priv_data)
  507. {
  508. *outbuf = malloc(inlen);
  509. if (*outbuf) {
  510. *outlen = inlen;
  511. memcpy(*outbuf, inbuf, inlen);
  512. } else {
  513. ESP_LOGE(TAG, "Error allocating response outbuf");
  514. *outbuf = NULL;
  515. *outlen = 0;
  516. }
  517. uint32_t *priv = (uint32_t *) priv_data;
  518. if ((&test_priv_data != priv) || (test_priv_data != *priv)) {
  519. ESP_LOGE(TAG, "Handler private data doesn't match");
  520. return ESP_FAIL;
  521. }
  522. return ESP_OK;
  523. }
  524. static esp_err_t start_test_service(uint8_t sec_ver, const protocomm_security_pop_t *pop)
  525. {
  526. test_pc = protocomm_new();
  527. if (test_pc == NULL) {
  528. ESP_LOGE(TAG, "Failed to create new protocomm instance");
  529. return ESP_FAIL;
  530. }
  531. if (sec_ver == 0) {
  532. if (protocomm_set_security(test_pc, "test-sec", &protocomm_security0, NULL) != ESP_OK) {
  533. ESP_LOGE(TAG, "Failed to set Security0");
  534. return ESP_FAIL;
  535. }
  536. test_sec = &protocomm_security0;
  537. } else if (sec_ver == 1) {
  538. if (protocomm_set_security(test_pc, "test-sec", &protocomm_security1, pop) != ESP_OK) {
  539. ESP_LOGE(TAG, "Failed to set Security1");
  540. return ESP_FAIL;
  541. }
  542. test_sec = &protocomm_security1;
  543. }
  544. if (protocomm_set_version(test_pc, "test-ver", TEST_VER_STR) != ESP_OK) {
  545. ESP_LOGE(TAG, "Failed to set version");
  546. return ESP_FAIL;
  547. }
  548. if (protocomm_add_endpoint(test_pc, "test-ep",
  549. test_req_handler,
  550. (void *) &test_priv_data) != ESP_OK) {
  551. ESP_LOGE(TAG, "Failed to set test-ep endpoint handler");
  552. return ESP_FAIL;
  553. }
  554. return ESP_OK;
  555. }
  556. static void stop_test_service(void)
  557. {
  558. test_sec = NULL;
  559. protocomm_delete(test_pc);
  560. test_pc = NULL;
  561. }
  562. static esp_err_t test_security1_no_encryption (void)
  563. {
  564. ESP_LOGI(TAG, "Starting Security 1 no encryption test");
  565. const char *pop_data = "test pop";
  566. protocomm_security_pop_t pop = {
  567. .data = (const uint8_t *)pop_data,
  568. .len = strlen(pop_data)
  569. };
  570. session_t *session = calloc(1, sizeof(session_t));
  571. if (session == NULL) {
  572. ESP_LOGE(TAG, "Error allocating session");
  573. return ESP_ERR_NO_MEM;
  574. }
  575. session->id = 1;
  576. session->sec_ver = 1;
  577. session->pop = &pop;
  578. // Start protocomm service
  579. if (start_test_service(1, &pop) != ESP_OK) {
  580. ESP_LOGE(TAG, "Error starting test");
  581. free(session);
  582. return ESP_ERR_INVALID_STATE;
  583. }
  584. // Intialise protocomm session with zero public keys
  585. if (test_new_session(session) != ESP_OK) {
  586. ESP_LOGE(TAG, "Error creating new session");
  587. stop_test_service();
  588. free(session);
  589. return ESP_FAIL;
  590. }
  591. // Perform 25519 security handshake to set public keys
  592. if (test_sec_endpoint(session) != ESP_OK) {
  593. ESP_LOGE(TAG, "Error testing security endpoint");
  594. test_delete_session(session);
  595. stop_test_service();
  596. free(session);
  597. return ESP_FAIL;
  598. }
  599. // Force endpoint with un-encrypted data
  600. session->sec_ver = 0;
  601. // Send unencrypted request data to echo endpoint.
  602. // Response would be encrypted causing echoed back
  603. // data to not match that which was sent, hence failing.
  604. if (test_req_endpoint(session) == ESP_OK) {
  605. ESP_LOGE(TAG, "Error testing request endpoint");
  606. session->sec_ver = 1;
  607. test_delete_session(session);
  608. stop_test_service();
  609. free(session);
  610. return ESP_FAIL;
  611. }
  612. session->sec_ver = 1;
  613. test_delete_session(session);
  614. stop_test_service();
  615. free(session);
  616. ESP_LOGI(TAG, "Protocomm test successful");
  617. return ESP_OK;
  618. }
  619. static esp_err_t test_security1_session_overflow (void)
  620. {
  621. ESP_LOGI(TAG, "Starting Security 1 session overflow test");
  622. const char *pop_data = "test pop";
  623. protocomm_security_pop_t pop = {
  624. .data = (const uint8_t *)pop_data,
  625. .len = strlen(pop_data)
  626. };
  627. session_t *session1 = calloc(1, sizeof(session_t));
  628. if (session1 == NULL) {
  629. ESP_LOGE(TAG, "Error allocating session");
  630. return ESP_ERR_NO_MEM;
  631. }
  632. session1->id = 2;
  633. session1->sec_ver = 1;
  634. session1->pop = &pop;
  635. session_t *session2 = calloc(1, sizeof(session_t));
  636. if (session2 == NULL) {
  637. ESP_LOGE(TAG, "Error allocating session");
  638. free(session1);
  639. return ESP_ERR_NO_MEM;
  640. }
  641. session2->id = 3;
  642. session2->sec_ver = 1;
  643. session2->pop = NULL;
  644. // Start protocomm service
  645. if (start_test_service(1, &pop) != ESP_OK) {
  646. ESP_LOGE(TAG, "Error starting test");
  647. free(session1);
  648. free(session2);
  649. return ESP_FAIL;
  650. }
  651. // Intialise protocomm session with zero public keys
  652. if (test_new_session(session1) != ESP_OK) {
  653. ESP_LOGE(TAG, "Error creating new session");
  654. stop_test_service();
  655. free(session1);
  656. free(session2);
  657. return ESP_FAIL;
  658. }
  659. // Perform 25519 security handshake to set public keys
  660. if (test_sec_endpoint(session1) != ESP_OK) {
  661. ESP_LOGE(TAG, "Error testing security endpoint");
  662. test_delete_session(session1);
  663. stop_test_service();
  664. free(session1);
  665. free(session2);
  666. return ESP_FAIL;
  667. }
  668. // Try to perform security handshake again with different
  669. // session ID without registering new session, hence failing
  670. if (test_sec_endpoint(session2) == ESP_OK) {
  671. ESP_LOGE(TAG, "Error testing security endpoint");
  672. test_delete_session(session1);
  673. stop_test_service();
  674. free(session1);
  675. free(session2);
  676. return ESP_FAIL;
  677. }
  678. test_delete_session(session1);
  679. stop_test_service();
  680. free(session1);
  681. free(session2);
  682. ESP_LOGI(TAG, "Protocomm test successful");
  683. return ESP_OK;
  684. }
  685. static esp_err_t test_security1_wrong_pop (void)
  686. {
  687. ESP_LOGI(TAG, "Starting Security 1 wrong auth test");
  688. const char *pop_data = "test pop";
  689. protocomm_security_pop_t pop = {
  690. .data = (const uint8_t *)pop_data,
  691. .len = strlen(pop_data)
  692. };
  693. session_t *session = calloc(1, sizeof(session_t));
  694. if (session == NULL) {
  695. ESP_LOGE(TAG, "Error allocating session");
  696. return ESP_ERR_NO_MEM;
  697. }
  698. session->id = 4;
  699. session->sec_ver = 1;
  700. session->pop = &pop;
  701. // Start protocomm service
  702. if (start_test_service(1, &pop) != ESP_OK) {
  703. ESP_LOGE(TAG, "Error starting test");
  704. free(session);
  705. return ESP_FAIL;
  706. }
  707. // Intialise protocomm session with zero public keys
  708. if (test_new_session(session) != ESP_OK) {
  709. ESP_LOGE(TAG, "Error creating new session");
  710. stop_test_service();
  711. free(session);
  712. return ESP_FAIL;
  713. }
  714. const char *wrong_pop_data = "wrong pop";
  715. protocomm_security_pop_t wrong_pop = {
  716. .data = (const uint8_t *)wrong_pop_data,
  717. .len = strlen(wrong_pop_data)
  718. };
  719. // Force wrong pop during authentication
  720. session->pop = &wrong_pop;
  721. // Perform 25519 security handshake with
  722. // wrong pop, hence failing
  723. if (test_sec_endpoint(session) == ESP_OK) {
  724. ESP_LOGE(TAG, "Error testing security endpoint");
  725. test_delete_session(session);
  726. stop_test_service();
  727. free(session);
  728. return ESP_FAIL;
  729. }
  730. test_delete_session(session);
  731. stop_test_service();
  732. free(session);
  733. ESP_LOGI(TAG, "Protocomm test successful");
  734. return ESP_OK;
  735. }
  736. static esp_err_t test_security1_insecure_client (void)
  737. {
  738. ESP_LOGI(TAG, "Starting Security 1 insecure client test");
  739. const char *pop_data = "test pop";
  740. protocomm_security_pop_t pop = {
  741. .data = (const uint8_t *)pop_data,
  742. .len = strlen(pop_data)
  743. };
  744. session_t *session = calloc(1, sizeof(session_t));
  745. if (session == NULL) {
  746. ESP_LOGE(TAG, "Error allocating session");
  747. return ESP_ERR_NO_MEM;
  748. }
  749. session->id = 5;
  750. session->sec_ver = 1;
  751. session->pop = &pop;
  752. // Start protocomm service
  753. if (start_test_service(1, &pop) != ESP_OK) {
  754. ESP_LOGE(TAG, "Error starting test");
  755. free(session);
  756. return ESP_FAIL;
  757. }
  758. // Perform 25519 security handshake without
  759. // initialising session, hence failing
  760. if (test_sec_endpoint(session) == ESP_OK) {
  761. ESP_LOGE(TAG, "Error testing security endpoint");
  762. stop_test_service();
  763. free(session);
  764. return ESP_FAIL;
  765. }
  766. // Communicating with request endpoint without
  767. // initialising session, hence failing
  768. if (test_req_endpoint(session) == ESP_OK) {
  769. ESP_LOGE(TAG, "Error testing request endpoint");
  770. stop_test_service();
  771. free(session);
  772. return ESP_FAIL;
  773. }
  774. stop_test_service();
  775. free(session);
  776. ESP_LOGI(TAG, "Protocomm test successful");
  777. return ESP_OK;
  778. }
  779. static esp_err_t test_security1_weak_session (void)
  780. {
  781. ESP_LOGI(TAG, "Starting Security 1 weak session test");
  782. const char *pop_data = "test pop";
  783. protocomm_security_pop_t pop = {
  784. .data = (const uint8_t *)pop_data,
  785. .len = strlen(pop_data)
  786. };
  787. session_t *session = calloc(1, sizeof(session_t));
  788. if (session == NULL) {
  789. ESP_LOGE(TAG, "Error allocating session");
  790. return ESP_ERR_NO_MEM;
  791. }
  792. session->id = 6;
  793. session->sec_ver = 1;
  794. session->pop = &pop;
  795. session->weak = 1;
  796. // Start protocomm service
  797. if (start_test_service(1, &pop) != ESP_OK) {
  798. ESP_LOGE(TAG, "Error starting test");
  799. free(session);
  800. return ESP_FAIL;
  801. }
  802. // Intialise protocomm session with zero public keys
  803. if (test_new_session(session) != ESP_OK) {
  804. ESP_LOGE(TAG, "Error creating new session");
  805. stop_test_service();
  806. free(session);
  807. return ESP_FAIL;
  808. }
  809. // Perform 25519 security handshake with weak (zero)
  810. // client public key, hence failing
  811. if (test_sec_endpoint(session) == ESP_OK) {
  812. ESP_LOGE(TAG, "Error testing security endpoint");
  813. test_delete_session(session);
  814. stop_test_service();
  815. free(session);
  816. return ESP_FAIL;
  817. }
  818. // Sending request data to echo endpoint encrypted with zero
  819. // public keys on both client and server side should fail
  820. if (test_req_endpoint(session) == ESP_OK) {
  821. ESP_LOGE(TAG, "Error testing request endpoint");
  822. test_delete_session(session);
  823. stop_test_service();
  824. free(session);
  825. return ESP_FAIL;
  826. }
  827. test_delete_session(session);
  828. stop_test_service();
  829. free(session);
  830. ESP_LOGI(TAG, "Protocomm test successful");
  831. return ESP_OK;
  832. }
  833. static esp_err_t test_protocomm (session_t *session)
  834. {
  835. ESP_LOGI(TAG, "Starting Protocomm test");
  836. // Start protocomm service
  837. if (start_test_service(session->sec_ver, session->pop) != ESP_OK) {
  838. ESP_LOGE(TAG, "Error starting test");
  839. return ESP_FAIL;
  840. }
  841. // Check version endpoint
  842. if (test_ver_endpoint(session) != ESP_OK) {
  843. ESP_LOGE(TAG, "Error testing version endpoint");
  844. stop_test_service();
  845. return ESP_FAIL;
  846. }
  847. // Intialise protocomm session with zero public keys
  848. if (test_new_session(session) != ESP_OK) {
  849. ESP_LOGE(TAG, "Error creating new session");
  850. stop_test_service();
  851. return ESP_FAIL;
  852. }
  853. // Perform 25519 security handshake to set public keys
  854. if (test_sec_endpoint(session) != ESP_OK) {
  855. ESP_LOGE(TAG, "Error testing security endpoint");
  856. test_delete_session(session);
  857. stop_test_service();
  858. return ESP_FAIL;
  859. }
  860. // Send request data to echo endpoint encrypted with
  861. // the set public keys on both client and server side
  862. if (test_req_endpoint(session) != ESP_OK) {
  863. ESP_LOGE(TAG, "Error testing request endpoint");
  864. test_delete_session(session);
  865. stop_test_service();
  866. return ESP_FAIL;
  867. }
  868. // Stop protocomm service
  869. test_delete_session(session);
  870. stop_test_service();
  871. ESP_LOGI(TAG, "Protocomm test successful");
  872. return ESP_OK;
  873. }
  874. static esp_err_t test_security1 (void)
  875. {
  876. ESP_LOGI(TAG, "Starting Sec1 test");
  877. const char *pop_data = "test pop";
  878. protocomm_security_pop_t pop = {
  879. .data = (const uint8_t *)pop_data,
  880. .len = strlen(pop_data)
  881. };
  882. session_t *session = calloc(1, sizeof(session_t));
  883. if (session == NULL) {
  884. ESP_LOGE(TAG, "Error allocating session");
  885. return ESP_ERR_NO_MEM;
  886. }
  887. session->id = 7;
  888. session->sec_ver = 1;
  889. session->pop = &pop;
  890. if (test_protocomm (session) != ESP_OK) {
  891. ESP_LOGE(TAG, "Sec1 test failed");
  892. free(session);
  893. return ESP_FAIL;
  894. }
  895. ESP_LOGI(TAG, "Sec1 test successful");
  896. free(session);
  897. return ESP_OK;
  898. }
  899. static esp_err_t test_security0 (void)
  900. {
  901. ESP_LOGI(TAG, "Starting Sec0 test");
  902. session_t *session = calloc(1, sizeof(session_t));
  903. if (session == NULL) {
  904. ESP_LOGE(TAG, "Error allocating session");
  905. return ESP_ERR_NO_MEM;
  906. }
  907. session->id = 8;
  908. session->sec_ver = 0;
  909. session->pop = NULL;
  910. if (test_protocomm (session) != ESP_OK) {
  911. ESP_LOGE(TAG, "Sec0 test failed");
  912. free(session);
  913. return ESP_FAIL;
  914. }
  915. ESP_LOGI(TAG, "Sec0 test successful");
  916. free(session);
  917. return ESP_OK;
  918. }
  919. TEST_CASE("leak test", "[PROTOCOMM]")
  920. {
  921. #ifdef CONFIG_HEAP_TRACING
  922. heap_trace_init_standalone(trace_record, NUM_RECORDS);
  923. heap_trace_start(HEAP_TRACE_LEAKS);
  924. #endif
  925. /* Run basic tests for the first time to allow for internal long
  926. * time allocations to happen (not related to protocomm) */
  927. test_security0();
  928. test_security1();
  929. usleep(1000);
  930. #ifdef CONFIG_HEAP_TRACING
  931. heap_trace_stop();
  932. heap_trace_dump();
  933. #endif
  934. /* Run all tests passively. Any leaks due
  935. * to protocomm should show up now */
  936. unsigned pre_start_mem = esp_get_free_heap_size();
  937. test_security0();
  938. test_security1();
  939. test_security1_no_encryption();
  940. test_security1_session_overflow();
  941. test_security1_wrong_pop();
  942. test_security1_insecure_client();
  943. test_security1_weak_session();
  944. usleep(1000);
  945. unsigned post_stop_mem = esp_get_free_heap_size();
  946. if (pre_start_mem != post_stop_mem) {
  947. ESP_LOGE(TAG, "Mismatch in free heap size : %d bytes", post_stop_mem - pre_start_mem);
  948. }
  949. TEST_ASSERT(pre_start_mem == post_stop_mem);
  950. }
  951. TEST_CASE("security 0 basic test", "[PROTOCOMM]")
  952. {
  953. TEST_ASSERT(test_security0() == ESP_OK);
  954. }
  955. TEST_CASE("security 1 basic test", "[PROTOCOMM]")
  956. {
  957. TEST_ASSERT(test_security1() == ESP_OK);
  958. }
  959. TEST_CASE("security 1 no encryption test", "[PROTOCOMM]")
  960. {
  961. TEST_ASSERT(test_security1_no_encryption() == ESP_OK);
  962. }
  963. TEST_CASE("security 1 session overflow test", "[PROTOCOMM]")
  964. {
  965. TEST_ASSERT(test_security1_session_overflow() == ESP_OK);
  966. }
  967. TEST_CASE("security 1 wrong pop test", "[PROTOCOMM]")
  968. {
  969. TEST_ASSERT(test_security1_wrong_pop() == ESP_OK);
  970. }
  971. TEST_CASE("security 1 insecure client test", "[PROTOCOMM]")
  972. {
  973. TEST_ASSERT(test_security1_insecure_client() == ESP_OK);
  974. }
  975. TEST_CASE("security 1 weak session test", "[PROTOCOMM]")
  976. {
  977. TEST_ASSERT(test_security1_weak_session() == ESP_OK);
  978. }