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> |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 29 | |
DRC | 3e465a6 | 2010-09-30 06:25:28 +0000 | [diff] [blame] | 30 | #ifdef HAVE_OLD_GNUTLS |
| 31 | #define gnutls_transport_set_global_errno(A) do { errno = (A); } while(0) |
| 32 | #endif |
| 33 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 34 | #ifdef HAVE_GNUTLS |
| 35 | using namespace rdr; |
| 36 | |
| 37 | enum { DEFAULT_BUF_SIZE = 16384 }; |
| 38 | |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame^] | 39 | ssize_t TLSOutStream::push(gnutls_transport_ptr str, const void* data, |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 40 | size_t size) |
| 41 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame^] | 42 | TLSOutStream* self= (TLSOutStream*) str; |
| 43 | OutStream *out = self->out; |
Adam Tkac | fab093c | 2010-08-25 13:52:49 +0000 | [diff] [blame] | 44 | |
| 45 | try { |
| 46 | out->writeBytes(data, size); |
| 47 | out->flush(); |
| 48 | } catch (Exception& e) { |
| 49 | gnutls_transport_set_global_errno(EINVAL); |
| 50 | return -1; |
| 51 | } |
| 52 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 53 | return size; |
| 54 | } |
| 55 | |
| 56 | TLSOutStream::TLSOutStream(OutStream* _out, gnutls_session _session) |
| 57 | : session(_session), out(_out), bufSize(DEFAULT_BUF_SIZE), offset(0) |
| 58 | { |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame^] | 59 | gnutls_transport_ptr recv, send; |
| 60 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 61 | ptr = start = new U8[bufSize]; |
| 62 | end = start + bufSize; |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame^] | 63 | |
| 64 | gnutls_transport_set_push_function(session, push); |
| 65 | gnutls_transport_get_ptr2(session, &recv, &send); |
| 66 | gnutls_transport_set_ptr2(session, recv, this); |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TLSOutStream::~TLSOutStream() |
| 70 | { |
| 71 | #if 0 |
| 72 | try { |
| 73 | // flush(); |
| 74 | } catch (Exception&) { |
| 75 | } |
| 76 | #endif |
Pierre Ossman | fe48cd4 | 2012-07-03 14:43:38 +0000 | [diff] [blame^] | 77 | gnutls_transport_set_push_function(session, NULL); |
| 78 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 79 | delete [] start; |
| 80 | } |
| 81 | |
| 82 | int TLSOutStream::length() |
| 83 | { |
| 84 | return offset + ptr - start; |
| 85 | } |
| 86 | |
| 87 | void TLSOutStream::flush() |
| 88 | { |
| 89 | U8* sentUpTo = start; |
| 90 | while (sentUpTo < ptr) { |
| 91 | int n = writeTLS(sentUpTo, ptr - sentUpTo); |
| 92 | sentUpTo += n; |
| 93 | offset += n; |
| 94 | } |
| 95 | |
| 96 | ptr = start; |
| 97 | out->flush(); |
| 98 | } |
| 99 | |
| 100 | int TLSOutStream::overrun(int itemSize, int nItems) |
| 101 | { |
| 102 | if (itemSize > bufSize) |
| 103 | throw Exception("TLSOutStream overrun: max itemSize exceeded"); |
| 104 | |
| 105 | flush(); |
| 106 | |
| 107 | if (itemSize * nItems > end - ptr) |
| 108 | nItems = (end - ptr) / itemSize; |
| 109 | |
| 110 | return nItems; |
| 111 | } |
| 112 | |
| 113 | int TLSOutStream::writeTLS(const U8* data, int length) |
| 114 | { |
| 115 | int n; |
| 116 | |
| 117 | n = gnutls_record_send(session, data, length); |
| 118 | if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN) |
| 119 | return 0; |
| 120 | |
| 121 | if (n < 0) |
| 122 | throw TLSException("writeTLS", n); |
| 123 | |
| 124 | return n; |
| 125 | } |
| 126 | |
| 127 | #endif |