blob: e553085f33c53ca4e8b70f15fa8ebfb28e7db63d [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
DRC3e465a62010-09-30 06:25:28 +000030#ifdef HAVE_OLD_GNUTLS
31#define gnutls_transport_set_global_errno(A) do { errno = (A); } while(0)
32#endif
33
Adam Tkac35e6d4c2010-04-23 14:12:18 +000034#ifdef HAVE_GNUTLS
35using namespace rdr;
36
37enum { DEFAULT_BUF_SIZE = 16384 };
38
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000039ssize_t TLSInStream::pull(gnutls_transport_ptr str, void* data, size_t size)
Adam Tkac35e6d4c2010-04-23 14:12:18 +000040{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000041 TLSInStream* self= (TLSInStream*) str;
42 InStream *in = self->in;
Adam Tkac35e6d4c2010-04-23 14:12:18 +000043
Adam Tkacfab093c2010-08-25 13:52:49 +000044 try {
45 if (!in->check(1, 1, false)) {
46 gnutls_transport_set_global_errno(EAGAIN);
47 return -1;
48 }
49
50 if (in->getend() - in->getptr() < size)
51 size = in->getend() - in->getptr();
52
53 in->readBytes(data, size);
54
55 } catch (Exception& e) {
56 gnutls_transport_set_global_errno(EINVAL);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000057 return -1;
58 }
59
Adam Tkac35e6d4c2010-04-23 14:12:18 +000060 return size;
61}
62
63TLSInStream::TLSInStream(InStream* _in, gnutls_session _session)
64 : session(_session), in(_in), bufSize(DEFAULT_BUF_SIZE), offset(0)
65{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000066 gnutls_transport_ptr recv, send;
67
Adam Tkac35e6d4c2010-04-23 14:12:18 +000068 ptr = end = start = new U8[bufSize];
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000069
70 gnutls_transport_set_pull_function(session, pull);
71 gnutls_transport_get_ptr2(session, &recv, &send);
72 gnutls_transport_set_ptr2(session, this, send);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000073}
74
75TLSInStream::~TLSInStream()
76{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000077 gnutls_transport_set_pull_function(session, NULL);
78
Adam Tkac35e6d4c2010-04-23 14:12:18 +000079 delete[] start;
80}
81
82int TLSInStream::pos()
83{
84 return offset + ptr - start;
85}
86
87int TLSInStream::overrun(int itemSize, int nItems, bool wait)
88{
89 if (itemSize > bufSize)
90 throw Exception("TLSInStream overrun: max itemSize exceeded");
91
92 if (end - ptr != 0)
93 memmove(start, ptr, end - ptr);
94
95 offset += ptr - start;
96 end -= ptr - start;
97 ptr = start;
98
99 while (end < start + itemSize) {
100 int n = readTLS((U8*) end, start + bufSize - end, wait);
101 if (!wait && n == 0)
102 return 0;
103 end += n;
104 }
105
106 if (itemSize * nItems > end - ptr)
107 nItems = (end - ptr) / itemSize;
108
109 return nItems;
110}
111
112int TLSInStream::readTLS(U8* buf, int len, bool wait)
113{
114 int n;
115
116 n = in->check(1, 1, wait);
117 if (n == 0)
118 return 0;
119
120 n = gnutls_record_recv(session, (void *) buf, len);
121 if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
122 return 0;
123
124 if (n < 0) throw TLSException("readTLS", n);
125
126 return n;
127}
128
129#endif