Skip to content

Commit 84e53bc

Browse files
committed
Add a CertPathBuilder reuse test, covering that the failure recorded by one build is no longer reported by the next and that a successful build leaves nothing behind, exercising both the SPI the provider registers and the plain PKIXCertPathBuilderSpi it only hands out where PKIXRevocationChecker is absent.
1 parent 989cdcc commit 84e53bc

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

‎prov/src/test/java/org/bouncycastle/jce/provider/test/CertPathBuilderTest.java‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.security.cert.CertPath;
1111
import java.security.cert.CertPathBuilder;
1212
import java.security.cert.CertPathBuilderException;
13+
import java.security.cert.CertPathBuilderSpi;
14+
import java.security.cert.CertPathParameters;
1315
import java.security.cert.CertStore;
1416
import java.security.cert.CertificateFactory;
1517
import java.security.cert.CollectionCertStoreParameters;
@@ -58,7 +60,9 @@
5860
import org.bouncycastle.asn1.x509.V2TBSCertListGenerator;
5961
import org.bouncycastle.asn1.x509.V3TBSCertificateGenerator;
6062
import org.bouncycastle.crypto.digests.SHA1Digest;
63+
import org.bouncycastle.jcajce.PKIXExtendedBuilderParameters;
6164
import org.bouncycastle.jce.provider.BouncyCastleProvider;
65+
import org.bouncycastle.jce.provider.PKIXCertPathBuilderSpi;
6266
import org.bouncycastle.util.Properties;
6367
import org.bouncycastle.util.test.SimpleTest;
6468

@@ -349,6 +353,146 @@ private void multipleTrustAnchorsWithCRLTest()
349353
}
350354
}
351355

