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.io.FileNotFoundException;
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.net.URL;
20  import java.security.KeyStore;
21  import java.security.KeyStoreException;
22  import java.security.NoSuchAlgorithmException;
23  import java.security.NoSuchProviderException;
24  
25  import ch.qos.logback.core.util.LocationUtil;
26  
27  /**
28   * A factory bean for a JCA {@link KeyStore}.
29   * <p>
30   * This object holds the configurable properties of a key store and uses
31   * them to create and load a {@link KeyStore} instance.
32   *
33   * @author Carl Harris
34   */
35  public class KeyStoreFactoryBean {
36  
37      private String location;
38      private String provider;
39      private String type;
40      private String password;
41  
42      /**
43       * Creates a new {@link KeyStore} using the receiver's configuration.
44       * @return key store
45       * @throws NoSuchProviderException if the provider specified by 
46       *    {@link #setProvider(String)} is not known to the platform
47       * @throws NoSuchAlgorithmException if the key store type specified by
48       *    {@link #setType(String)} is not known to the specified provider
49       *    (or the platform's default provider if the provider isn't specified)
50       * @throws KeyStoreException if some other error occurs in loading
51       *    the key store from the resource specified by 
52       *    {@link #setLocation(String)}
53       */
54      public KeyStore createKeyStore() throws NoSuchProviderException, NoSuchAlgorithmException, KeyStoreException {
55  
56          if (getLocation() == null) {
57              throw new IllegalArgumentException("location is required");
58          }
59  
60          InputStream inputStream = null;
61          try {
62              URL url = LocationUtil.urlForResource(getLocation());
63              inputStream = url.openStream();
64              KeyStore keyStore = newKeyStore();
65              keyStore.load(inputStream, getPassword().toCharArray());
66              return keyStore;
67          } catch (NoSuchProviderException ex) {
68              throw new NoSuchProviderException("no such keystore provider: " + getProvider());
69          } catch (NoSuchAlgorithmException ex) {
70              throw new NoSuchAlgorithmException("no such keystore type: " + getType());
71          } catch (FileNotFoundException ex) {
72              throw new KeyStoreException(getLocation() + ": file not found");
73          } catch (Exception ex) {
74              throw new KeyStoreException(getLocation() + ": " + ex.getMessage(), ex);
75          } finally {
76              try {
77                  if (inputStream != null) {
78                      inputStream.close();
79                  }
80              } catch (IOException ex) {
81                  ex.printStackTrace(System.err);
82              }
83          }
84      }
85  
86      /**
87       * Invokes the appropriate JCE factory method to obtain a new
88       * {@link KeyStore} object.
89       */
90      private KeyStore newKeyStore() throws NoSuchAlgorithmException, NoSuchProviderException, KeyStoreException {
91  
92          return getProvider() != null ? KeyStore.getInstance(getType(), getProvider()) : KeyStore.getInstance(getType());
93      }
94  
95      /**
96       * Gets the location of the key store resource.
97       * @return a String containing a URL for the resource
98       */
99      public String getLocation() {
100         return location;
101     }
102 
103     /**
104      * Sets the location of the key store resource.
105      * @param location a String containing a URL for the resource; if the 
106      *    URL string isn't prefixed by a scheme, the path is assumed to be 
107      *    relative to the root of the classpath.
108      */
109     public void setLocation(String location) {
110         this.location = location;
111     }
112 
113     /**
114      * Gets the type of key store to load.
115      * @return a key store type name (e.g. {@code JKS}); the 
116      *    {@link SSL#DEFAULT_KEYSTORE_TYPE} is returned if no type has been configured
117      */
118     public String getType() {
119         if (type == null) {
120             return SSL.DEFAULT_KEYSTORE_TYPE;
121         }
122         return type;
123     }
124 
125     /**
126      * Sets the type of key store to load.
127      * @param type a key store type name (e.g. {@code JKS}, {@code PKCS12});
128      *    the type specified must be supported by the provider specified by
129      *    {@link #setProvider(String)} or by the platform's default provider
130      *    if no provider is specified
131      */
132     public void setType(String type) {
133         this.type = type;
134     }
135 
136     /**
137      * Gets the JCA key store provider name.
138      * @return provider name or {@code null} if no provider has been configured
139      */
140     public String getProvider() {
141         return provider;
142     }
143 
144     /**
145      * Sets the JCA key store provider name.
146      * @param provider name of the JCA provider to utilize in creating the
147      *    key store
148      */
149     public void setProvider(String provider) {
150         this.provider = provider;
151     }
152 
153     /**
154      * Gets the password to use to access the key store.
155      * @return password string; the {@link SSL#DEFAULT_KEYSTORE_PASSWORD} is returned
156      *    if no password has been configured
157      */
158     public String getPassword() {
159         if (password == null) {
160             return SSL.DEFAULT_KEYSTORE_PASSWORD;
161         }
162         return password;
163     }
164 
165     /**
166      * Sets the password to use to access the keystore.
167      * @param password the password to set
168      */
169     public void setPassword(String password) {
170         this.password = password;
171     }
172 
173 }