%--
-
- Copyright (C) 2017-2026 Ignite Realtime Foundation. All rights reserved.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page errorPage="error.jsp"%>
<%@ page import="org.jivesoftware.openfire.spi.ConnectionType" %>
<%@ page import="org.jivesoftware.openfire.XMPPServer" %>
<%@ page import="org.jivesoftware.openfire.keystore.CertificateStoreManager" %>
<%@ page import="org.jivesoftware.openfire.keystore.CertificateStoreConfiguration" %>
<%@ page import="java.io.File" %>
<%@ page import="org.jivesoftware.admin.CertificateStorePasswordField" %>
<%@ page import="org.jivesoftware.util.CookieUtils" %>
<%@ page import="org.jivesoftware.util.ParamUtils" %>
<%@ page import="org.jivesoftware.util.StringUtils" %>
<%@ page import="java.util.*" %>
<%@ page import="static org.jivesoftware.openfire.spi.ConnectionType.SOCKET_C2S" %>
<%@ taglib uri="admin" prefix="admin" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<% webManager.init(request, response, session, application, out );
final CertificateStoreManager certificateStoreManager = XMPPServer.getInstance().getCertificateStoreManager();
final Map errors = new HashMap<>();
pageContext.setAttribute( "errors", errors );
// Store files that could not be opened with the password the administrator entered.
final Set wrongPasswordFiles = new LinkedHashSet<>();
pageContext.setAttribute( "wrongPasswordFiles", wrongPasswordFiles );
// Store files that could not be opened with the current password, when no new password was entered (typically
// after a change of location to a file with a different password).
final Set currentPasswordFailedFiles = new LinkedHashSet<>();
pageContext.setAttribute( "currentPasswordFailedFiles", currentPasswordFailedFiles );
boolean showAll = certificateStoreManager.usesDistinctConfigurationForEachType();
if ( !showAll )
{
// Allow 'showAll' to be overridden, even when the current configuration does not call for an 'expanded' view.
showAll = ParamUtils.getBooleanParameter( request, "showAll", false );
}
// OF-1415: Show distinct boxes for all connection types, but only when their configuration differs!
pageContext.setAttribute( "showAll", showAll );
pageContext.setAttribute( "connectionTypes", ConnectionType.values() );
pageContext.setAttribute( "defaultClientConnectionType", SOCKET_C2S );
pageContext.setAttribute( "defaultServerConnectionType", ConnectionType.SOCKET_S2S );
pageContext.setAttribute( "certificateStoreManager", certificateStoreManager );
boolean update = request.getParameter("update") != null;
Cookie csrfCookie = CookieUtils.getCookie(request, "csrf");
String csrfParam = ParamUtils.getParameter(request, "csrf");
if (update) {
if (csrfCookie == null || csrfParam == null || !csrfCookie.getValue().equals(csrfParam)) {
update = false;
errors.put("csrf", "CSRF Failure!");
}
}
csrfParam = StringUtils.randomString(15);
CookieUtils.setCookie(request, response, "csrf", csrfParam, -1);
pageContext.setAttribute("csrf", csrfParam);
if ( update ) {
final String cType = request.getParameter( "connectionType" );
final boolean updateAll = "all".equals( cType );
Set toUpdate = new HashSet<>();
if ( updateAll )
{
toUpdate.addAll( Arrays.asList( ConnectionType.values() ) );
}
else
{
try
{
toUpdate.add( ConnectionType.valueOf( cType ) );
}
catch ( IllegalArgumentException ex )
{
errors.put( "connectionType", ex.getMessage() );
}
}
final String locKey = request.getParameter( "loc-key" );
final String pwdKey = request.getParameter( "pwd-key" );
final String locTrustS2S = request.getParameter( "loc-trust-s2s" );
final String pwdTrustS2S = request.getParameter( "pwd-trust-s2s" );
final String locTrustC2S = request.getParameter( "loc-trust-c2s" );
final String pwdTrustC2S = request.getParameter( "pwd-trust-c2s" );
// The page never sends a store's current password to the browser. A password field is submitted only when the
// administrator revealed it with its "Update password" link, so a missing or empty field keeps the current
// password of each store (see CertificateStorePasswordField). Only the locations are mandatory.
if ( locKey == null || locKey.isEmpty() ) {
errors.put( "locKey", "Identity Store location must be defined." );
}
for ( final ConnectionType connectionType : toUpdate )
{
if ( connectionType.isClientOriented() )
{
if ( locTrustC2S == null || locTrustC2S.isEmpty() )
{
errors.put( "locTrustC2S", "Trust Store (C2S) location must be defined." );
}
}
else
{
if ( locTrustS2S == null || locTrustS2S.isEmpty() )
{
errors.put( "locTrustS2S", "Trust Store (S2S) location must be defined." );
}
}
}
if ( errors.isEmpty() ) {
// The identity store and the trust store are replaced independently, so that one failing does not stop the
// other from being saved. A store that fails to load leaves the manager unchanged for that store.
boolean anySaved = false;
for ( final ConnectionType connectionType : toUpdate )
{
final String locTrust;
final String pwdTrust;
if ( connectionType.isClientOriented() )
{
locTrust = locTrustC2S;
pwdTrust = pwdTrustC2S;
}
else
{
locTrust = locTrustS2S;
pwdTrust = pwdTrustS2S;
}
try
{
final char[] keyPassword = CertificateStorePasswordField.passwordToSave( pwdKey, certificateStoreManager.getIdentityStoreConfiguration( connectionType ).getPassword() );
final File backupKey = new File( CertificateStoreManager.getIdentityStoreBackupDirectory( connectionType ) );
final CertificateStoreConfiguration configKey = new CertificateStoreConfiguration( CertificateStoreManager.getIdentityStoreType(connectionType), new File( locKey ), keyPassword, backupKey );
certificateStoreManager.replaceIdentityStore( connectionType, configKey, false );
anySaved = true;
}
catch ( Exception ex )
{
reportSaveFailure( locKey, pwdKey, ex, wrongPasswordFiles, currentPasswordFailedFiles, errors );
}
try
{
final char[] trustPassword = CertificateStorePasswordField.passwordToSave( pwdTrust, certificateStoreManager.getTrustStoreConfiguration( connectionType ).getPassword() );
final File backupTrust = new File( CertificateStoreManager.getTrustStoreBackupDirectory( connectionType ) );
final CertificateStoreConfiguration configTrust = new CertificateStoreConfiguration( CertificateStoreManager.getTrustStoreType(connectionType), new File( locTrust ), trustPassword, backupTrust );
certificateStoreManager.replaceTrustStore( connectionType, configTrust, false );
anySaved = true;
}
catch ( Exception ex )
{
reportSaveFailure( locTrust, pwdTrust, ex, wrongPasswordFiles, currentPasswordFailedFiles, errors );
}
}
// Log the event only when a store was saved, so that the audit log does not record a change that did not happen.
if ( anySaved ) {
webManager.logEvent("Updated certificate store configuration", Arrays.toString( toUpdate.toArray() ) );
}
// Redirect only on success: a store that fails to load (for example a wrong password) must be reported,
// not followed by a success message.
if ( errors.isEmpty() && wrongPasswordFiles.isEmpty() && currentPasswordFailedFiles.isEmpty() ) {
response.sendRedirect("security-certificate-store-management.jsp?updated=true&showAll=" + showAll );
return;
}
// A partial failure can leave the connection types with different stores, so decide the view again from
// the configuration as it is now.
showAll = showAll || certificateStoreManager.usesDistinctConfigurationForEachType();
pageContext.setAttribute( "showAll", showAll );
}
// The save did not succeed, so show the locations the administrator submitted in the form they submitted,
// rather than the current configuration, so that they need not be typed again. Passwords are never echoed.
pageContext.setAttribute( "keepSubmitted", true );
pageContext.setAttribute( "submittedConnectionType", cType );
pageContext.setAttribute( "submittedLocKey", locKey );
pageContext.setAttribute( "submittedLocTrustS2S", locTrustS2S );
pageContext.setAttribute( "submittedLocTrustC2S", locTrustC2S );
}
%>
<%!
// Records why replacing a store failed, under the message that fits the reason (see
// CertificateStorePasswordField.classifyFailure).
private static void reportSaveFailure( final String location, final String submittedPassword, final Exception failure, final Set wrongPasswordFiles, final Set currentPasswordFailedFiles, final Map errors )
{
switch ( CertificateStorePasswordField.classifyFailure( submittedPassword, failure ) )
{
case WRONG_PASSWORD_ENTERED:
wrongPasswordFiles.add( location );
break;
case CURRENT_PASSWORD_DOES_NOT_OPEN_FILE:
currentPasswordFailedFiles.add( location );
break;
default:
errors.put( "update", failure.getMessage() );
break;
}
}
%>
:
()