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 */ 017package org.apache.logging.log4j.core.net.ssl; 018 019import java.io.FileNotFoundException; 020import java.io.IOException; 021import java.io.InputStream; 022import java.security.KeyStore; 023import java.security.KeyStoreException; 024import java.security.NoSuchAlgorithmException; 025import java.security.cert.CertificateException; 026import java.util.Arrays; 027 028import org.apache.logging.log4j.core.config.ConfigurationSource; 029import org.apache.logging.log4j.core.util.NetUtils; 030 031/** 032 * Configuration of the KeyStore 033 */ 034public class AbstractKeyStoreConfiguration extends StoreConfiguration<KeyStore> { 035 private final KeyStore keyStore; 036 private final String keyStoreType; 037 038 public AbstractKeyStoreConfiguration(final String location, final PasswordProvider passwordProvider, final String keyStoreType) 039 throws StoreConfigurationException { 040 super(location, passwordProvider); 041 this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType; 042 this.keyStore = this.load(); 043 } 044 045 /** 046 * @deprecated Use {@link #AbstractKeyStoreConfiguration(String, PasswordProvider, String)} instead 047 */ 048 @Deprecated 049 public AbstractKeyStoreConfiguration(final String location, final char[] password, final String keyStoreType) 050 throws StoreConfigurationException { 051 this(location, new MemoryPasswordProvider(password), keyStoreType); 052 } 053 054 /** 055 * @deprecated Use {@link #AbstractKeyStoreConfiguration(String, PasswordProvider, String)} instead 056 */ 057 @Deprecated 058 public AbstractKeyStoreConfiguration(final String location, final String password, final String keyStoreType) 059 throws StoreConfigurationException { 060 this(location, new MemoryPasswordProvider(password == null ? null : password.toCharArray()), keyStoreType); 061 } 062 063 @Override 064 protected KeyStore load() throws StoreConfigurationException { 065 final String loadLocation = this.getLocation(); 066 LOGGER.debug("Loading keystore from location {}", loadLocation); 067 try { 068 if (loadLocation == null) { 069 throw new IOException("The location is null"); 070 } 071 try (final InputStream fin = openInputStream(loadLocation)) { 072 final KeyStore ks = KeyStore.getInstance(this.keyStoreType); 073 final char[] password = this.getPasswordAsCharArray(); 074 try { 075 ks.load(fin, password); 076 } finally { 077 if (password != null) { 078 Arrays.fill(password, '\0'); 079 } 080 } 081 LOGGER.debug("KeyStore successfully loaded from location {}", loadLocation); 082 return ks; 083 } 084 } catch (final CertificateException e) { 085 LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {} for location {}", this.keyStoreType, loadLocation, e); 086 throw new StoreConfigurationException(loadLocation, e); 087 } catch (final NoSuchAlgorithmException e) { 088 LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found for location {}", loadLocation, e); 089 throw new StoreConfigurationException(loadLocation, e); 090 } catch (final KeyStoreException e) { 091 LOGGER.error("KeyStoreException for location {}", loadLocation, e); 092 throw new StoreConfigurationException(loadLocation, e); 093 } catch (final FileNotFoundException e) { 094 LOGGER.error("The keystore file {} is not found", loadLocation, e); 095 throw new StoreConfigurationException(loadLocation, e); 096 } catch (final IOException e) { 097 LOGGER.error("Something is wrong with the format of the keystore or the given password for location", loadLocation, e); 098 throw new StoreConfigurationException(loadLocation, e); 099 } 100 } 101 102 private InputStream openInputStream(final String filePathOrUri) { 103 return ConfigurationSource.fromUri(NetUtils.toURI(filePathOrUri)).getInputStream(); 104 } 105 106 public KeyStore getKeyStore() { 107 return this.keyStore; 108 } 109 110 @Override 111 public int hashCode() { 112 final int prime = 31; 113 int result = super.hashCode(); 114 result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode()); 115 result = prime * result + ((keyStoreType == null) ? 0 : keyStoreType.hashCode()); 116 return result; 117 } 118 119 @Override 120 public boolean equals(final Object obj) { 121 if (this == obj) { 122 return true; 123 } 124 if (!super.equals(obj)) { 125 return false; 126 } 127 if (getClass() != obj.getClass()) { 128 return false; 129 } 130 final AbstractKeyStoreConfiguration other = (AbstractKeyStoreConfiguration) obj; 131 if (keyStore == null) { 132 if (other.keyStore != null) { 133 return false; 134 } 135 } else if (!keyStore.equals(other.keyStore)) { 136 return false; 137 } 138 if (keyStoreType == null) { 139 if (other.keyStoreType != null) { 140 return false; 141 } 142 } else if (!keyStoreType.equals(other.keyStoreType)) { 143 return false; 144 } 145 return true; 146 } 147 148 public String getKeyStoreType() { 149 return keyStoreType; 150 } 151 152}