356+
/**
357+
* A CertPathBuilder may be used for more than one build, and the outcome of one must not
358+
* colour the next. The failure recorded while a build was searching used to be kept in an
359+
* instance field that was never cleared, so a following build that found no path while
360+
* recording no failure of its own - here with the target certificate excluded, so the
361+
* search steps out before any validation happens - reported the earlier build's message
362+
* in place of "Unable to find certificate chain.".
363+
* <p>
364+
* PKIXCertPathBuilderSpi is exercised directly as well as through the provider:
365+
* CertPathBuilder.getInstance("PKIX", "BC") resolves to PKIXCertPathBuilderSpi_8 wherever
366+
* java.security.cert.PKIXRevocationChecker is present, so the plain SPI - which carries
367+
* the same search - is only reached through getInstance on older VMs.
368+
*/
369+
private void builderReuseTest()
370+
throws Exception
371+
{
372+
KeyPair rootPair = TestUtils.generateRSAKeyPair();
373+
KeyPair interPair = TestUtils.generateRSAKeyPair();
374+
KeyPair endPair = TestUtils.generateRSAKeyPair();
375+
376+
X509Certificate rootCert = TestUtils.generateRootCert(rootPair);
377+
X509Certificate interCert = TestUtils.generateIntermediateCert(
378+
interPair.getPublic(), rootPair.getPrivate(), rootCert);
379+
X509Certificate endCert = TestUtils.generateEndEntityCert(
380+
endPair.getPublic(), interPair.getPrivate(), interCert);
381+
382+
List list = new ArrayList();
383+
list.add(interCert);
384+
list.add(endCert);
385+
CertStore store = CertStore.getInstance("Collection", new CollectionCertStoreParameters(list), "BC");
386+
387+
Set anchors = Collections.singleton(new TrustAnchor(rootCert, null));
388+
389+
// TestUtils certificates are valid for thirty minutes from creation
390+
Date validDate = new Date();
391+
Date expiredDate = new Date(validDate.getTime() + 24 * 60 * 60 * 1000L);
392+
393+
// a build that records a failure of its own: the validator rejects the expired path
394+
CertPathParameters recordsFailure = reuseParams(anchors, store, endCert, expiredDate, null);
395+
// a build that records nothing: the target is excluded, so no path is ever validated
396+
CertPathParameters recordsNothing = reuseParams(anchors, store, endCert, validDate, endCert);
397+
398+
// baseline - on a builder used once, each case reports its own outcome
399+
checkBuildFails("fresh builder, excluded target", CertPathBuilder.getInstance("PKIX", "BC"),
400+
recordsNothing, "Unable to find certificate chain.");
401+
checkBuildFails("fresh builder, expired target", CertPathBuilder.getInstance("PKIX", "BC"),
402+
recordsFailure, "Certification path could not be validated.");
403+
404+
// the reuse the baseline cannot see: the second build must not inherit the first's failure
405+
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX", "BC");
406+
checkBuildFails("reused builder, expired target", builder, recordsFailure,
407+
"Certification path could not be validated.");
408+
checkBuildFails("reused builder, excluded target", builder, recordsNothing,
409+
"Unable to find certificate chain.");
410+
checkBuildFails("reused builder, expired target again", builder, recordsFailure,
411+
"Certification path could not be validated.");
412+
413+
// a successful build in between must leave nothing behind either
414+
CertPathParameters succeeds = reuseParams(anchors, store, endCert, validDate, null);
415+
builder = CertPathBuilder.getInstance("PKIX", "BC");
416+
if (((PKIXCertPathBuilderResult)builder.build(succeeds)).getCertPath().getCertificates().size() != 2)
417+
{
418+
fail("wrong number of certs in builderReuseTest path");
419+
}
420+
checkBuildFails("reused builder after success", builder, recordsNothing,
421+
"Unable to find certificate chain.");
422+
423+
// and the same sequence against the SPI the provider does not hand out on this VM
424+
CertPathBuilderSpi spi = new PKIXCertPathBuilderSpi();
425+
checkBuildFails("reused SPI, expired target", spi, recordsFailure,
426+
"Certification path could not be validated.");
427+
checkBuildFails("reused SPI, excluded target", spi, recordsNothing,
428+
"Unable to find certificate chain.");
429+
}
430+
431+
private static CertPathParameters reuseParams(Set anchors, CertStore store, X509Certificate target,
432+
Date date, X509Certificate excluded)
433+
throws Exception
434+
{
435+
X509CertSelector pathConstraints = new X509CertSelector();
436+
pathConstraints.setCertificate(target);
437+
438+
PKIXBuilderParameters baseParams = new PKIXBuilderParameters(anchors, pathConstraints);
439+
baseParams.addCertStore(store);
440+
baseParams.setDate(date);
441+
baseParams.setRevocationEnabled(false);
442+
443+
PKIXExtendedBuilderParameters.Builder bldr = new PKIXExtendedBuilderParameters.Builder(baseParams);
444+
if (excluded != null)
445+
{
446+
bldr.addExcludedCerts(Collections.singleton(excluded));
447+
}
448+
return bldr.build();
449+
}
450+
451+
private void checkBuildFails(String label, CertPathBuilder builder, CertPathParameters params,
452+
String expectedMessage)
453+
throws Exception
454+
{
455+
try
456+
{
457+
builder.build(params);
458+
fail("no exception in " + label);
459+
}
460+
catch (CertPathBuilderException e)
461+
{
462+
checkBuildFailure(label, e, expectedMessage);
463+
}
464+
}
465+
466+
private void checkBuildFails(String label, CertPathBuilderSpi builder, CertPathParameters params,
467+
String expectedMessage)
468+
throws Exception
469+
{
470+
try
471+
{
472+
builder.engineBuild(params);
473+
fail("no exception in " + label);
474+
}
475+
catch (CertPathBuilderException e)
476+
{
477+
checkBuildFailure(label, e, expectedMessage);
478+
}
479+
}
480+
481+
private void checkBuildFailure(String label, CertPathBuilderException e, String expectedMessage)
482+
{
483+
if (!expectedMessage.equals(e.getMessage()))
484+
{
485+
fail("wrong message in " + label + ": " + e.getMessage());
486+
}
487+
// "Unable to find certificate chain." reports the absence of a recorded failure, so it
488+
// must not arrive carrying one
489+
boolean expectCause = !"Unable to find certificate chain.".equals(expectedMessage);
490+
if (expectCause != (e.getCause() != null))
491+
{
492+
fail("wrong cause in " + label + ": " + e.getCause());
493+
}
494+
}
495+
352496
public void performTest()
353497
throws Exception
354498
{
@@ -360,6 +504,7 @@ public void performTest()
360504
multipleTrustAnchorsWithCRLTest();
361505
manyTrustAnchorsAkiNarrowingPerfTest();
362506
builderNodeBudgetTest();
507+
builderReuseTest();
363508
}
364509

365510
private static final String SHA256_RSA = "SHA256withRSA";

0 commit comments

Comments
 (0)