View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net.ssl;
15  
16  import java.security.NoSuchAlgorithmException;
17  import java.security.NoSuchProviderException;
18  import java.security.SecureRandom;
19  
20  /**
21   * A factory bean for a JCA {@link SecureRandom} generator.
22   * <p>
23   * This object holds the configurable properties of a secure random generator
24   * and uses them to create and load a {@link SecureRandom} instance.
25   *
26   * @author Carl Harris
27   */
28  public class SecureRandomFactoryBean {
29  
30      private String algorithm;
31      private String provider;
32  
33      /**
34       * Creates a new {@link SecureRandom} generator using the receiver's 
35       * configuration.
36       * @return secure random generator instance
37       * @throws NoSuchProviderException if the provider name specified by
38       *    {@link #setProvider(String)} is not known to the platform
39       * @throws NoSuchAlgorithmException if the algorithm name specified by
40       *    {@link #setAlgorithm(String)} is not recognized by the specified
41       *    provider (or the platform's default provider if the provider isn't 
42       *    specified)
43       */
44      public SecureRandom createSecureRandom() throws NoSuchProviderException, NoSuchAlgorithmException {
45          try {
46              return getProvider() != null ? SecureRandom.getInstance(getAlgorithm(), getProvider()) : SecureRandom.getInstance(getAlgorithm());
47          } catch (NoSuchProviderException ex) {
48              throw new NoSuchProviderException("no such secure random provider: " + getProvider());
49          } catch (NoSuchAlgorithmException ex) {
50              throw new NoSuchAlgorithmException("no such secure random algorithm: " + getAlgorithm());
51          }
52      }
53  
54      /**
55       * Gets the secure random generator algorithm name. 
56       * @return an algorithm name (e.g. {@code SHA1PRNG}); the 
57       *    {@link SSL#DEFAULT_SECURE_RANDOM_ALGORITHM} is returned if no algorithm has been
58       *    specified
59       */
60      public String getAlgorithm() {
61          if (algorithm == null) {
62              return SSL.DEFAULT_SECURE_RANDOM_ALGORITHM;
63          }
64          return algorithm;
65      }
66  
67      /**
68       * Sets the secure random generator algorithm name.
69       * @param algorithm an algorithm name, which must be recognized by the
70       *    provider specified via {@link #setProvider(String)} or by the
71       *    platform's default provider if no provider is specified.
72       */
73      public void setAlgorithm(String algorithm) {
74          this.algorithm = algorithm;
75      }
76  
77      /**
78       * Gets the JCA provider name for the secure random generator.
79       * @return provider name
80       */
81      public String getProvider() {
82          return provider;
83      }
84  
85      /**
86       * Sets the JCA provider name for the secure random generator.
87       * @param provider name of the JCA provider to utilize in creating the
88       *    secure random generator
89       */
90      public void setProvider(String provider) {
91          this.provider = provider;
92      }
93  
94  }