Example usage for java.security.cert CertificateFactory getProvider

List of usage examples for java.security.cert CertificateFactory getProvider

Introduction

In this page you can find the example usage for java.security.cert CertificateFactory getProvider.

Prototype

public final Provider getProvider() 

Source Link

Document

Returns the provider of this certificate factory.

Usage

From source file:org.apache.ws.security.components.crypto.CryptoBase.java

/**
 * Singleton certificate factory for this Crypto instance.
 * <p/>/*from   w w  w .java  2  s.  co m*/
 *
 * @return Returns a <code>CertificateFactory</code> to construct
 *         X509 certificates
 * @throws org.apache.ws.security.WSSecurityException
 *
 */
public synchronized CertificateFactory getCertificateFactory() throws WSSecurityException {
    String provider = getCryptoProvider();
    String keyStoreProvider = keystore == null ? null : keystore.getProvider().getName();

    //Try to find a CertificateFactory that generates certs that are fully
    //compatible with the certs in the KeyStore  (Sun -> Sun, BC -> BC, etc...)
    CertificateFactory factory = null;
    if (provider != null) {
        factory = (CertificateFactory) certFactMap.get(provider);
    } else if (keyStoreProvider != null) {
        factory = (CertificateFactory) certFactMap.get(mapKeystoreProviderToCertProvider(keyStoreProvider));
        if (factory == null) {
            factory = (CertificateFactory) certFactMap.get(keyStoreProvider);
        }
    } else {
        factory = (CertificateFactory) certFactMap.get("DEFAULT");
    }
    if (factory == null) {
        try {
            if (provider == null || provider.length() == 0) {
                if (keyStoreProvider != null && keyStoreProvider.length() != 0) {
                    try {
                        factory = CertificateFactory.getInstance("X.509",
                                mapKeystoreProviderToCertProvider(keyStoreProvider));
                        certFactMap.put(keyStoreProvider, factory);
                        certFactMap.put(mapKeystoreProviderToCertProvider(keyStoreProvider), factory);
                    } catch (Exception ex) {
                        log.debug(ex);
                        //Ignore, we'll just use the default since they didn't specify one.
                        //Hopefully that will work for them.
                    }
                }
                if (factory == null) {
                    factory = CertificateFactory.getInstance("X.509");
                    certFactMap.put("DEFAULT", factory);
                }
            } else {
                factory = CertificateFactory.getInstance("X.509", provider);
                certFactMap.put(provider, factory);
            }
            certFactMap.put(factory.getProvider().getName(), factory);
        } catch (CertificateException e) {
            throw new WSSecurityException(WSSecurityException.SECURITY_TOKEN_UNAVAILABLE, "unsupportedCertType",
                    null, e);
        } catch (NoSuchProviderException e) {
            throw new WSSecurityException(WSSecurityException.SECURITY_TOKEN_UNAVAILABLE, "noSecProvider", null,
                    e);
        }
    }
    return factory;
}

From source file:org.apache.ws.security.components.crypto.Merlin.java

/**
 * Singleton certificate factory for this Crypto instance.
 * <p/>/*ww  w  .j  a v a  2s  .  co m*/
 *
 * @return Returns a <code>CertificateFactory</code> to construct
 *         X509 certificates
 * @throws org.apache.ws.security.WSSecurityException
 */
@Override
public CertificateFactory getCertificateFactory() throws WSSecurityException {
    String provider = getCryptoProvider();
    String keyStoreProvider = null;
    if (keystore != null) {
        keyStoreProvider = keystore.getProvider().getName();
    }

    //Try to find a CertificateFactory that generates certs that are fully
    //compatible with the certs in the KeyStore  (Sun -> Sun, BC -> BC, etc...)
    CertificateFactory factory = null;
    if (provider != null) {
        factory = certFactMap.get(provider);
    } else if (keyStoreProvider != null) {
        factory = certFactMap.get(mapKeystoreProviderToCertProvider(keyStoreProvider));
        if (factory == null) {
            factory = certFactMap.get(keyStoreProvider);
        }
    } else {
        factory = certFactMap.get("DEFAULT");
    }
    if (factory == null) {
        try {
            if (provider == null || provider.length() == 0) {
                if (keyStoreProvider != null && keyStoreProvider.length() != 0) {
                    try {
                        factory = CertificateFactory.getInstance("X.509",
                                mapKeystoreProviderToCertProvider(keyStoreProvider));
                        certFactMap.put(keyStoreProvider, factory);
                        certFactMap.put(mapKeystoreProviderToCertProvider(keyStoreProvider), factory);
                    } catch (Exception ex) {
                        LOG.debug(ex);
                        //Ignore, we'll just use the default since they didn't specify one.
                        //Hopefully that will work for them.
                    }
                }
                if (factory == null) {
                    factory = CertificateFactory.getInstance("X.509");
                    certFactMap.put("DEFAULT", factory);
                }
            } else {
                factory = CertificateFactory.getInstance("X.509", provider);
                certFactMap.put(provider, factory);
            }
            certFactMap.put(factory.getProvider().getName(), factory);
        } catch (CertificateException e) {
            throw new WSSecurityException(WSSecurityException.SECURITY_TOKEN_UNAVAILABLE, "unsupportedCertType",
                    null, e);
        } catch (NoSuchProviderException e) {
            throw new WSSecurityException(WSSecurityException.SECURITY_TOKEN_UNAVAILABLE, "noSecProvider", null,
                    e);
        }
    }
    return factory;
}