blob: ef32d7db9613fe5cf88aa89d9d60f15c5c0364c4 [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/TLSOutStream.h>
Pierre Ossman2137f4f2012-07-03 14:52:26 +000028#include <rdr/TLSErrno.h>
Adam Tkacfab093c2010-08-25 13:52:49 +000029#include <errno.h>
Adam Tkac35e6d4c2010-04-23 14:12:18 +000030
31#ifdef HAVE_GNUTLS
32using namespace rdr;
33
34enum { DEFAULT_BUF_SIZE = 16384 };
35
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000036ssize_t TLSOutStream::push(gnutls_transport_ptr str, const void* data,
Adam Tkac35e6d4c2010-04-23 14:12:18 +000037 size_t size)
38{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000039 TLSOutStream* self= (TLSOutStream*) str;
40 OutStream *out = self->out;
Adam Tkacfab093c2010-08-25 13:52:49 +000041
42 try {
43 out->writeBytes(data, size);
44 out->flush();
45 } catch (Exception& e) {
Pierre Ossman2137f4f2012-07-03 14:52:26 +000046 gnutls_errno_helper(self->session, EINVAL);
Adam Tkacfab093c2010-08-25 13:52:49 +000047 return -1;
48 }
49
Adam Tkac35e6d4c2010-04-23 14:12:18 +000050 return size;
51}
52
53TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session _session)
54 : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0)
55{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000056 gnutls_transport_ptr recv, send;
57
Adam Tkac35e6d4c2010-04-23 14:12:18 +000058 ptr = start = new U8[bufSize];
59 end = start + bufSize;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000060
61 gnutls_transport_set_push_function(session, push);
62 gnutls_transport_get_ptr2(session, &recv, &send);
63 gnutls_transport_set_ptr2(session, recv, this);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000064}
65
66TLSOutStream::~TLSOutStream()
67{
68#if 0
69 try {
70// flush();
71 } catch (Exception&) {
72 }
73#endif
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000074 gnutls_transport_set_push_function(session, NULL);
75
Adam Tkac35e6d4c2010-04-23 14:12:18 +000076 delete [] start;
77}
78
79int TLSOutStream::length()
80{
81 return offset + ptr - start;
82}
83
84void TLSOutStream::flush()
85{
86 U8* sentUpTo = start;
87 while (sentUpTo < ptr) {
88 int n = writeTLS(sentUpTo, ptr - sentUpTo);
89 sentUpTo += n;
90 offset += n;
91 }
92
93 ptr = start;
94 out->flush();
95}
96
97int TLSOutStream::overrun(int itemSize, int nItems)
98{
99 if (itemSize > bufSize)
100 throw Exception("TLSOutStream overrun: max itemSize exceeded");
101
102 flush();
103
104 if (itemSize * nItems > end - ptr)
105 nItems = (end - ptr) / itemSize;
106
107 return nItems;
108}
109
110int TLSOutStream::writeTLS(const U8* data, int length)
111{
112 int n;
113
114 n = gnutls_record_send(session, data, length);
115 if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
116 return 0;
117
118 if (n < 0)
119 throw TLSException("writeTLS", n);
120
121 return n;
122}
123
124#endif