home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 6.9 KB | 263 lines |
- /*
- * @(#)CodeSource.java 1.17 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.security;
-
-
- import java.net.URL;
- import java.net.SocketPermission;
-
- /**
- *
- * <p>This class extends the concept of a codebase to
- * encapsulate not only the location (URL) but also the public key(s)
- * that should be used to verify signed code originating from that
- * location.
- *
- * <p>Two CodeSource objects are considered equal if their locations
- * are of identical value and if the two sets of public keys are of
- * identical values.
- *
- * @version 1.17, 03/18/98
- * @author Li Gong */
-
- public class CodeSource implements java.io.Serializable {
-
- /** use serialVersionUID from JDK 1.2 for interoperability */
- private static final long serialVersionUID = 63658367122772714L;
-
- /* when multiple locations and/or keys are associated with a
- CodeSource in the future, the following two fields can be
- replaced with arrays. */
-
- // code location.
- private URL location;
-
- // public key.
- private PublicKey pubkey[];
-
- // cached SocketPermission used for matchLocation
- private transient SocketPermission sp;
-
- /**
- * Constructs a CodeSource and associates it with the specified
- * location and set of public keys.
- *
- * @param url the location (URL).
- *
- * @param key the public key(s).
- */
- public CodeSource(URL url, PublicKey key[]) {
- this.location = url;
- this.pubkey = key;
- }
-
- /**
- * 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 the two sets of
- * public keys are of identical values. It is not required that
- * the keys 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;
- }
-
- // Public keys must match
- if (pubkey == null) {
- // if pubkey is null, then cs.pubkey must be null as well
- if (cs.pubkey != null) return false;
- } else {
- // if pubkey is not null, then it must equal cs.pubkey
- // equality means that both arrays of keys are the same set
- // step 1 -- every key in pubkey[] must match one in cs.pubkey[]
- if (cs.pubkey == null)
- return false;
-
- boolean match;
- for (int i = 0; i < pubkey.length; i++) {
- match = false;
- for (int j = 0; j < cs.pubkey.length; j++) {
- if (pubkey[i].equals(cs.pubkey[j])) {
- match = true;
- break;
- }
- }
- if (!match) return false;
- }
- // step 2 -- every key in cs.pubkey[] must match one in pubkey[]
- for (int i = 0; i < cs.pubkey.length; i++) {
- match = false;
- for (int j = 0; j < pubkey.length; j++) {
- if (cs.pubkey[i].equals(pubkey[j])) {
- match = true;
- break;
- }
- }
- if (!match) return false;
- }
- }
-
- // they must be equal if we got here...
- return 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 public keys associated with this CodeSource.
- *
- * @return the public keys.
- */
- public final PublicKey[] getKeys() {
- /* return a clone copy, to avoid malicious modification to the
- original object */
- if (this.pubkey != null) {
- return (PublicKey[])this.pubkey.clone();
- } else {
- return null;
- }
- }
-
- /**
- * Returns true if two CodeSource's have the "same" location.
- *
- * @param that CodeSource to compare against
- */
- boolean matchLocation(CodeSource that)
- {
- if (location == null) {
- return ((that == null) || (that.location == null));
- }
-
- if ((that == null) || (that.location == null))
- return false;
-
- if (location.equals(that.location))
- return true;
-
- if (!location.getProtocol().equals(that.location.getProtocol()))
- return false;
-
- if ((location.getHost() != null)) {
- if ((location.getHost().equals("") ||
- location.getHost().equals("localhost")) &&
- (that.location.getHost().equals("") ||
- that.location.getHost().equals("localhost"))) {
- // ok
- } else if (!location.getHost().equals(
- that.location.getHost())) {
- if (this.sp == null) {
- this.sp =
- new SocketPermission(location.getHost(),"resolve");
- }
- if (that.sp == null) {
- if (that.location.getHost() == null ||
- that.location.getHost().equals(""))
- return false;
- that.sp =
- new SocketPermission(that.location.getHost(),"resolve");
- }
-
- boolean ok = this.sp.implies(that.sp);
- if (!ok)
- return false;
- }
- }
-
- if (location.getPort() != -1) {
- if (location.getPort() != that.location.getPort())
- return false;
- }
-
- if (location.getFile().endsWith("/")) {
- if (!that.location.getFile().startsWith(location.getFile()))
- return false;
- } else {
- if ((!location.getFile().equals(that.location.getFile())) &&
- (!that.location.getFile().startsWith(location.getFile()+"/"))) {
- return false;
- }
- }
-
- if (location.getRef() == null)
- return true;
- else
- return location.getRef().equals(that.location.getRef());
- }
-
- /**
- * Returns a string describing this CodeSource, telling its
- * URL and public keys.
- *
- * @return information about this CodeSource.
- */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("(");
- sb.append(this.location);
- if (this.pubkey != null && this.pubkey.length > 0) {
- for (int i=0; i < this.pubkey.length; i++) {
- sb.append( " "+this.pubkey[i]);
- }
- } else {
- sb.append(" <no public keys>");
- }
- sb.append(")");
- return sb.toString();
- }
- }
-
-