u2f-emulated.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * U2F USB Emulated device.
  3. *
  4. * Copyright (c) 2020 César Belley <cesar.belley@lse.epita.fr>
  5. * Written by César Belley <cesar.belley@lse.epita.fr>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/module.h"
  27. #include "qemu/thread.h"
  28. #include "qemu/main-loop.h"
  29. #include "qapi/error.h"
  30. #include "hw/usb.h"
  31. #include "hw/qdev-properties.h"
  32. #include <u2f-emu/u2f-emu.h>
  33. #include "u2f.h"
  34. /* Counter which sync with a file */
  35. struct synced_counter {
  36. /* Emulated device counter */
  37. struct u2f_emu_vdev_counter vdev_counter;
  38. /* Private attributes */
  39. uint32_t value;
  40. FILE *fp;
  41. };
  42. static void counter_increment(struct u2f_emu_vdev_counter *vdev_counter)
  43. {
  44. struct synced_counter *counter = (struct synced_counter *)vdev_counter;
  45. ++counter->value;
  46. /* Write back */
  47. if (fseek(counter->fp, 0, SEEK_SET) == -1) {
  48. return;
  49. }
  50. fprintf(counter->fp, "%u\n", counter->value);
  51. }
  52. static uint32_t counter_read(struct u2f_emu_vdev_counter *vdev_counter)
  53. {
  54. struct synced_counter *counter = (struct synced_counter *)vdev_counter;
  55. return counter->value;
  56. }
  57. typedef struct U2FEmulatedState U2FEmulatedState;
  58. #define PENDING_OUT_NUM 32
  59. struct U2FEmulatedState {
  60. U2FKeyState base;
  61. /* U2F virtual emulated device */
  62. u2f_emu_vdev *vdev;
  63. QemuMutex vdev_mutex;
  64. /* Properties */
  65. char *dir;
  66. char *cert;
  67. char *privkey;
  68. char *entropy;
  69. char *counter;
  70. struct synced_counter synced_counter;
  71. /* Pending packets received from the guest */
  72. uint8_t pending_out[PENDING_OUT_NUM][U2FHID_PACKET_SIZE];
  73. uint8_t pending_out_start;
  74. uint8_t pending_out_end;
  75. uint8_t pending_out_num;
  76. QemuMutex pending_out_mutex;
  77. /* Emulation thread and sync */
  78. QemuCond key_cond;
  79. QemuMutex key_mutex;
  80. QemuThread key_thread;
  81. bool stop_thread;
  82. EventNotifier notifier;
  83. };
  84. #define TYPE_U2F_EMULATED "u2f-emulated"
  85. #define EMULATED_U2F_KEY(obj) \
  86. OBJECT_CHECK(U2FEmulatedState, (obj), TYPE_U2F_EMULATED)
  87. static void u2f_emulated_reset(U2FEmulatedState *key)
  88. {
  89. key->pending_out_start = 0;
  90. key->pending_out_end = 0;
  91. key->pending_out_num = 0;
  92. }
  93. static void u2f_pending_out_add(U2FEmulatedState *key,
  94. const uint8_t packet[U2FHID_PACKET_SIZE])
  95. {
  96. int index;
  97. if (key->pending_out_num >= PENDING_OUT_NUM) {
  98. return;
  99. }
  100. index = key->pending_out_end;
  101. key->pending_out_end = (index + 1) % PENDING_OUT_NUM;
  102. ++key->pending_out_num;
  103. memcpy(&key->pending_out[index], packet, U2FHID_PACKET_SIZE);
  104. }
  105. static uint8_t *u2f_pending_out_get(U2FEmulatedState *key)
  106. {
  107. int index;
  108. if (key->pending_out_num == 0) {
  109. return NULL;
  110. }
  111. index = key->pending_out_start;
  112. key->pending_out_start = (index + 1) % PENDING_OUT_NUM;
  113. --key->pending_out_num;
  114. return key->pending_out[index];
  115. }
  116. static void u2f_emulated_recv_from_guest(U2FKeyState *base,
  117. const uint8_t packet[U2FHID_PACKET_SIZE])
  118. {
  119. U2FEmulatedState *key = EMULATED_U2F_KEY(base);
  120. qemu_mutex_lock(&key->pending_out_mutex);
  121. u2f_pending_out_add(key, packet);
  122. qemu_mutex_unlock(&key->pending_out_mutex);
  123. qemu_mutex_lock(&key->key_mutex);
  124. qemu_cond_signal(&key->key_cond);
  125. qemu_mutex_unlock(&key->key_mutex);
  126. }
  127. static void *u2f_emulated_thread(void* arg)
  128. {
  129. U2FEmulatedState *key = arg;
  130. uint8_t packet[U2FHID_PACKET_SIZE];
  131. uint8_t *packet_out = NULL;
  132. while (true) {
  133. /* Wait signal */
  134. qemu_mutex_lock(&key->key_mutex);
  135. qemu_cond_wait(&key->key_cond, &key->key_mutex);
  136. qemu_mutex_unlock(&key->key_mutex);
  137. /* Exit thread check */
  138. if (key->stop_thread) {
  139. key->stop_thread = false;
  140. break;
  141. }
  142. qemu_mutex_lock(&key->pending_out_mutex);
  143. packet_out = u2f_pending_out_get(key);
  144. if (packet_out == NULL) {
  145. qemu_mutex_unlock(&key->pending_out_mutex);
  146. continue;
  147. }
  148. memcpy(packet, packet_out, U2FHID_PACKET_SIZE);
  149. qemu_mutex_unlock(&key->pending_out_mutex);
  150. qemu_mutex_lock(&key->vdev_mutex);
  151. u2f_emu_vdev_send(key->vdev, U2F_EMU_USB, packet,
  152. U2FHID_PACKET_SIZE);
  153. /* Notify response */
  154. if (u2f_emu_vdev_has_response(key->vdev, U2F_EMU_USB)) {
  155. event_notifier_set(&key->notifier);
  156. }
  157. qemu_mutex_unlock(&key->vdev_mutex);
  158. }
  159. return NULL;
  160. }
  161. static ssize_t u2f_emulated_read(const char *path, char *buffer,
  162. size_t buffer_len)
  163. {
  164. int fd;
  165. ssize_t ret;
  166. fd = qemu_open_old(path, O_RDONLY);
  167. if (fd < 0) {
  168. return -1;
  169. }
  170. ret = read(fd, buffer, buffer_len);
  171. close(fd);
  172. return ret;
  173. }
  174. static bool u2f_emulated_setup_counter(const char *path,
  175. struct synced_counter *counter)
  176. {
  177. int fd, ret;
  178. FILE *fp;
  179. fd = qemu_open_old(path, O_RDWR);
  180. if (fd < 0) {
  181. return false;
  182. }
  183. fp = fdopen(fd, "r+");
  184. if (fp == NULL) {
  185. close(fd);
  186. return false;
  187. }
  188. ret = fscanf(fp, "%u", &counter->value);
  189. if (ret == EOF) {
  190. fclose(fp);
  191. return false;
  192. }
  193. counter->fp = fp;
  194. counter->vdev_counter.counter_increment = counter_increment;
  195. counter->vdev_counter.counter_read = counter_read;
  196. return true;
  197. }
  198. static u2f_emu_rc u2f_emulated_setup_vdev_manualy(U2FEmulatedState *key)
  199. {
  200. ssize_t ret;
  201. char cert_pem[4096], privkey_pem[2048];
  202. struct u2f_emu_vdev_setup setup_info;
  203. /* Certificate */
  204. ret = u2f_emulated_read(key->cert, cert_pem, sizeof(cert_pem));
  205. if (ret < 0) {
  206. return -1;
  207. }
  208. /* Private key */
  209. ret = u2f_emulated_read(key->privkey, privkey_pem, sizeof(privkey_pem));
  210. if (ret < 0) {
  211. return -1;
  212. }
  213. /* Entropy */
  214. ret = u2f_emulated_read(key->entropy, (char *)&setup_info.entropy,
  215. sizeof(setup_info.entropy));
  216. if (ret < 0) {
  217. return -1;
  218. }
  219. /* Counter */
  220. if (!u2f_emulated_setup_counter(key->counter, &key->synced_counter)) {
  221. return -1;
  222. }
  223. /* Setup */
  224. setup_info.certificate = cert_pem;
  225. setup_info.private_key = privkey_pem;
  226. setup_info.counter = (struct u2f_emu_vdev_counter *)&key->synced_counter;
  227. return u2f_emu_vdev_new(&key->vdev, &setup_info);
  228. }
  229. static void u2f_emulated_event_handler(EventNotifier *notifier)
  230. {
  231. U2FEmulatedState *key = container_of(notifier, U2FEmulatedState, notifier);
  232. size_t packet_size;
  233. uint8_t *packet_in = NULL;
  234. event_notifier_test_and_clear(&key->notifier);
  235. qemu_mutex_lock(&key->vdev_mutex);
  236. while (u2f_emu_vdev_has_response(key->vdev, U2F_EMU_USB)) {
  237. packet_size = u2f_emu_vdev_get_response(key->vdev, U2F_EMU_USB,
  238. &packet_in);
  239. if (packet_size == U2FHID_PACKET_SIZE) {
  240. u2f_send_to_guest(&key->base, packet_in);
  241. }
  242. u2f_emu_vdev_free_response(packet_in);
  243. }
  244. qemu_mutex_unlock(&key->vdev_mutex);
  245. }
  246. static void u2f_emulated_realize(U2FKeyState *base, Error **errp)
  247. {
  248. U2FEmulatedState *key = EMULATED_U2F_KEY(base);
  249. u2f_emu_rc rc;
  250. if (key->cert != NULL || key->privkey != NULL || key->entropy != NULL
  251. || key->counter != NULL) {
  252. if (key->cert != NULL && key->privkey != NULL
  253. && key->entropy != NULL && key->counter != NULL) {
  254. rc = u2f_emulated_setup_vdev_manualy(key);
  255. } else {
  256. error_setg(errp, "%s: cert, priv, entropy and counter "
  257. "parameters must be provided to manually configure "
  258. "the emulated device", TYPE_U2F_EMULATED);
  259. return;
  260. }
  261. } else if (key->dir != NULL) {
  262. rc = u2f_emu_vdev_new_from_dir(&key->vdev, key->dir);
  263. } else {
  264. rc = u2f_emu_vdev_new_ephemeral(&key->vdev);
  265. }
  266. if (rc != U2F_EMU_OK) {
  267. error_setg(errp, "%s: Failed to setup the key", TYPE_U2F_EMULATED);
  268. return;
  269. }
  270. if (event_notifier_init(&key->notifier, false) < 0) {
  271. error_setg(errp, "%s: Failed to initialize notifier",
  272. TYPE_U2F_EMULATED);
  273. return;
  274. }
  275. /* Notifier */
  276. event_notifier_set_handler(&key->notifier, u2f_emulated_event_handler);
  277. /* Synchronization */
  278. qemu_cond_init(&key->key_cond);
  279. qemu_mutex_init(&key->vdev_mutex);
  280. qemu_mutex_init(&key->pending_out_mutex);
  281. qemu_mutex_init(&key->key_mutex);
  282. u2f_emulated_reset(key);
  283. /* Thread */
  284. key->stop_thread = false;
  285. qemu_thread_create(&key->key_thread, "u2f-key", u2f_emulated_thread,
  286. key, QEMU_THREAD_JOINABLE);
  287. }
  288. static void u2f_emulated_unrealize(U2FKeyState *base)
  289. {
  290. U2FEmulatedState *key = EMULATED_U2F_KEY(base);
  291. /* Thread */
  292. key->stop_thread = true;
  293. qemu_cond_signal(&key->key_cond);
  294. qemu_thread_join(&key->key_thread);
  295. /* Notifier */
  296. event_notifier_set_handler(&key->notifier, NULL);
  297. event_notifier_cleanup(&key->notifier);
  298. /* Synchronization */
  299. qemu_cond_destroy(&key->key_cond);
  300. qemu_mutex_destroy(&key->vdev_mutex);
  301. qemu_mutex_destroy(&key->key_mutex);
  302. qemu_mutex_destroy(&key->pending_out_mutex);
  303. /* Vdev */
  304. u2f_emu_vdev_free(key->vdev);
  305. if (key->synced_counter.fp != NULL) {
  306. fclose(key->synced_counter.fp);
  307. }
  308. }
  309. static Property u2f_emulated_properties[] = {
  310. DEFINE_PROP_STRING("dir", U2FEmulatedState, dir),
  311. DEFINE_PROP_STRING("cert", U2FEmulatedState, cert),
  312. DEFINE_PROP_STRING("privkey", U2FEmulatedState, privkey),
  313. DEFINE_PROP_STRING("entropy", U2FEmulatedState, entropy),
  314. DEFINE_PROP_STRING("counter", U2FEmulatedState, counter),
  315. DEFINE_PROP_END_OF_LIST(),
  316. };
  317. static void u2f_emulated_class_init(ObjectClass *klass, void *data)
  318. {
  319. DeviceClass *dc = DEVICE_CLASS(klass);
  320. U2FKeyClass *kc = U2F_KEY_CLASS(klass);
  321. kc->realize = u2f_emulated_realize;
  322. kc->unrealize = u2f_emulated_unrealize;
  323. kc->recv_from_guest = u2f_emulated_recv_from_guest;
  324. dc->desc = "QEMU U2F emulated key";
  325. device_class_set_props(dc, u2f_emulated_properties);
  326. }
  327. static const TypeInfo u2f_key_emulated_info = {
  328. .name = TYPE_U2F_EMULATED,
  329. .parent = TYPE_U2F_KEY,
  330. .instance_size = sizeof(U2FEmulatedState),
  331. .class_init = u2f_emulated_class_init
  332. };
  333. static void u2f_key_emulated_register_types(void)
  334. {
  335. type_register_static(&u2f_key_emulated_info);
  336. }
  337. type_init(u2f_key_emulated_register_types)