blob: 44d2d9f33af4dd98f95d1b4d40abb69bd3eacb77 [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>
Adam Tkacfab093c2010-08-25 13:52:49 +000028#include <errno.h>
Adam Tkac35e6d4c2010-04-23 14:12:18 +000029
30#ifdef HAVE_GNUTLS
31using namespace rdr;
32
33enum { DEFAULT_BUF_SIZE = 16384 };
34
Pierre Ossman88c24ed2015-01-29 13:12:22 +010035ssize_t TLSOutStream::push(gnutls_transport_ptr_t str, const void* data,
Adam Tkac35e6d4c2010-04-23 14:12:18 +000036 size_t size)
37{
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000038 TLSOutStream* self= (TLSOutStream*) str;
39 OutStream *out = self->out;
Adam Tkacfab093c2010-08-25 13:52:49 +000040
41 try {
42 out->writeBytes(data, size);
43 out->flush();
44 } catch (Exception& e) {
Pierre Ossman88c24ed2015-01-29 13:12:22 +010045 gnutls_transport_set_errno(self->session, EINVAL);
Adam Tkacfab093c2010-08-25 13:52:49 +000046 return -1;
47 }
48
Adam Tkac35e6d4c2010-04-23 14:12:18 +000049 return size;
50}
51
Pierre Ossman88c24ed2015-01-29 13:12:22 +010052TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session_t _session)
Adam Tkac35e6d4c2010-04-23 14:12:18 +000053 : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0)
54{
Pierre Ossman88c24ed2015-01-29 13:12:22 +010055 gnutls_transport_ptr_t recv, send;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000056
Adam Tkac35e6d4c2010-04-23 14:12:18 +000057 ptr = start = new U8[bufSize];
58 end = start + bufSize;
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000059
60 gnutls_transport_set_push_function(session, push);
61 gnutls_transport_get_ptr2(session, &recv, &send);
62 gnutls_transport_set_ptr2(session, recv, this);
Adam Tkac35e6d4c2010-04-23 14:12:18 +000063}
64
65TLSOutStream::~TLSOutStream()
66{
67#if 0
68 try {
69// flush();
70 } catch (Exception&) {
71 }
72#endif
Pierre Ossmanfe48cd42012-07-03 14:43:38 +000073 gnutls_transport_set_push_function(session, NULL);
74
Adam Tkac35e6d4c2010-04-23 14:12:18 +000075 delete [] start;
76}
77
78int TLSOutStream::length()
79{
80 return offset + ptr - start;
81}
82
83void TLSOutStream::flush()
84{
85 U8* sentUpTo = start;
86 while (sentUpTo < ptr) {
87 int n = writeTLS(sentUpTo, ptr - sentUpTo);
88 sentUpTo += n;
89 offset += n;
90 }
91
92 ptr = start;
93 out->flush();
94}
95
96int TLSOutStream::overrun(int itemSize, int nItems)
97{
98 if (itemSize > bufSize)
99 throw Exception("TLSOutStream overrun: max itemSize exceeded");
100
101 flush();
102
103 if (itemSize * nItems > end - ptr)
104 nItems = (end - ptr) / itemSize;
105
106 return nItems;
107}
108
109int TLSOutStream::writeTLS(const U8* data, int length)
110{
111 int n;
112
113 n = gnutls_record_send(session, data, length);
114 if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
115 return 0;
116
117 if (n < 0)
118 throw TLSException("writeTLS", n);
119
120 return n;
121}
122
123#endif