All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wss4j.common.crypto.CryptoFactory Maven / Gradle / Ivy

There is a newer version: 3.0.4
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.wss4j.common.crypto;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Properties;

import org.apache.wss4j.common.ext.WSSecurityException;
import org.apache.wss4j.common.util.Loader;

/**
 * CryptoFactory.
 */
public abstract class CryptoFactory {
    private static final org.slf4j.Logger LOG =
        org.slf4j.LoggerFactory.getLogger(CryptoFactory.class);

    /**
     * getInstance
     * 

* Returns an instance of Crypto. This method uses the file * crypto.properties to determine which implementation to * use. Thus the property org.apache.wss4j.crypto.provider * must define the classname of the Crypto implementation. The file * may contain other property definitions as well. These properties are * handed over to the Crypto implementation. The file * crypto.properties is loaded with the * Loader.getResource() method. *

* * @return The crypto implementation was defined * @throws WSSecurityException if there is an error in loading the crypto properties */ public static Crypto getInstance() throws WSSecurityException { return getInstance("crypto.properties"); } /** * getInstance *

* Returns an instance of Crypto. The properties are handed over the the crypto * implementation. The properties must at least contain the Crypto implementation * class name as the value of the property : org.apache.wss4j.crypto.provider *

* * @param properties The Properties that are forwarded to the crypto implementation * and the Crypto impl class name. * These properties are dependent on the crypto implementation * @return The cyrpto implementation or null if no cryptoClassName was defined * @throws WSSecurityException if there is an error in loading the crypto properties */ public static Crypto getInstance(Properties properties) throws WSSecurityException { if (properties == null) { LOG.debug("Cannot load Crypto instance as properties object is null"); throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "empty", new Object[] {"Cannot load Crypto instance as properties object is null"}); } return getInstance(properties, Loader.getClassLoader(CryptoFactory.class), null); } /** * getInstance *

* Returns an instance of Crypto loaded with the given classloader. * The properties are handed over the the crypto implementation. * The properties must at least contain the Crypto implementation * class name as the value of the property : org.apache.wss4j.crypto.provider *

* * @param properties The Properties that are forwarded to the crypto implementation * and the Crypto impl class name. * These properties are dependent on the crypto implementation * @param classLoader The class loader to use * @param passwordEncryptor The PasswordEncryptor to use to decrypt encrypted passwords * @return The crypto implementation or null if no cryptoClassName was defined * @throws WSSecurityException if there is an error in loading the crypto properties */ public static Crypto getInstance( Properties properties, ClassLoader classLoader, PasswordEncryptor passwordEncryptor ) throws WSSecurityException { if (properties == null) { LOG.debug("Cannot load Crypto instance as properties object is null"); throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "empty", new Object[] {"Cannot load Crypto instance as properties object is null"}); } String cryptoClassName = properties.getProperty("org.apache.wss4j.crypto.provider"); if (cryptoClassName == null) { cryptoClassName = properties.getProperty("org.apache.ws.security.crypto.provider"); } Class cryptoClass = null; if (cryptoClassName == null || cryptoClassName.equals("org.apache.wss4j.common.crypto.Merlin") || cryptoClassName.equals("org.apache.ws.security.components.crypto.Merlin")) { try { return new Merlin(properties, classLoader, passwordEncryptor); } catch (java.lang.Exception e) { LOG.debug("Unable to instantiate Merlin", e); throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e, "empty", new Object[] {"Cannot create Crypto class " + cryptoClassName}); } } else { try { // instruct the class loader to load the crypto implementation cryptoClass = Loader.loadClass(cryptoClassName, Crypto.class); } catch (ClassNotFoundException ex) { LOG.debug(ex.getMessage(), ex); throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, ex, "empty", new Object[] {cryptoClassName + " Not Found"}); } } return loadClass(cryptoClass, properties, classLoader); } /** * getInstance *

* Returns an instance of Crypto. The supplied map is handed over the the crypto * implementation. The map can be null. It is dependent on the * Crypto implementation how the initialization is done in this case. *

* * @param cryptoClass This is the crypto implementation class. No default is * provided here. * @param map The Maps that is forwarded to the crypto implementation. * These contents of the map are dependent on the * underlying crypto implementation specified in the * cryptoClassName parameter. * @return The crypto implementation or null if no cryptoClassName was defined * @throws WSSecurityException if there is an error in loading the crypto properties */ public static Crypto getInstance( Class cryptoClass, Map map ) throws WSSecurityException { return loadClass(cryptoClass, map, Loader.getClassLoader(CryptoFactory.class)); } /** * getInstance *

* Returns an instance of Crypto. This method uses the specified filename * to load a property file. This file shall use the property * org.apache.wss4j.crypto.provider * to define the classname of the Crypto implementation. The file * may contain other property definitions as well. These properties are * handed over to the Crypto implementation. The specified file * is loaded with the Loader.getResource() method. *

* * @param propFilename The name of the property file to load * @return The crypto implementation that was defined * @throws WSSecurityException if there is an error in loading the crypto properties */ public static Crypto getInstance(String propFilename) throws WSSecurityException { return getInstance(propFilename, Loader.getClassLoader(CryptoFactory.class)); } public static Crypto getInstance( String propFilename, ClassLoader customClassLoader ) throws WSSecurityException { Properties properties = getProperties(propFilename, customClassLoader); return getInstance(properties, customClassLoader, null); } /** * This allows loading the classes with a custom class loader * @param cryptoClass * @param map * @param loader * @throws WSSecurityException if there is an error in loading the crypto properties */ private static Crypto loadClass( Class cryptoClass, Map map, ClassLoader loader ) throws WSSecurityException { LOG.debug("Using Crypto Engine [{}]", cryptoClass); try { Constructor c = null; try { Class[] classes = new Class[]{Map.class, ClassLoader.class}; c = cryptoClass.getConstructor(classes); return c.newInstance(map, loader); } catch (NoSuchMethodException ex) { Class[] classes = new Class[]{Properties.class, Map.class, PasswordEncryptor.class}; c = cryptoClass.getConstructor(classes); return c.newInstance(map, loader, null); } } catch (java.lang.Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("Unable to instantiate: " + cryptoClass.getName(), e); } throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e, "empty", new Object[] {cryptoClass + " cannot create instance"}); } } /** * This allows loading the classes with a custom class loader * @param cryptoClass * @param map * @param loader * @throws WSSecurityException if there is an error in loading the crypto properties */ private static Crypto loadClass( Class cryptoClass, Properties map, ClassLoader loader ) throws WSSecurityException { LOG.debug("Using Crypto Engine [{}]", cryptoClass); try { Constructor c = null; try { Class[] classes = new Class[]{Properties.class, ClassLoader.class}; c = cryptoClass.getConstructor(classes); return c.newInstance(map, loader); } catch (NoSuchMethodException ex) { Class[] classes = new Class[]{Properties.class, ClassLoader.class, PasswordEncryptor.class}; c = cryptoClass.getConstructor(classes); return c.newInstance(map, loader, null); } } catch (java.lang.Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("Unable to instantiate: " + cryptoClass.getName(), e); } throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e, "empty", new Object[] {cryptoClass + " cannot create instance"}); } } /** * This allows loading the resources with a custom class loader * @param propFilename * @param loader * @return a Properties object loaded from the propFilename argument * @throws WSSecurityException if there is an error in loading the crypto properties */ public static Properties getProperties( String propFilename, ClassLoader loader ) throws WSSecurityException { Properties properties = new Properties(); try { InputStream is = Loader.loadInputStream(loader, propFilename); if (is == null) { throw new WSSecurityException( WSSecurityException.ErrorCode.FAILURE, "resourceNotFound", new Object[] {propFilename} ); } properties.load(is); is.close(); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Cannot find resource: " + propFilename, e); } throw new WSSecurityException( WSSecurityException.ErrorCode.FAILURE, e, "resourceNotFound", new Object[] {propFilename} ); } return properties; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy