|
62 | 62 | import java.security.cert.CertificateException; |
63 | 63 | import java.security.cert.TrustAnchor; |
64 | 64 | import java.security.cert.X509CertSelector; |
| 65 | +import java.security.cert.CertSelector; |
65 | 66 | import java.security.cert.CertStore; |
66 | 67 | import java.security.cert.CollectionCertStoreParameters; |
67 | 68 | import java.lang.IllegalArgumentException; |
@@ -4478,5 +4479,47 @@ public void testBuildWithNoCertStoresFindsAnchor() |
4478 | 4479 | assertNotNull("CertPathBuilderResult should not be null", result); |
4479 | 4480 | checkPKIXCertPathBuilderResult(result, caCert, caCert.getPublicKey()); |
4480 | 4481 | } |
| 4482 | + |
| 4483 | + /** |
| 4484 | + * Test that building with a non-X509CertSelector target constraint |
| 4485 | + * throws InvalidAlgorithmParameterException. |
| 4486 | + */ |
| 4487 | + @Test |
| 4488 | + public void testNonX509CertSelectorThrowsInvalidAlgParam() |
| 4489 | + throws Exception { |
| 4490 | + |
| 4491 | + X509Certificate caCert = loadCertFromFile(caCertDer); |
| 4492 | + TrustAnchor anchor = new TrustAnchor(caCert, null); |
| 4493 | + |
| 4494 | + /* Custom CertSelector that is not X509CertSelector */ |
| 4495 | + CertSelector oddSelector = new CertSelector() { |
| 4496 | + public boolean match(Certificate cert) { |
| 4497 | + return false; |
| 4498 | + } |
| 4499 | + public Object clone() { |
| 4500 | + try { |
| 4501 | + return super.clone(); |
| 4502 | + } catch (CloneNotSupportedException e) { |
| 4503 | + throw new RuntimeException(e); |
| 4504 | + } |
| 4505 | + } |
| 4506 | + }; |
| 4507 | + |
| 4508 | + PKIXBuilderParameters params = new PKIXBuilderParameters( |
| 4509 | + Collections.singleton(anchor), oddSelector); |
| 4510 | + params.setRevocationEnabled(false); |
| 4511 | + |
| 4512 | + CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX", provider); |
| 4513 | + |
| 4514 | + try { |
| 4515 | + cpb.build(params); |
| 4516 | + fail("Expected InvalidAlgorithmParameterException for " + |
| 4517 | + "non-X509CertSelector"); |
| 4518 | + } catch (InvalidAlgorithmParameterException e) { |
| 4519 | + /* Expected */ |
| 4520 | + assertNotNull("Exception message should not be null", |
| 4521 | + e.getMessage()); |
| 4522 | + } |
| 4523 | + } |
4481 | 4524 | } |
4482 | 4525 |
|
0 commit comments