001    package org.apache.fulcrum.pbe;
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    
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    import java.security.GeneralSecurityException;
026    
027    import org.apache.avalon.framework.configuration.Configurable;
028    import org.apache.avalon.framework.configuration.Configuration;
029    import org.apache.avalon.framework.configuration.ConfigurationException;
030    import org.apache.avalon.framework.logger.AbstractLogEnabled;
031    import org.apache.fulcrum.jce.crypto.CryptoParameters;
032    import org.apache.fulcrum.jce.crypto.CryptoStreamFactory;
033    import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl;
034    import org.apache.fulcrum.jce.crypto.CryptoUtil;
035    import org.apache.fulcrum.jce.crypto.HexConverter;
036    import org.apache.fulcrum.jce.crypto.PasswordFactory;
037    import org.apache.fulcrum.jce.crypto.PasswordParameters;
038    
039    /**
040     * Encapsulates an PBE (Password Based Encryption) functionality
041     * from the JCE (Java Crypto Extension).
042     *
043     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
044     */
045    
046    public class PBEServiceImpl
047        extends AbstractLogEnabled
048        implements PBEService, Configurable
049    {
050        /** the internally used factory to create cipher streams */
051        private CryptoStreamFactory cryptoStreamFactory;
052    
053        /** the salt for generating the password */
054        private byte[] passwordSalt;
055    
056        /** the invocations of MessageDigest */
057        private int passwordCount;
058    
059        /** the default password */
060        private char[] defaultPassword;
061    
062        /**
063         * Constructor
064         */
065        public PBEServiceImpl()
066        {
067            // nothing to do
068        }
069    
070        /////////////////////////////////////////////////////////////////////////
071        // Avalon Service Lifecycle Implementation
072        /////////////////////////////////////////////////////////////////////////
073    
074        /**
075         * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
076         */
077        public void configure(Configuration configuration)
078            throws ConfigurationException
079        {
080            // read the parameters for CryptoStreamFactory
081    
082            byte[] cryptoSalt = CryptoParameters.SALT;
083            int cryptoCount = configuration.getChild("cyrptoCount").getValueAsInteger(CryptoParameters.COUNT);
084            String tempCryptoSalt = configuration.getChild("cryptoSalt").getValue("");
085    
086            if( tempCryptoSalt.length() > 0 )
087            {
088                cryptoSalt = HexConverter.toBytes( tempCryptoSalt );
089            }
090    
091            // create the CryptoStreamFactory to be used
092    
093            this.cryptoStreamFactory = new CryptoStreamFactoryImpl(
094                cryptoSalt,
095                cryptoCount
096                );
097    
098            // read the parameters for PasswordFactory
099    
100            this.passwordSalt = PasswordParameters.SALT;
101            this.passwordCount = configuration.getChild("passwordCount").getValueAsInteger(PasswordParameters.COUNT);
102            this.defaultPassword = PasswordParameters.DEFAULTPASSWORD;
103        }
104    
105    
106        /////////////////////////////////////////////////////////////////////////
107        // PBE Service Implementation
108        /////////////////////////////////////////////////////////////////////////
109    
110        /**
111         * @see org.apache.fulcrum.pbe.PBEService#createPassword()
112         */
113        public char[] createPassword() throws Exception
114        {
115            return PasswordFactory.create(
116                this.defaultPassword,
117                this.passwordSalt,
118                this.passwordCount
119                );
120        }
121    
122        /**
123         * @see org.apache.fulcrum.pbe.PBEService#createPassword(char[])
124         */
125        public char [] createPassword(char [] seed) throws Exception
126        {
127            return PasswordFactory.create(
128                seed,
129                this.passwordSalt,
130                this.passwordCount
131                );
132        }
133    
134        /**
135         * @see org.apache.fulcrum.pbe.PBEService#decryptString(java.lang.String, char[])
136         */
137        public String decryptString(String cipherText, char [] password)
138            throws GeneralSecurityException, IOException
139        {
140            return CryptoUtil.decryptString(
141                this.getCryptoStreamFactory(),
142                cipherText,
143                password
144                );
145        }
146    
147        /**
148         * @see org.apache.fulcrum.pbe.PBEService#encryptString(java.lang.String, char[])
149         */
150        public String encryptString(String plainText, char [] password)
151            throws GeneralSecurityException, IOException
152        {
153            return CryptoUtil.encryptString(
154                this.getCryptoStreamFactory(),
155                plainText,
156                password
157                );
158        }
159    
160        /**
161         * @see org.apache.fulcrum.pbe.PBEService#getInputStream(java.io.InputStream, char[])
162         */
163        public InputStream getInputStream(InputStream is, char [] password)
164            throws GeneralSecurityException, IOException
165        {
166            return this.getCryptoStreamFactory().getInputStream(
167                is,
168                password
169                );
170        }
171    
172        /**
173         * @see org.apache.fulcrum.pbe.PBEService#getSmartInputStream(java.io.InputStream, char[])
174         */
175        public InputStream getSmartInputStream(InputStream is, char [] password)
176            throws GeneralSecurityException, IOException
177        {
178            return this.getCryptoStreamFactory().getSmartInputStream(
179                is,
180                password
181                );
182        }
183    
184        /**
185         * @see org.apache.fulcrum.pbe.PBEService#getOutputStream(java.io.OutputStream, char[])
186         */
187        public OutputStream getOutputStream(OutputStream os, char [] password)
188            throws GeneralSecurityException, IOException
189        {
190            return this.getCryptoStreamFactory().getOutputStream(
191                os,
192                password
193                );
194        }
195    
196        /**
197         * @see org.apache.fulcrum.pbe.PBEService#decrypt(java.lang.Object, java.lang.Object, char[])
198         */
199        public void decrypt(Object source, Object target, char [] password)
200            throws GeneralSecurityException, IOException
201        {
202            CryptoUtil.decrypt(
203                this.getCryptoStreamFactory(),
204                source,
205                target,
206                password
207                );
208        }
209    
210        /**
211         * @see org.apache.fulcrum.pbe.PBEService#encrypt(java.lang.Object, java.lang.Object, char[])
212         */
213        public void encrypt(Object source, Object target, char [] password)
214            throws GeneralSecurityException, IOException
215        {
216            CryptoUtil.encrypt(
217                this.getCryptoStreamFactory(),
218                source,
219                target,
220                password
221                );
222        }
223    
224        /////////////////////////////////////////////////////////////////////////
225        // Service Implementation
226        /////////////////////////////////////////////////////////////////////////
227    
228        /**
229         * @return Returns the cryptoStreamFactory.
230         */
231        private CryptoStreamFactory getCryptoStreamFactory()
232        {
233            return cryptoStreamFactory;
234        }
235    }