cdc-protocol.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Motenpoche
  2. *
  3. * (c) 2023 Daniele Lacamera <root@danielinux.net>
  4. *
  5. *
  6. * Motenpoche is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Motenpoche is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  19. *
  20. */
  21. #include <stdint.h>
  22. #include "class/cdc/cdc.h"
  23. #include "class/cdc/cdc_device.h"
  24. #include "bsp/board.h"
  25. #include "wolfssl/wolfcrypt/settings.h"
  26. #include "cryptoengine.h"
  27. #include "flash.h"
  28. #include "fsm.h"
  29. #include "hardware/gpio.h"
  30. #include "display.h"
  31. extern uint16_t flash_info;
  32. extern int thread_control_ui;
  33. extern void system_reboot(void);
  34. extern uint8_t checked_digest[];
  35. // Invoked when cdc when line state changed e.g connected/disconnected
  36. void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
  37. {
  38. (void) itf;
  39. (void) rts;
  40. // TODO set some indicator
  41. if ( dtr )
  42. {
  43. // Terminal connected
  44. }else
  45. {
  46. // Terminal disconnected
  47. }
  48. }
  49. static uint8_t cdc_rxbuf[MAX_CDC_CMD];
  50. static uint32_t cdc_rx_idx = 0;
  51. static uint32_t cdc_rx_expected = 0xFFFFFFFF;
  52. static int rx_busy = 0;
  53. static void send_OK(void)
  54. {
  55. struct cdc_packet_hdr OKhdr;
  56. OKhdr.magic = VAULT_MAGIC;
  57. OKhdr.cmd = CDC_OK;
  58. OKhdr.len = 0;
  59. tud_cdc_write((void *)&OKhdr, 8);
  60. tud_cdc_write_flush();
  61. }
  62. static void send_fail(char *errmsg)
  63. {
  64. uint8_t failbuf[256 + 8];
  65. struct cdc_packet_hdr *hdr = (struct cdc_packet_hdr*)failbuf;
  66. memset(failbuf, 0, 256 + 8);
  67. snprintf(failbuf + 8, 256, "%s", errmsg);
  68. hdr->magic = VAULT_MAGIC;
  69. hdr->cmd = CDC_FAIL;
  70. hdr->len = strlen(failbuf + 8);
  71. tud_cdc_write((void *)hdr, 8 + strlen(failbuf + 8));
  72. tud_cdc_write_flush();
  73. }
  74. static void parse_cmd(int rxbuf_size)
  75. {
  76. struct cdc_packet_hdr *hdr;
  77. uint16_t svc_active, svc_avail;
  78. hdr = (struct cdc_packet_hdr *)(cdc_rxbuf);
  79. switch(hdr->cmd & 0xFF) {
  80. case CDC_STATUS:
  81. int i;
  82. enum vault_state st = fsm_get();
  83. struct vault_status *vst;
  84. uint32_t n_st = (uint32_t)st;
  85. uint8_t status_buffer[sizeof(struct cdc_packet_hdr) +
  86. sizeof(struct vault_status)];
  87. hdr = (struct cdc_packet_hdr *)status_buffer;
  88. vst = (struct vault_status *)(status_buffer +
  89. sizeof(struct cdc_packet_hdr));
  90. hdr->magic = VAULT_MAGIC;
  91. hdr->cmd = CDC_STATUS;
  92. hdr->len = sizeof(struct cdc_packet_hdr) + sizeof(struct vault_status);
  93. memset(vst, 0, sizeof(struct vault_status));
  94. vst->state = fsm_get();
  95. if (cryptoengine_check_vault() == 0) {
  96. cryptoengine_fill_vault_status(vst);
  97. }
  98. svc_active = 0;
  99. svc_avail = 0;
  100. cryptoengine_service_count(&svc_active, &svc_avail);
  101. vst->services_active = svc_active;
  102. vst->first_avail = svc_avail;
  103. for (i = 0; i < sizeof(status_buffer);) {
  104. int len = 64;
  105. if ((sizeof(status_buffer) - i) < len)
  106. len = (sizeof(status_buffer) - i);
  107. tud_cdc_write((void *)status_buffer + i, len);
  108. tud_cdc_write_flush();
  109. i += len;
  110. }
  111. break;
  112. case CDC_TOFU_INIT:
  113. if (fsm_get() != VAULT_TOFU) {
  114. send_fail("Already provisioned");
  115. break;
  116. }
  117. if (hdr->len == sizeof(struct vault_header)) {
  118. struct vault_header *vh = (struct vault_header *)
  119. (cdc_rxbuf + sizeof(struct cdc_packet_hdr));
  120. if (rxbuf_size != (hdr->len + sizeof(struct cdc_packet_hdr))) {
  121. char msg[64];
  122. sprintf(msg, "packet len mismatch exp: %d got %d",
  123. (hdr->len + sizeof(struct cdc_packet_hdr)),
  124. rxbuf_size);
  125. send_fail(msg);
  126. }
  127. if (vh->magic != VAULT_MAGIC) {
  128. send_fail("TOFU Rejected.");
  129. break;
  130. }
  131. if (cryptoengine_hdr_sha_check(vh) < 0) {
  132. send_fail("TOFU Rejected.");
  133. break;
  134. }
  135. /* Take control of display/buttons */
  136. thread_control_ui = 1;
  137. sleep_ms(100);
  138. display_clear();
  139. display_text(0, "Provisioning...");
  140. flash_sector_erase(VAULT_FLASH_OFFSET);
  141. flash_write(VAULT_FLASH_OFFSET, cdc_rxbuf + sizeof(struct cdc_packet_hdr), sizeof(struct vault_header));
  142. display_text(1, "Confirming... ");
  143. send_OK();
  144. display_text(2, "Rebooting... ");
  145. system_reboot();
  146. break;
  147. } else {
  148. send_fail("TOFU Rejected.");
  149. break;
  150. }
  151. break;
  152. case CDC_CHALLENGE:
  153. break;
  154. case CDC_REKEY:
  155. break;
  156. case CDC_ADDSERV:
  157. int ret;
  158. uint16_t count, idx;
  159. struct vault_service *vs;
  160. gpio_put(16,1);
  161. if (rxbuf_size != SVC_SIZE + sizeof(struct cdc_packet_hdr)) {
  162. char msg[64];
  163. sprintf(msg, "packet len mismatch exp: %d got %d", hdr->len, rxbuf_size);
  164. send_fail(msg);
  165. break;
  166. }
  167. vs = (struct vault_service *)(cdc_rxbuf +sizeof(struct cdc_packet_hdr));
  168. /* Retrieve the implicit destination slot (current first slot available) */
  169. cryptoengine_service_count(&count, &idx);
  170. if (cryptoengine_check_vault() < 0)
  171. break;
  172. ret = cryptoengine_import_service(vs, idx);
  173. if (ret == 0)
  174. send_OK();
  175. else {
  176. char msg[40];
  177. sprintf(msg, "failed import: svc %s retval %d", vs->name, ret);
  178. send_fail(msg);
  179. }
  180. break;
  181. case CDC_DELSERV:
  182. break;
  183. default:
  184. send_fail("Bad cmd");
  185. }
  186. rx_busy = 0;
  187. }
  188. // Invoked when CDC interface received data from host
  189. void tud_cdc_rx_cb(uint8_t itf)
  190. {
  191. (void) itf;
  192. char buf[64];
  193. uint32_t count;
  194. struct cdc_packet_hdr *hdr;
  195. if (rx_busy) {
  196. tud_cdc_read_flush();
  197. return;
  198. }
  199. count = tud_cdc_read(buf, sizeof(buf));
  200. if (count > 0) {
  201. if (count + cdc_rx_idx > MAX_CDC_CMD) {
  202. cdc_rx_idx = 0;
  203. }
  204. memcpy(cdc_rxbuf + cdc_rx_idx, buf, count);
  205. hdr = (struct cdc_packet_hdr *)cdc_rxbuf;
  206. if (cdc_rx_idx == 0) {
  207. if (hdr->magic != VAULT_MAGIC) {
  208. send_fail("Bad magic");
  209. cdc_rx_idx = 0;
  210. return;
  211. }
  212. if (hdr->len > MAX_CDC_CMD) {
  213. send_fail("Bad cmd: too big");
  214. cdc_rx_idx = 0;
  215. return;
  216. }
  217. cdc_rx_expected = hdr->len + sizeof(struct cdc_packet_hdr);
  218. }
  219. cdc_rx_idx += count;
  220. if (cdc_rx_idx >= cdc_rx_expected) {
  221. rx_busy = 1;
  222. parse_cmd(cdc_rx_expected);
  223. if (cdc_rx_expected < cdc_rx_idx) {
  224. memcpy(cdc_rxbuf, cdc_rxbuf + cdc_rx_expected, cdc_rx_idx - cdc_rx_expected);
  225. }
  226. cdc_rx_idx-=cdc_rx_expected;
  227. }
  228. }
  229. }