Adam Tkac | 4be9da8 | 2010-11-18 14:00:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Sun Microsystems, Inc. |
| 3 | * Copyright (C) 2003-2010 Martin Koegler |
| 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 | package com.tigervnc.vncviewer; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.net.*; |
| 25 | import javax.net.ssl.*; |
| 26 | |
| 27 | public abstract class TLSTunnelBase |
| 28 | { |
| 29 | |
| 30 | public TLSTunnelBase (Socket sock_) |
| 31 | { |
| 32 | sock = sock_; |
| 33 | } |
| 34 | |
| 35 | protected void initContext (SSLContext sc) throws java.security. |
| 36 | GeneralSecurityException |
| 37 | { |
| 38 | sc.init (null, null, null); |
| 39 | } |
| 40 | |
| 41 | public void setup (RfbProto cc) throws Exception |
| 42 | { |
| 43 | if (cc.readU8 () == 0) |
| 44 | throw new Exception("Setup on the server failed"); |
| 45 | try |
| 46 | { |
| 47 | SSLSocketFactory sslfactory; |
| 48 | SSLSocket sslsock; |
| 49 | SSLContext sc = SSLContext.getInstance ("TLS"); |
| 50 | System.out.println("Generating TLS context"); |
| 51 | initContext (sc); |
| 52 | System.out.println("Doing TLS handshake"); |
| 53 | sslfactory = sc.getSocketFactory (); |
| 54 | sslsock = (SSLSocket) sslfactory.createSocket (sock, |
| 55 | sock.getInetAddress (). |
| 56 | getHostName (), |
| 57 | sock.getPort (), true); |
| 58 | |
| 59 | setParam (sslsock); |
| 60 | |
| 61 | /* Not neccessary - just ensures that we know what cipher |
| 62 | * suite we are using for the output of toString() |
| 63 | */ |
| 64 | sslsock.startHandshake (); |
| 65 | |
| 66 | System.out.println("TLS done"); |
| 67 | |
| 68 | cc.setStreams (sslsock.getInputStream (), |
| 69 | sslsock.getOutputStream ()); |
| 70 | } |
| 71 | catch (java.io.IOException e) |
| 72 | { |
| 73 | throw new Exception("TLS handshake failed " + e.toString ()); |
| 74 | } |
| 75 | catch (java.security.GeneralSecurityException e) |
| 76 | { |
| 77 | throw new Exception("TLS handshake failed " + e.toString ()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | protected abstract void setParam (SSLSocket sock); |
| 83 | |
| 84 | Socket sock; |
| 85 | |
| 86 | } |