1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
/* * @(#)CodeSource.java 1.41 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.security; import java.net.URL; import java.net.SocketPermission; import java.util.ArrayList; import java.util.List; import java.util.Hashtable; import java.io.ByteArrayInputStream; import java.io.IOException; import java.security.cert.*; /** * * <p>This class extends the concept of a codebase to * encapsulate not only the location (URL) but also the certificate chains * that were used to verify signed code originating from that location. * * @version 1.41, 11/17/05 * @author Li Gong * @author Roland Schemers */ public class CodeSource implements java.io.Serializable { private static final long serialVersionUID = 4977541819976013951L; /** * The code location. * * @serial */ private URL location; /* * The code signers. */ private transient CodeSigner[] signers = null; /* * The code signers. Certificate chains are concatenated. */ private transient java.security.cert.Certificate certs[] = null; // cached SocketPermission used for matchLocation private transient SocketPermission sp; // for generating cert paths private transient CertificateFactory factory = null; /** * Constructs a CodeSource and associates it with the specified * location and set of certificates. * * @param url the location (URL). * * @param certs the certificate(s). It may be null. The contents of the * array are copied to protect against subsequent modification. */ public CodeSource(URL url, java.security.cert.Certificate certs[]) { this.location = url; // Copy the supplied certs if (certs != null) { this.certs = (java.security.cert.Certificate[]) certs.clone(); } } /** * Constructs a CodeSource and associates it with the specified * location and set of code signers. * * @param url the location (URL). * @param signers the code signers. It may be null. The contents of the * array are copied to protect against subsequent modification. * * @since 1.5 */ public CodeSource(URL url, CodeSigner[] signers) { this.location = url; // Copy the supplied signers if (signers != null) { this.signers = (CodeSigner[])signers.clone(); } } /** * Returns the hash code value for this object. * * @return a hash code value for this object. */ public int hashCode() { if (location != null) return location.hashCode(); else return 0; } /** * Tests for equality between the specified object and this * object. Two CodeSource objects are considered equal if their * locations are of identical value and if their signer certificate * chains are of identical value. It is not required that * the certificate chains be in the same order. * * @param obj the object to test for equality with this object. * * @return true if the objects are considered equal, false otherwise. */ public boolean equals(Object obj) { if (obj == this) return true; // objects types must be equal if (!(obj instanceof CodeSource)) return false; CodeSource cs = (CodeSource) obj; // URLs must match if (location == null) { // if location is null, then cs.location must be null as well if (cs.location != null) return false; } else { // if location is not null, then it must equal cs.location if (!location.equals(cs.location)) return false; } // certs must match return matchCerts(cs, true); } /** * Returns the location associated with this CodeSource. * * @return the location (URL). */ public final URL getLocation() { /* since URL is practically immutable, returning itself is not a security problem */ return this.location; } /** * Returns the certificates associated with this CodeSource. * <p> * If this CodeSource object was created using the * {@link #CodeSource(URL url, CodeSigner[] signers)} * constructor then its certificate chains are extracted and used to * create an array of Certificate objects. Each signer certificate is * followed by its supporting certificate chain (which may be empty). * Each signer certificate and its supporting certificate chain is ordered * bottom-to-top (i.e., with the signer certificate first and the (root) * certificate authority last). * * @return A copy of the certificates array, or null if there is none. */ public final java.security.cert.Certificate[] getCertificates() { if (certs != null) { return (java.security.cert.Certificate[]) certs.clone(); } else if (signers != null) { // Convert the code signers to certs ArrayList certChains = new ArrayList(); for (int i = 0; i < signers.length; i++) { certChains.addAll( signers[i].getSignerCertPath().getCertificates()); } certs = (java.security.cert.Certificate[]) certChains.toArray( new java.security.cert.Certificate[certChains.size()]); return (java.security.cert.Certificate[]) certs.clone(); } else { return null; } } /** * Returns the code signers associated with this CodeSource. * <p> * If this CodeSource object was created using the * {@link #CodeSource(URL url, Certificate[] certs)} * constructor then its certificate chains are extracted and used to * create an array of CodeSigner objects. Note that only X.509 certificates * are examined - all other certificate types are ignored. * * @return A copy of the code signer array, or null if there is none. * * @since 1.5 */ public final CodeSigner[] getCodeSigners() { if (signers != null) { return (CodeSigner[]) signers.clone(); } else if (certs != null) { // Convert the certs to code signers signers = convertCertArrayToSignerArray(certs); return (CodeSigner[]) signers.clone(); } else { return null; } } /** * Returns true if this CodeSource object "implies" the specified CodeSource. * <P> * More specifically, this method makes the following checks, in order. * If any fail, it returns false. If they all succeed, it returns true.<p> * <ol> * <li> <i>codesource</i> must not be null. * <li> If this object's certificates are not null, then all * of this object's certificates must be present in <i>codesource</i>'s * certificates. * <li> If this object's location (getLocation()) is not null, then the * following checks are made against this object's location and * <i>codesource</i>'s:<p> * <ol> * <li> <i>codesource</i>'s location must not be null. * * <li> If this object's location * equals <i>codesource</i>'s location, then return true. * * <li> This object's protocol (getLocation().getProtocol()) must be * equal to <i>codesource</i>'s protocol. * * <li> If this object's host (getLocation().getHost()) is not null, * then the SocketPermission * constructed with this object's host must imply the * SocketPermission constructed with <i>codesource</i>'s host. * * <li> If this object's port (getLocation().getPort()) is not * equal to -1 (that is, if a port is specified), it must equal * <i>codesource</i>'s port. * * <li> If this object's file (getLocation().getFile()) doesn't equal * <i>codesource</i>'s file, then the following checks are made: * If this object's file ends with "/-", * then <i>codesource</i>'s file must start with this object's * file (exclusive the trailing "-"). * If this object's file ends with a "/*", * then <i>codesource</i>'s file must start with this object's * file and must not have any further "/" separators. * If this object's file doesn't end with a "/", * then <i>codesource</i>'s file must match this object's * file with a '/' appended. * * <li> If this object's reference (getLocation().getRef()) is * not null, it must equal <i>codesource</i>'s reference. * * </ol> * </ol> * <p> * For example, the codesource objects with the following locations * and null certificates all imply * the codesource with the location "http://java.sun.com/classes/foo.jar" * and null certificates: * <pre> * http: * http://*.sun.com/classes/* * http://java.sun.com/classes/- * http://java.sun.com/classes/foo.jar * </pre> * * Note that if this CodeSource has a null location and a null * certificate chain, then it implies every other CodeSource. * * @param codesource CodeSource to compare against. * * @return true if the specified codesource is implied by this codesource, * false if not. */ public boolean implies(CodeSource codesource) { if (codesource == null) return false; return matchCerts(codesource, false) && matchLocation(codesource); } /** * Returns true if all the certs in this * CodeSource are also in <i>that</i>. * * @param that the CodeSource to check against. * @param strict If true then a strict equality match is performed. * Otherwise a subset match is performed. */ private boolean matchCerts(CodeSource that, boolean strict) { boolean match; // match any key if (certs == null && signers == null) { if (strict) { return (that.certs == null && that.signers == null); } else { return true; } // both have signers } else if (signers != null && that.signers != null) { if (strict && signers.length != that.signers.length) { return false; } for (int i = 0; i < signers.length; i++) { match = false; for (int j = 0; j < that.signers.length; j++) { if (signers[i].equals(that.signers[j])) { match = true; break; } } if (!match) return false; } return true; // both have certs } else if (certs != null && that.certs != null) { if (strict && certs.length != that.certs.length) { return false; } for (int i = 0; i < certs.length; i++) { match = false; for (int j = 0; j < that.certs.length; j++) { if (certs[i].equals(that.certs[j])) { match = true; break; } } if (!match) return false; } return true; } return false; } /** * Returns true if two CodeSource's have the "same" location. * * @param that CodeSource to compare against */ private boolean matchLocation(CodeSource that) { if (location == null) { return true; } if ((that == null) || (that.location == null)) return false; if (location.equals(that.location)) return true; if (!location.getProtocol().equals(that.location.getProtocol())) return false; String thisHost = location.getHost(); String thatHost = that.location.getHost(); if (thisHost != null) { if (("".equals(thisHost) || "localhost".equals(thisHost)) && ("".equals(thatHost) || "localhost".equals(thatHost))) { // ok } else if (!thisHost.equals(thatHost)) { if (thatHost == null) { return false; } if (this.sp == null) { this.sp = new SocketPermission(thisHost, "resolve"); } if (that.sp == null) { that.sp = new SocketPermission(thatHost, "resolve"); } if (!this.sp.implies(that.sp)) { return false; } } } if (location.getPort() != -1) { if (location.getPort() != that.location.getPort()) return false; } if (location.getFile().endsWith("/-")) { // Matches the directory and (recursively) all files // and subdirectories contained in that directory. // For example, "/a/b/-" implies anything that starts with // "/a/b/" String thisPath = location.getFile().substring(0, location.getFile().length()-1); if (!that.location.getFile().startsWith(thisPath)) return false; } else if (location.getFile().endsWith("__JOT_PIECE_19__ private CodeSigner[] convertCertArrayToSignerArray( java.security.cert.Certificate[] certs) { if (certs == null) { return null; } try { // Initialize certificate factory if (factory == null) { factory = CertificateFactory.getInstance("X.509"); } // Iterate through all the certificates int i = 0; List signers = new ArrayList(); while (i < certs.length) { List certChain = new ArrayList(); certChain.add(certs[i++]); // first cert is an end-entity cert int j = i; // Extract chain of certificates // (loop while certs are not end-entity certs) while (j < certs.length && certs[j] instanceof X509Certificate && ((X509Certificate)certs[j]).getBasicConstraints() != -1) { certChain.add(certs[j]); j++; } i = j; CertPath certPath = factory.generateCertPath(certChain); signers.add(new CodeSigner(certPath, null)); } if (signers.isEmpty()) { return null; } else { return (CodeSigner[]) signers.toArray(new CodeSigner[signers.size()]); } } catch (CertificateException e) { return null; //TODO - may be better to throw an ex. here } } }