1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
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 }