Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 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 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <rdr/Exception.h> |
| 26 | #include <rdr/TLSException.h> |
| 27 | #include <rdr/TLSInStream.h> |
Pierre Ossman | 2137f4f | 2012-07-03 14:52:26 +0000 | [diff] [blame] | 28 | #include <rdr/TLSErrno.h> |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | |
| 31 | #ifdef HAVE_GNUTLS |
| 32 | using namespace rdr; |
| 33 | |
| 34 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 35 | |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 36 | ssize_t TLSInStream::pull(gnutls_transport_ptr str, void* data, size_t size) |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 37 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 38 | TLSInStream* self= (TLSInStream*) str; |
| 39 | InStream *in = self->in; |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 40 | |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 41 | try { |
| 42 | if (!in->check(1, 1, false)) { |
Pierre Ossman | 2137f4f | 2012-07-03 14:52:26 +0000 | [diff] [blame] | 43 | gnutls_errno_helper(self->session, EAGAIN); |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | if (in->getend() - in->getptr() < size) |
| 48 | size = in->getend() - in->getptr(); |
| 49 | |
| 50 | in->readBytes(data, size); |
| 51 | |
| 52 | } catch (Exception& e) { |
Pierre Ossman | 2137f4f | 2012-07-03 14:52:26 +0000 | [diff] [blame] | 53 | gnutls_errno_helper(self->session, EINVAL); |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 57 | return size; |
| 58 | } |
| 59 | |
| 60 | TLSInStream::TLSInStream(InStream* _in, gnutls_session _session) |
| 61 | : session(_session), in(_in), bufSize(DEFAULT_BUF_SIZE), offset(0) |
| 62 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 63 | gnutls_transport_ptr recv, send; |
| 64 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 65 | ptr = end = start = new U8[bufSize]; |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 66 | |
| 67 | gnutls_transport_set_pull_function(session, pull); |
| 68 | gnutls_transport_get_ptr2(session, &recv, &send); |
| 69 | gnutls_transport_set_ptr2(session, this, send); |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TLSInStream::~TLSInStream() |
| 73 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 74 | gnutls_transport_set_pull_function(session, NULL); |
| 75 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 76 | delete[] start; |
| 77 | } |
| 78 | |
| 79 | int TLSInStream::pos() |
| 80 | { |
| 81 | return offset + ptr - start; |
| 82 | } |
| 83 | |
| 84 | int TLSInStream::overrun(int itemSize, int nItems, bool wait) |
| 85 | { |
| 86 | if (itemSize > bufSize) |
| 87 | throw Exception("TLSInStream overrun: max itemSize exceeded"); |
| 88 | |
| 89 | if (end - ptr != 0) |
| 90 | memmove(start, ptr, end - ptr); |
| 91 | |
| 92 | offset += ptr - start; |
| 93 | end -= ptr - start; |
| 94 | ptr = start; |
| 95 | |
| 96 | while (end < start + itemSize) { |
| 97 | int n = readTLS((U8*) end, start + bufSize - end, wait); |
| 98 | if (!wait && n == 0) |
| 99 | return 0; |
| 100 | end += n; |
| 101 | } |
| 102 | |
| 103 | if (itemSize * nItems > end - ptr) |
| 104 | nItems = (end - ptr) / itemSize; |
| 105 | |
| 106 | return nItems; |
| 107 | } |
| 108 | |
| 109 | int TLSInStream::readTLS(U8* buf, int len, bool wait) |
| 110 | { |
| 111 | int n; |
| 112 | |
| 113 | n = in->check(1, 1, wait); |
| 114 | if (n == 0) |
| 115 | return 0; |
| 116 | |
| 117 | n = gnutls_record_recv(session, (void *) buf, len); |
| 118 | if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN) |
| 119 | return 0; |
| 120 | |
| 121 | if (n < 0) throw TLSException("readTLS", n); |
| 122 | |
| 123 | return n; |
| 124 | } |
| 125 | |
| 126 | #endif |