Skip to content

Commit 32f10fd

Browse files
committed
Stop the misc S/MIME example smoke test contacting smtp.gmail.com, where a silently dropped outbound port 25 left Transport.send() blocked without a connect timeout and hung the whole :misc:test run, delivering to an in-process loopback SMTP stub with connect / read / write timeouts instead and asserting the message arrives, relates to github #2407.
1 parent 513f10a commit 32f10fd

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h2>2.0 Release History</h2>
2323
Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2026, TBD
2424
<h3>2.1.2 Defects Fixed</h3>
2525
<ul>
26+
<li>The S/MIME example smoke test in the misc module (org.bouncycastle.mail.smime.examples.test.AllTests) drove SendSignedAndEncryptedMail against smtp.gmail.com, and that example finishes with Transport.send() under JavaMail's default settings, which have no connect timeout. Where outbound port 25 is refused the failure was swallowed and the test passed; where it is silently dropped, as on many home networks, the connect blocked and ./gradlew build hung in :misc:test indefinitely with "0 tests completed". The test now delivers to an SMTP stub on a loopback port, with connect / read / write timeouts as a backstop, and asserts the message arrived (github #2407).</li>
2627
<li>Composite ML-KEM encapsulation took the traditional component public key bytes it feeds the KEM combiner from the recipient key's own encoding, while decapsulation recomputes the point from the private key and so always produced an uncompressed one. Section 4 of draft-ietf-lamps-pq-composite-kem requires an EC component to be carried as an uncompressed point, but a component key that encodes itself compressed - a BC EC key whose point format has been set through org.bouncycastle.jce.interfaces.ECPointEncoder, or a key from a provider that preserves a compressed encoding - was passed through as it came. Both sides then combined a different tradPK and derived different shared secrets, with no error reported on either: encapsulation and decapsulation both succeeded and the recipient simply could not decrypt. The EC component is now normalised to an uncompressed point wherever the engine serialises one, which covers the ephemeral key that forms the ciphertext as well. X25519 and X448 components have a single encoding and were unaffected, as were EC keys left in their default (uncompressed) format, whose shared secrets are unchanged. CompositePublicKey.getEncoded() took its component bytes the same way, so such a key also encoded to a composite key other implementations reject and whose bytes changed across an encode / decode / encode round trip - 1238 bytes rather than 1270 for MLKEM768-ECDH-P256, and for the composite ML-DSA keys sharing that method, 2006 rather than 2038 for MLDSA65-ECDSA-P256. It now normalises the component the same way. This is a write-side change only: a composite key carrying a compressed EC component is still decoded, since the component key factories accept either form, and continues to verify signatures as before - it simply re-encodes in the normalised form. The shared normalisation is org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil.getUncompressedSubjectPublicKeyBytes.</li>
2728
<li>Composite ML-KEM encapsulation threw a NullPointerException, wrapped in an IllegalStateException out of KeyGenerator.generateKey(), when the SecureRandom it was given was null - which javax.crypto.KEM.newEncapsulator() documents as a request for the provider's default, and which KeyGenerator.init(spec, null) passes straight through. The three RSA-OAEP composites draw the traditional shared secret from that random directly, so they were the ones affected; the ECDH and X25519 / X448 composites escaped only because their component KeyPairGenerators default a random of their own. CompositeMLKEMEngine now defaults one through CryptoServicesRegistrar.getSecureRandom() on first use, as the composite KEM Cipher's wrap path already did, and as the KEM generators corrected earlier in this cycle now do. Related, the engine now also clears the ML-KEM component's shared secret alongside the traditional one on both the encapsulate and decapsulate paths - the copy handed back by getEncoded() was left in the heap - as section 3.5 of draft-ietf-lamps-pq-composite-kem requires.</li>
2829
<li>CompositePublicKey.getAlgorithm() and CompositePrivateKey.getAlgorithm() returned null for all twelve Composite ML-KEM (draft-ietf-lamps-pq-composite-kem) parameter sets. Both classes resolved the name through the composite signature index only, which holds the composite ML-DSA OIDs, so a composite KEM key pair - generated, parsed from a certificate, or read from PKCS#8 - reported no algorithm at all, and the standard JCA idiom of reconstructing a key with KeyFactory.getInstance(key.getAlgorithm()) raised a NullPointerException. The lookup now falls back to the composite KEM index, so the name returned is the one the provider registers the algorithm under (e.g. MLKEM768-X25519-SHA3-256), matching the composite ML-DSA behaviour. The same single-index assumption made the CompositePublicKey(SubjectPublicKeyInfo) and CompositePrivateKey(PrivateKeyInfo) constructors reject a composite KEM key with "unable to create CompositePublicKey from SubjectPublicKeyInfo"; they now dispatch to the composite KEM key factory for those OIDs. Keys obtained through KeyFactory or through BouncyCastleProvider.getPublicKey / getPrivateKey were unaffected and are unchanged (github #2404).</li>

