Example usage for javax.net.ssl X509KeyManager getPrivateKey

List of usage examples for javax.net.ssl X509KeyManager getPrivateKey

Introduction

In this page you can find the example usage for javax.net.ssl X509KeyManager getPrivateKey.

Prototype

public PrivateKey getPrivateKey(String alias);

Source Link

Document

Returns the key associated with the given alias.

Usage

From source file:de.betterform.connector.http.ssl.BetterFORMKeyStoreManager.java

public PrivateKey getPrivateKey(String alias) {
    Iterator<X509KeyManager> iterator = this.customX509KeyManagers.iterator();
    while (iterator.hasNext()) {
        X509KeyManager x509KeyManager = iterator.next();
        if (x509KeyManager.getPrivateKey(alias) != null) {
            BetterFORMKeyStoreManager.LOGGER
                    .trace("BetterFORMKeyStoreManager.getPrivateKey: Private key found for " + alias
                            + " in custom keystore: " + x509KeyManager.toString());
            //Found private key in a custom keystore return it.
            return x509KeyManager.getPrivateKey(alias);
        }//from  w  w  w  . j a va 2  s . c o m
    }

    //Return private key from JAVA VM keystor or null;
    return javaDefaultKeyManager.getPrivateKey(alias);
}

From source file:org.elasticsearch.xpack.security.authc.saml.SamlRealm.java

private static List<X509Credential> buildCredential(RealmConfig config, X509KeyPairSettings keyPairSettings,
        Setting<String> aliasSetting, final boolean allowMultiple) {
    final X509KeyManager keyManager = CertParsingUtils.getKeyManager(keyPairSettings, config.settings(), null,
            config.env());/*from   ww w  .  ja  va2s.  c om*/

    if (keyManager == null) {
        return null;
    }

    final Set<String> aliases = new HashSet<>();
    final String configuredAlias = aliasSetting.get(config.settings());
    if (Strings.isNullOrEmpty(configuredAlias)) {

        final String[] serverAliases = keyManager.getServerAliases("RSA", null);
        if (serverAliases != null) {
            aliases.addAll(Arrays.asList(serverAliases));
        }

        if (aliases.isEmpty()) {
            throw new IllegalArgumentException("The configured key store for "
                    + RealmSettings.getFullSettingKey(config, keyPairSettings.getPrefix())
                    + " does not contain any RSA key pairs");
        } else if (allowMultiple == false && aliases.size() > 1) {
            throw new IllegalArgumentException("The configured key store for "
                    + RealmSettings.getFullSettingKey(config, keyPairSettings.getPrefix())
                    + " has multiple keys but no alias has been specified (from setting "
                    + RealmSettings.getFullSettingKey(config, aliasSetting) + ")");
        }
    } else {
        aliases.add(configuredAlias);
    }

    final List<X509Credential> credentials = new ArrayList<>();
    for (String alias : aliases) {
        if (keyManager.getPrivateKey(alias) == null) {
            throw new IllegalArgumentException("The configured key store for "
                    + RealmSettings.getFullSettingKey(config, keyPairSettings.getPrefix())
                    + " does not have a key associated with alias [" + alias + "] "
                    + ((Strings.isNullOrEmpty(configuredAlias) == false)
                            ? "(from setting " + RealmSettings.getFullSettingKey(config, aliasSetting) + ")"
                            : ""));
        }

        final String keyType = keyManager.getPrivateKey(alias).getAlgorithm();
        if (keyType.equals("RSA") == false) {
            throw new IllegalArgumentException("The key associated with alias [" + alias + "] "
                    + "(from setting " + RealmSettings.getFullSettingKey(config, aliasSetting)
                    + ") uses unsupported key algorithm type [" + keyType + "], only RSA is supported");
        }
        credentials.add(new X509KeyManagerX509CredentialAdapter(keyManager, alias));
    }

    return credentials;
}