View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2006 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library 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 GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.cldc.ssl;
21  
22  import java.io.IOException;
23  import java.security.KeyManagementException;
24  import java.security.NoSuchAlgorithmException;
25  import java.security.SecureRandom;
26  import java.security.cert.Certificate;
27  import java.security.cert.X509Certificate;
28  
29  import javax.microedition.io.SecureConnection;
30  import javax.microedition.io.SecurityInfo;
31  import javax.net.ssl.SSLContext;
32  import javax.net.ssl.SSLSession;
33  import javax.net.ssl.SSLSocket;
34  import javax.net.ssl.SSLSocketFactory;
35  import javax.net.ssl.TrustManager;
36  import javax.net.ssl.X509TrustManager;
37  
38  import org.microemu.cldc.CertificateImpl;
39  import org.microemu.cldc.ClosedConnection;
40  import org.microemu.cldc.SecurityInfoImpl;
41  
42  public class Connection extends org.microemu.cldc.socket.SocketConnection implements SecureConnection, ClosedConnection {
43  	
44  	private SecurityInfo securityInfo;
45  	
46  	public Connection() {
47  		securityInfo = null;
48  	}
49  
50  	public javax.microedition.io.Connection open(String name) throws IOException {
51  		
52  		if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
53  			throw new IOException("No network");
54  		}
55  		
56  		int portSepIndex = name.lastIndexOf(':');
57  		int port = Integer.parseInt(name.substring(portSepIndex + 1));
58  		String host = name.substring("ssl://".length(), portSepIndex);
59  		
60  		// TODO validate certificate chains
61  	    TrustManager[] trustAllCerts = new TrustManager[]{
62  	        new X509TrustManager() {
63  	            public X509Certificate[] getAcceptedIssuers() {
64  	                return null;
65  	            }
66  	            public void checkClientTrusted(
67  	                X509Certificate[] certs, String authType) {
68  	            }
69  	            public void checkServerTrusted(
70  	                X509Certificate[] certs, String authType) {
71  	            }
72  	        }
73  	    };
74  		
75  		try {
76  			SSLContext sc = SSLContext.getInstance("SSL");			
77  			sc.init(null, trustAllCerts, new SecureRandom());
78  			SSLSocketFactory factory = sc.getSocketFactory();
79  			socket = factory.createSocket(host, port);
80  		} catch (NoSuchAlgorithmException ex) {
81  			throw new IOException(ex.toString());
82  		} catch (KeyManagementException ex) {
83  			throw new IOException(ex.toString());
84  		}
85  		
86  		return this;
87  	}
88  
89  	public void close() throws IOException {
90  		// TODO fix differences between Java ME and Java SE
91  		
92  		socket.close();
93  	}
94  
95  	public SecurityInfo getSecurityInfo() throws IOException {
96  		if (securityInfo == null) {
97  			SSLSession session = ((SSLSocket) socket).getSession();
98  			
99  			Certificate[] certs = session.getPeerCertificates();
100 			if (certs.length == 0) {
101 				throw new IOException();
102 			}
103 			
104 			securityInfo = new SecurityInfoImpl(
105 					session.getCipherSuite(),
106 					session.getProtocol(),
107 					new CertificateImpl((X509Certificate) certs[0]));
108 		}
109 
110 		return securityInfo;
111 	}
112 
113 }