
org.microemu.cldc.ssl.Connection Maven / Gradle / Ivy
/*
* MicroEmulator
* Copyright (C) 2006 Bartek Teodorczyk
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.microemu.cldc.ssl;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import javax.microedition.io.SecureConnection;
import javax.microedition.io.SecurityInfo;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.microemu.cldc.CertificateImpl;
import org.microemu.cldc.ClosedConnection;
import org.microemu.cldc.SecurityInfoImpl;
public class Connection extends org.microemu.cldc.socket.SocketConnection implements SecureConnection, ClosedConnection {
private SecurityInfo securityInfo;
public Connection() {
securityInfo = null;
}
public javax.microedition.io.Connection open(String name) throws IOException {
if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
throw new IOException("No network");
}
int portSepIndex = name.lastIndexOf(':');
int port = Integer.parseInt(name.substring(portSepIndex + 1));
String host = name.substring("ssl://".length(), portSepIndex);
// TODO validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
X509Certificate[] certs, String authType) {
}
}
};
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
SSLSocketFactory factory = sc.getSocketFactory();
socket = factory.createSocket(host, port);
} catch (NoSuchAlgorithmException ex) {
throw new IOException(ex.toString());
} catch (KeyManagementException ex) {
throw new IOException(ex.toString());
}
return this;
}
public void close() throws IOException {
// TODO fix differences between Java ME and Java SE
socket.close();
}
public SecurityInfo getSecurityInfo() throws IOException {
if (securityInfo == null) {
SSLSession session = ((SSLSocket) socket).getSession();
Certificate[] certs = session.getPeerCertificates();
if (certs.length == 0) {
throw new IOException();
}
securityInfo = new SecurityInfoImpl(
session.getCipherSuite(),
session.getProtocol(),
new CertificateImpl((X509Certificate) certs[0]));
}
return securityInfo;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy