All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.firefly.net.tcp.secure.jdk.AbstractJdkSSLContextFactory Maven / Gradle / Ivy
package com.firefly.net.tcp.secure.jdk;
import com.firefly.net.ApplicationProtocolSelector;
import com.firefly.net.SSLContextFactory;
import com.firefly.utils.lang.Pair;
import com.firefly.utils.time.Millisecond100Clock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.*;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.List;
/**
* @author Pengtao Qiu
*/
abstract public class AbstractJdkSSLContextFactory implements SSLContextFactory {
protected static final Logger log = LoggerFactory.getLogger("firefly-system");
private List supportedProtocols;
public SSLContext getSSLContextWithManager(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws NoSuchAlgorithmException, KeyManagementException {
long start = Millisecond100Clock.currentTimeMillis();
final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(km, tm, random);
long end = Millisecond100Clock.currentTimeMillis();
log.info("creating SSL context spends {} ms", (end - start));
return sslContext;
}
public SSLContext getSSLContext(InputStream in, String keystorePassword, String keyPassword)
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
UnrecoverableKeyException, KeyManagementException {
return getSSLContext(in, keystorePassword, keyPassword, null, null, null);
}
public SSLContext getSSLContext(InputStream in, String keystorePassword, String keyPassword,
String keyManagerFactoryType, String trustManagerFactoryType, String sslProtocol)
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
UnrecoverableKeyException, KeyManagementException {
long start = Millisecond100Clock.currentTimeMillis();
final SSLContext sslContext;
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(in, keystorePassword != null ? keystorePassword.toCharArray() : null);
// PKIX,SunX509
KeyManagerFactory kmf = KeyManagerFactory.getInstance(keyManagerFactoryType == null ? "SunX509" : keyManagerFactoryType);
kmf.init(ks, keyPassword != null ? keyPassword.toCharArray() : null);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerFactoryType == null ? "SunX509" : trustManagerFactoryType);
tmf.init(ks);
// TLSv1 TLSv1.2
sslContext = SSLContext.getInstance(sslProtocol == null ? "TLSv1.2" : sslProtocol);
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
long end = Millisecond100Clock.currentTimeMillis();
log.info("creating SSL context spends {} ms", (end - start));
return sslContext;
}
abstract public SSLContext getSSLContext();
@Override
public Pair createSSLEngine(boolean clientMode) {
SSLEngine sslEngine = getSSLContext().createSSLEngine();
sslEngine.setUseClientMode(clientMode);
return new Pair<>(sslEngine, new EmptyALPNSelector(sslEngine, supportedProtocols));
}
@Override
public Pair createSSLEngine(boolean clientMode, String peerHost, int peerPort) {
SSLEngine sslEngine = getSSLContext().createSSLEngine(peerHost, peerPort);
sslEngine.setUseClientMode(clientMode);
return new Pair<>(sslEngine, new EmptyALPNSelector(sslEngine, supportedProtocols));
}
@Override
public List getSupportedProtocols() {
return supportedProtocols;
}
@Override
public void setSupportedProtocols(List supportedProtocols) {
this.supportedProtocols = supportedProtocols;
}
}