blob: 77b16729b7404de79571cd3b98f1aaecc5fda74a [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>
28#include <errno.h>
29
30#ifdef HAVE_GNUTLS
31using namespace rdr;
32
33enum { DEFAULT_BUF_SIZE = 16384 };
34
Pierre Ossman88c24ed2015-01-29 13:12:22 +010035ssize_t TLSInStream::pull(gnutls_transport_ptr_t str, void* data, size_t size)
Adam Tkac35e6d4c2010-04-23 14:12:18 +000036{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000037 TLSInStream* self= (TLSInStream*) str;
38 InStream *in = self->in;
Adam Tkac35e6d4c2010-04-23 14:12:18 +000039
Adam Tkacfab093c2010-08-25 13:52:49 +000040 try {
41 if (!in->check(1, 1, false)) {
Pierre Ossman88c24ed2015-01-29 13:12:22 +010042 gnutls_transport_set_errno(self->session, EAGAIN);
Adam Tkacfab093c2010-08-25 13:52:49 +000043 return -1;
44 }
45
Pierre Ossman5c23b9e2015-03-03 16:26:03 +010046 if (in->getend() - in->getptr() < (ptrdiff_t)size)
Adam Tkacfab093c2010-08-25 13:52:49 +000047 size = in->getend() - in->getptr();
48
49 in->readBytes(data, size);
50
51 } catch (Exception& e) {
Pierre Ossman88c24ed2015-01-29 13:12:22 +010052 gnutls_transport_set_errno(self->session, EINVAL);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000053 return -1;
54 }
55
Adam Tkac35e6d4c2010-04-23 14:12:18 +000056 return size;
57}
58
Pierre Ossman88c24ed2015-01-29 13:12:22 +010059TLSInStream::TLSInStream(InStream* _in, gnutls_session_t _session)
Adam Tkac35e6d4c2010-04-23 14:12:18 +000060 : session(_session), in(_in), bufSize(DEFAULT_BUF_SIZE), offset(0)
61{
Pierre Ossman88c24ed2015-01-29 13:12:22 +010062 gnutls_transport_ptr_t recv, send;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000063
Adam Tkac35e6d4c2010-04-23 14:12:18 +000064 ptr = end = start = new U8[bufSize];
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000065
66 gnutls_transport_set_pull_function(session, pull);
67 gnutls_transport_get_ptr2(session, &recv, &send);
68 gnutls_transport_set_ptr2(session, this, send);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000069}
70
71TLSInStream::~TLSInStream()
72{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000073 gnutls_transport_set_pull_function(session, NULL);
74
Adam Tkac35e6d4c2010-04-23 14:12:18 +000075 delete[] start;
76}
77
78int TLSInStream::pos()
79{
80 return offset + ptr - start;
81}
82
83int TLSInStream::overrun(int itemSize, int nItems, bool wait)
84{
85 if (itemSize > bufSize)
86 throw Exception("TLSInStream overrun: max itemSize exceeded");
87
88 if (end - ptr != 0)
89 memmove(start, ptr, end - ptr);
90
91 offset += ptr - start;
92 end -= ptr - start;
93 ptr = start;
94
95 while (end < start + itemSize) {
96 int n = readTLS((U8*) end, start + bufSize - end, wait);
97 if (!wait && n == 0)
98 return 0;
99 end += n;
100 }
101
102 if (itemSize * nItems > end - ptr)
103 nItems = (end - ptr) / itemSize;
104
105 return nItems;
106}
107
108int TLSInStream::readTLS(U8* buf, int len, bool wait)
109{
110 int n;
111
112 n = in->check(1, 1, wait);
113 if (n == 0)
114 return 0;
115
116 n = gnutls_record_recv(session, (void *) buf, len);
117 if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
118 return 0;
119
120 if (n < 0) throw TLSException("readTLS", n);
121
122 return n;
123}
124
125#endif