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 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2005 Martin Koegler
  3. * Copyright (C) 2010 TigerVNC Team
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <rdr/Exception.h>
  24. #include <rdr/TLSException.h>
  25. #include <rdr/TLSInStream.h>
  26. #include <rfb/LogWriter.h>
  27. #include <errno.h>
  28. #ifdef HAVE_GNUTLS
  29. using namespace rdr;
  30. static rfb::LogWriter vlog("TLSInStream");
  31. ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
  32. {
  33. TLSInStream* self= (TLSInStream*) str;
  34. InStream *in = self->in;
  35. try {
  36. if (!in->hasData(1)) {
  37. gnutls_transport_set_errno(self->session, EAGAIN);
  38. return -1;
  39. }
  40. if (in->avail() < size)
  41. size = in->avail();
  42. in->readBytes(data, size);
  43. } catch (EndOfStream&) {
  44. return 0;
  45. } catch (Exception& e) {
  46. vlog.error("Failure reading TLS data: %s", e.str());
  47. gnutls_transport_set_errno(self->session, EINVAL);
  48. return -1;
  49. }
  50. return size;
  51. }
  52. TLSInStream::TLSInStream(InStream* _in, gnutls_session_t _session)
  53. : session(_session), in(_in)
  54. {
  55. gnutls_transport_ptr_t recv, send;
  56. gnutls_transport_set_pull_function(session, pull);
  57. gnutls_transport_get_ptr2(session, &recv, &send);
  58. gnutls_transport_set_ptr2(session, this, send);
  59. }
  60. TLSInStream::~TLSInStream()
  61. {
  62. gnutls_transport_set_pull_function(session, NULL);
  63. }
  64. bool TLSInStream::fillBuffer(size_t maxSize)
  65. {
  66. size_t n = readTLS((U8*) end, maxSize);
  67. if (n == 0)
  68. return false;
  69. end += n;
  70. return true;
  71. }
  72. size_t TLSInStream::readTLS(U8* buf, size_t len)
  73. {
  74. int n;
  75. if (gnutls_record_check_pending(session) == 0) {
  76. if (!in->hasData(1))
  77. return 0;
  78. }
  79. n = gnutls_record_recv(session, (void *) buf, len);
  80. if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
  81. return 0;
  82. if (n < 0) throw TLSException("readTLS", n);
  83. return n;
  84. }
  85. #endif