You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TLSOutStream.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2005 Martin Koegler
  3. * Copyright (C) 2010 TigerVNC Team
  4. * Copyright (C) 2012-2021 Pierre Ossman for Cendio AB
  5. *
  6. * This 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. * This software 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 software; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <rdr/Exception.h>
  25. #include <rdr/TLSException.h>
  26. #include <rdr/TLSOutStream.h>
  27. #include <rfb/LogWriter.h>
  28. #include <errno.h>
  29. #ifdef HAVE_GNUTLS
  30. using namespace rdr;
  31. static rfb::LogWriter vlog("TLSOutStream");
  32. ssize_t TLSOutStream::push(gnutls_transport_ptr_t str, const void* data,
  33. size_t size)
  34. {
  35. TLSOutStream* self= (TLSOutStream*) str;
  36. OutStream *out = self->out;
  37. delete self->saved_exception;
  38. self->saved_exception = NULL;
  39. try {
  40. out->writeBytes((const uint8_t*)data, size);
  41. out->flush();
  42. } catch (SystemException &e) {
  43. vlog.error("Failure sending TLS data: %s", e.str());
  44. gnutls_transport_set_errno(self->session, e.err);
  45. self->saved_exception = new SystemException(e);
  46. return -1;
  47. } catch (Exception& e) {
  48. vlog.error("Failure sending TLS data: %s", e.str());
  49. gnutls_transport_set_errno(self->session, EINVAL);
  50. self->saved_exception = new Exception(e);
  51. return -1;
  52. }
  53. return size;
  54. }
  55. TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session_t _session)
  56. : session(_session), out(_out), saved_exception(NULL)
  57. {
  58. gnutls_transport_ptr_t recv, send;
  59. gnutls_transport_set_push_function(session, push);
  60. gnutls_transport_get_ptr2(session, &recv, &send);
  61. gnutls_transport_set_ptr2(session, recv, this);
  62. }
  63. TLSOutStream::~TLSOutStream()
  64. {
  65. #if 0
  66. try {
  67. // flush();
  68. } catch (Exception&) {
  69. }
  70. #endif
  71. gnutls_transport_set_push_function(session, NULL);
  72. delete saved_exception;
  73. }
  74. void TLSOutStream::flush()
  75. {
  76. BufferedOutStream::flush();
  77. out->flush();
  78. }
  79. void TLSOutStream::cork(bool enable)
  80. {
  81. BufferedOutStream::cork(enable);
  82. out->cork(enable);
  83. }
  84. bool TLSOutStream::flushBuffer()
  85. {
  86. while (sentUpTo < ptr) {
  87. size_t n = writeTLS(sentUpTo, ptr - sentUpTo);
  88. sentUpTo += n;
  89. }
  90. return true;
  91. }
  92. size_t TLSOutStream::writeTLS(const uint8_t* data, size_t length)
  93. {
  94. int n;
  95. n = gnutls_record_send(session, data, length);
  96. if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
  97. return 0;
  98. if (n == GNUTLS_E_PUSH_ERROR)
  99. throw *saved_exception;
  100. if (n < 0)
  101. throw TLSException("writeTLS", n);
  102. return n;
  103. }
  104. #endif