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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <errno.h>
  27. #ifdef HAVE_GNUTLS
  28. using namespace rdr;
  29. enum { DEFAULT_BUF_SIZE = 16384 };
  30. ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
  31. {
  32. TLSInStream* self= (TLSInStream*) str;
  33. InStream *in = self->in;
  34. try {
  35. if (!in->check(1, 1, false)) {
  36. gnutls_transport_set_errno(self->session, EAGAIN);
  37. return -1;
  38. }
  39. if (in->getend() - in->getptr() < (ptrdiff_t)size)
  40. size = in->getend() - in->getptr();
  41. in->readBytes(data, size);
  42. } catch (Exception& e) {
  43. gnutls_transport_set_errno(self->session, EINVAL);
  44. return -1;
  45. }
  46. return size;
  47. }
  48. TLSInStream::TLSInStream(InStream* _in, gnutls_session_t _session)
  49. : session(_session), in(_in), bufSize(DEFAULT_BUF_SIZE), offset(0)
  50. {
  51. gnutls_transport_ptr_t recv, send;
  52. ptr = end = start = new U8[bufSize];
  53. gnutls_transport_set_pull_function(session, pull);
  54. gnutls_transport_get_ptr2(session, &recv, &send);
  55. gnutls_transport_set_ptr2(session, this, send);
  56. }
  57. TLSInStream::~TLSInStream()
  58. {
  59. gnutls_transport_set_pull_function(session, NULL);
  60. delete[] start;
  61. }
  62. size_t TLSInStream::pos()
  63. {
  64. return offset + ptr - start;
  65. }
  66. size_t TLSInStream::overrun(size_t itemSize, size_t nItems, bool wait)
  67. {
  68. if (itemSize > bufSize)
  69. throw Exception("TLSInStream overrun: max itemSize exceeded");
  70. if (end - ptr != 0)
  71. memmove(start, ptr, end - ptr);
  72. offset += ptr - start;
  73. end -= ptr - start;
  74. ptr = start;
  75. while (end < start + itemSize) {
  76. size_t n = readTLS((U8*) end, start + bufSize - end, wait);
  77. if (!wait && n == 0)
  78. return 0;
  79. end += n;
  80. }
  81. if (itemSize * nItems > (size_t)(end - ptr))
  82. nItems = (end - ptr) / itemSize;
  83. return nItems;
  84. }
  85. size_t TLSInStream::readTLS(U8* buf, size_t len, bool wait)
  86. {
  87. int n;
  88. n = in->check(1, 1, wait);
  89. if (n == 0)
  90. return 0;
  91. n = gnutls_record_recv(session, (void *) buf, len);
  92. if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
  93. return 0;
  94. if (n < 0) throw TLSException("readTLS", n);
  95. return n;
  96. }
  97. #endif