use of javax.net.ssl.KeyManager in project ddf by codice.
the class SecureCxfClientFactoryImpl method configureConduit.
@SuppressWarnings("squid:S3776")
private void configureConduit(ClientConfiguration clientConfig) {
HTTPConduit httpConduit = clientConfig.getHttpConduit();
if (httpConduit == null) {
LOGGER.info("HTTPConduit was null for {}. Unable to configure security.", this);
return;
}
if (allowRedirects) {
HTTPClientPolicy clientPolicy = httpConduit.getClient();
if (clientPolicy != null) {
clientPolicy.setAutoRedirect(true);
Bus bus = clientConfig.getBus();
if (bus != null) {
bus.getProperties().put(AUTO_REDIRECT_ALLOW_REL_URI, true);
bus.getProperties().put(AUTO_REDIRECT_MAX_SAME_URI_COUNT, getSameUriRedirectMax());
}
}
}
TLSClientParameters tlsParams = httpConduit.getTlsClientParameters();
if (tlsParams == null) {
tlsParams = new TLSClientParameters();
}
tlsParams.setDisableCNCheck(disableCnCheck);
tlsParams.setUseHttpsURLConnectionDefaultHostnameVerifier(!disableCnCheck);
String cipherSuites = System.getProperty("https.cipherSuites");
if (cipherSuites != null) {
tlsParams.setCipherSuites(Arrays.asList(cipherSuites.split(",")));
}
KeyStore keyStore = null;
KeyStore trustStore = null;
try {
keyStore = SecurityConstants.newKeystore();
trustStore = SecurityConstants.newTruststore();
} catch (KeyStoreException e) {
LOGGER.debug("Unable to create keystore instance of type {}", System.getProperty(SecurityConstants.KEYSTORE_TYPE), e);
}
Path keyStoreFile;
if (keyInfo != null && keyInfo.getKeystorePath() != null) {
keyStoreFile = keyInfo.getKeystorePath();
} else {
keyStoreFile = Paths.get(SecurityConstants.getKeystorePath());
}
Path trustStoreFile = Paths.get(SecurityConstants.getTruststorePath());
String ddfHome = System.getProperty("ddf.home");
if (ddfHome != null) {
Path ddfHomePath = Paths.get(ddfHome);
if (!keyStoreFile.isAbsolute()) {
keyStoreFile = Paths.get(ddfHomePath.toString(), keyStoreFile.toString());
}
if (!trustStoreFile.isAbsolute()) {
trustStoreFile = Paths.get(ddfHomePath.toString(), trustStoreFile.toString());
}
}
String keyStorePassword = SecurityConstants.getKeystorePassword();
String trustStorePassword = SecurityConstants.getTruststorePassword();
if (!Files.isReadable(keyStoreFile) || !Files.isReadable(trustStoreFile)) {
LOGGER.debug("Unable to read system key/trust store files: [ {} ] [ {} ]", keyStoreFile, trustStoreFile);
return;
}
try (InputStream kfis = Files.newInputStream(keyStoreFile)) {
if (keyStore != null) {
keyStore.load(kfis, keyStorePassword.toCharArray());
}
} catch (NoSuchAlgorithmException | CertificateException | IOException e) {
LOGGER.debug("Unable to load system key file.", e);
}
try (InputStream tfis = Files.newInputStream(trustStoreFile)) {
if (trustStore != null) {
trustStore.load(tfis, trustStorePassword.toCharArray());
}
} catch (NoSuchAlgorithmException | CertificateException | IOException e) {
LOGGER.debug("Unable to load system trust file.", e);
}
KeyManager[] keyManagers = null;
try {
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
keyManagers = keyManagerFactory.getKeyManagers();
tlsParams.setKeyManagers(keyManagers);
} catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException e) {
LOGGER.debug("Unable to initialize KeyManagerFactory.", e);
}
TrustManager[] trustManagers = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
trustManagers = trustManagerFactory.getTrustManagers();
tlsParams.setTrustManagers(trustManagers);
} catch (NoSuchAlgorithmException | KeyStoreException e) {
LOGGER.debug("Unable to initialize TrustManagerFactory.", e);
}
if (keyInfo != null) {
LOGGER.trace("Using keystore file: {}, alias: {}", keyStoreFile, keyInfo.getAlias());
tlsParams.setUseHttpsURLConnectionDefaultSslSocketFactory(false);
tlsParams.setCertAlias(keyInfo.getAlias());
try {
if (keyManagers == null) {
throw new KeyManagementException("keyManagers was null");
}
boolean validProtocolFound = false;
String validProtocolsStr = System.getProperty("jdk.tls.client.protocols");
if (StringUtils.isNotBlank(validProtocolsStr)) {
String[] validProtocols = validProtocolsStr.split(",");
for (String validProtocol : validProtocols) {
if (validProtocol.equals(sslProtocol)) {
validProtocolFound = true;
break;
}
}
if (!validProtocolFound) {
LOGGER.error("{} is not in list of valid SSL protocols {}", sslProtocol, validProtocolsStr);
}
} else {
validProtocolFound = true;
}
if (validProtocolFound) {
tlsParams.setSSLSocketFactory(getSSLSocketFactory(sslProtocol, keyInfo.getAlias(), keyManagers, trustManagers));
}
} catch (KeyManagementException | NoSuchAlgorithmException e) {
LOGGER.debug("Unable to override default SSL Socket Factory", e);
}
} else {
tlsParams.setUseHttpsURLConnectionDefaultSslSocketFactory(true);
tlsParams.setCertAlias(SystemBaseUrl.INTERNAL.getHost());
}
httpConduit.setTlsClientParameters(tlsParams);
}
use of javax.net.ssl.KeyManager in project Payara by payara.
the class JSSE14SocketFactory method getKeyManagers.
/**
* Gets the initialized key managers.
*/
protected KeyManager[] getKeyManagers(String algorithm, String keyAlias) throws Exception {
KeyManager[] kms = null;
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
String keystorePass = getKeystorePassword();
ArrayList<KeyStore> ks = getKeystore(keystorePass);
if (keyAlias != null) {
boolean keyEntryMatch = false;
for (KeyStore keystore : ks) {
if (keystore.isKeyEntry(keyAlias)) {
kmf.init(keystore, keystorePass.toCharArray());
kms = kmf.getKeyManagers();
kms[0] = new JSSEKeyManager((X509KeyManager) kms[0], keyAlias);
keyEntryMatch = true;
break;
}
}
if (!keyEntryMatch) {
throw new IOException(sm.getString("jsse.alias_no_key_entry", keyAlias));
}
} else {
kmf.init(ks.get(0), keystorePass.toCharArray());
kms = kmf.getKeyManagers();
}
return kms;
}
use of javax.net.ssl.KeyManager in project Payara by payara.
the class SSLUtils method getAdminSSLContext.
/*
* @param alias the admin key alias
*
* @param protocol the protocol or null, uses "TLS" if this argument is null.
*
* @return the initialized SSLContext
*/
public SSLContext getAdminSSLContext(String alias, String protocol) {
try {
if (protocol == null) {
protocol = "TLS";
}
SSLContext adminSSLContextxt = SSLContext.getInstance(protocol);
KeyManager[] keyManagers = getKeyManagers();
if (alias != null && alias.length() > 0 && keyManagers != null) {
for (int i = 0; i < keyManagers.length; i++) {
keyManagers[i] = new J2EEKeyManager((X509KeyManager) keyManagers[i], alias);
}
}
adminSSLContextxt.init(keyManagers, getTrustManagers(), null);
return adminSSLContextxt;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.net.ssl.KeyManager in project chuidiang-ejemplos by chuidiang.
the class Main method coinfigureJettyTLs.
public static void coinfigureJettyTLs() throws Exception {
/*
* create a JettyHTTPServerEngineFactory to handle the configuration of
* network port numbers for use with "HTTPS"
*/
JettyHTTPServerEngineFactory jettyHTTPServerEngineFactory = new JettyHTTPServerEngineFactory();
// load the key store containing the server certificate
File keyStoreFile = new File(KEY_STORE_PATH_NAME);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(keyStoreFile), KEY_STORE_PASSWORD.toCharArray());
// create a key manager to handle the server private/public key pair
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, PRIVATE_KEY_PASSWORD.toCharArray());
KeyManager[] keyManager = keyManagerFactory.getKeyManagers();
// set the TLSServerParameters on theJettyHTTPServerEngineFactory
TLSServerParameters tLSServerParameters = new TLSServerParameters();
tLSServerParameters.setKeyManagers(keyManager);
jettyHTTPServerEngineFactory.setTLSServerParametersForPort(9443, tLSServerParameters);
}
use of javax.net.ssl.KeyManager in project qpid-broker-j by apache.
the class SSLUtil method createSslContext.
public static SSLContext createSslContext(final org.apache.qpid.server.model.KeyStore keyStore, final Collection<TrustStore> trustStores, final String portName) {
SSLContext sslContext;
try {
sslContext = tryGetSSLContext();
KeyManager[] keyManagers = keyStore.getKeyManagers();
TrustManager[] trustManagers;
if (trustStores == null || trustStores.isEmpty()) {
trustManagers = null;
} else if (trustStores.size() == 1) {
trustManagers = trustStores.iterator().next().getTrustManagers();
} else {
Collection<TrustManager> trustManagerList = new ArrayList<>();
final QpidMultipleTrustManager mulTrustManager = new QpidMultipleTrustManager();
for (TrustStore ts : trustStores) {
TrustManager[] managers = ts.getTrustManagers();
if (managers != null) {
for (TrustManager manager : managers) {
if (manager instanceof X509TrustManager) {
mulTrustManager.addTrustManager((X509TrustManager) manager);
} else {
trustManagerList.add(manager);
}
}
}
}
if (!mulTrustManager.isEmpty()) {
trustManagerList.add(mulTrustManager);
}
trustManagers = trustManagerList.toArray(new TrustManager[trustManagerList.size()]);
}
sslContext.init(keyManagers, trustManagers, null);
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException(String.format("Cannot configure TLS on port '%s'", portName), e);
}
return sslContext;
}
Aggregations