// Create the set of base DNs that we will handle. In this case, it's just
// the DN of the base trust store entry.
SortedSet<DN> baseDNSet = configuration.getBaseDN();
if (baseDNSet.size() != 1)
{
Message message = ERR_TRUSTSTORE_REQUIRES_ONE_BASE_DN.get(
String.valueOf(configEntryDN));
throw new InitializationException(message);
}
baseDN = baseDNSet.first();
baseDNs = new DN[] {baseDN};
// Get the path to the trust store file.
trustStoreFile = configuration.getTrustStoreFile();
// Get the trust store type. If none is specified, then use the default
// type.
trustStoreType = configuration.getTrustStoreType();
if (trustStoreType == null)
{
trustStoreType = KeyStore.getDefaultType();
}
try
{
KeyStore.getInstance(trustStoreType);
}
catch (KeyStoreException kse)
{
if (debugEnabled())
{
TRACER.debugCaught(DebugLogLevel.ERROR, kse);
}
Message message = ERR_TRUSTSTORE_INVALID_TYPE.
get(String.valueOf(trustStoreType), String.valueOf(configEntryDN),
getExceptionMessage(kse));
throw new InitializationException(message);
}
// Get the PIN needed to access the contents of the trust store 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. If no PIN is provided, then
// it will be assumed that none is required to access the information in the
// trust store.
String pinProperty = configuration.getTrustStorePinProperty();
if (pinProperty == null)
{
String pinEnVar = configuration.getTrustStorePinEnvironmentVariable();
if (pinEnVar == null)
{
String pinFilePath = configuration.getTrustStorePinFile();
if (pinFilePath == null)
{
String pinStr = configuration.getTrustStorePin();
if (pinStr == null)
{
trustStorePIN = null;
}
else
{
trustStorePIN = pinStr.toCharArray();
}
}
else
{
File pinFile = getFileForPath(pinFilePath);
if (! pinFile.exists())
{
try
{
// Generate a PIN.
trustStorePIN = createKeystorePassword();
// Store the PIN in the pin file.
createPINFile(pinFile.getPath(), new String(trustStorePIN));
}
catch (Exception e)
{
Message message = ERR_TRUSTSTORE_PIN_FILE_CANNOT_CREATE.get(
String.valueOf(pinFilePath), String.valueOf(configEntryDN));
throw new InitializationException(message);
}
}
else
{
String pinStr;
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(pinFile));
pinStr = br.readLine();
}
catch (IOException ioe)
{
Message message = ERR_TRUSTSTORE_PIN_FILE_CANNOT_READ.
get(String.valueOf(pinFilePath),
String.valueOf(configEntryDN), getExceptionMessage(ioe));
throw new InitializationException(message, ioe);
}
finally
{
try
{
br.close();
} catch (Exception e) {
// ignore
}
}
if (pinStr == null)
{
Message message = ERR_TRUSTSTORE_PIN_FILE_EMPTY.get(
String.valueOf(pinFilePath), String.valueOf(configEntryDN));
throw new InitializationException(message);
}
else
{
trustStorePIN = pinStr.toCharArray();
}
}
}
}
else
{
String pinStr = System.getenv(pinEnVar);
if (pinStr == null)
{
Message message = ERR_TRUSTSTORE_PIN_ENVAR_NOT_SET.get(
String.valueOf(pinProperty), String.valueOf(configEntryDN));
throw new InitializationException(message);
}
else
{
trustStorePIN = pinStr.toCharArray();
}
}
}
else
{
String pinStr = System.getProperty(pinProperty);
if (pinStr == null)
{
Message message = ERR_TRUSTSTORE_PIN_PROPERTY_NOT_SET.get(
String.valueOf(pinProperty), String.valueOf(configEntryDN));
throw new InitializationException(message);
}
else
{
trustStorePIN = pinStr.toCharArray();
}
}
// Create a certificate manager.
certificateManager =
new CertificateManager(getFileForPath(trustStoreFile).getPath(),
trustStoreType,
new String(trustStorePIN));
// Generate a self-signed certificate, if there is none.
generateInstanceCertificateIfAbsent();
// Construct the trust store base entry.
LinkedHashMap<ObjectClass,String> objectClasses =
new LinkedHashMap<ObjectClass,String>(2);
objectClasses.put(DirectoryServer.getTopObjectClass(), OC_TOP);
ObjectClass branchOC =
DirectoryServer.getObjectClass("ds-cfg-branch", true);
objectClasses.put(branchOC, "ds-cfg-branch");
LinkedHashMap<AttributeType,List<Attribute>> opAttrs =
new LinkedHashMap<AttributeType,List<Attribute>>(0);
LinkedHashMap<AttributeType,List<Attribute>> userAttrs =
new LinkedHashMap<AttributeType,List<Attribute>>(1);
RDN rdn = baseDN.getRDN();
int numAVAs = rdn.getNumValues();
for (int i=0; i < numAVAs; i++)
{
AttributeType attrType = rdn.getAttributeType(i);
ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
attrList.add(Attributes.create(attrType, rdn.getAttributeValue(i)));
userAttrs.put(attrType, attrList);
}
baseEntry = new Entry(baseDN, objectClasses, userAttrs,
opAttrs);
// Define empty sets for the supported controls and features.
supportedControls = new HashSet<String>(0);
supportedFeatures = new HashSet<String>(0);
// Register this as a change listener.
configuration.addTrustStoreChangeListener(this);
// Register the trust store base as a private suffix.
try
{
DirectoryServer.registerBaseDN(baseDN, this, true);
}
catch (Exception e)
{
if (debugEnabled())
{
TRACER.debugCaught(DebugLogLevel.ERROR, e);
}
Message message = ERR_BACKEND_CANNOT_REGISTER_BASEDN.get(
String.valueOf(baseDN), String.valueOf(e));
throw new InitializationException(message, e);
}
}