channel-tls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * QEMU I/O channels TLS driver
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/module.h"
  23. #include "io/channel-tls.h"
  24. #include "trace.h"
  25. static ssize_t qio_channel_tls_write_handler(const char *buf,
  26. size_t len,
  27. void *opaque)
  28. {
  29. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
  30. ssize_t ret;
  31. ret = qio_channel_write(tioc->master, buf, len, NULL);
  32. if (ret == QIO_CHANNEL_ERR_BLOCK) {
  33. errno = EAGAIN;
  34. return -1;
  35. } else if (ret < 0) {
  36. errno = EIO;
  37. return -1;
  38. }
  39. return ret;
  40. }
  41. static ssize_t qio_channel_tls_read_handler(char *buf,
  42. size_t len,
  43. void *opaque)
  44. {
  45. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
  46. ssize_t ret;
  47. ret = qio_channel_read(tioc->master, buf, len, NULL);
  48. if (ret == QIO_CHANNEL_ERR_BLOCK) {
  49. errno = EAGAIN;
  50. return -1;
  51. } else if (ret < 0) {
  52. errno = EIO;
  53. return -1;
  54. }
  55. return ret;
  56. }
  57. QIOChannelTLS *
  58. qio_channel_tls_new_server(QIOChannel *master,
  59. QCryptoTLSCreds *creds,
  60. const char *aclname,
  61. Error **errp)
  62. {
  63. QIOChannelTLS *ioc;
  64. ioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
  65. ioc->master = master;
  66. object_ref(OBJECT(master));
  67. ioc->session = qcrypto_tls_session_new(
  68. creds,
  69. NULL,
  70. aclname,
  71. QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
  72. errp);
  73. if (!ioc->session) {
  74. goto error;
  75. }
  76. qcrypto_tls_session_set_callbacks(
  77. ioc->session,
  78. qio_channel_tls_write_handler,
  79. qio_channel_tls_read_handler,
  80. ioc);
  81. trace_qio_channel_tls_new_server(ioc, master, creds, aclname);
  82. return ioc;
  83. error:
  84. object_unref(OBJECT(ioc));
  85. return NULL;
  86. }
  87. QIOChannelTLS *
  88. qio_channel_tls_new_client(QIOChannel *master,
  89. QCryptoTLSCreds *creds,
  90. const char *hostname,
  91. Error **errp)
  92. {
  93. QIOChannelTLS *tioc;
  94. QIOChannel *ioc;
  95. tioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
  96. ioc = QIO_CHANNEL(tioc);
  97. tioc->master = master;
  98. if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) {
  99. qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
  100. }
  101. object_ref(OBJECT(master));
  102. tioc->session = qcrypto_tls_session_new(
  103. creds,
  104. hostname,
  105. NULL,
  106. QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
  107. errp);
  108. if (!tioc->session) {
  109. goto error;
  110. }
  111. qcrypto_tls_session_set_callbacks(
  112. tioc->session,
  113. qio_channel_tls_write_handler,
  114. qio_channel_tls_read_handler,
  115. tioc);
  116. trace_qio_channel_tls_new_client(tioc, master, creds, hostname);
  117. return tioc;
  118. error:
  119. object_unref(OBJECT(tioc));
  120. return NULL;
  121. }
  122. struct QIOChannelTLSData {
  123. QIOTask *task;
  124. GMainContext *context;
  125. };
  126. typedef struct QIOChannelTLSData QIOChannelTLSData;
  127. static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
  128. GIOCondition condition,
  129. gpointer user_data);
  130. static void qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
  131. QIOTask *task,
  132. GMainContext *context)
  133. {
  134. Error *err = NULL;
  135. QCryptoTLSSessionHandshakeStatus status;
  136. if (qcrypto_tls_session_handshake(ioc->session, &err) < 0) {
  137. trace_qio_channel_tls_handshake_fail(ioc);
  138. qio_task_set_error(task, err);
  139. qio_task_complete(task);
  140. return;
  141. }
  142. status = qcrypto_tls_session_get_handshake_status(ioc->session);
  143. if (status == QCRYPTO_TLS_HANDSHAKE_COMPLETE) {
  144. trace_qio_channel_tls_handshake_complete(ioc);
  145. if (qcrypto_tls_session_check_credentials(ioc->session,
  146. &err) < 0) {
  147. trace_qio_channel_tls_credentials_deny(ioc);
  148. qio_task_set_error(task, err);
  149. } else {
  150. trace_qio_channel_tls_credentials_allow(ioc);
  151. }
  152. qio_task_complete(task);
  153. } else {
  154. GIOCondition condition;
  155. QIOChannelTLSData *data = g_new0(typeof(*data), 1);
  156. data->task = task;
  157. data->context = context;
  158. if (context) {
  159. g_main_context_ref(context);
  160. }
  161. if (status == QCRYPTO_TLS_HANDSHAKE_SENDING) {
  162. condition = G_IO_OUT;
  163. } else {
  164. condition = G_IO_IN;
  165. }
  166. trace_qio_channel_tls_handshake_pending(ioc, status);
  167. qio_channel_add_watch_full(ioc->master,
  168. condition,
  169. qio_channel_tls_handshake_io,
  170. data,
  171. NULL,
  172. context);
  173. }
  174. }
  175. static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
  176. GIOCondition condition,
  177. gpointer user_data)
  178. {
  179. QIOChannelTLSData *data = user_data;
  180. QIOTask *task = data->task;
  181. GMainContext *context = data->context;
  182. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
  183. qio_task_get_source(task));
  184. g_free(data);
  185. qio_channel_tls_handshake_task(tioc, task, context);
  186. if (context) {
  187. g_main_context_unref(context);
  188. }
  189. return FALSE;
  190. }
  191. void qio_channel_tls_handshake(QIOChannelTLS *ioc,
  192. QIOTaskFunc func,
  193. gpointer opaque,
  194. GDestroyNotify destroy,
  195. GMainContext *context)
  196. {
  197. QIOTask *task;
  198. task = qio_task_new(OBJECT(ioc),
  199. func, opaque, destroy);
  200. trace_qio_channel_tls_handshake_start(ioc);
  201. qio_channel_tls_handshake_task(ioc, task, context);
  202. }
  203. static void qio_channel_tls_init(Object *obj G_GNUC_UNUSED)
  204. {
  205. }
  206. static void qio_channel_tls_finalize(Object *obj)
  207. {
  208. QIOChannelTLS *ioc = QIO_CHANNEL_TLS(obj);
  209. object_unref(OBJECT(ioc->master));
  210. qcrypto_tls_session_free(ioc->session);
  211. }
  212. static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
  213. const struct iovec *iov,
  214. size_t niov,
  215. int **fds,
  216. size_t *nfds,
  217. Error **errp)
  218. {
  219. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  220. size_t i;
  221. ssize_t got = 0;
  222. for (i = 0 ; i < niov ; i++) {
  223. ssize_t ret = qcrypto_tls_session_read(tioc->session,
  224. iov[i].iov_base,
  225. iov[i].iov_len);
  226. if (ret < 0) {
  227. if (errno == EAGAIN) {
  228. if (got) {
  229. return got;
  230. } else {
  231. return QIO_CHANNEL_ERR_BLOCK;
  232. }
  233. } else if (errno == ECONNABORTED &&
  234. (tioc->shutdown & QIO_CHANNEL_SHUTDOWN_READ)) {
  235. return 0;
  236. }
  237. error_setg_errno(errp, errno,
  238. "Cannot read from TLS channel");
  239. return -1;
  240. }
  241. got += ret;
  242. if (ret < iov[i].iov_len) {
  243. break;
  244. }
  245. }
  246. return got;
  247. }
  248. static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
  249. const struct iovec *iov,
  250. size_t niov,
  251. int *fds,
  252. size_t nfds,
  253. Error **errp)
  254. {
  255. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  256. size_t i;
  257. ssize_t done = 0;
  258. for (i = 0 ; i < niov ; i++) {
  259. ssize_t ret = qcrypto_tls_session_write(tioc->session,
  260. iov[i].iov_base,
  261. iov[i].iov_len);
  262. if (ret <= 0) {
  263. if (errno == EAGAIN) {
  264. if (done) {
  265. return done;
  266. } else {
  267. return QIO_CHANNEL_ERR_BLOCK;
  268. }
  269. }
  270. error_setg_errno(errp, errno,
  271. "Cannot write to TLS channel");
  272. return -1;
  273. }
  274. done += ret;
  275. if (ret < iov[i].iov_len) {
  276. break;
  277. }
  278. }
  279. return done;
  280. }
  281. static int qio_channel_tls_set_blocking(QIOChannel *ioc,
  282. bool enabled,
  283. Error **errp)
  284. {
  285. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  286. return qio_channel_set_blocking(tioc->master, enabled, errp);
  287. }
  288. static void qio_channel_tls_set_delay(QIOChannel *ioc,
  289. bool enabled)
  290. {
  291. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  292. qio_channel_set_delay(tioc->master, enabled);
  293. }
  294. static void qio_channel_tls_set_cork(QIOChannel *ioc,
  295. bool enabled)
  296. {
  297. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  298. qio_channel_set_cork(tioc->master, enabled);
  299. }
  300. static int qio_channel_tls_shutdown(QIOChannel *ioc,
  301. QIOChannelShutdown how,
  302. Error **errp)
  303. {
  304. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  305. tioc->shutdown |= how;
  306. return qio_channel_shutdown(tioc->master, how, errp);
  307. }
  308. static int qio_channel_tls_close(QIOChannel *ioc,
  309. Error **errp)
  310. {
  311. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  312. return qio_channel_close(tioc->master, errp);
  313. }
  314. static void qio_channel_tls_set_aio_fd_handler(QIOChannel *ioc,
  315. AioContext *ctx,
  316. IOHandler *io_read,
  317. IOHandler *io_write,
  318. void *opaque)
  319. {
  320. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  321. qio_channel_set_aio_fd_handler(tioc->master, ctx, io_read, io_write, opaque);
  322. }
  323. static GSource *qio_channel_tls_create_watch(QIOChannel *ioc,
  324. GIOCondition condition)
  325. {
  326. QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
  327. return qio_channel_create_watch(tioc->master, condition);
  328. }
  329. QCryptoTLSSession *
  330. qio_channel_tls_get_session(QIOChannelTLS *ioc)
  331. {
  332. return ioc->session;
  333. }
  334. static void qio_channel_tls_class_init(ObjectClass *klass,
  335. void *class_data G_GNUC_UNUSED)
  336. {
  337. QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
  338. ioc_klass->io_writev = qio_channel_tls_writev;
  339. ioc_klass->io_readv = qio_channel_tls_readv;
  340. ioc_klass->io_set_blocking = qio_channel_tls_set_blocking;
  341. ioc_klass->io_set_delay = qio_channel_tls_set_delay;
  342. ioc_klass->io_set_cork = qio_channel_tls_set_cork;
  343. ioc_klass->io_close = qio_channel_tls_close;
  344. ioc_klass->io_shutdown = qio_channel_tls_shutdown;
  345. ioc_klass->io_create_watch = qio_channel_tls_create_watch;
  346. ioc_klass->io_set_aio_fd_handler = qio_channel_tls_set_aio_fd_handler;
  347. }
  348. static const TypeInfo qio_channel_tls_info = {
  349. .parent = TYPE_QIO_CHANNEL,
  350. .name = TYPE_QIO_CHANNEL_TLS,
  351. .instance_size = sizeof(QIOChannelTLS),
  352. .instance_init = qio_channel_tls_init,
  353. .instance_finalize = qio_channel_tls_finalize,
  354. .class_init = qio_channel_tls_class_init,
  355. };
  356. static void qio_channel_tls_register_types(void)
  357. {
  358. type_register_static(&qio_channel_tls_info);
  359. }
  360. type_init(qio_channel_tls_register_types);