Search in sources :

Example 31 with X509KeyManager

use of javax.net.ssl.X509KeyManager in project Payara by payara.

the class J2EEKeyManager method getPrivateKey.

/**
 * Return the private key for the specified alias.
 *
 * @param the alias.
 * @return the private key.
 */
@Override
public PrivateKey getPrivateKey(String alias) {
    LOGGER.log(FINE, "Getting private key for alias:{0}", alias);
    X509KeyManager keyMgr = getManagerFromToken(alias);
    if (keyMgr != null) {
        String aliasName = alias.substring(alias.indexOf(':') + 1);
        return keyMgr.getPrivateKey(aliasName);
    }
    return x509KeyManager.getPrivateKey(alias);
}
Also used : X509KeyManager(javax.net.ssl.X509KeyManager) UnifiedX509KeyManager(com.sun.enterprise.security.ssl.manager.UnifiedX509KeyManager)

Example 32 with X509KeyManager

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;
}
Also used : X509KeyManager(javax.net.ssl.X509KeyManager) UnifiedX509KeyManager(com.sun.enterprise.security.ssl.manager.UnifiedX509KeyManager)

Example 33 with X509KeyManager

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 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);
    }
}
Also used : X509KeyManager(javax.net.ssl.X509KeyManager) SSLContext(javax.net.ssl.SSLContext) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AccessControlException(java.security.AccessControlException)

Example 34 with X509KeyManager

use of javax.net.ssl.X509KeyManager in project Payara by payara.

the class RestClientSslContextAliasListener method buildSSlContext.

/**
 * This method evaluate the alias on the global keystore and return the corresponding SSLContext based on the alias
 * if not available the SSLContext should be the default that Jersey implementation set
 *
 * @param alias name of the certificate
 * @return the SSLContext with the corresponding certificate and alias name
 */
protected SSLContext buildSSlContext(String alias) {
    logger.log(Level.FINE, "Building the SSLContext for the alias");
    try {
        KeyManager[] managers = getKeyManagers();
        Optional<X509KeyManager> optionalKeyManager = null;
        optionalKeyManager = Arrays.stream(managers).filter(m -> (m instanceof X509KeyManager)).map(m -> ((X509KeyManager) m)).findFirst();
        KeyStore[] keyStores = getKeyStores();
        for (KeyStore ks : keyStores) {
            if (ks.containsAlias(alias) && optionalKeyManager.isPresent()) {
                X509KeyManager customKeyManager = new SingleCertificateKeyManager(alias, optionalKeyManager.get());
                SSLContext customSSLContext = SSLContext.getInstance("TLS");
                customSSLContext.init(new KeyManager[] { customKeyManager }, null, null);
                return customSSLContext;
            }
        }
    } catch (IOException e) {
        logger.severe("An IOException was thrown with the following message" + e.getMessage());
    } catch (KeyStoreException e) {
        logger.severe("A KeyStoreException was thrown with the following message" + e.getMessage());
    } catch (Exception e) {
        logger.severe("An Exception was thrown with the following message" + e.getMessage());
    }
    return null;
}
Also used : X509Certificate(java.security.cert.X509Certificate) X509KeyManager(javax.net.ssl.X509KeyManager) SSLContext(javax.net.ssl.SSLContext) Socket(java.net.Socket) Arrays(java.util.Arrays) Globals(org.glassfish.internal.api.Globals) IOException(java.io.IOException) KeyStore(java.security.KeyStore) Config(org.eclipse.microprofile.config.Config) KeyStoreException(java.security.KeyStoreException) Logger(java.util.logging.Logger) KeyManager(javax.net.ssl.KeyManager) Level(java.util.logging.Level) SSLUtils(com.sun.enterprise.security.ssl.SSLUtils) Principal(java.security.Principal) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) PrivateKey(java.security.PrivateKey) REST_CLIENT_CERTIFICATE_ALIAS(fish.payara.security.client.PayaraConstants.REST_CLIENT_CERTIFICATE_ALIAS) Optional(java.util.Optional) RestClientListener(org.eclipse.microprofile.rest.client.spi.RestClientListener) RestClientBuilder(org.eclipse.microprofile.rest.client.RestClientBuilder) NoSuchElementException(java.util.NoSuchElementException) MP_CONFIG_CLIENT_CERTIFICATE_ALIAS(fish.payara.security.client.PayaraConstants.MP_CONFIG_CLIENT_CERTIFICATE_ALIAS) X509KeyManager(javax.net.ssl.X509KeyManager) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchElementException(java.util.NoSuchElementException)

Example 35 with X509KeyManager

use of javax.net.ssl.X509KeyManager in project cxf by apache.

the class HttpConduitConfigurationTest method verifyConduit.

private void verifyConduit(HTTPConduit conduit) {
    AuthorizationPolicy authp = conduit.getAuthorization();
    assertNotNull(authp);
    assertEquals("Betty", authp.getUserName());
    assertEquals("password", authp.getPassword());
    TLSClientParameters tlscps = conduit.getTlsClientParameters();
    assertNotNull(tlscps);
    assertTrue(tlscps.isDisableCNCheck());
    assertEquals(3600000, tlscps.getSslCacheTimeout());
    KeyManager[] kms = tlscps.getKeyManagers();
    assertTrue(kms != null && kms.length == 1);
    assertTrue(kms[0] instanceof X509KeyManager);
    TrustManager[] tms = tlscps.getTrustManagers();
    assertTrue(tms != null && tms.length == 1);
    assertTrue(tms[0] instanceof X509TrustManager);
    FiltersType csfs = tlscps.getCipherSuitesFilter();
    assertNotNull(csfs);
    assertEquals(1, csfs.getInclude().size());
    assertEquals(1, csfs.getExclude().size());
    HTTPClientPolicy clientPolicy = conduit.getClient();
    assertEquals(10240, clientPolicy.getChunkLength());
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) TLSClientParameters(org.apache.cxf.configuration.jsse.TLSClientParameters) X509TrustManager(javax.net.ssl.X509TrustManager) X509KeyManager(javax.net.ssl.X509KeyManager) HTTPClientPolicy(org.apache.cxf.transports.http.configuration.HTTPClientPolicy) FiltersType(org.apache.cxf.configuration.security.FiltersType) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Aggregations

X509KeyManager (javax.net.ssl.X509KeyManager)66 KeyManager (javax.net.ssl.KeyManager)32 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)22 IOException (java.io.IOException)18 X509Certificate (java.security.cert.X509Certificate)17 KeyStore (java.security.KeyStore)16 SSLContext (javax.net.ssl.SSLContext)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 MethodSource (org.junit.jupiter.params.provider.MethodSource)12 GeneralSecurityException (java.security.GeneralSecurityException)8 CertificateException (java.security.cert.CertificateException)8 X509TrustManager (javax.net.ssl.X509TrustManager)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 PrivateKey (java.security.PrivateKey)7 TrustManager (javax.net.ssl.TrustManager)7 X509ExtendedKeyManager (javax.net.ssl.X509ExtendedKeyManager)7 UnifiedX509KeyManager (com.sun.enterprise.security.ssl.manager.UnifiedX509KeyManager)4 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 KeyPairGenerator (java.security.KeyPairGenerator)4