@@ -284,8 +284,11 @@ private void checkSignerKeyConstraints(
284284 /**
285285 * Find the target certificate based on the X509CertSelector in params.
286286 *
287- * Searches through all CertStores for a certificate matching the
288- * target constraints.
287+ * Searches for a certificate matching the target constraints in the
288+ * following order:
289+ * 1. Directly set in the selector via setCertificate()
290+ * 2. In CertStores attached to the parameters
291+ * 3. Among trust anchor certificates
289292 *
290293 * @param params PKIXBuilderParameters containing target selector
291294 *
@@ -302,8 +305,7 @@ private X509Certificate findTargetCertificate(PKIXBuilderParameters params)
302305 List <CertStore > certStores = null ;
303306
304307 if (params == null ) {
305- throw new CertPathBuilderException (
306- "PKIXBuilderParameters is null" );
308+ throw new CertPathBuilderException ("PKIXBuilderParameters is null" );
307309 }
308310
309311 selector = params .getTargetCertConstraints ();
@@ -319,7 +321,8 @@ private X509Certificate findTargetCertificate(PKIXBuilderParameters params)
319321 }
320322
321323 x509Selector = (X509CertSelector )selector ;
322- log ("searching for target certificate with selector: " + x509Selector );
324+ log ("searching for target certificate with selector: " +
325+ x509Selector );
323326
324327 /* Check if target cert is directly set in selector */
325328 targetCert = x509Selector .getCertificate ();
@@ -330,32 +333,42 @@ private X509Certificate findTargetCertificate(PKIXBuilderParameters params)
330333
331334 /* Search through CertStores for matching certificate */
332335 certStores = params .getCertStores ();
333- if (certStores == null || certStores .isEmpty ()) {
334- throw new CertPathBuilderException (
335- "No CertStores provided in PKIXBuilderParameters" );
336- }
336+ if (certStores != null ) {
337+ for (CertStore store : certStores ) {
338+ try {
339+ Collection <? extends Certificate > certs =
340+ store .getCertificates (x509Selector );
337341
338- for (CertStore store : certStores ) {
339- try {
340- Collection <? extends Certificate > certs =
341- store .getCertificates (x509Selector );
342-
343- if (certs != null && !certs .isEmpty ()) {
344- /* Return first matching certificate */
345- targetCert = (X509Certificate ) certs .iterator ().next ();
346- log ("found target certificate: " +
347- targetCert .getSubjectX500Principal ().getName ());
348- return targetCert ;
342+ if (certs != null && !certs .isEmpty ()) {
343+ targetCert = (X509Certificate )certs .iterator ().next ();
344+ log ("found target certificate in CertStore: " +
345+ targetCert .getSubjectX500Principal ().getName ());
346+ return targetCert ;
347+ }
348+
349+ } catch (CertStoreException e ) {
350+ log ("error searching CertStore: " + e .getMessage ());
351+ /* Continue to next store */
349352 }
353+ }
354+ }
350355
351- } catch (CertStoreException e ) {
352- log ("error searching CertStore: " + e .getMessage ());
353- /* Continue to next store */
356+ /* Search trust anchor certificates as fallback */
357+ Set <TrustAnchor > anchors = params .getTrustAnchors ();
358+ if (anchors != null ) {
359+ for (TrustAnchor anchor : anchors ) {
360+ X509Certificate anchorCert = anchor .getTrustedCert ();
361+ if ((anchorCert != null ) && x509Selector .match (anchorCert )) {
362+
363+ log ("found target certificate in trust anchors: " +
364+ anchorCert .getSubjectX500Principal ().getName ());
365+ return anchorCert ;
366+ }
354367 }
355368 }
356369
357370 throw new CertPathBuilderException (
358- "Target certificate not found in CertStores " );
371+ "Target certificate not found matching selector " );
359372 }
360373
361374 /**
0 commit comments