use of password.pwm.config.value.PrivateKeyValue in project pwm by pwm-project.
the class HttpsServerCertificateManager method importKey.
public static void importKey(final StoredConfiguration storedConfiguration, final KeyStoreFormat keyStoreFormat, final InputStream inputStream, final PasswordData password, final String alias) throws PwmUnrecoverableException {
final char[] charPassword = password == null ? new char[0] : password.getStringValue().toCharArray();
final PrivateKeyCertificate privateKeyCertificate;
try {
final KeyStore keyStore = KeyStore.getInstance(keyStoreFormat.toString());
keyStore.load(inputStream, charPassword);
final String effectiveAlias;
{
final List<String> allAliases = new ArrayList<>();
for (final Enumeration enu = keyStore.aliases(); enu.hasMoreElements(); ) {
final String value = (String) enu.nextElement();
allAliases.add(value);
}
effectiveAlias = allAliases.size() == 1 ? allAliases.iterator().next() : alias;
}
final KeyStore.PasswordProtection passwordProtection = new KeyStore.PasswordProtection(charPassword);
final KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(effectiveAlias, passwordProtection);
if (entry == null) {
final String errorMsg = "unable to import https key entry with alias '" + alias + "'";
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_CERTIFICATE_ERROR, errorMsg, new String[] { "no key entry alias '" + alias + "' in keystore" }));
}
final PrivateKey key = entry.getPrivateKey();
final List<X509Certificate> certificates = Arrays.asList((X509Certificate[]) entry.getCertificateChain());
LOGGER.debug("importing certificate chain: " + JsonUtil.serializeCollection(X509Utils.makeDebugInfoMap(certificates)));
privateKeyCertificate = new PrivateKeyCertificate(certificates, key);
} catch (Exception e) {
final String errorMsg = "unable to load configured https certificate: " + e.getMessage();
final String[] errorDetail = new String[] { e.getMessage() };
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_CERTIFICATE_ERROR, errorMsg, errorDetail));
}
final StoredValue storedValue = new PrivateKeyValue(privateKeyCertificate);
storedConfiguration.writeSetting(PwmSetting.HTTPS_CERT, storedValue, null);
}
use of password.pwm.config.value.PrivateKeyValue in project pwm by pwm-project.
the class ConfigEditorServlet method restReadSetting.
@ActionHandler(action = "readSetting")
private ProcessStatus restReadSetting(final PwmRequest pwmRequest) throws IOException, PwmUnrecoverableException {
final ConfigManagerBean configManagerBean = getBean(pwmRequest);
final StoredConfigurationImpl storedConfig = configManagerBean.getStoredConfiguration();
final String key = pwmRequest.readParameterAsString("key");
final Object returnValue;
final LinkedHashMap<String, Object> returnMap = new LinkedHashMap<>();
final PwmSetting theSetting = PwmSetting.forKey(key);
if (key.startsWith("localeBundle")) {
final StringTokenizer st = new StringTokenizer(key, "-");
st.nextToken();
final PwmLocaleBundle bundleName = PwmLocaleBundle.valueOf(st.nextToken());
final String keyName = st.nextToken();
final Map<String, String> bundleMap = storedConfig.readLocaleBundleMap(bundleName.getTheClass().getName(), keyName);
if (bundleMap == null || bundleMap.isEmpty()) {
final Map<String, String> defaultValueMap = new LinkedHashMap<>();
final String defaultLocaleValue = ResourceBundle.getBundle(bundleName.getTheClass().getName(), PwmConstants.DEFAULT_LOCALE).getString(keyName);
for (final Locale locale : pwmRequest.getConfig().getKnownLocales()) {
final ResourceBundle localeBundle = ResourceBundle.getBundle(bundleName.getTheClass().getName(), locale);
if (locale.toString().equalsIgnoreCase(PwmConstants.DEFAULT_LOCALE.toString())) {
defaultValueMap.put("", defaultLocaleValue);
} else {
final String valueStr = localeBundle.getString(keyName);
if (!defaultLocaleValue.equals(valueStr)) {
final String localeStr = locale.toString();
defaultValueMap.put(localeStr, localeBundle.getString(keyName));
}
}
}
returnValue = defaultValueMap;
returnMap.put("isDefault", true);
} else {
returnValue = bundleMap;
returnMap.put("isDefault", false);
}
returnMap.put("key", key);
} else if (theSetting == null) {
final String errorStr = "readSettingAsString request for unknown key: " + key;
LOGGER.warn(errorStr);
pwmRequest.outputJsonResult(RestResultBean.fromError(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorStr)));
return ProcessStatus.Halt;
} else {
final String profile = theSetting.getCategory().hasProfiles() ? pwmRequest.readParameterAsString("profile") : null;
switch(theSetting.getSyntax()) {
case PASSWORD:
returnValue = Collections.singletonMap("isDefault", storedConfig.isDefaultValue(theSetting, profile));
break;
case X509CERT:
returnValue = ((X509CertificateValue) storedConfig.readSetting(theSetting, profile)).toInfoMap(true);
break;
case PRIVATE_KEY:
returnValue = ((PrivateKeyValue) storedConfig.readSetting(theSetting, profile)).toInfoMap(true);
break;
case ACTION:
returnValue = ((ActionValue) storedConfig.readSetting(theSetting, profile)).toInfoMap();
break;
case REMOTE_WEB_SERVICE:
returnValue = ((RemoteWebServiceValue) storedConfig.readSetting(theSetting, profile)).toInfoMap();
break;
case FILE:
returnValue = ((FileValue) storedConfig.readSetting(theSetting, profile)).toInfoMap();
break;
default:
returnValue = storedConfig.readSetting(theSetting, profile).toNativeObject();
}
returnMap.put("isDefault", storedConfig.isDefaultValue(theSetting, profile));
if (theSetting.getSyntax() == PwmSettingSyntax.SELECT) {
returnMap.put("options", theSetting.getOptions());
}
{
final ValueMetaData settingMetaData = storedConfig.readSettingMetadata(theSetting, profile);
if (settingMetaData != null) {
if (settingMetaData.getModifyDate() != null) {
returnMap.put("modifyTime", settingMetaData.getModifyDate());
}
if (settingMetaData.getUserIdentity() != null) {
returnMap.put("modifyUser", settingMetaData.getUserIdentity());
}
}
}
returnMap.put("key", key);
returnMap.put("category", theSetting.getCategory().toString());
returnMap.put("syntax", theSetting.getSyntax().toString());
}
returnMap.put("value", returnValue);
pwmRequest.outputJsonResult(RestResultBean.withData(returnMap));
return ProcessStatus.Halt;
}
Aggregations