001package org.apache.fulcrum.crypto;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.security.NoSuchAlgorithmException;
023import java.util.HashMap;
024
025import org.apache.avalon.framework.activity.Initializable;
026import org.apache.avalon.framework.configuration.Configurable;
027import org.apache.avalon.framework.configuration.Configuration;
028import org.apache.avalon.framework.configuration.ConfigurationException;
029import org.apache.avalon.framework.logger.AbstractLogEnabled;
030import org.apache.avalon.framework.thread.ThreadSafe;
031
032/**
033 * An implementation of CryptoService that uses either supplied crypto
034 * Algorithms (provided in the component config xml file) or tries to get them
035 * via the normal java mechanisms if this fails.
036 *
037 * avalon.component name="crypto" lifestyle="singleton" avalon.service
038 * type="org.apache.fulcrum.crypto.CryptoService"
039 *
040 *
041 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
042 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
043 * @version $Id: DefaultCryptoService.java 1852158 2019-01-25 18:19:46Z painter $
044 */
045public class DefaultCryptoService extends AbstractLogEnabled
046                implements CryptoService, Configurable, Initializable, ThreadSafe {
047
048        
049        /** Key Prefix for our algorithms */
050        private static final String ALGORITHM = "algorithm";
051
052        /** Default Key */
053        private static final String DEFAULT_KEY = "default";
054
055        /** Default Encryption Class */
056        private static final String DEFAULT_CLASS = "org.apache.fulcrum.crypto.provider.JavaCrypt";
057
058        /** Names of the registered algorithms and the wanted classes */
059        private HashMap<String, String> algos = new HashMap<>();
060
061        /**
062         * Returns a CryptoAlgorithm Object which represents the requested crypto
063         * algorithm.
064         *
065         * @see org.apache.fulcrum.crypto.CryptoService#getCryptoAlgorithm(java.lang.String)
066         *
067         * @param algo Name of the requested algorithm
068         *
069         * @return An Object representing the algorithm
070         *
071         * @throws NoSuchAlgorithmException Requested algorithm is not available
072         *
073         */
074        public CryptoAlgorithm getCryptoAlgorithm(String algo) throws NoSuchAlgorithmException 
075        {
076                String cryptoClass = (String) algos.get(algo);
077                CryptoAlgorithm ca = null;
078                if (cryptoClass == null) {
079                        cryptoClass = (String) algos.get(DEFAULT_KEY);
080                }
081                if (cryptoClass == null || cryptoClass.equalsIgnoreCase("none")) {
082                        throw new NoSuchAlgorithmException("TurbineCryptoService: No Algorithm for " + algo + " found");
083                }
084                try {
085                        ca = (CryptoAlgorithm) Class.forName(cryptoClass).newInstance();
086                } catch (Exception e) {
087                        throw new NoSuchAlgorithmException(
088                                        "TurbineCryptoService: Error instantiating " + cryptoClass + " for " + algo);
089                }
090                ca.setCipher(algo);
091                return ca;
092        }
093
094
095        // ---------------- Avalon Lifecycle Methods ---------------------
096
097        /**
098         * Avalon component lifecycle method
099         * 
100         * @param conf the configuration
101         * @throws ConfigurationException if not found
102         */
103        public void configure(Configuration conf) throws ConfigurationException 
104        {
105                // Initialize the hash and setup default
106                // Can be overriden by default key from properties
107                this.algos = new HashMap<>();
108                this.algos.put(DEFAULT_KEY, DEFAULT_CLASS);
109                
110                final Configuration algorithms = conf.getChild(ALGORITHM, false);
111                if (algorithms != null) {
112                        Configuration[] nameVal = algorithms.getChildren();
113                        for ( Configuration entry : nameVal )
114                        {
115                                algos.put(entry.getName(), entry.getValue());
116                        }
117                }
118        }
119        
120        /**
121         * Initialize the service
122         * 
123         * @see org.apache.avalon.framework.activity.Initializable#initialize()
124         * 
125         * @throws Exception if initialization fails
126         */
127        public void initialize() throws Exception 
128        {
129                getLogger().debug("initialize()");
130        }
131
132        /**
133         * Avalon component lifecycle method
134         */
135        public void dispose() 
136        {
137                algos = null;
138        }
139
140}