001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.net.ssl; 015 016import java.security.NoSuchAlgorithmException; 017import java.security.NoSuchProviderException; 018import java.security.SecureRandom; 019 020/** 021 * A factory bean for a JCA {@link SecureRandom} generator. 022 * <p> 023 * This object holds the configurable properties of a secure random generator 024 * and uses them to create and load a {@link SecureRandom} instance. 025 * 026 * @author Carl Harris 027 */ 028public class SecureRandomFactoryBean { 029 030 private String algorithm; 031 private String provider; 032 033 /** 034 * Creates a new {@link SecureRandom} generator using the receiver's 035 * configuration. 036 * 037 * @return secure random generator instance 038 * @throws NoSuchProviderException if the provider name specified by 039 * {@link #setProvider(String)} is not known to 040 * the platform 041 * @throws NoSuchAlgorithmException if the algorithm name specified by 042 * {@link #setAlgorithm(String)} is not 043 * recognized by the specified provider (or the 044 * platform's default provider if the provider 045 * isn't specified) 046 */ 047 public SecureRandom createSecureRandom() throws NoSuchProviderException, NoSuchAlgorithmException { 048 try { 049 return getProvider() != null ? SecureRandom.getInstance(getAlgorithm(), getProvider()) 050 : SecureRandom.getInstance(getAlgorithm()); 051 } catch (NoSuchProviderException ex) { 052 throw new NoSuchProviderException("no such secure random provider: " + getProvider()); 053 } catch (NoSuchAlgorithmException ex) { 054 throw new NoSuchAlgorithmException("no such secure random algorithm: " + getAlgorithm()); 055 } 056 } 057 058 /** 059 * Gets the secure random generator algorithm name. 060 * 061 * @return an algorithm name (e.g. {@code SHA1PRNG}); the 062 * {@link SSL#DEFAULT_SECURE_RANDOM_ALGORITHM} is returned if no 063 * algorithm has been specified 064 */ 065 public String getAlgorithm() { 066 if (algorithm == null) { 067 return SSL.DEFAULT_SECURE_RANDOM_ALGORITHM; 068 } 069 return algorithm; 070 } 071 072 /** 073 * Sets the secure random generator algorithm name. 074 * 075 * @param algorithm an algorithm name, which must be recognized by the provider 076 * specified via {@link #setProvider(String)} or by the 077 * platform's default provider if no provider is specified. 078 */ 079 public void setAlgorithm(String algorithm) { 080 this.algorithm = algorithm; 081 } 082 083 /** 084 * Gets the JCA provider name for the secure random generator. 085 * 086 * @return provider name 087 */ 088 public String getProvider() { 089 return provider; 090 } 091 092 /** 093 * Sets the JCA provider name for the secure random generator. 094 * 095 * @param provider name of the JCA provider to utilize in creating the secure 096 * random generator 097 */ 098 public void setProvider(String provider) { 099 this.provider = provider; 100 } 101 102}