1 7 8 package java.security; 9 10 11 import java.net.URL ; 12 import java.net.SocketPermission ; 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.Hashtable ; 16 import java.io.ByteArrayInputStream ; 17 import java.io.IOException ; 18 import java.security.cert.*; 19 20 30 31 public class CodeSource implements java.io.Serializable { 32 33 private static final long serialVersionUID = 4977541819976013951L; 34 35 40 private URL location; 41 42 45 private transient CodeSigner [] signers = null; 46 47 50 private transient java.security.cert.Certificate certs[] = null; 51 52 private transient SocketPermission sp; 54 55 private transient CertificateFactory factory = null; 57 58 67 public CodeSource(URL url, java.security.cert.Certificate certs[]) { 68 this.location = url; 69 70 if (certs != null) { 72 this.certs = (java.security.cert.Certificate []) certs.clone(); 73 } 74 } 75 76 86 public CodeSource(URL url, CodeSigner [] signers) { 87 this.location = url; 88 89 if (signers != null) { 91 this.signers = (CodeSigner [])signers.clone(); 92 } 93 } 94 95 100 101 public int hashCode() { 102 if (location != null) 103 return location.hashCode(); 104 else 105 return 0; 106 } 107 108 119 public boolean equals(Object obj) { 120 if (obj == this) 121 return true; 122 123 if (!(obj instanceof CodeSource )) 125 return false; 126 127 CodeSource cs = (CodeSource ) obj; 128 129 if (location == null) { 131 if (cs.location != null) return false; 133 } else { 134 if (!location.equals(cs.location)) return false; 136 } 137 138 return matchCerts(cs, true); 140 } 141 142 147 public final URL getLocation() { 148 150 return this.location; 151 } 152 153 167 public final java.security.cert.Certificate [] getCertificates() { 168 if (certs != null) { 169 return (java.security.cert.Certificate []) certs.clone(); 170 171 } else if (signers != null) { 172 ArrayList certChains = new ArrayList (); 174 for (int i = 0; i < signers.length; i++) { 175 certChains.addAll( 176 signers[i].getSignerCertPath().getCertificates()); 177 } 178 certs = (java.security.cert.Certificate []) 179 certChains.toArray( 180 new java.security.cert.Certificate [certChains.size()]); 181 return (java.security.cert.Certificate []) certs.clone(); 182 183 } else { 184 return null; 185 } 186 } 187 188 201 public final CodeSigner [] getCodeSigners() { 202 if (signers != null) { 203 return (CodeSigner []) signers.clone(); 204 205 } else if (certs != null) { 206 signers = convertCertArrayToSignerArray(certs); 208 return (CodeSigner []) signers.clone(); 209 210 } else { 211 return null; 212 } 213 } 214 215 283 284 public boolean implies(CodeSource codesource) 285 { 286 if (codesource == null) 287 return false; 288 289 return matchCerts(codesource, false) && matchLocation(codesource); 290 } 291 292 300 private boolean matchCerts(CodeSource that, boolean strict) 301 { 302 if (certs == null && signers == null) 304 return true; 305 306 if (that.certs == null && that.signers == null) 308 return false; 309 310 boolean match; 311 if (signers != null && that.signers != null) { 313 if (strict && signers.length != that.signers.length) { 314 return false; 315 } 316 for (int i = 0; i < signers.length; i++) { 317 match = false; 318 for (int j = 0; j < that.signers.length; j++) { 319 if (signers[i].equals(that.signers[j])) { 320 match = true; 321 break; 322 } 323 } 324 if (!match) return false; 325 } 326 return true; 327 328 } else { 330 if (strict && certs.length != that.certs.length) { 331 return false; 332 } 333 for (int i = 0; i < certs.length; i++) { 334 match = false; 335 for (int j = 0; j < that.certs.length; j++) { 336 if (certs[i].equals(that.certs[j])) { 337 match = true; 338 break; 339 } 340 } 341 if (!match) return false; 342 } 343 return true; 344 } 345 } 346 347 348 353 private boolean matchLocation(CodeSource that) 354 { 355 if (location == null) { 356 return true; 357 } 358 359 if ((that == null) || (that.location == null)) 360 return false; 361 362 if (location.equals(that.location)) 363 return true; 364 365 if (!location.getProtocol().equals(that.location.getProtocol())) 366 return false; 367 368 String thisHost = location.getHost(); 369 String thatHost = that.location.getHost(); 370 371 if (thisHost != null) { 372 if (("".equals(thisHost) || "localhost".equals(thisHost)) && 373 ("".equals(thatHost) || "localhost".equals(thatHost))) { 374 } else if (!thisHost.equals(thatHost)) { 376 if (thatHost == null) { 377 return false; 378 } 379 if (this.sp == null) { 380 this.sp = new SocketPermission (thisHost, "resolve"); 381 } 382 if (that.sp == null) { 383 that.sp = new SocketPermission (thatHost, "resolve"); 384 } 385 if (!this.sp.implies(that.sp)) { 386 return false; 387 } 388 } 389 } 390 391 if (location.getPort() != -1) { 392 if (location.getPort() != that.location.getPort()) 393 return false; 394 } 395 396 if (location.getFile().endsWith("/-")) { 397 String thisPath = location.getFile().substring(0, 402 location.getFile().length()-1); 403 if (!that.location.getFile().startsWith(thisPath)) 404 return false; 405 } else if (location.getFile().endsWith("/*")) { 406 int last = that.location.getFile().lastIndexOf('/'); 411 if (last == -1) 412 return false; 413 String thisPath = location.getFile().substring(0, 414 location.getFile().length()-1); 415 String thatPath = that.location.getFile().substring(0, last+1); 416 if (!thatPath.equals(thisPath)) 417 return false; 418 } else { 419 if ((!that.location.getFile().equals(location.getFile())) 422 && (!that.location.getFile().equals(location.getFile()+"/"))) { 423 return false; 424 } 425 } 426 427 if (location.getRef() == null) 428 return true; 429 else 430 return location.getRef().equals(that.location.getRef()); 431 } 432 433 439 public String toString() { 440 StringBuilder sb = new StringBuilder (); 441 sb.append("("); 442 sb.append(this.location); 443 444 if (this.certs != null && this.certs.length > 0) { 445 for (int i = 0; i < this.certs.length; i++) { 446 sb.append( " " + this.certs[i]); 447 } 448 449 } else if (this.signers != null && this.signers.length > 0) { 450 for (int i = 0; i < this.signers.length; i++) { 451 sb.append( " " + this.signers[i]); 452 } 453 } else { 454 sb.append(" <no signer certificates>"); 455 } 456 sb.append(")"); 457 return sb.toString(); 458 } 459 460 474 private synchronized void writeObject(java.io.ObjectOutputStream oos) 475 throws IOException 476 { 477 oos.defaultWriteObject(); 479 if (certs == null || certs.length == 0) { 481 oos.writeInt(0); 482 } else { 483 oos.writeInt(certs.length); 485 for (int i = 0; i < certs.length; i++) { 487 java.security.cert.Certificate cert = certs[i]; 488 try { 489 oos.writeUTF(cert.getType()); 490 byte[] encoded = cert.getEncoded(); 491 oos.writeInt(encoded.length); 492 oos.write(encoded); 493 } catch (CertificateEncodingException cee) { 494 throw new IOException (cee.getMessage()); 495 } 496 } 497 } 498 499 if (signers != null && signers.length > 0) { 501 oos.writeObject(signers); 502 } 503 } 504 505 508 private synchronized void readObject(java.io.ObjectInputStream ois) 509 throws IOException , ClassNotFoundException 510 { 511 CertificateFactory cf; 512 Hashtable cfs = null; 513 514 ois.defaultReadObject(); 516 int size = ois.readInt(); 518 if (size > 0) { 519 cfs = new Hashtable (3); 522 this.certs = new java.security.cert.Certificate [size]; 523 } 524 525 for (int i = 0; i < size; i++) { 526 String certType = ois.readUTF(); 529 if (cfs.containsKey(certType)) { 530 cf = (CertificateFactory)cfs.get(certType); 532 } else { 533 try { 535 cf = CertificateFactory.getInstance(certType); 536 } catch (CertificateException ce) { 537 throw new ClassNotFoundException 538 ("Certificate factory for " + certType + " not found"); 539 } 540 cfs.put(certType, cf); 542 } 543 byte[] encoded = null; 545 try { 546 encoded = new byte[ois.readInt()]; 547 } catch (OutOfMemoryError oome) { 548 throw new IOException ("Certificate too big"); 549 } 550 ois.readFully(encoded); 551 ByteArrayInputStream bais = new ByteArrayInputStream (encoded); 552 try { 553 this.certs[i] = cf.generateCertificate(bais); 554 } catch (CertificateException ce) { 555 throw new IOException (ce.getMessage()); 556 } 557 bais.close(); 558 } 559 560 try { 562 this.signers = (CodeSigner [])ois.readObject(); 563 } catch (IOException ioe) { 564 } 566 } 567 568 575 private CodeSigner [] convertCertArrayToSignerArray( 576 java.security.cert.Certificate [] certs) { 577 578 if (certs == null) { 579 return null; 580 } 581 582 try { 583 if (factory == null) { 585 factory = CertificateFactory.getInstance("X.509"); 586 } 587 588 int i = 0; 590 List signers = new ArrayList (); 591 while (i < certs.length) { 592 List certChain = new ArrayList (); 593 certChain.add(certs[i++]); int j = i; 595 596 while (j < certs.length && 599 certs[j] instanceof X509Certificate && 600 ((X509Certificate)certs[j]).getBasicConstraints() != -1) { 601 certChain.add(certs[j]); 602 j++; 603 } 604 i = j; 605 CertPath certPath = factory.generateCertPath(certChain); 606 signers.add(new CodeSigner (certPath, null)); 607 } 608 609 if (signers.isEmpty()) { 610 return null; 611 } else { 612 return (CodeSigner []) 613 signers.toArray(new CodeSigner [signers.size()]); 614 } 615 616 } catch (CertificateException e) { 617 return null; } 619 } 620 } 621 622 | Popular Tags |