mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
23.26.2007 feb5d90ec016c99712f19c5485cf7633cd38f111
opends/src/server/org/opends/server/extensions/FileBasedKeyManagerProvider.java
@@ -42,6 +42,7 @@
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import org.opends.server.admin.std.server.FileBasedKeyManagerCfg;
import org.opends.server.api.ConfigurableComponent;
import org.opends.server.api.KeyManagerProvider;
import org.opends.server.config.ConfigAttribute;
@@ -70,7 +71,7 @@
 * file located on the Directory Server filesystem.
 */
public class FileBasedKeyManagerProvider
       extends KeyManagerProvider
       extends KeyManagerProvider<FileBasedKeyManagerCfg>
       implements ConfigurableComponent
{
@@ -483,9 +484,158 @@
  /**
   * Performs any finalization that may be necessary for this key manager
   * provider.
   * {@inheritDoc}
   */
  @Override
  public void initializeKeyManagerProvider(
      FileBasedKeyManagerCfg configuration)
      throws ConfigException, InitializationException {
    // Store the DN of the configuration entry.
    configEntryDN = configuration.dn();
    // Get the path to the key store file.
    keyStoreFile = configuration.getKeyStoreFile();
    try {
      File f = getFileForPath(keyStoreFile);
      if (!(f.exists() && f.isFile())) {
        int msgID = MSGID_FILE_KEYMANAGER_NO_SUCH_FILE;
        String message = getMessage(msgID, String
            .valueOf(keyStoreFile), String.valueOf(configEntryDN));
        throw new InitializationException(msgID, message);
      }
    } catch (SecurityException e) {
      if (debugEnabled())
      {
        debugCaught(DebugLogLevel.ERROR, e);
      }
      int msgID = MSGID_FILE_KEYMANAGER_CANNOT_DETERMINE_FILE;
      String message = getMessage(msgID, String
          .valueOf(configEntryDN), stackTraceToSingleLineString(e));
      throw new InitializationException(msgID, message, e);
    }
    // Get the keystore type. If none is specified, then use the
    // default type.
    if (configuration.getKeyStoreType() != null) {
      try {
        KeyStore.getInstance(configuration.getKeyStoreType());
        keyStoreType = configuration.getKeyStoreType();
      } catch (KeyStoreException kse) {
        if (debugEnabled())
        {
          debugCaught(DebugLogLevel.ERROR, kse);
        }
        int msgID = MSGID_FILE_KEYMANAGER_INVALID_TYPE;
        String message = getMessage(msgID, String
            .valueOf(configuration.getKeyStoreType()), String
            .valueOf(configEntryDN),
            stackTraceToSingleLineString(kse));
        throw new InitializationException(msgID, message);
      }
    } else {
      keyStoreType = KeyStore.getDefaultType();
    }
    // Get the PIN needed to access the contents of the keystore file.
    //
    // We will offer several places to look for the PIN, and we will
    // do so in the following order:
    //
    // - In a specified Java property
    // - In a specified environment variable
    // - In a specified file on the server filesystem.
    // - As the value of a configuration attribute.
    //
    // In any case, the PIN must be in the clear.
    keyStorePIN = null;
    keyStorePINEnVar = null;
    keyStorePINFile = null;
    keyStorePINProperty = null;
    if (configuration.getKeyStorePinProperty() != null) {
      String propertyName = configuration.getKeyStorePinProperty();
      String pinStr = System.getProperty(propertyName);
      if (pinStr == null) {
        int msgID = MSGID_FILE_KEYMANAGER_PIN_PROPERTY_NOT_SET;
        String message = getMessage(msgID, String
            .valueOf(propertyName), String.valueOf(configEntryDN));
        throw new InitializationException(msgID, message);
      }
      keyStorePIN = pinStr.toCharArray();
      keyStorePINProperty = propertyName;
    } else if (configuration.getKeyStorePinEnvironmentVariable() != null) {
      String enVarName = configuration
          .getKeyStorePinEnvironmentVariable();
      String pinStr = System.getenv(enVarName);
      if (pinStr == null) {
        int msgID = MSGID_FILE_KEYMANAGER_PIN_ENVAR_NOT_SET;
        String message = getMessage(msgID, String.valueOf(enVarName),
            String.valueOf(configEntryDN));
        throw new InitializationException(msgID, message);
      }
      keyStorePIN = pinStr.toCharArray();
      keyStorePINEnVar = enVarName;
    } else if (configuration.getKeyStorePinFile() != null) {
      String fileName = configuration.getKeyStorePinFile();
      File pinFile = getFileForPath(fileName);
      if (!pinFile.exists()) {
        int msgID = MSGID_FILE_KEYMANAGER_PIN_NO_SUCH_FILE;
        String message = getMessage(msgID, String.valueOf(fileName),
            String.valueOf(configEntryDN));
        throw new InitializationException(msgID, message);
      }
      String pinStr;
      try {
        BufferedReader br = new BufferedReader(
            new FileReader(pinFile));
        pinStr = br.readLine();
        br.close();
      } catch (IOException ioe) {
        int msgID = MSGID_FILE_KEYMANAGER_PIN_FILE_CANNOT_READ;
        String message = getMessage(msgID, String.valueOf(fileName),
            String.valueOf(configEntryDN),
            stackTraceToSingleLineString(ioe));
        throw new InitializationException(msgID, message, ioe);
      }
      if (pinStr == null) {
        int msgID = MSGID_FILE_KEYMANAGER_PIN_FILE_EMPTY;
        String message = getMessage(msgID, String.valueOf(fileName),
            String.valueOf(configEntryDN));
        throw new InitializationException(msgID, message);
      }
      keyStorePIN = pinStr.toCharArray();
      keyStorePINFile = fileName;
    } else if (configuration.getKeyStorePin() != null) {
      keyStorePIN = configuration.getKeyStorePin().toCharArray();
    } else {
      // Pin wasn't defined anywhere.
      int msgID = MSGID_FILE_KEYMANAGER_NO_PIN;
      String message = getMessage(msgID, String
          .valueOf(configEntryDN));
      throw new ConfigException(msgID, message);
    }
    DirectoryServer.registerConfigurableComponent(this);
  }
  /**
   * Performs any finalization that may be necessary for this key
   * manager provider.
   */
  public void finalizeKeyManagerProvider()
  {