Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 1 | /* 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 Ossman | 2137f4f | 2012-07-03 14:52:26 +0000 | [diff] [blame] | 28 | #include <rdr/TLSErrno.h> |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_GNUTLS |
| 32 | using namespace rdr; |
| 33 | |
| 34 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 35 | |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 36 | ssize_t TLSOutStream::push(gnutls_transport_ptr str, const void* data, |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 37 | size_t size) |
| 38 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 39 | TLSOutStream* self= (TLSOutStream*) str; |
| 40 | OutStream *out = self->out; |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 41 | |
| 42 | try { |
| 43 | out->writeBytes(data, size); |
| 44 | out->flush(); |
| 45 | } catch (Exception& e) { |
Pierre Ossman | 2137f4f | 2012-07-03 14:52:26 +0000 | [diff] [blame] | 46 | gnutls_errno_helper(self->session, EINVAL); |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 47 | return -1; |
| 48 | } |
| 49 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 50 | return size; |
| 51 | } |
| 52 | |
| 53 | TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session _session) |
| 54 | : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0) |
| 55 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 56 | gnutls_transport_ptr recv, send; |
| 57 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 58 | ptr = start = new U8[bufSize]; |
| 59 | end = start + bufSize; |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 60 | |
| 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 Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TLSOutStream::~TLSOutStream() |
| 67 | { |
| 68 | #if 0 |
| 69 | try { |
| 70 | // flush(); |
| 71 | } catch (Exception&) { |
| 72 | } |
| 73 | #endif |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame] | 74 | gnutls_transport_set_push_function(session, NULL); |
| 75 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 76 | delete [] start; |
| 77 | } |
| 78 | |
| 79 | int TLSOutStream::length() |
| 80 | { |
| 81 | return offset + ptr - start; |
| 82 | } |
| 83 | |
| 84 | void 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 | |
| 97 | int 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 | |
| 110 | int 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 |