1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. 2 * Copyright (C) 2005 Martin Koegler 3 * Copyright (C) 2010 TigerVNC Team 4 * Copyright (C) 2011-2019 Brian P. Hinz 5 * 6 * This is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This software is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this software; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19 * USA. 20 */ 21 22 package com.tigervnc.rdr; 23 24 import java.nio.ByteBuffer; 25 import java.nio.channels.*; 26 import javax.net.ssl.*; 27 28 import com.tigervnc.network.*; 29 30 public class TLSInStream extends InStream { 31 32 static final int defaultBufSize = 16384; 33 TLSInStream(InStream _in, SSLEngineManager _manager)34 public TLSInStream(InStream _in, SSLEngineManager _manager) { 35 in = (FdInStream)_in; 36 manager = _manager; 37 offset = 0; 38 SSLSession session = manager.getSession(); 39 bufSize = session.getApplicationBufferSize(); 40 b = new byte[bufSize]; 41 ptr = end = start = 0; 42 } 43 pos()44 public final int pos() { 45 return offset + ptr - start; 46 } 47 startTiming()48 public final void startTiming() { 49 in.startTiming(); 50 } 51 stopTiming()52 public final void stopTiming() { 53 in.stopTiming(); 54 } 55 kbitsPerSecond()56 public final long kbitsPerSecond() { 57 return in.kbitsPerSecond(); 58 } 59 timeWaited()60 public final long timeWaited() { 61 return in.timeWaited(); 62 } 63 overrun(int itemSize, int nItems, boolean wait)64 protected final int overrun(int itemSize, int nItems, boolean wait) { 65 if (itemSize > bufSize) 66 throw new Exception("TLSInStream overrun: max itemSize exceeded"); 67 68 if (end - ptr != 0) 69 System.arraycopy(b, ptr, b, 0, end - ptr); 70 71 offset += ptr - start; 72 end -= ptr - start; 73 ptr = start; 74 75 while ((end - start) < itemSize) { 76 int n = readTLS(b, end, start + bufSize - end, wait); 77 if (!wait && n == 0) 78 return 0; 79 end += n; 80 } 81 82 int nAvail; 83 nAvail = (end - ptr) / itemSize; 84 if (nAvail < nItems) 85 return nAvail; 86 87 return nItems; 88 } 89 readTLS(byte[] buf, int bufPtr, int len, boolean wait)90 protected int readTLS(byte[] buf, int bufPtr, int len, boolean wait) 91 { 92 int n = -1; 93 94 try { 95 n = manager.read(ByteBuffer.wrap(buf, bufPtr, len), len); 96 } catch (java.io.IOException e) { 97 e.printStackTrace(); 98 } 99 100 if (n < 0) throw new TLSException("readTLS", n); 101 102 return n; 103 } 104 105 private SSLEngineManager manager; 106 private int offset; 107 private int start; 108 private int bufSize; 109 private FdInStream in; 110 } 111