1 7 8 package java.security; 9 10 import java.io.IOException ; 11 import java.io.ByteArrayInputStream ; 12 import java.util.ArrayList ; 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 import java.util.Vector ; 16 import java.lang.reflect.*; 17 import java.security.cert.*; 18 19 85 86 public final class UnresolvedPermission extends Permission 87 implements java.io.Serializable 88 { 89 90 private static final long serialVersionUID = -4821973115467008846L; 91 92 private static final sun.security.util.Debug debug = 93 sun.security.util.Debug.getInstance 94 ("policy,access", "UnresolvedPermission"); 95 96 102 private String type; 103 104 109 private String name; 110 111 116 private String actions; 117 118 private transient java.security.cert.Certificate certs[]; 119 120 137 public UnresolvedPermission(String type, 138 String name, 139 String actions, 140 java.security.cert.Certificate certs[]) 141 { 142 super(type); 143 144 if (type == null) 145 throw new NullPointerException ("type can't be null"); 146 147 this.type = type; 148 this.name = name; 149 this.actions = actions; 150 if (certs != null) { 151 for (int i=0; i<certs.length; i++) { 153 if (!(certs[i] instanceof X509Certificate)) { 154 this.certs = 157 (java.security.cert.Certificate [])certs.clone(); 158 break; 159 } 160 } 161 162 if (this.certs == null) { 163 int i = 0; 166 int count = 0; 167 while (i < certs.length) { 168 count++; 169 while (((i+1) < certs.length) && 170 ((X509Certificate)certs[i]).getIssuerDN().equals( 171 ((X509Certificate)certs[i+1]).getSubjectDN())) { 172 i++; 173 } 174 i++; 175 } 176 if (count == certs.length) { 177 this.certs = 180 (java.security.cert.Certificate [])certs.clone(); 181 } 182 183 if (this.certs == null) { 184 ArrayList signerCerts = new ArrayList (); 186 i = 0; 187 while (i < certs.length) { 188 signerCerts.add(certs[i]); 189 while (((i+1) < certs.length) && 190 ((X509Certificate)certs[i]).getIssuerDN().equals( 191 ((X509Certificate)certs[i+1]).getSubjectDN())) { 192 i++; 193 } 194 i++; 195 } 196 this.certs = 197 new java.security.cert.Certificate [signerCerts.size()]; 198 signerCerts.toArray(this.certs); 199 } 200 } 201 } 202 } 203 204 205 private static final Class [] PARAMS0 = { }; 206 private static final Class [] PARAMS1 = { String .class }; 207 private static final Class [] PARAMS2 = { String .class, String .class }; 208 209 213 Permission resolve(Permission p, java.security.cert.Certificate certs[]) { 214 if (this.certs != null) { 215 if (certs == null) { 217 return null; 218 } 219 220 boolean match; 222 for (int i = 0; i < this.certs.length; i++) { 223 match = false; 224 for (int j = 0; j < certs.length; j++) { 225 if (this.certs[i].equals(certs[j])) { 226 match = true; 227 break; 228 } 229 } 230 if (!match) return null; 231 } 232 } 233 try { 234 Class pc = p.getClass(); 235 236 if (name == null && actions == null) { 237 try { 238 Constructor c = pc.getConstructor(PARAMS0); 239 return (Permission )c.newInstance(new Object [] {}); 240 } catch (NoSuchMethodException ne) { 241 try { 242 Constructor c = pc.getConstructor(PARAMS1); 243 return (Permission ) c.newInstance( 244 new Object [] { name}); 245 } catch (NoSuchMethodException ne1) { 246 Constructor c = pc.getConstructor(PARAMS2); 247 return (Permission ) c.newInstance( 248 new Object [] { name, actions }); 249 } 250 } 251 } else { 252 if (name != null && actions == null) { 253 try { 254 Constructor c = pc.getConstructor(PARAMS1); 255 return (Permission ) c.newInstance( 256 new Object [] { name}); 257 } catch (NoSuchMethodException ne) { 258 Constructor c = pc.getConstructor(PARAMS2); 259 return (Permission ) c.newInstance( 260 new Object [] { name, actions }); 261 } 262 } else { 263 Constructor c = pc.getConstructor(PARAMS2); 264 return (Permission ) c.newInstance( 265 new Object [] { name, actions }); 266 } 267 } 268 } catch (NoSuchMethodException nsme) { 269 if (debug != null ) { 270 debug.println("NoSuchMethodException:\n could not find " + 271 "proper constructor for " + type); 272 nsme.printStackTrace(); 273 } 274 return null; 275 } catch (Exception e) { 276 if (debug != null ) { 277 debug.println("unable to instantiate " + name); 278 e.printStackTrace(); 279 } 280 return null; 281 } 282 } 283 284 293 public boolean implies(Permission p) { 294 return false; 295 } 296 297 313 public boolean equals(Object obj) { 314 if (obj == this) 315 return true; 316 317 if (! (obj instanceof UnresolvedPermission )) 318 return false; 319 UnresolvedPermission that = (UnresolvedPermission ) obj; 320 321 if (!this.type.equals(that.type)) { 323 return false; 324 } 325 326 if (this.name == null) { 328 if (that.name != null) { 329 return false; 330 } 331 } else if (!this.name.equals(that.name)) { 332 return false; 333 } 334 335 if (this.actions == null) { 337 if (that.actions != null) { 338 return false; 339 } 340 } else { 341 if (!this.actions.equals(that.actions)) { 342 return false; 343 } 344 } 345 346 if ((this.certs == null && that.certs != null) || 348 (this.certs != null && that.certs == null) || 349 (this.certs != null && that.certs != null && 350 this.certs.length != that.certs.length)) { 351 return false; 352 } 353 354 int i,j; 355 boolean match; 356 357 for (i = 0; this.certs != null && i < this.certs.length; i++) { 358 match = false; 359 for (j = 0; j < that.certs.length; j++) { 360 if (this.certs[i].equals(that.certs[j])) { 361 match = true; 362 break; 363 } 364 } 365 if (!match) return false; 366 } 367 368 for (i = 0; that.certs != null && i < that.certs.length; i++) { 369 match = false; 370 for (j = 0; j < this.certs.length; j++) { 371 if (that.certs[i].equals(this.certs[j])) { 372 match = true; 373 break; 374 } 375 } 376 if (!match) return false; 377 } 378 return true; 379 } 380 381 386 387 public int hashCode() { 388 int hash = type.hashCode(); 389 if (name != null) 390 hash ^= name.hashCode(); 391 if (actions != null) 392 hash ^= actions.hashCode(); 393 return hash; 394 } 395 396 406 public String getActions() 407 { 408 return ""; 409 } 410 411 420 public String getUnresolvedType() { 421 return type; 422 } 423 424 434 public String getUnresolvedName() { 435 return name; 436 } 437 438 448 public String getUnresolvedActions() { 449 return actions; 450 } 451 452 462 public java.security.cert.Certificate [] getUnresolvedCerts() { 463 return (certs == null) ? null : 464 (java.security.cert.Certificate [])certs.clone(); 465 } 466 467 474 public String toString() { 475 return "(unresolved " + type + " " + name + " " + actions + ")"; 476 } 477 478 485 486 public PermissionCollection newPermissionCollection() { 487 return new UnresolvedPermissionCollection (); 488 } 489 490 506 private synchronized void writeObject(java.io.ObjectOutputStream oos) 507 throws IOException 508 { 509 oos.defaultWriteObject(); 510 511 if (certs==null || certs.length==0) { 512 oos.writeInt(0); 513 } else { 514 oos.writeInt(certs.length); 516 for (int i=0; i < certs.length; i++) { 518 java.security.cert.Certificate cert = certs[i]; 519 try { 520 oos.writeUTF(cert.getType()); 521 byte[] encoded = cert.getEncoded(); 522 oos.writeInt(encoded.length); 523 oos.write(encoded); 524 } catch (CertificateEncodingException cee) { 525 throw new IOException (cee.getMessage()); 526 } 527 } 528 } 529 } 530 531 534 private synchronized void readObject(java.io.ObjectInputStream ois) 535 throws IOException , ClassNotFoundException 536 { 537 CertificateFactory cf; 538 Hashtable cfs=null; 539 540 ois.defaultReadObject(); 541 542 if (type == null) 543 throw new NullPointerException ("type can't be null"); 544 545 int size = ois.readInt(); 547 if (size > 0) { 548 cfs = new Hashtable (3); 551 this.certs = new java.security.cert.Certificate [size]; 552 } 553 554 for (int i=0; i<size; i++) { 555 String certType = ois.readUTF(); 558 if (cfs.containsKey(certType)) { 559 cf = (CertificateFactory)cfs.get(certType); 561 } else { 562 try { 564 cf = CertificateFactory.getInstance(certType); 565 } catch (CertificateException ce) { 566 throw new ClassNotFoundException 567 ("Certificate factory for "+certType+" not found"); 568 } 569 cfs.put(certType, cf); 571 } 572 byte[] encoded=null; 574 try { 575 encoded = new byte[ois.readInt()]; 576 } catch (OutOfMemoryError oome) { 577 throw new IOException ("Certificate too big"); 578 } 579 ois.readFully(encoded); 580 ByteArrayInputStream bais = new ByteArrayInputStream (encoded); 581 try { 582 this.certs[i] = cf.generateCertificate(bais); 583 } catch (CertificateException ce) { 584 throw new IOException (ce.getMessage()); 585 } 586 bais.close(); 587 } 588 } 589 } 590 | Popular Tags |