Adam Tkac | 28d8389 | 2010-11-18 14:17:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Sun Microsystems, Inc. |
| 3 | * Copyright (C) 2003-2010 Martin Koegler |
| 4 | * Copyright (C) 2006 OCCAM Financial Technology |
| 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 19 | * USA. |
| 20 | */ |
| 21 | |
| 22 | package com.tigervnc.vncviewer; |
| 23 | |
| 24 | import java.util.*; |
| 25 | import java.net.*; |
| 26 | import javax.net.ssl.*; |
| 27 | import java.security.*; |
| 28 | import java.security.cert.*; |
| 29 | |
| 30 | public class X509Tunnel extends TLSTunnelBase |
| 31 | { |
| 32 | |
| 33 | public X509Tunnel (Socket sock_) |
| 34 | { |
| 35 | super (sock_); |
| 36 | } |
| 37 | |
| 38 | protected void setParam (SSLSocket sock) |
| 39 | { |
| 40 | String[]supported; |
| 41 | ArrayList enabled = new ArrayList (); |
| 42 | |
| 43 | supported = sock.getSupportedCipherSuites (); |
| 44 | |
| 45 | for (int i = 0; i < supported.length; i++) |
| 46 | if (!supported[i].matches (".*DH_anon.*")) |
| 47 | enabled.add (supported[i]); |
| 48 | |
| 49 | sock.setEnabledCipherSuites ((String[])enabled.toArray (new String[0])); |
| 50 | } |
| 51 | |
| 52 | protected void initContext (SSLContext sc) throws java.security. |
| 53 | GeneralSecurityException |
| 54 | { |
| 55 | TrustManager[] myTM = new TrustManager[] |
| 56 | { |
| 57 | new MyX509TrustManager ()}; |
| 58 | sc.init (null, myTM, null); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | class MyX509TrustManager implements X509TrustManager |
| 63 | { |
| 64 | |
| 65 | X509TrustManager tm; |
| 66 | |
| 67 | MyX509TrustManager () throws java.security.GeneralSecurityException |
| 68 | { |
| 69 | TrustManagerFactory tmf = |
| 70 | TrustManagerFactory.getInstance ("SunX509", "SunJSSE"); |
| 71 | KeyStore ks = KeyStore.getInstance ("JKS"); |
| 72 | tmf.init (ks); |
| 73 | tm = (X509TrustManager) tmf.getTrustManagers ()[0]; |
| 74 | } |
| 75 | public void checkClientTrusted (X509Certificate[]chain, |
| 76 | String authType) throws |
| 77 | CertificateException |
| 78 | { |
| 79 | tm.checkClientTrusted (chain, authType); |
| 80 | } |
| 81 | |
| 82 | public void checkServerTrusted (X509Certificate[]chain, |
| 83 | String authType) |
| 84 | throws CertificateException |
| 85 | { |
| 86 | try |
| 87 | { |
| 88 | tm.checkServerTrusted (chain, authType); |
| 89 | } catch (CertificateException e) |
| 90 | { |
| 91 | MessageBox m = |
| 92 | new MessageBox (e.toString (), MessageBox.MB_OKAYCANCEL); |
| 93 | if (!m.result ()) |
| 94 | throw e; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public X509Certificate[] getAcceptedIssuers () |
| 99 | { |
| 100 | return tm.getAcceptedIssuers (); |
| 101 | } |
| 102 | } |
| 103 | } |