‎misc/src/test/java/org/bouncycastle/mail/smime/examples/test/AllTests.java‎

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package org.bouncycastle.mail.smime.examples.test;
22

3+
import java.io.BufferedReader;
34
import java.io.ByteArrayOutputStream;
45
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
58
import java.io.PrintStream;
9+
import java.io.PrintWriter;
10+
import java.net.InetAddress;
11+
import java.net.ServerSocket;
12+
import java.net.Socket;
613
import java.security.KeyStore;
714
import java.util.Enumeration;
815

@@ -91,10 +98,139 @@ public void testExamples()
9198
}
9299
}
93100

94-
SendSignedAndEncryptedMail.main(new String[]{"id.p12", "hello world", keyAlias, "smtp.gmail.com", "recipient@example.com"});
101+
// SendSignedAndEncryptedMail ends with Transport.send(), so the test must not point it
102+
// at a real mail host: JavaMail has no connect timeout by default, and where outbound
103+
// port 25 is silently dropped rather than refused the connect blocked the whole
104+
// :misc:test run indefinitely (github #2407). Deliver to a loopback SMTP stub instead,
105+
// with timeouts as a backstop; the example reads the mail.smtp.* settings from the
106+
// system properties.
107+
SmtpStub smtp = new SmtpStub();
108+
smtp.start();
109+
try
110+
{
111+
System.setProperty("mail.smtp.port", Integer.toString(smtp.getPort()));
112+
System.setProperty("mail.smtp.connectiontimeout", "10000");
113+
System.setProperty("mail.smtp.timeout", "10000");
114+
System.setProperty("mail.smtp.writetimeout", "10000");
115+
116+
SendSignedAndEncryptedMail.main(new String[]{"id.p12", "hello world", keyAlias, "127.0.0.1", "recipient@example.com"});
117+
}
118+
finally
119+
{
120+
smtp.close();
121+
}
122+
assertTrue("SMTP stub did not receive the message: " + _currentErr, smtp.receivedMessage());
123+
95124
ValidateSignedMail.main(null);
96125
}
97126

127+
/**
128+
* Minimal single-connection SMTP responder on a loopback port: greets, answers every command
129+
* with 250, accepts one DATA body, and records that a message was delivered.
130+
*/
131+
private static class SmtpStub
132+
extends Thread
133+
{
134+
private final ServerSocket serverSocket;
135+
private volatile boolean received;
136+
137+
SmtpStub()
138+
throws IOException
139+
{
140+
serverSocket = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"));
141+
serverSocket.setSoTimeout(30000);
142+
setDaemon(true);
143+
}
144+
145+
int getPort()
146+
{
147+
return serverSocket.getLocalPort();
148+
}
149+
150+
boolean receivedMessage()
151+
{
152+
return received;
153+
}
154+
155+
public void run()
156+
{
157+
try
158+
{
159+
Socket socket = serverSocket.accept();
160+
try
161+
{
162+
socket.setSoTimeout(30000);
163+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
164+
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
165+
166+
out.print("220 localhost ESMTP stub\r\n");
167+
out.flush();
168+
169+
boolean inData = false;
170+
String line;
171+
while ((line = in.readLine()) != null)
172+
{
173+
if (inData)
174+
{
175+
if (line.equals("."))
176+
{
177+
inData = false;
178+
received = true;
179+
out.print("250 OK\r\n");
180+
out.flush();
181+
}
182+
continue;
183+
}
184+
185+
String cmd = line.toUpperCase();
186+
if (cmd.startsWith("EHLO") || cmd.startsWith("HELO"))
187+
{
188+
out.print("250 localhost\r\n");
189+
}
190+
else if (cmd.startsWith("DATA"))
191+
{
192+
inData = true;
193+
out.print("354 End data with <CR><LF>.<CR><LF>\r\n");
194+
}
195+
else if (cmd.startsWith("QUIT"))
196+
{
197+
out.print("221 Bye\r\n");
198+
out.flush();
199+
break;
200+
}
201+
else
202+
{
203+
out.print("250 OK\r\n");
204+
}
205+
out.flush();
206+
}
207+
}
208+
finally
209+
{
210+
socket.close();
211+
}
212+
}
213+
catch (IOException e)
214+
{
215+
// accept timed out or the client went away: receivedMessage() reports the result
216+
}
217+
}
218+
219+
void close()
220+
throws IOException
221+
{
222+
serverSocket.close();
223+
try
224+
{
225+
join(30000);
226+
}
227+
catch (InterruptedException e)
228+
{
229+
Thread.currentThread().interrupt();
230+
}
231+
}
232+
}
233+
98234
public static void main(String[] args)
99235
{
100236
PrintTestResult.printResult(junit.textui.TestRunner.run(suite()));

0 commit comments

Comments
 (0)