Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changes log

- 2.7.1 (??-??-2026)
- Bugs fixed
- `DefaultSslContextFactory` now uses the JVM default algorithms for the trust manager (`TrustManagerFactory.getDefaultAlgorithm()`, `PKIX`) and the key manager (`KeyManagerFactory.getDefaultAlgorithm()`) instead of the obsolete `SunX509`, unless the `ssl.TrustManagerFactory.algorithm` or `ssl.KeyManagerFactory.algorithm` system property, the `trustManagerAlgorithm` or `keyManagerAlgorithm` parameter, or the setters say otherwise. A valid certificate chain that is not in order is no longer rejected with a "Certificate chaining error". The `PKIX` validation is the standard one, and stricter than `SunX509` on unusual chains: set the trust manager algorithm to `SunX509` to get the previous behavior. Issue #1397.
- `Redirector.getTargetRef()` now removes the dot segments (`.` and `..`) of the target URI (RFC 3986, section 5.2.4), so the `Location` header of a client redirection and the URI of a server redirection are normalized. Relative-path targets such as `../x` are left untouched, as they need their base reference. A reverse proxy built on `http://backend/root/{rr}` now resolves a `..` found in the remaining part itself, which it did not before the request reached the backend. Issue #238.
- The Jackson, Gson and JSON (org.json) converters now handle the `application/*+json` media types, such as `application/vnd.example+json`. They were ignored, so an incoming `+json` entity was not converted, and a `+json` response was left to another converter (such as JAXB) when it scored higher than the 0.5 given by Jackson. A custom converter that won for these media types may now lose to the JSON converters. Issue #1185.
- Misc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* <tr>
* <td>keyManagerAlgorithm</td>
* <td>String</td>
* <td>System property "ssl.KeyManagerFactory.algorithm" or "SunX509"</td>
* <td>System property "ssl.KeyManagerFactory.algorithm" or the JVM default algorithm</td>
* <td>Certificate algorithm for the key manager.</td>
* </tr>
* <tr>
Expand Down Expand Up @@ -116,7 +116,7 @@
* <tr>
* <td>trustManagerAlgorithm</td>
* <td>String</td>
* <td>System property "ssl.TrustManagerFactory.algorithm" or "SunX509"</td>
* <td>System property "ssl.TrustManagerFactory.algorithm" or the JVM default algorithm</td>
* <td>Certificate algorithm for the trust manager.</td>
* </tr>
* <tr>
Expand Down Expand Up @@ -184,11 +184,11 @@ public class DefaultSslContextFactory extends SslContextFactory {
/** The whitespace-separated list of enabled SSL protocols. */
private volatile String[] enabledProtocols = null;

private static final String SUN_X509 = "SunX509";

/** The name of the KeyManager algorithm. */
private volatile String keyManagerAlgorithm =
System.getProperty("ssl.KeyManagerFactory.algorithm", SUN_X509);
System.getProperty(
"ssl.KeyManagerFactory.algorithm",
javax.net.ssl.KeyManagerFactory.getDefaultAlgorithm());

/** The password for the key in the keystore (as a String). */
private volatile char[] keyStoreKeyPassword =
Expand Down Expand Up @@ -240,7 +240,9 @@ private static String getDefaultKeyStorePath() {

/** The name of the TrustManager algorithm. */
private volatile String trustManagerAlgorithm =
System.getProperty("ssl.TrustManagerFactory.algorithm", SUN_X509);
System.getProperty(
"ssl.TrustManagerFactory.algorithm",
javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm());

/** The password for the trust store keystore. */
private volatile char[] trustStorePassword =
Expand Down Expand Up @@ -638,7 +640,9 @@ public void init(Series<Parameter> helperParameters) {
helperParameters.getFirstValue(
"keyManagerAlgorithm",
true,
System.getProperty("ssl.KeyManagerFactory.algorithm", SUN_X509)));
System.getProperty(
"ssl.KeyManagerFactory.algorithm",
javax.net.ssl.KeyManagerFactory.getDefaultAlgorithm())));
setKeyStorePassword(
helperParameters.getFirstValue(
"keyStorePassword",
Expand Down Expand Up @@ -667,7 +671,9 @@ public void init(Series<Parameter> helperParameters) {
helperParameters.getFirstValue(
"trustManagerAlgorithm",
true,
System.getProperty("ssl.TrustManagerFactory.algorithm", SUN_X509)));
System.getProperty(
"ssl.TrustManagerFactory.algorithm",
javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm())));
setTrustStorePassword(
helperParameters.getFirstValue(
"trustStorePassword",
Expand Down Expand Up @@ -769,8 +775,9 @@ public void setEnabledProtocols(String[] enabledProtocols) {

/**
* Sets the KeyManager algorithm. The default value is that of the
* <i>ssl.KeyManagerFactory.algorithm</i> system property, or <i>"SunX509"</i> if the system
* property has not been set up.
* <i>ssl.KeyManagerFactory.algorithm</i> system property, or the JVM default algorithm (see
* {@link javax.net.ssl.KeyManagerFactory#getDefaultAlgorithm()}) if the system property has not
* been set up.
*
* @param keyManagerAlgorithm The KeyManager algorithm.
*/
Expand Down Expand Up @@ -881,8 +888,11 @@ public void setSecureRandomAlgorithm(String secureRandomAlgorithm) {

/**
* Sets the TrustManager algorithm. The default value is that of the
* <i>ssl.TrustManagerFactory.algorithm</i> system property, or <i>"SunX509"</i> if the system
* property has not been set up.
* <i>ssl.TrustManagerFactory.algorithm</i> system property, or the JVM default algorithm (see
* {@link javax.net.ssl.TrustManagerFactory#getDefaultAlgorithm()}, <i>"PKIX"</i> unless the
* <i>ssl.TrustManagerFactory.algorithm</i> security property says otherwise) if the system
* property has not been set up. The <i>"SunX509"</i> algorithm is obsolete: it fails on a valid
* certificate chain that is not in order.
*
* @param trustManagerAlgorithm The TrustManager algorithm.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/**
* Copyright 2005-2026 Qlik
*<p>
* The content of this file is subject to the terms of the Apache 2.0 open
* source license available at https://www.opensource.org/licenses/apache-2.0
*<p>
* Restlet is a registered trademark of QlikTech International AB.
*/
package org.restlet.engine.ssl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.restlet.data.Form;

/** Unit tests for the default algorithms of {@link DefaultSslContextFactory} (#1397). */
class DefaultSslContextFactoryTestCase {

private static final String TRUST_PROPERTY = "ssl.TrustManagerFactory.algorithm";

private static final String KEY_PROPERTY = "ssl.KeyManagerFactory.algorithm";

private static final char[] PASSWORD = "changeit".toCharArray();

private String savedTrustProperty;

private String savedKeyProperty;

@TempDir Path tempDir;

@BeforeEach
void clearAlgorithmProperties() {
savedTrustProperty = System.getProperty(TRUST_PROPERTY);
savedKeyProperty = System.getProperty(KEY_PROPERTY);
System.clearProperty(TRUST_PROPERTY);
System.clearProperty(KEY_PROPERTY);
}

@AfterEach
void restoreAlgorithmProperties() {
restore(TRUST_PROPERTY, savedTrustProperty);
restore(KEY_PROPERTY, savedKeyProperty);
}

private static void restore(String name, String value) {
if (value == null) {
System.clearProperty(name);
} else {
System.setProperty(name, value);
}
}

// Defaults

@Test
void trustManagerAlgorithm_defaultsToTheJvmDefault() {
assertEquals(
TrustManagerFactory.getDefaultAlgorithm(),
new DefaultSslContextFactory().getTrustManagerAlgorithm());
}

@Test
void keyManagerAlgorithm_defaultsToTheJvmDefault() {
assertEquals(
KeyManagerFactory.getDefaultAlgorithm(),
new DefaultSslContextFactory().getKeyManagerAlgorithm());
}

@Test
void algorithms_systemPropertiesOverrideTheJvmDefaults() {
System.setProperty(TRUST_PROPERTY, "SunX509");
System.setProperty(KEY_PROPERTY, "NewSunX509");

DefaultSslContextFactory factory = new DefaultSslContextFactory();

assertEquals("SunX509", factory.getTrustManagerAlgorithm());
assertEquals("NewSunX509", factory.getKeyManagerAlgorithm());
}

@Test
void algorithms_settersOverrideTheDefaults() {
DefaultSslContextFactory factory = new DefaultSslContextFactory();
factory.setTrustManagerAlgorithm("SunX509");
factory.setKeyManagerAlgorithm("NewSunX509");

assertEquals("SunX509", factory.getTrustManagerAlgorithm());
assertEquals("NewSunX509", factory.getKeyManagerAlgorithm());
}

@Test
void init_withoutAlgorithmParameters_usesTheJvmDefaults() {
DefaultSslContextFactory factory = new DefaultSslContextFactory();
factory.setTrustManagerAlgorithm("SunX509");
factory.setKeyManagerAlgorithm("NewSunX509");

factory.init(new Form());

assertEquals(TrustManagerFactory.getDefaultAlgorithm(), factory.getTrustManagerAlgorithm());
assertEquals(KeyManagerFactory.getDefaultAlgorithm(), factory.getKeyManagerAlgorithm());
}

@Test
void init_withAlgorithmParameters_usesTheParameters() {
Form parameters = new Form();
parameters.add("trustManagerAlgorithm", "SunX509");
parameters.add("keyManagerAlgorithm", "NewSunX509");
DefaultSslContextFactory factory = new DefaultSslContextFactory();

factory.init(parameters);

assertEquals("SunX509", factory.getTrustManagerAlgorithm());
assertEquals("NewSunX509", factory.getKeyManagerAlgorithm());
}

// TLS handshakes: a valid chain served out of order (the symptom of #1397)

@Test
void handshake_withChainInOrder_succeedsWithTheDefaultAlgorithms() throws Exception {
assertNull(handshake(chain("leaf", "intermediate"), null));
}

@Test
void handshake_withChainOutOfOrder_succeedsWithTheDefaultAlgorithms() throws Exception {
assertNull(handshake(chain("leaf", "root", "intermediate"), null));
}

@Test
void handshake_withChainOutOfOrder_failsWithSunX509() throws Exception {
// Documents the escape hatch: the old algorithm can still be selected, with its limits.
assertNotNull(handshake(chain("leaf", "root", "intermediate"), "SunX509"));
}

@Test
void handshake_withChainInOrder_succeedsWithSunX509() throws Exception {
assertNull(handshake(chain("leaf", "intermediate"), "SunX509"));
}

private Certificate[] chain(String... names) throws Exception {
Certificate[] result = new Certificate[names.length];
for (int i = 0; i < names.length; i++) {
result[i] = certificate(names[i]);
}
return result;
}

private Certificate certificate(String name) throws Exception {
try (InputStream in = resource("test-ca-" + name + ".pem")) {
return CertificateFactory.getInstance("X.509").generateCertificate(in);
}
}

private PrivateKey leafKey() throws Exception {
try (InputStream in = resource("test-ca-leaf-key.pem")) {
String pem =
new String(in.readAllBytes(), java.nio.charset.StandardCharsets.US_ASCII)
.replaceAll("-----[A-Z ]+-----", "")
.replaceAll("\\s", "");
return KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(pem)));
}
}

private InputStream resource(String name) throws IOException {
InputStream in = getClass().getResourceAsStream(name);
if (in == null) {
throw new IOException("Missing test resource " + name);
}
return in;
}

/**
* Performs a TLS handshake over the loopback interface, both sides being built by {@link
* DefaultSslContextFactory}. The server serves the given chain from a JKS store (a PKCS12 store
* refuses a chain that is not in order), and the client trusts only the root certificate.
*
* @param serverChain The chain served by the server, leaf first.
* @param clientTrustAlgorithm The trust manager algorithm of the client, or null for the
* default.
* @return Null if the handshake succeeded, the description of the failure otherwise.
*/
private String handshake(Certificate[] serverChain, String clientTrustAlgorithm)
throws Exception {
Path keyStoreFile = tempDir.resolve("server.jks");
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(null, null);
keyStore.setKeyEntry("server", leafKey(), PASSWORD, serverChain);
try (OutputStream out = Files.newOutputStream(keyStoreFile)) {
keyStore.store(out, PASSWORD);
}

Path trustStoreFile = tempDir.resolve("trust.p12");
KeyStore trustStore = KeyStore.getInstance("PKCS12");
trustStore.load(null, null);
trustStore.setCertificateEntry("root", certificate("root"));
try (OutputStream out = Files.newOutputStream(trustStoreFile)) {
trustStore.store(out, PASSWORD);
}

DefaultSslContextFactory serverFactory = new DefaultSslContextFactory();
serverFactory.setKeyStorePath(keyStoreFile.toString());
serverFactory.setKeyStoreType("JKS");
serverFactory.setKeyStorePassword(PASSWORD);
serverFactory.setKeyStoreKeyPassword(PASSWORD);

DefaultSslContextFactory clientFactory = new DefaultSslContextFactory();
// The client has no key of its own: "NONE" avoids the default ~/.keystore.
clientFactory.setKeyStorePath("NONE");
clientFactory.setKeyStorePassword(PASSWORD);
clientFactory.setKeyStoreKeyPassword(PASSWORD);
clientFactory.setTrustStorePath(trustStoreFile.toString());
clientFactory.setTrustStoreType("PKCS12");
clientFactory.setTrustStorePassword(PASSWORD);
if (clientTrustAlgorithm != null) {
clientFactory.setTrustManagerAlgorithm(clientTrustAlgorithm);
}

AtomicReference<Throwable> serverFailure = new AtomicReference<>();
try (SSLServerSocket serverSocket =
(SSLServerSocket)
serverFactory
.createSslContext()
.getServerSocketFactory()
.createServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
serverSocket.setSoTimeout(10_000);
Thread server =
new Thread(
() -> {
try (SSLSocket socket = (SSLSocket) serverSocket.accept()) {
socket.setSoTimeout(10_000);
socket.startHandshake();
socket.getOutputStream().write(1);
} catch (Exception e) {
serverFailure.set(e);
}
});
server.start();

try (SSLSocket client =
(SSLSocket)
clientFactory
.createSslContext()
.getSocketFactory()
.createSocket(
InetAddress.getLoopbackAddress(),
serverSocket.getLocalPort())) {
client.setSoTimeout(10_000);
client.startHandshake();
return null;
} catch (IOException e) {
Throwable root = e;
while (root.getCause() != null) {
root = root.getCause();
}
return root.getClass().getSimpleName() + ": " + root.getMessage();
} finally {
server.join(10_000);
}
}
}
}
Loading
Loading