1 7 8 package java.security.cert; 9 10 import java.io.IOException ; 11 import java.math.BigInteger ; 12 import java.util.*; 13 14 import javax.security.auth.x500.X500Principal ; 15 16 import sun.security.util.Debug; 17 import sun.security.util.DerInputStream; 18 import sun.security.x509.CRLNumberExtension; 19 import sun.security.x509.X500Name; 20 21 55 public class X509CRLSelector implements CRLSelector { 56 57 static { 58 CertPathHelperImpl.initialize(); 59 } 60 61 private static final Debug debug = Debug.getInstance("certpath"); 62 private HashSet<Object > issuerNames; 63 private HashSet<X500Principal > issuerX500Principals; 64 private BigInteger minCRL; 65 private BigInteger maxCRL; 66 private Date dateAndTime; 67 private X509Certificate certChecking; 68 69 73 public X509CRLSelector() {} 74 75 102 public void setIssuers(Collection<X500Principal > issuers) { 103 if ((issuers == null) || issuers.isEmpty()) { 104 issuerNames = null; 105 issuerX500Principals = null; 106 } else { 107 issuerX500Principals = new HashSet(issuers); 109 issuerNames = new HashSet<Object >(); 110 for (X500Principal p : issuerX500Principals) { 111 issuerNames.add(p.getEncoded()); 112 } 113 } 114 } 115 116 177 public void setIssuerNames(Collection<?> names) throws IOException { 178 if (names == null || names.size() == 0) { 179 issuerNames = null; 180 issuerX500Principals = null; 181 } else { 182 HashSet<Object > tempNames = cloneAndCheckIssuerNames(names); 183 issuerX500Principals = parseIssuerNames(tempNames); 185 issuerNames = tempNames; 186 } 187 } 188 189 202 public void addIssuer(X500Principal issuer) { 203 addIssuerNameInternal(issuer.getEncoded(), issuer); 204 } 205 206 226 public void addIssuerName(String name) throws IOException { 227 addIssuerNameInternal(name, new X500Name(name).asX500Principal()); 228 } 229 230 254 public void addIssuerName(byte[] name) throws IOException { 255 addIssuerNameInternal(name.clone(), new X500Name(name).asX500Principal()); 257 } 258 259 269 private void addIssuerNameInternal(Object name, X500Principal principal) { 270 if (issuerNames == null) { 271 issuerNames = new HashSet<Object >(); 272 } 273 if (issuerX500Principals == null) { 274 issuerX500Principals = new HashSet<X500Principal >(); 275 } 276 issuerNames.add(name); 277 issuerX500Principals.add(principal); 278 } 279 280 291 private static HashSet<Object > cloneAndCheckIssuerNames(Collection<?> names) 292 throws IOException 293 { 294 HashSet<Object > namesCopy = new HashSet<Object >(); 295 Iterator i = names.iterator(); 296 while (i.hasNext()) { 297 Object nameObject = i.next(); 298 if (!(nameObject instanceof byte []) && 299 !(nameObject instanceof String )) 300 throw new IOException ("name not byte array or String"); 301 if (nameObject instanceof byte []) 302 namesCopy.add(((byte []) nameObject).clone()); 303 else 304 namesCopy.add(nameObject); 305 } 306 return(namesCopy); 307 } 308 309 324 private static HashSet<Object > cloneIssuerNames(Collection<Object > names) { 325 try { 326 return cloneAndCheckIssuerNames(names); 327 } catch (IOException ioe) { 328 throw new RuntimeException (ioe); 329 } 330 } 331 332 344 private static HashSet<X500Principal > parseIssuerNames(Collection<Object > names) 345 throws IOException { 346 HashSet<X500Principal > x500Principals = new HashSet<X500Principal >(); 347 for (Iterator t = names.iterator(); t.hasNext(); ) { 348 Object nameObject = t.next(); 349 if (nameObject instanceof String ) { 350 x500Principals.add(new X500Name((String )nameObject).asX500Principal()); 351 } else { 352 try { 353 x500Principals.add(new X500Principal ((byte[])nameObject)); 354 } catch (IllegalArgumentException e) { 355 throw (IOException )new IOException ("Invalid name").initCause(e); 356 } 357 } 358 } 359 return x500Principals; 360 } 361 362 370 public void setMinCRLNumber(BigInteger minCRL) { 371 this.minCRL = minCRL; 372 } 373 374 382 public void setMaxCRLNumber(BigInteger maxCRL) { 383 this.maxCRL = maxCRL; 384 } 385 386 401 public void setDateAndTime(Date dateAndTime) { 402 if (dateAndTime == null) 403 this.dateAndTime = null; 404 else 405 this.dateAndTime = (Date) dateAndTime.clone(); 406 } 407 408 419 public void setCertificateChecking(X509Certificate cert) { 420 certChecking = cert; 421 } 422 423 437 public Collection<X500Principal > getIssuers() { 438 if (issuerX500Principals == null) { 439 return null; 440 } 441 return Collections.unmodifiableCollection(issuerX500Principals); 442 } 443 444 467 public Collection<Object > getIssuerNames() { 468 if (issuerNames == null) { 469 return null; 470 } 471 return cloneIssuerNames(issuerNames); 472 } 473 474 481 public BigInteger getMinCRL() { 482 return minCRL; 483 } 484 485 493 public BigInteger getMaxCRL() { 494 return maxCRL; 495 } 496 497 511 public Date getDateAndTime() { 512 if (dateAndTime == null) 513 return null; 514 return (Date) dateAndTime.clone(); 515 } 516 517 527 public X509Certificate getCertificateChecking() { 528 return certChecking; 529 } 530 531 537 public String toString() { 538 StringBuffer sb = new StringBuffer (); 539 sb.append("X509CRLSelector: [\n"); 540 if (issuerNames != null) { 541 sb.append(" IssuerNames:\n"); 542 Iterator i = issuerNames.iterator(); 543 while (i.hasNext()) 544 sb.append(" " + i.next() + "\n"); 545 } 546 if (minCRL != null) 547 sb.append(" minCRLNumber: " + minCRL + "\n"); 548 if (maxCRL != null) 549 sb.append(" maxCRLNumber: " + maxCRL + "\n"); 550 if (dateAndTime != null) 551 sb.append(" dateAndTime: " + dateAndTime + "\n"); 552 if (certChecking != null) 553 sb.append(" Certificate being checked: " + certChecking + "\n"); 554 sb.append("]"); 555 return sb.toString(); 556 } 557 558 565 public boolean match(CRL crl) { 566 if (!(crl instanceof X509CRL )) { 567 return false; 568 } 569 X509CRL xcrl = (X509CRL )crl; 570 571 572 if (issuerNames != null) { 573 X500Principal issuer = xcrl.getIssuerX500Principal(); 574 Iterator i = issuerX500Principals.iterator(); 575 boolean found = false; 576 while (!found && i.hasNext()) { 577 if (i.next().equals(issuer)) { 578 found = true; 579 } 580 } 581 if (!found) { 582 if (debug != null) { 583 debug.println("X509CRLSelector.match: issuer DNs " 584 + "don't match"); 585 } 586 return false; 587 } 588 } 589 590 if ((minCRL != null) || (maxCRL != null)) { 591 592 byte[] crlNumExtVal = xcrl.getExtensionValue("2.5.29.20"); 593 if (crlNumExtVal == null) { 594 if (debug != null) { 595 debug.println("X509CRLSelector.match: no CRLNumber"); 596 } 597 } 598 BigInteger crlNum; 599 try { 600 DerInputStream in = new DerInputStream(crlNumExtVal); 601 byte[] encoded = in.getOctetString(); 602 CRLNumberExtension crlNumExt = 603 new CRLNumberExtension(Boolean.FALSE, encoded); 604 crlNum = (BigInteger )crlNumExt.get(CRLNumberExtension.NUMBER); 605 } catch (IOException ex) { 606 if (debug != null) { 607 debug.println("X509CRLSelector.match: exception in " 608 + "decoding CRL number"); 609 } 610 return false; 611 } 612 613 614 if (minCRL != null) { 615 if (crlNum.compareTo(minCRL) < 0) { 616 if (debug != null) { 617 debug.println("X509CRLSelector.match: CRLNumber too small"); 618 } 619 return false; 620 } 621 } 622 623 624 if (maxCRL != null) { 625 if (crlNum.compareTo(maxCRL) > 0) { 626 if (debug != null) { 627 debug.println("X509CRLSelector.match: CRLNumber too large"); 628 } 629 return false; 630 } 631 } 632 } 633 634 635 636 if (dateAndTime != null) { 637 Date crlThisUpdate = xcrl.getThisUpdate(); 638 Date nextUpdate = xcrl.getNextUpdate(); 639 if (nextUpdate == null) { 640 if (debug != null) { 641 debug.println("X509CRLSelector.match: nextUpdate null"); 642 } 643 return false; 644 } 645 if (crlThisUpdate.after(dateAndTime) 646 || nextUpdate.before(dateAndTime)) { 647 if (debug != null) { 648 debug.println("X509CRLSelector.match: update out of range"); 649 } 650 return false; 651 } 652 } 653 654 return true; 655 } 656 657 662 public Object clone() { 663 try { 664 X509CRLSelector copy = (X509CRLSelector )super.clone(); 665 if (issuerNames != null) { 666 copy.issuerNames = 667 new HashSet<Object >(issuerNames); 668 copy.issuerX500Principals = 669 new HashSet<X500Principal >(issuerX500Principals); 670 } 671 return copy; 672 } catch (CloneNotSupportedException e) { 673 674 throw new InternalError (e.toString()); 675 } 676 } 677 } 678 | Popular Tags |