Skip to content

Commit 21153e3

Browse files
committed
Move the shared javax.crypto.KEM SPI prologue off jcajce.util.SpiUtil, whose jdk25 twin cannot carry it, onto the never-overlaid KemSpiUtil, so that encapsulate and decapsulate no longer fail with NoSuchMethodError on a JDK 25 runtime, where the jdk17-only SPIs resolve against the versions/25 copy of the hook, with a src/test/jdk25 regression test because test25 is the only task that puts a JDK 25 in front of the multi-release jar and the eleven jdk17 KEM tests cannot see the mismatch, and the rule recorded in the architecture guide.
1 parent 8f331ed commit 21153e3

25 files changed

Lines changed: 312 additions & 103 deletions

‎docs/claude/architecture.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,56 @@ Before changing a method in `src/main/java`, check whether that class has a vers
3636

3737
Because MR-jar resolution is per class file, a **base-tree class can call a class that is overlaid** — on JDK ≥ N the versioned copy is loaded even though the caller came from the jar root. `ECUtil.getNameFrom` → `SpecUtil` (jdk1.11 twin adds the `NamedParameterSpec` fast path) was the original example. So when a provider SPI needs version-specific behaviour, don't fork the whole SPI into `src/main/jdk1.N` — keep the SPI (and its registered inner classes) once in the base tree, and route the genuinely version-specific pieces through a small package-private hook class whose jdk1.N twin swaps them. The `edec` package is the worked example after its 2026 de-duplication: the five SPIs exist only in base and delegate key construction/conversion to `XDHKeys` (jdk1.11 twin: `XECKey` integration) and `EdDSAKeys` (jdk1.15 twin: `EdECKey` + JDK `EdDSAParameterSpec` bridging), with shared byte-level helpers in the never-overlaid `EdECUtil`. The pre-restructure whole-SPI forks had accumulated four behavioural drifts (UKM salt ignored, `EMULATE_ORACLE` dead, RFC 8418 registrations missing per github #2131, third-party-key fallback lost) — the hook shape makes that class of drift impossible, and shrinks what must be kept in step to the hook's method set (state the twin-sync rule in both copies' javadoc). Base copies of hook methods that only exist for a newer JDK return null ("not bridged here"), and the base tree must stay Java-4-source-floor clean since new base classes are swept into the legacy Ant builds.
3838

39+
### The mirror-image trap: an overlaid class must not host code a lower overlay calls
40+
41+
The hook pattern above works because resolution is per class file — but that cuts both ways, and the
42+
reverse direction is a runtime `NoSuchMethodError` rather than silent drift. If class `H` exists in
43+
`jdk17` **and** `jdk25`, and class `C` exists **only** in `jdk17`, then on a JDK 25 runtime `C` loads
44+
from `META-INF/versions/17` while `H` loads from `META-INF/versions/25`. A call from `C` to a method
45+
that only the jdk17 copy of `H` defines compiles fine (javac sees one `H` per source set) and dies at
46+
the call site on JDK 25 only.
47+
48+
So: **a class that has versioned twins is a version hook and nothing else.** Put the version-varying
49+
answers on it and nothing more; shared helper bodies belong on a class with *no* twin anywhere above
50+
the overlay that calls it. `org.bouncycastle.jcajce.util.SpiUtil` is the cautionary example — it
51+
exists in base/`jdk17`/`jdk25` purely so `hasKDF()` / `hasKEM()` can differ, and when the twenty
52+
`javax.crypto.KEM` SPI classes (`jdk17`-only) had their shared prologue extracted onto it, every
53+
encapsulate and decapsulate threw `NoSuchMethodError` on JDK 25 while JDK 21 stayed green. The fix
54+
was to move the two helpers to `jcajce.provider.asymmetric.util.KemSpiUtil`, a `jdk17`-only class in
55+
a package with no overlay above the base tree, and to say so in both classes' javadoc.
56+
57+
Checking this is mechanical, and worth doing whenever you add a cross-class call inside an overlay:
58+
for the callee, `find <module>/src/main -name <Callee>.java` — if that lists a directory whose
59+
version is **higher** than the caller's, the call is unsafe. Confirm against the built jar rather
60+
than the source tree, since the jar is what resolves:
61+
62+
```
63+
unzip -l <module>/build/libs/bcprov-jdk18on-*.jar | grep '<Callee>.class' # one entry per overlay
64+
```
65+
66+
Two entries at `versions/17` and `versions/25` for the callee, one at `versions/17` for the caller,
67+
is the broken shape.
68+
3969
Crucially, the MR overlay's behaviour is **not covered by the normal test suite**. The `test11`/`test15`/`test17`/`test25` tasks run *only* the classes compiled from `src/test/jdk1.N` (their `testClassesDirs`), wired through the `AllTests11`/`AllTests15`/… suites — the base `src/test/java` tests are on the classpath but are **not executed** there. So a base test that would catch the drift never runs against the MR-jar, and the divergence ships undetected. When you touch (or find drift in) an MR-overlaid class, add a `src/test/jdk1.N` test that exercises the overlaid path against the multi-release jar and register it in the matching `AllTestsN` suite. A thin JUnit `TestCase` that runs the relevant base `SimpleTest` via `new XxxTest().perform()` / `assertTrue(result.isSuccessful())` reuses the existing vectors without duplication — see `prov/src/test/jdk1.11|jdk1.15/.../OpenSSHKeyFactoryMRTest.java`, added after the `edec` `KeyFactorySpi` OpenSSH path was found to have drifted (jdk1.11/jdk1.15 lacked passphrase support and threw the wrong exception type, uncaught because no `test11`/`test15` test covered it).
4070

71+
**Which overlay's test tree, though?** Each `testN` task is the *only* one that puts a JDK N runtime
72+
in front of the multi-release jar, so a test only ever exercises the resolution its own version
73+
produces. A test under `src/test/jdk17` cannot see a jdk17-vs-jdk25 mismatch at all: there the caller
74+
and every hook alike come from `versions/17`. So when a class in overlay N calls anything that has a
75+
copy in a *higher* overlay, the test has to go in the **highest** overlay's test tree — today
76+
`src/test/jdk25`, run by `test25` (`BC_JDK25` must be exported for it to run at all; each task is
77+
`onlyIf` its `BC_JDK<N>` env var is set, so an unset one skips silently and prints nothing).
78+
79+
This is exactly how the `SpiUtil` break above shipped: eleven `*KEM17Test` classes covered all ten
80+
KEM families through `javax.crypto.KEM` under `src/test/jdk17`, and every one passed, while
81+
`src/test/jdk25` held three KDF tests and nothing else — so the jdk17-only SPIs had no JDK 25
82+
coverage of any kind. `prov/src/test/jdk25/.../KemSpiMRTest.java` closes it, and is the model: pick
83+
one class per SPI *package tree* rather than all of them (they share the helper, so one is enough to
84+
catch a break) and add an `AllTests25` in its package, since `test25`'s filter is
85+
`includeTestsMatching "AllTest*"`. Note `test25` emits its results as the HTML report plus
86+
`test-results/test25/binary`, not per-class XML — read
87+
`prov/build/reports/tests/test25/index.html` for the counts rather than looking for `TEST-*.xml`.
88+
4189
## Update `module-info.java` when you add or remove package
4290

4391
Each Gradle-built module has a JPMS descriptor at `<module>/src/main/jdk1.9/module-info.java` (e.g. `prov/src/main/jdk1.9/module-info.java`, `pkix/src/main/jdk1.9/module-info.java`) listing every exported package. The Java 8 sources under `<module>/src/main/java` and the descriptor are bundled into the same multi-release jar; the descriptor is the source of truth for what's visible when downstream code runs on JDK 9+ with `--module-path`. A package that exists in the source tree but isn't listed in `module-info.java` is invisible to modular consumers — class-path consumers still see it, which is why the omission is easy to miss locally. Note: `core` itself has no module-info; its sources are bundled into the published `bcprov` jar via the core-into-prov srcDirs trick, so the `prov` module-info exports `core` packages.

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/cmce/CMCEDecapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.bouncycastle.crypto.params.CMCEPrivateKeyParameters;
99
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
1010
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
11-
import org.bouncycastle.jcajce.util.SpiUtil;
11+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1212

1313
/*
1414
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -33,7 +33,7 @@ class CMCEDecapsulatorSpi
3333
public SecretKey engineDecapsulate(byte[] encapsulation, int from, int to, String algorithm)
3434
throws DecapsulateException
3535
{
36-
algorithm = SpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
36+
algorithm = KemSpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
3737

3838
// CMCEEngine allocates its digest per call, so a shared extractor would be safe here and
3939
// the families whose engines are also safe - NTRU LPRime, SMAUG-T and the four older ones -

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/cmce/CMCEEncapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.bouncycastle.crypto.kems.CMCEKEMGenerator;
99
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -33,7 +33,7 @@ class CMCEEncapsulatorSpi
3333
@Override
3434
public KEM.Encapsulated engineEncapsulate(int from, int to, String algorithm)
3535
{
36-
return SpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParams(), parameterSpec);
36+
return KemSpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParams(), parameterSpec);
3737
}
3838

3939
@Override

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/frodokem/FrodoKEMDecapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.bouncycastle.crypto.kems.FrodoKEMExtractor;
88
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
99
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -30,7 +30,7 @@ class FrodoKEMDecapsulatorSpi
3030
public SecretKey engineDecapsulate(byte[] encapsulation, int from, int to, String algorithm)
3131
throws DecapsulateException
3232
{
33-
algorithm = SpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
33+
algorithm = KemSpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
3434

3535
// FrodoKEMEngine builds its SHAKE instance per call, so one extractor is safe to share
3636
// across the concurrent decapsulate calls javax.crypto.KEM requires.

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/frodokem/FrodoKEMEncapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.bouncycastle.crypto.kems.FrodoKEMGenerator;
99
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -33,7 +33,7 @@ class FrodoKEMEncapsulatorSpi
3333
@Override
3434
public KEM.Encapsulated engineEncapsulate(int from, int to, String algorithm)
3535
{
36-
return SpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParams(), parameterSpec);
36+
return KemSpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParams(), parameterSpec);
3737
}
3838

3939
@Override

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/mlkem/MLKEMDecapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
88
import org.bouncycastle.crypto.kems.MLKEMExtractor;
99
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -30,7 +30,7 @@ class MLKEMDecapsulatorSpi
3030
public SecretKey engineDecapsulate(byte[] encapsulation, int from, int to, String algorithm)
3131
throws DecapsulateException
3232
{
33-
algorithm = SpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
33+
algorithm = KemSpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
3434

3535
byte[] kemSecret = kemExt.extractSecret(encapsulation);
3636

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/mlkem/MLKEMEncapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
99
import org.bouncycastle.crypto.kems.MLKEMGenerator;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -31,7 +31,7 @@ class MLKEMEncapsulatorSpi
3131
@Override
3232
public KEM.Encapsulated engineEncapsulate(int from, int to, String algorithm)
3333
{
34-
return SpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParams(), parameterSpec);
34+
return KemSpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParams(), parameterSpec);
3535
}
3636

3737
@Override

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/sm9/SM9DecapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.bouncycastle.crypto.kems.SM9KEMExtractor;
88
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
99
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -33,7 +33,7 @@ class SM9DecapsulatorSpi
3333
public SecretKey engineDecapsulate(byte[] encapsulation, int from, int to, String algorithm)
3434
throws DecapsulateException
3535
{
36-
algorithm = SpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
36+
algorithm = KemSpiUtil.resolveDecapsulateAlgorithm(encapsulation, from, to, algorithm, engineSecretSize(), engineEncapsulationSize(), parameterSpec);
3737

3838
byte[] kemSecret;
3939
try

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/sm9/SM9EncapsulatorSpi.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.bouncycastle.crypto.kems.SM9KEMGenerator;
99
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
10-
import org.bouncycastle.jcajce.util.SpiUtil;
10+
import org.bouncycastle.jcajce.provider.asymmetric.util.KemSpiUtil;
1111

1212
/*
1313
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -34,7 +34,7 @@ class SM9EncapsulatorSpi
3434
@Override
3535
public KEM.Encapsulated engineEncapsulate(int from, int to, String algorithm)
3636
{
37-
return SpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParameters(), parameterSpec);
37+
return KemSpiUtil.buildEncapsulated(from, to, algorithm, engineSecretSize(), kemGen, publicKey.getKeyParameters(), parameterSpec);
3838
}
3939

4040
@Override
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.bouncycastle.jcajce.provider.asymmetric.util;
2+
3+
import java.util.Objects;
4+
5+
import javax.crypto.DecapsulateException;
6+
import javax.crypto.KEM;
7+
import javax.crypto.SecretKey;
8+
import javax.security.auth.DestroyFailedException;
9+
10+
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
11+
import org.bouncycastle.crypto.SecretWithEncapsulation;
12+
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
13+
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
14+
15+
/**
16+
* The prologue and the encapsulate body shared by every {@code javax.crypto.KEMSpi} implementation
17+
* in this overlay - the ten {@code *EncapsulatorSpi} / {@code *DecapsulatorSpi} pairs across
18+
* {@code jcajce.provider.asymmetric} and {@code pqc.jcajce.provider}, which differ only in the
19+
* engine and key they hand in.
20+
* <p>
21+
* <b>This class must not be given a {@code jdk1.N} twin, and these methods must not be moved onto a
22+
* class that has one.</b> Multi-release resolution is per class file: the SPIs exist only in the
23+
* {@code jdk17} overlay, so on a JDK 25 runtime they are loaded from {@code META-INF/versions/17}
24+
* while a class that also has a {@code jdk25} copy is loaded from {@code META-INF/versions/25}. A
25+
* helper hosted on such a class is therefore a different class than the one the SPIs were compiled
26+
* against. These two methods first lived on {@code org.bouncycastle.jcajce.util.SpiUtil}, whose
27+
* jdk25 twin exists because {@code hasKDF()} differs there and which consequently carries neither
28+
* method, and every encapsulate and decapsulate failed with {@code NoSuchMethodError} on JDK 25
29+
* while JDK 21 was unaffected. {@code jcajce.provider.asymmetric.util} has no overlay above the
30+
* base tree, so a class added here resolves from {@code versions/17} on every runtime that has the
31+
* SPIs at all. {@code KemSpiMRTest} in {@code prov/src/test/jdk25} guards this.
32+
*/
33+
public class KemSpiUtil
34+
{
35+
private KemSpiUtil()
36+
{
37+
}
38+
39+
public static String resolveDecapsulateAlgorithm(byte[] encapsulation, int from, int to, String algorithm,
40+
int engineSecretSize, int engineEncapsulationSize, KTSParameterSpec parameterSpec)
41+
throws DecapsulateException
42+
{
43+
Objects.checkFromToIndex(from, to, engineSecretSize);
44+
Objects.requireNonNull(algorithm, "null algorithm");
45+
Objects.requireNonNull(encapsulation, "null encapsulation");
46+
47+
if (encapsulation.length != engineEncapsulationSize)
48+
{
49+
throw new DecapsulateException("incorrect encapsulation size");
50+
}
51+
52+
return KdfUtil.resolveAlgorithm(parameterSpec, algorithm);
53+
}
54+
55+
public static KEM.Encapsulated buildEncapsulated(int from, int to, String algorithm, int engineSecretSize,
56+
EncapsulatedSecretGenerator kemGen, AsymmetricKeyParameter recipientKey,
57+
KTSParameterSpec parameterSpec)
58+
{
59+
Objects.checkFromToIndex(from, to, engineSecretSize);
60+
Objects.requireNonNull(algorithm, "null algorithm");
61+
62+
algorithm = KdfUtil.resolveAlgorithm(parameterSpec, algorithm);
63+
64+
SecretWithEncapsulation secEnc = kemGen.generateEncapsulated(recipientKey);
65+
66+
try
67+
{
68+
// getEncapsulation()/getSecret() hand back clones, so the originals have to be
69+
// destroyed as well - KdfUtil.makeSecretKey only clears the secret clone it is passed.
70+
byte[] encapsulation = secEnc.getEncapsulation();
71+
72+
SecretKey secretKey = KdfUtil.makeSecretKey(parameterSpec, secEnc.getSecret(),
73+
from, to, algorithm);
74+
75+
return new KEM.Encapsulated(secretKey, encapsulation, null);
76+
}
77+
finally
78+
{
79+
try
80+
{
81+
secEnc.destroy();
82+
}
83+
catch (DestroyFailedException e)
84+
{
85+
// ignore
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)