use of javax.net.ssl.SSLServerSocketFactory in project opennms by OpenNMS.
the class SSLServer method init.
/**
* <p>init</p>
*
* @throws java.lang.Exception if any.
*/
@Override
public void init() throws Exception {
super.init();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(getKeyManagerAlgorithm(), getKeyManagerProvider());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
char[] password = getPassword().toCharArray();
java.io.FileInputStream fis = null;
try {
fis = new java.io.FileInputStream(getPathToKeyStore());
ks.load(fis, password);
} finally {
if (fis != null) {
fis.close();
}
}
kmf.init(ks, password);
KeyManager[] km = kmf.getKeyManagers();
SSLContext sslContext = SSLContext.getInstance(getSslContextProtocol());
sslContext.init(km, null, new SecureRandom());
SSLServerSocketFactory serverFactory = sslContext.getServerSocketFactory();
setServerSocket(serverFactory.createServerSocket(getPort()));
onInit();
}
use of javax.net.ssl.SSLServerSocketFactory in project apjp by jvansteirteghem.
the class HTTPS method createSSLServerSocket.
public static synchronized SSLServerSocket createSSLServerSocket(String remoteAddress, int remotePort) throws HTTPSException {
try {
KeyStore defaultKeyStore = getDefaultKeyStore();
PrivateKey privateKey = (PrivateKey) defaultKeyStore.getKey("APJP", "APJP".toCharArray());
Certificate certificateAuthority = defaultKeyStore.getCertificate("APJP");
String certificateAlias;
if (remotePort == 443) {
certificateAlias = remoteAddress;
} else {
certificateAlias = remoteAddress + ":" + remotePort;
}
Certificate certificate = defaultKeyStore.getCertificate(certificateAlias);
if (certificate == null) {
X509Certificate x509CertificateAuthority = new X509Certificate(certificateAuthority.getEncoded());
X509Certificate x509Certificate = new X509Certificate();
Name name = new Name();
//CN
name.addRDN(new ObjectID("2.5.4.3"), certificateAlias);
// O
name.addRDN(new ObjectID("2.5.4.10"), "APJP");
// OU
name.addRDN(new ObjectID("2.5.4.11"), "APJP");
x509Certificate.setSubjectDN(name);
x509Certificate.setIssuerDN(x509CertificateAuthority.getIssuerDN());
x509Certificate.setValidNotBefore(new Date(new Date().getTime() - 1 * (1000L * 60 * 60 * 24 * 365)));
x509Certificate.setValidNotAfter(new Date(new Date().getTime() + 10 * (1000L * 60 * 60 * 24 * 365)));
x509Certificate.setSerialNumber(BigInteger.valueOf(new Date().getTime()));
x509Certificate.setPublicKey(x509CertificateAuthority.getPublicKey());
// SHA1_WITH_RSA_ENCRYPTION
x509Certificate.sign(new AlgorithmID(new ObjectID("1.2.840.113549.1.1.5")), privateKey);
X509Certificate[] x509CertificateArray = new X509Certificate[2];
x509CertificateArray[0] = x509Certificate;
x509CertificateArray[1] = x509CertificateAuthority;
defaultKeyStore.setCertificateEntry(certificateAlias, x509Certificate);
defaultKeyStore.setKeyEntry(certificateAlias, privateKey, "APJP".toCharArray(), x509CertificateArray);
certificate = x509Certificate;
}
Certificate[] certificateArray = new Certificate[2];
certificateArray[0] = certificate;
certificateArray[1] = certificateAuthority;
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, "APJP".toCharArray());
keyStore.setCertificateEntry("APJP", certificate);
keyStore.setKeyEntry("APJP", privateKey, "APJP".toCharArray(), certificateArray);
SSLContext sslContext = SSLContext.getInstance("TLS");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "APJP".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) sslContext.getServerSocketFactory();
return (SSLServerSocket) sslServerSocketFactory.createServerSocket();
} catch (Exception e) {
logger.log(2, "HTTPS/CREATE_SSL_SERVER_SOCKET: EXCEPTION", e);
throw new HTTPSException("HTTPS/CREATE_SSL_SERVER_SOCKET", e);
}
}
use of javax.net.ssl.SSLServerSocketFactory in project jetty.project by eclipse.
the class SslContextFactory method newSslServerSocket.
public SSLServerSocket newSslServerSocket(String host, int port, int backlog) throws IOException {
checkIsStarted();
SSLContext context = getSslContext();
SSLServerSocketFactory factory = context.getServerSocketFactory();
SSLServerSocket socket = (SSLServerSocket) (host == null ? factory.createServerSocket(port, backlog) : factory.createServerSocket(port, backlog, InetAddress.getByName(host)));
socket.setSSLParameters(customize(socket.getSSLParameters()));
return socket;
}
use of javax.net.ssl.SSLServerSocketFactory in project camel by apache.
the class HttpTestServer method start.
/**
* Starts this test server.
*/
public void start() throws Exception {
if (servicedSocket != null) {
throw new IllegalStateException(this.toString() + " already running");
}
ServerSocket ssock;
if (sslcontext != null) {
SSLServerSocketFactory sf = sslcontext.getServerSocketFactory();
ssock = sf.createServerSocket();
} else {
ssock = new ServerSocket();
}
// probably pointless for port '0'
ssock.setReuseAddress(true);
ssock.bind(TEST_SERVER_ADDR);
servicedSocket = ssock;
listenerThread = new ListenerThread();
listenerThread.setDaemon(false);
listenerThread.start();
}
use of javax.net.ssl.SSLServerSocketFactory in project android by cSploit.
the class HTTPSRedirector method getSSLSocket.
private SSLServerSocket getSSLSocket() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(mContext.getAssets().open(KEYSTORE_FILE), KEYSTORE_PASS.toCharArray());
KeyManagerFactory keyMan = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyMan.init(keyStore, KEYSTORE_PASS.toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyMan.getKeyManagers(), null, null);
SSLServerSocketFactory sslFactory = sslContext.getServerSocketFactory();
return (SSLServerSocket) sslFactory.createServerSocket(mPort, BACKLOG, mAddress);
}
Aggregations