From 5d5bf1bf0e43530aca8c66c0ad7c218f19c4df51 Mon Sep 17 00:00:00 2001 From: Matthew Vivian Date: Thu, 12 Nov 2026 11:58:15 +0000 Subject: [PATCH 1/3] OF-3465: Show the certificate, not its public key, in the S2S connection test The S2S connection test wrote each remote certificate's public key between BEGIN CERTIFICATE and END CERTIFICATE markers, so the block could not be read as a certificate by tools such as openssl x509. It now encodes the whole certificate. The formatting moves into formatCertificates, so that a test can parse the block back into the certificate. --- .../org/jivesoftware/util/S2STestService.java | 64 +++++++++++-------- .../jivesoftware/util/S2STestServiceTest.java | 53 +++++++++++++++ 2 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java diff --git a/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java b/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java index dbf06d1d7b..d90ca696d4 100644 --- a/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java +++ b/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java @@ -46,6 +46,7 @@ import java.io.StringWriter; import java.io.Writer; import java.security.cert.Certificate; +import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.List; @@ -193,37 +194,50 @@ private void logSessionStatus() { /** * @return A String representation of the certificate chain for the connection to the domain under test. + * @throws CertificateEncodingException when a certificate cannot be encoded. */ - private String getCertificates() { + private String getCertificates() throws CertificateEncodingException { final DomainPair pair = new DomainPair(XMPPServer.getInstance().getServerInfo().getXMPPDomain(), domain.getDomain()); Session session = XMPPServer.getInstance().getSessionManager().getOutgoingServerSession(pair); + if (session == null) { + return ""; + } + Log.info("Successfully negotiated TLS connection."); + return formatCertificates(session.getPeerCertificates()); + } + + /** + * Describes each certificate (subject, alternative names, validity) followed by the certificate itself in PEM + * format, so that it can be copied into other tools. + * + * @param certificates the certificate chain presented by the remote server. + * @return the description, never null. + * @throws CertificateEncodingException when a certificate cannot be encoded. + */ + static String formatCertificates(final Certificate[] certificates) throws CertificateEncodingException { StringBuilder certs = new StringBuilder(); - if (session != null) { - Log.info("Successfully negotiated TLS connection."); - Certificate[] certificates = session.getPeerCertificates(); - for (Certificate certificate : certificates) { - X509Certificate x509cert = (X509Certificate) certificate; - certs.append("--\nSubject: "); - certs.append(x509cert.getSubjectDN()); - - List subjectAltNames = new SANCertificateIdentityMapping().mapIdentity(x509cert); - if (!subjectAltNames.isEmpty()) { - certs.append("\nSubject Alternative Names: "); - for (String subjectAltName : subjectAltNames) { - certs.append("\n "); - certs.append(subjectAltName); - } + for (Certificate certificate : certificates) { + X509Certificate x509cert = (X509Certificate) certificate; + certs.append("--\nSubject: "); + certs.append(x509cert.getSubjectDN()); + + List subjectAltNames = new SANCertificateIdentityMapping().mapIdentity(x509cert); + if (!subjectAltNames.isEmpty()) { + certs.append("\nSubject Alternative Names: "); + for (String subjectAltName : subjectAltNames) { + certs.append("\n "); + certs.append(subjectAltName); } - - certs.append("\nNot Before: "); - certs.append(x509cert.getNotBefore()); - certs.append("\nNot After: "); - certs.append(x509cert.getNotAfter()); - certs.append("\n\n-----BEGIN CERTIFICATE-----\n"); - certs.append(DatatypeConverter.printBase64Binary( - certificate.getPublicKey().getEncoded()).replaceAll("(.{64})", "$1\n")); - certs.append("\n-----END CERTIFICATE-----\n\n"); } + + certs.append("\nNot Before: "); + certs.append(x509cert.getNotBefore()); + certs.append("\nNot After: "); + certs.append(x509cert.getNotAfter()); + certs.append("\n\n-----BEGIN CERTIFICATE-----\n"); + certs.append(DatatypeConverter.printBase64Binary( + certificate.getEncoded()).replaceAll("(.{64})", "$1\n")); + certs.append("\n-----END CERTIFICATE-----\n\n"); } return certs.toString(); } diff --git a/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java b/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java new file mode 100644 index 0000000000..75c57afaec --- /dev/null +++ b/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 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. + */ +package org.jivesoftware.util; + +import org.jivesoftware.openfire.keystore.KeystoreTestUtils; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.security.cert.Certificate; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit tests that verify the functionality of {@link S2STestService}. + */ +public class S2STestServiceTest +{ + @Test + public void testCertificateOutputIsTheCertificateInPem() throws Exception + { + // Setup test fixture. + final X509Certificate certificate = KeystoreTestUtils.generateValidCertificateChain("remote.example.org").getCertificateChain()[0]; + + // Execute system under test. + final String output = S2STestService.formatCertificates(new Certificate[] { certificate }); + + // Verify results. + final int begin = output.indexOf("-----BEGIN CERTIFICATE-----"); + final int end = output.indexOf("-----END CERTIFICATE-----") + "-----END CERTIFICATE-----".length(); + assertTrue(begin >= 0 && end > begin, "the output must contain a PEM block"); + final Certificate parsed = CertificateFactory.getInstance("X.509") + .generateCertificate(new ByteArrayInputStream(output.substring(begin, end).getBytes(StandardCharsets.US_ASCII))); + assertEquals(certificate, parsed, "the PEM block must hold the certificate itself, not only its public key"); + assertTrue(output.contains("remote.example.org"), "the output must still show the subject for reading at a glance"); + } +} From b09ca253809ef157de1de9df260c4835f187f498 Mon Sep 17 00:00:00 2001 From: Matthew Vivian Date: Fri, 13 Nov 2026 11:00:36 +0000 Subject: [PATCH 2/3] OF-3465: Write the certificate PEM block without an empty line Wrapping the base64 text with replaceAll("(.{64})", "$1\n") also breaks after the last full line, so when the text is an exact multiple of 64 characters (about one certificate in sixteen) an empty line comes before the END marker. OpenSSL refuses such a block; Java's CertificateFactory accepts it, which is why the existing test passed. Use CertificateManager.toPemRepresentation, which the certificate details page already uses. The new test signs certificates with Ed25519 so that the encoded length can be stepped through every remainder modulo 48. --- .../org/jivesoftware/util/S2STestService.java | 18 +++--- .../jivesoftware/util/S2STestServiceTest.java | 58 +++++++++++++++++++ 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java b/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java index d90ca696d4..6877735d61 100644 --- a/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java +++ b/xmppserver/src/main/java/org/jivesoftware/util/S2STestService.java @@ -42,11 +42,10 @@ import org.xmpp.packet.JID; import org.xmpp.packet.Packet; -import javax.xml.bind.DatatypeConverter; +import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.security.cert.Certificate; -import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.List; @@ -194,9 +193,9 @@ private void logSessionStatus() { /** * @return A String representation of the certificate chain for the connection to the domain under test. - * @throws CertificateEncodingException when a certificate cannot be encoded. + * @throws IOException when a certificate cannot be encoded. */ - private String getCertificates() throws CertificateEncodingException { + private String getCertificates() throws IOException { final DomainPair pair = new DomainPair(XMPPServer.getInstance().getServerInfo().getXMPPDomain(), domain.getDomain()); Session session = XMPPServer.getInstance().getSessionManager().getOutgoingServerSession(pair); if (session == null) { @@ -212,9 +211,9 @@ private String getCertificates() throws CertificateEncodingException { * * @param certificates the certificate chain presented by the remote server. * @return the description, never null. - * @throws CertificateEncodingException when a certificate cannot be encoded. + * @throws IOException when a certificate cannot be encoded. */ - static String formatCertificates(final Certificate[] certificates) throws CertificateEncodingException { + static String formatCertificates(final Certificate[] certificates) throws IOException { StringBuilder certs = new StringBuilder(); for (Certificate certificate : certificates) { X509Certificate x509cert = (X509Certificate) certificate; @@ -234,10 +233,9 @@ static String formatCertificates(final Certificate[] certificates) throws Certif certs.append(x509cert.getNotBefore()); certs.append("\nNot After: "); certs.append(x509cert.getNotAfter()); - certs.append("\n\n-----BEGIN CERTIFICATE-----\n"); - certs.append(DatatypeConverter.printBase64Binary( - certificate.getEncoded()).replaceAll("(.{64})", "$1\n")); - certs.append("\n-----END CERTIFICATE-----\n\n"); + certs.append("\n\n"); + certs.append(CertificateManager.toPemRepresentation(x509cert)); + certs.append("\n"); } return certs.toString(); } diff --git a/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java b/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java index 75c57afaec..f36e16569a 100644 --- a/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java +++ b/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java @@ -15,16 +15,28 @@ */ package org.jivesoftware.util; +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; +import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; import org.jivesoftware.openfire.keystore.KeystoreTestUtils; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; +import java.math.BigInteger; import java.nio.charset.StandardCharsets; +import java.security.KeyPair; +import java.security.KeyPairGenerator; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -32,6 +44,9 @@ */ public class S2STestServiceTest { + /** Two line breaks in a row, which is an empty line. */ + private static final Pattern EMPTY_LINE = Pattern.compile("\\R\\R"); + @Test public void testCertificateOutputIsTheCertificateInPem() throws Exception { @@ -50,4 +65,47 @@ public void testCertificateOutputIsTheCertificateInPem() throws Exception assertEquals(certificate, parsed, "the PEM block must hold the certificate itself, not only its public key"); assertTrue(output.contains("remote.example.org"), "the output must still show the subject for reading at a glance"); } + + /** + * Verifies that the PEM block has no empty line, whatever the length of the certificate. OpenSSL refuses a block + * with an empty line before the END marker, which a naive wrap at 64 characters produces when the base64 text is + * an exact multiple of 64 characters long. Java's CertificateFactory accepts such a block, so parsing it back, as + * the other test does, cannot catch this. + * + * The certificates are signed with Ed25519, whose signature always has the same length, so each extra character + * in the subject name adds one byte to the encoding (a few more where a length field grows), and 96 consecutive + * name lengths cover every length of the encoding modulo 48 (48 bytes being 64 base64 characters). + */ + @Test + public void testCertificateOutputHasNoEmptyLineInThePemBlockForAnyLength() throws Exception + { + // Setup test fixture. + final KeyPair keyPair = KeyPairGenerator.getInstance("Ed25519").generateKeyPair(); + final Set remaindersCovered = new HashSet<>(); + for (int nameLength = 1; nameLength <= 96; nameLength++) { + final X509Certificate certificate = selfSignedCertificate(keyPair, "a".repeat(nameLength)); + remaindersCovered.add(certificate.getEncoded().length % 48); + + // Execute system under test. + final String output = S2STestService.formatCertificates(new Certificate[] { certificate }); + + // Verify results. + final int begin = output.indexOf("-----BEGIN CERTIFICATE-----"); + final int end = output.indexOf("-----END CERTIFICATE-----"); + assertTrue(begin >= 0 && end > begin, "the output must contain a PEM block"); + final String block = output.substring(begin, end); + assertFalse(EMPTY_LINE.matcher(block).find(), "no line in the PEM block may be empty (encoded length " + certificate.getEncoded().length + ")"); + } + assertEquals(48, remaindersCovered.size(), "the certificates must cover every encoded length modulo 48"); + } + + private static X509Certificate selfSignedCertificate(final KeyPair keyPair, final String commonName) throws Exception + { + final X500Name name = new X500Name("CN=" + commonName); + final Date notBefore = new Date(0); + final Date notAfter = new Date(86_400_000L); + return new JcaX509CertificateConverter().getCertificate( + new JcaX509v3CertificateBuilder(name, BigInteger.ONE, notBefore, notAfter, name, keyPair.getPublic()) + .build(new JcaContentSignerBuilder("Ed25519").build(keyPair.getPrivate()))); + } } From b05f184fccd162316a271a7152cf93fdc4f2709b Mon Sep 17 00:00:00 2001 From: Matthew Vivian Date: Fri, 13 Nov 2026 12:30:57 +0000 Subject: [PATCH 3/3] OF-3465: Treat CRLF as one line break in the PEM block test PemWriter ends its lines with the platform's line separator, so on Windows the block holds CRLF. The test looked for an empty line with \R\R, which also matches a single CRLF (\R can match the CR and the LF separately), so it failed on every certificate on Windows. It now turns CRLF into LF before looking for two line breaks in a row. --- .../java/org/jivesoftware/util/S2STestServiceTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java b/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java index f36e16569a..a50d951985 100644 --- a/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java +++ b/xmppserver/src/test/java/org/jivesoftware/util/S2STestServiceTest.java @@ -33,7 +33,6 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; -import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -44,9 +43,6 @@ */ public class S2STestServiceTest { - /** Two line breaks in a row, which is an empty line. */ - private static final Pattern EMPTY_LINE = Pattern.compile("\\R\\R"); - @Test public void testCertificateOutputIsTheCertificateInPem() throws Exception { @@ -93,8 +89,9 @@ public void testCertificateOutputHasNoEmptyLineInThePemBlockForAnyLength() throw final int begin = output.indexOf("-----BEGIN CERTIFICATE-----"); final int end = output.indexOf("-----END CERTIFICATE-----"); assertTrue(begin >= 0 && end > begin, "the output must contain a PEM block"); - final String block = output.substring(begin, end); - assertFalse(EMPTY_LINE.matcher(block).find(), "no line in the PEM block may be empty (encoded length " + certificate.getEncoded().length + ")"); + // PemWriter ends its lines with the platform's line separator, so treat CRLF as one line break. + final String block = output.substring(begin, end).replace("\r\n", "\n"); + assertFalse(block.contains("\n\n"), "no line in the PEM block may be empty (encoded length " + certificate.getEncoded().length + ")"); } assertEquals(48, remaindersCovered.size(), "the certificates must cover every encoded length modulo 48"); }