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 */ 017 package org.apache.camel.util.jsse; 018 019 import java.io.IOException; 020 import java.io.InputStream; 021 import java.security.GeneralSecurityException; 022 import java.security.KeyStore; 023 import java.security.Security; 024 025 import org.slf4j.Logger; 026 import org.slf4j.LoggerFactory; 027 028 /** 029 * A representation of configuration options for creating and loading a 030 * {@link KeyStore} instance. 031 */ 032 public class KeyStoreParameters extends JsseParameters { 033 034 private static final Logger LOG = LoggerFactory.getLogger(KeyStoreParameters.class); 035 036 /** 037 * The optional type of the key store to load. See Appendix A in the 038 * <a href="http://download.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#KeyStore"> 039 * Java Cryptography Architecture Standard Algorithm Name Documentation</a> for more information on standard names. 040 */ 041 protected String type; 042 043 /** 044 * The optional password for reading/opening/verifying the key store. 045 */ 046 protected String password; 047 048 /** 049 * The optional provider identifier for instantiating the key store. 050 */ 051 protected String provider; 052 053 /** 054 * The optional file path, class path resource, or URL of the resource 055 * used to load the key store. 056 */ 057 protected String resource; 058 059 /** 060 * @see #setType(String) 061 */ 062 public String getType() { 063 return type; 064 } 065 066 /** 067 * Sets the type of the key store to create and load. See Appendix A in the 068 * <a href="http://download.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#KeyStore" 069 * >Java Cryptography Architecture Standard Algorithm Name 070 * Documentation</a> for more information on standard names. 071 * 072 * @param value the key store type identifier (may be {@code null}) 073 */ 074 public void setType(String value) { 075 this.type = value; 076 } 077 078 /** 079 * @see #getPassword() 080 */ 081 public String getPassword() { 082 return password; 083 } 084 085 /** 086 * Set the optional password for reading/opening/verifying the key store. 087 * 088 * @param value the password value (may be {@code null}) 089 */ 090 public void setPassword(String value) { 091 this.password = value; 092 } 093 094 /** 095 * @see #setProvider(String) 096 */ 097 public String getProvider() { 098 return provider; 099 } 100 101 /** 102 * Sets the optional provider identifier for instantiating the key store. 103 * 104 * @param value the provider identifier (may be {@code null}) 105 * 106 * @see Security#getProviders() 107 */ 108 public void setProvider(String value) { 109 this.provider = value; 110 } 111 112 /** 113 * @see #getResource() 114 */ 115 public String getResource() { 116 return resource; 117 } 118 119 /** 120 * Sets the optional file path, class path resource, or URL of the resource 121 * used to load the key store. 122 * 123 * @param value the resource (may be {@code null}) 124 */ 125 public void setResource(String value) { 126 this.resource = value; 127 } 128 129 /** 130 * Creates a {@link KeyStoreParameters} instance based off of the configuration state 131 * of this instance. If {@link #getType()} returns {@code null}, the default 132 * key store type is loaded, otherwise the type will be of that specified. 133 * <p/> 134 * The created instance will always be loaded, but if the type requires an 135 * input stream and {@link #getResource()} returns {@code null}, the 136 * instance will be empty. The loading of the resource, if not {@code null}, 137 * is attempted by treating the resource as a file path, a class path 138 * resource, and a URL in that order. An exception is thrown if the resource 139 * cannot be resolved to readable input stream using any of the above 140 * methods. 141 * 142 * @return a configured and loaded key store 143 * @throws GeneralSecurityException if there is an error creating an instance 144 * with the given configuration 145 * @throws IOException if there is an error resolving the configured 146 * resource to an input stream 147 */ 148 public KeyStore createKeyStore() throws GeneralSecurityException, IOException { 149 LOG.debug("Creating KeyStore instance from KeyStoreParameters: {}", this); 150 151 String ksType = this.parsePropertyValue(this.type); 152 if (ksType == null) { 153 ksType = KeyStore.getDefaultType(); 154 } 155 156 char[] ksPassword = null; 157 if (this.password != null) { 158 ksPassword = this.parsePropertyValue(this.password).toCharArray(); 159 } 160 161 KeyStore ks; 162 if (this.provider == null) { 163 ks = KeyStore.getInstance(ksType); 164 } else { 165 ks = KeyStore.getInstance(ksType, this.parsePropertyValue(this.provider)); 166 } 167 168 if (this.resource == null) { 169 ks.load(null, ksPassword); 170 } else { 171 InputStream is = this.resolveResource(this.parsePropertyValue(this.resource)); 172 ks.load(is, ksPassword); 173 } 174 175 return ks; 176 } 177 178 @Override 179 public String toString() { 180 StringBuilder builder = new StringBuilder(); 181 builder.append("KeyStoreParameters [type="); 182 builder.append(type); 183 builder.append(", password="); 184 builder.append(password); 185 builder.append(", provider="); 186 builder.append(provider); 187 builder.append(", resource="); 188 builder.append(resource); 189 builder.append(", getContext()="); 190 builder.append(getCamelContext()); 191 builder.append("]"); 192 return builder.toString(); 193 } 194 }