View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2006 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  It is licensed under the following two licenses as alternatives:
6    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
7    *    2. Apache License (the "AL") Version 2.0
8    *
9    *  You may not use this file except in compliance with at least one of
10   *  the above two licenses.
11   *
12   *  You may obtain a copy of the LGPL at
13   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
14   *
15   *  You may obtain a copy of the AL at
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the LGPL or the AL for the specific language governing permissions and
22   *  limitations.
23   */
24  
25  package org.microemu.cldc.ssl;
26  
27  import java.io.IOException;
28  import java.security.KeyManagementException;
29  import java.security.NoSuchAlgorithmException;
30  import java.security.SecureRandom;
31  import java.security.cert.Certificate;
32  import java.security.cert.X509Certificate;
33  
34  import javax.microedition.io.SecureConnection;
35  import javax.microedition.io.SecurityInfo;
36  import javax.net.ssl.SSLContext;
37  import javax.net.ssl.SSLSession;
38  import javax.net.ssl.SSLSocket;
39  import javax.net.ssl.SSLSocketFactory;
40  import javax.net.ssl.TrustManager;
41  import javax.net.ssl.X509TrustManager;
42  
43  import org.microemu.cldc.CertificateImpl;
44  import org.microemu.cldc.ClosedConnection;
45  import org.microemu.cldc.SecurityInfoImpl;
46  
47  public class Connection extends org.microemu.cldc.socket.SocketConnection implements SecureConnection, ClosedConnection {
48  	
49  	private SecurityInfo securityInfo;
50  	
51  	public Connection() {
52  		securityInfo = null;
53  	}
54  
55  	public javax.microedition.io.Connection open(String name) throws IOException {
56  		
57  		if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
58  			throw new IOException("No network");
59  		}
60  		
61  		int portSepIndex = name.lastIndexOf(':');
62  		int port = Integer.parseInt(name.substring(portSepIndex + 1));
63  		String host = name.substring("ssl://".length(), portSepIndex);
64  		
65  		// TODO validate certificate chains
66  	    TrustManager[] trustAllCerts = new TrustManager[]{
67  	        new X509TrustManager() {
68  	            public X509Certificate[] getAcceptedIssuers() {
69  	                return null;
70  	            }
71  	            public void checkClientTrusted(
72  	                X509Certificate[] certs, String authType) {
73  	            }
74  	            public void checkServerTrusted(
75  	                X509Certificate[] certs, String authType) {
76  	            }
77  	        }
78  	    };
79  		
80  		try {
81  			SSLContext sc = SSLContext.getInstance("SSL");			
82  			sc.init(null, trustAllCerts, new SecureRandom());
83  			SSLSocketFactory factory = sc.getSocketFactory();
84  			socket = factory.createSocket(host, port);
85  		} catch (NoSuchAlgorithmException ex) {
86  			throw new IOException(ex.toString());
87  		} catch (KeyManagementException ex) {
88  			throw new IOException(ex.toString());
89  		}
90  		
91  		return this;
92  	}
93  
94  	public void close() throws IOException {
95  		// TODO fix differences between Java ME and Java SE
96  		
97  		socket.close();
98  	}
99  
100 	public SecurityInfo getSecurityInfo() throws IOException {
101 		if (securityInfo == null) {
102 			SSLSession session = ((SSLSocket) socket).getSession();
103 			
104 			Certificate[] certs = session.getPeerCertificates();
105 			if (certs.length == 0) {
106 				throw new IOException();
107 			}
108 			
109 			securityInfo = new SecurityInfoImpl(
110 					session.getCipherSuite(),
111 					session.getProtocol(),
112 					new CertificateImpl((X509Certificate) certs[0]));
113 		}
114 
115 		return securityInfo;
116 	}
117 
118 }