use of javax.net.ssl.X509KeyManager in project Payara by payara.
the class J2EEKeyManager method getManagerFromToken.
/**
* Find the corresponding X509KeyManager associated to token in alias.
* It returns null if there is n
* @param tokenAlias of the form <tokenName>:<aliasName>
*/
private X509KeyManager getManagerFromToken(String tokenAlias) {
X509KeyManager keyMgr = null;
int ind = -1;
if (supportTokenAlias && tokenAlias != null && (ind = tokenAlias.indexOf(':')) != -1) {
String tokenName = alias.substring(0, ind);
keyMgr = tokenName2MgrMap.get(tokenName);
}
return keyMgr;
}
use of javax.net.ssl.X509KeyManager 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 cntxt = SSLContext.getInstance(protocol);
KeyManager[] kMgrs = getKeyManagers();
if (alias != null && alias.length() > 0 && kMgrs != null) {
for (int i = 0; i < kMgrs.length; i++) {
kMgrs[i] = new J2EEKeyManager((X509KeyManager) kMgrs[i], alias);
}
}
cntxt.init(kMgrs, getTrustManagers(), null);
return cntxt;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.net.ssl.X509KeyManager 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;
String keystorePass = getKeystorePassword();
KeyStore ks = getKeystore(keystorePass);
if (keyAlias != null && !ks.isKeyEntry(keyAlias)) {
throw new IOException(sm.getString("jsse.alias_no_key_entry", keyAlias));
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(ks, keystorePass.toCharArray());
kms = kmf.getKeyManagers();
if (keyAlias != null) {
for (int i = 0; i < kms.length; i++) {
kms[i] = new JSSEKeyManager((X509KeyManager) kms[i], keyAlias);
}
}
return kms;
}
use of javax.net.ssl.X509KeyManager in project cas by apereo.
the class FileTrustStoreSslSocketFactory method getTrustedSslContext.
/**
* Gets the trusted ssl context.
*
* @param trustStoreFile the trust store file
* @param trustStorePassword the trust store password
* @param trustStoreType the trust store type
* @return the trusted ssl context
*/
private static SSLContext getTrustedSslContext(final Resource trustStoreFile, final String trustStorePassword, final String trustStoreType) {
try {
final KeyStore casTrustStore = KeyStore.getInstance(trustStoreType);
final char[] trustStorePasswordCharArray = trustStorePassword.toCharArray();
try (InputStream casStream = trustStoreFile.getInputStream()) {
casTrustStore.load(casStream, trustStorePasswordCharArray);
}
final String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
final X509KeyManager customKeyManager = getKeyManager(ALG_NAME_PKIX, casTrustStore, trustStorePasswordCharArray);
final X509KeyManager jvmKeyManager = getKeyManager(defaultAlgorithm, null, null);
final X509TrustManager customTrustManager = getTrustManager(ALG_NAME_PKIX, casTrustStore);
final X509TrustManager jvmTrustManager = getTrustManager(defaultAlgorithm, null);
final KeyManager[] keyManagers = { new CompositeX509KeyManager(Arrays.asList(jvmKeyManager, customKeyManager)) };
final TrustManager[] trustManagers = { new CompositeX509TrustManager(Arrays.asList(jvmTrustManager, customTrustManager)) };
final SSLContext context = SSLContexts.custom().useProtocol("SSL").build();
context.init(keyManagers, trustManagers, null);
return context;
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw Throwables.propagate(e);
}
}
use of javax.net.ssl.X509KeyManager in project tomcat by apache.
the class JSSEUtil method getKeyManagers.
@Override
public KeyManager[] getKeyManagers() throws Exception {
String keystoreType = certificate.getCertificateKeystoreType();
String keyAlias = certificate.getCertificateKeyAlias();
String algorithm = sslHostConfig.getKeyManagerAlgorithm();
String keyPass = certificate.getCertificateKeyPassword();
// defaults vary between JSSE and OpenSSL.
if (keyPass == null) {
keyPass = certificate.getCertificateKeystorePassword();
}
KeyManager[] kms = null;
KeyStore ks = certificate.getCertificateKeystore();
if (ks == null) {
// create an in-memory keystore and import the private key
// and the certificate chain from the PEM files
ks = KeyStore.getInstance("JKS");
ks.load(null, null);
PEMFile privateKeyFile = new PEMFile(SSLHostConfig.adjustRelativePath(certificate.getCertificateKeyFile() != null ? certificate.getCertificateKeyFile() : certificate.getCertificateFile()), keyPass);
PEMFile certificateFile = new PEMFile(SSLHostConfig.adjustRelativePath(certificate.getCertificateFile()));
Collection<Certificate> chain = new ArrayList<>();
chain.addAll(certificateFile.getCertificates());
if (certificate.getCertificateChainFile() != null) {
PEMFile certificateChainFile = new PEMFile(SSLHostConfig.adjustRelativePath(certificate.getCertificateChainFile()));
chain.addAll(certificateChainFile.getCertificates());
}
if (keyAlias == null) {
keyAlias = "tomcat";
}
ks.setKeyEntry(keyAlias, privateKeyFile.getPrivateKey(), keyPass.toCharArray(), chain.toArray(new Certificate[chain.size()]));
}
if (keyAlias != null && !ks.isKeyEntry(keyAlias)) {
throw new IOException(sm.getString("jsse.alias_no_key_entry", keyAlias));
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(ks, keyPass.toCharArray());
kms = kmf.getKeyManagers();
if (kms == null) {
return kms;
}
if (keyAlias != null) {
String alias = keyAlias;
// JKS keystores always convert the alias name to lower case
if ("JKS".equals(keystoreType)) {
alias = alias.toLowerCase(Locale.ENGLISH);
}
for (int i = 0; i < kms.length; i++) {
kms[i] = new JSSEKeyManager((X509KeyManager) kms[i], alias);
}
}
return kms;
}
Aggregations