blob: 4d2c9ecb02289bfa4fe305352b70d99cb42de069 [file] [log] [blame]
Adam Tkac35e6d4c2010-04-23 14:12:18 +00001/* 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 Ossman2137f4f2012-07-03 14:52:26 +000028#include <rdr/TLSErrno.h>
Adam Tkac35e6d4c2010-04-23 14:12:18 +000029#include <errno.h>
30
31#ifdef HAVE_GNUTLS
32using namespace rdr;
33
34enum { DEFAULT_BUF_SIZE = 16384 };
35
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000036ssize_t TLSInStream::pull(gnutls_transport_ptr str, void* data, size_t size)
Adam Tkac35e6d4c2010-04-23 14:12:18 +000037{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000038 TLSInStream* self= (TLSInStream*) str;
39 InStream *in = self->in;
Adam Tkac35e6d4c2010-04-23 14:12:18 +000040
Adam Tkacfab093c2010-08-25 13:52:49 +000041 try {
42 if (!in->check(1, 1, false)) {
Pierre Ossman2137f4f2012-07-03 14:52:26 +000043 gnutls_errno_helper(self->session, EAGAIN);
Adam Tkacfab093c2010-08-25 13:52:49 +000044 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 Ossman2137f4f2012-07-03 14:52:26 +000053 gnutls_errno_helper(self->session, EINVAL);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000054 return -1;
55 }
56
Adam Tkac35e6d4c2010-04-23 14:12:18 +000057 return size;
58}
59
60TLSInStream::TLSInStream(InStream* _in, gnutls_session _session)
61 : session(_session), in(_in), bufSize(DEFAULT_BUF_SIZE), offset(0)
62{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000063 gnutls_transport_ptr recv, send;
64
Adam Tkac35e6d4c2010-04-23 14:12:18 +000065 ptr = end = start = new U8[bufSize];
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000066
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 Tkac35e6d4c2010-04-23 14:12:18 +000070}
71
72TLSInStream::~TLSInStream()
73{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000074 gnutls_transport_set_pull_function(session, NULL);
75
Adam Tkac35e6d4c2010-04-23 14:12:18 +000076 delete[] start;
77}
78
79int TLSInStream::pos()
80{
81 return offset + ptr - start;
82}
83
84int 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
109int 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