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

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