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.

TLSInStream.cxx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/TLSInStream.h>
  27. #include <rfb/LogWriter.h>
  28. #include <errno.h>
  29. #ifdef HAVE_GNUTLS
  30. using namespace rdr;
  31. static rfb::LogWriter vlog("TLSInStream");
  32. ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
  33. {
  34. TLSInStream* self= (TLSInStream*) str;
  35. InStream *in = self->in;
  36. self->streamEmpty = false;
  37. delete self->saved_exception;
  38. self->saved_exception = NULL;
  39. try {
  40. if (!in->hasData(1)) {
  41. self->streamEmpty = true;
  42. gnutls_transport_set_errno(self->session, EAGAIN);
  43. return -1;
  44. }
  45. if (in->avail() < size)
  46. size = in->avail();
  47. in->readBytes((uint8_t*)data, size);
  48. } catch (EndOfStream&) {
  49. return 0;
  50. } catch (SystemException &e) {
  51. vlog.error("Failure reading TLS data: %s", e.str());
  52. gnutls_transport_set_errno(self->session, e.err);
  53. self->saved_exception = new SystemException(e);
  54. return -1;
  55. } catch (Exception& e) {
  56. vlog.error("Failure reading TLS data: %s", e.str());
  57. gnutls_transport_set_errno(self->session, EINVAL);
  58. self->saved_exception = new Exception(e);
  59. return -1;
  60. }
  61. return size;
  62. }
  63. TLSInStream::TLSInStream(InStream* _in, gnutls_session_t _session)
  64. : session(_session), in(_in), saved_exception(NULL)
  65. {
  66. gnutls_transport_ptr_t recv, send;
  67. gnutls_transport_set_pull_function(session, pull);
  68. gnutls_transport_get_ptr2(session, &recv, &send);
  69. gnutls_transport_set_ptr2(session, this, send);
  70. }
  71. TLSInStream::~TLSInStream()
  72. {
  73. gnutls_transport_set_pull_function(session, NULL);
  74. delete saved_exception;
  75. }
  76. bool TLSInStream::fillBuffer()
  77. {
  78. size_t n = readTLS((uint8_t*) end, availSpace());
  79. if (n == 0)
  80. return false;
  81. end += n;
  82. return true;
  83. }
  84. size_t TLSInStream::readTLS(uint8_t* buf, size_t len)
  85. {
  86. int n;
  87. while (true) {
  88. streamEmpty = false;
  89. n = gnutls_record_recv(session, (void *) buf, len);
  90. if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN) {
  91. // GnuTLS returns GNUTLS_E_AGAIN for a bunch of other scenarios
  92. // other than the pull function returning EAGAIN, so we have to
  93. // double check that the underlying stream really is empty
  94. if (!streamEmpty)
  95. continue;
  96. else
  97. return 0;
  98. }
  99. break;
  100. };
  101. if (n == GNUTLS_E_PULL_ERROR)
  102. throw *saved_exception;
  103. if (n < 0)
  104. throw TLSException("readTLS", n);
  105. if (n == 0)
  106. throw EndOfStream();
  107. return n;
  108. }
  109. #endif