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 * 37 * @return secure random generator instance 38 * @throws NoSuchProviderException if the provider name specified by 39 * {@link #setProvider(String)} is not known to 40 * the platform 41 * @throws NoSuchAlgorithmException if the algorithm name specified by 42 * {@link #setAlgorithm(String)} is not 43 * recognized by the specified provider (or the 44 * platform's default provider if the provider 45 * isn't specified) 46 */ 47 public SecureRandom createSecureRandom() throws NoSuchProviderException, NoSuchAlgorithmException { 48 try { 49 return getProvider() != null ? SecureRandom.getInstance(getAlgorithm(), getProvider()) 50 : SecureRandom.getInstance(getAlgorithm()); 51 } catch (NoSuchProviderException ex) { 52 throw new NoSuchProviderException("no such secure random provider: " + getProvider()); 53 } catch (NoSuchAlgorithmException ex) { 54 throw new NoSuchAlgorithmException("no such secure random algorithm: " + getAlgorithm()); 55 } 56 } 57 58 /** 59 * Gets the secure random generator algorithm name. 60 * 61 * @return an algorithm name (e.g. {@code SHA1PRNG}); the 62 * {@link SSL#DEFAULT_SECURE_RANDOM_ALGORITHM} is returned if no 63 * algorithm has been specified 64 */ 65 public String getAlgorithm() { 66 if (algorithm == null) { 67 return SSL.DEFAULT_SECURE_RANDOM_ALGORITHM; 68 } 69 return algorithm; 70 } 71 72 /** 73 * Sets the secure random generator algorithm name. 74 * 75 * @param algorithm an algorithm name, which must be recognized by the provider 76 * specified via {@link #setProvider(String)} or by the 77 * platform's default provider if no provider is specified. 78 */ 79 public void setAlgorithm(String algorithm) { 80 this.algorithm = algorithm; 81 } 82 83 /** 84 * Gets the JCA provider name for the secure random generator. 85 * 86 * @return provider name 87 */ 88 public String getProvider() { 89 return provider; 90 } 91 92 /** 93 * Sets the JCA provider name for the secure random generator. 94 * 95 * @param provider name of the JCA provider to utilize in creating the secure 96 * random generator 97 */ 98 public void setProvider(String provider) { 99 this.provider = provider; 100 } 101 102 }