001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util.jsse;
018
019import java.security.GeneralSecurityException;
020import java.security.SecureRandom;
021import java.security.Security;
022
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026public class SecureRandomParameters extends JsseParameters {
027    
028    private static final Logger LOG = LoggerFactory.getLogger(SecureRandomParameters.class);
029
030    /**
031     * The Random Number Generator algorithm identifier for the
032     * {@link SecureRandom} factory method used to create the
033     * {@link SecureRandom} represented by this object's configuration. See
034     * Appendix A in the <a href=
035     * "http://download.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"
036     * > Java Cryptography Architecture API Specification &amp; Reference</a>
037     * for information about standard RNG algorithm names.
038     */
039    protected String algorithm;
040    
041    /**
042     * The optional provider identifier for the {@link SecureRandom} factory
043     * method used to create the {@link SecureRandom} represented by this
044     * object's configuration.
045     */
046    protected String provider;
047
048    /**
049     * Returns a {@code SecureRandom} instance initialized using the configured
050     * algorithm and provider, if specified.
051     * 
052     * @return the configured instance
053     *
054     * @throws GeneralSecurityException if the algorithm is not implemented by
055     *             any registered provider or if the identified provider does
056     *             not exist.
057     */
058    public SecureRandom createSecureRandom() throws GeneralSecurityException {
059        LOG.debug("Creating SecureRandom from SecureRandomParameters: {}", this);
060
061        SecureRandom secureRandom;
062        if (this.getProvider() != null) {
063            secureRandom = SecureRandom.getInstance(this.parsePropertyValue(this.getAlgorithm()),
064                                                    this.parsePropertyValue(this.getProvider()));
065        } else {
066            secureRandom = SecureRandom.getInstance(this.parsePropertyValue(this.getAlgorithm()));
067        }
068        
069        LOG.debug("SecureRandom [{}] is using provider [{}] and algorithm [{}].",
070                  new Object[] {secureRandom, secureRandom.getProvider(), secureRandom.getAlgorithm()});
071
072        return secureRandom;
073    }
074    
075    /**
076     * @see #setAlgorithm(String)
077     */
078    public String getAlgorithm() {
079        return algorithm;
080    }
081
082    /**
083     * Sets the Random Number Generator (RNG) algorithm identifier for the
084     * {@link SecureRandom} factory method used to create the
085     * {@link SecureRandom} represented by this object's configuration.
086     * See Appendix A in the <a href=
087     * "http://download.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA"
088     * > Java Cryptography Architecture Reference Guide</a>
089     * for information about standard RNG algorithm names.
090     *
091     * @param value the algorithm identifier
092     */
093    public void setAlgorithm(String value) {
094        this.algorithm = value;
095    }
096
097    /**
098     * @see #setProvider(String)
099     */
100    public String getProvider() {
101        return provider;
102    }
103
104    /**
105     * Sets the optional provider identifier for the {@link SecureRandom}
106     * factory method used to create the {@link SecureRandom} represented by
107     * this object's configuration.
108     * 
109     * @param value the provider identifier or {@code null} to use the highest
110     *            priority provider implementing the desired algorithm
111     *            
112     * @see Security#getProviders()
113     */
114    public void setProvider(String value) {
115        this.provider = value;
116    }
117
118    @Override
119    public String toString() {
120        StringBuilder builder = new StringBuilder();
121        builder.append("SecureRandomParameters[algorithm=");
122        builder.append(algorithm);
123        builder.append(", provider=");
124        builder.append(provider);
125        builder.append("]");
126        return builder.toString();
127    }
128}