codesource.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 504 行 · 第 1/2 页

JAVA
504
字号
/* * @(#)CodeSource.java	1.12 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */ package java.security;import java.net.URL;import java.net.SocketPermission;import java.util.Hashtable;import java.io.ByteArrayInputStream;import java.io.IOException;import java.security.cert.*;/* J2ME CDC subsetted class *//** * * <p>This class extends the concept of a codebase to * encapsulate not only the location (URL) but also the certificate(s) * that were used to verify signed code originating from that * location. * * @version 	1.32, 01/23/03 * @author Li Gong * @author Roland Schemers */public class CodeSource implements java.io.Serializable {    /**     * The code location.     *     * @serial     */    private URL location;    // certificates    private transient java.security.cert.Certificate certs[];    // cached SocketPermission used for matchLocation    private transient SocketPermission sp;    /**     * Constructs a CodeSource and associates it with the specified      * location and set of certificates.     *      * @param url the location (URL).     *      * @param certs the certificate(s).     */    public CodeSource(URL url, java.security.cert.Certificate certs[]) {	this.location = url;	if (certs != null) 	    this.certs = (java.security.cert.Certificate[]) certs.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 the two sets of      * certificates are of identical values. It is not required that     * the certificates 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	if (certs == null) {	    // if certs is null, then cs.certs must be null as well	    if (cs.certs != null) return false;	} else {	    // if certs is not null, then it must equal cs.certs	    // equality means that both arrays of certs are the same set	    // step 1 -- every cert in certs[] must match one in cs.certs[]	    if (cs.certs == null) 		return false;	    boolean match;	    for (int i = 0; i < certs.length; i++) {		match = false;		for (int j = 0; j < cs.certs.length; j++) {		    if (certs[i].equals(cs.certs[j])) {			match = true;			break;		    }		}		if (!match) return false;	    }	    // step 2 -- every key in cs.certs[] must match one in certs[]	    for (int i = 0; i < cs.certs.length; i++) {		match = false;		for (int j = 0; j < certs.length; j++) {		    if (cs.certs[i].equals(certs[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 certificates associated with this CodeSource.     * <p>     * ***NOTE: In J2ME CDC, there is no support for certificates by design.       * If CDC by itself or a J2ME profile built on CDC does not add back the      * java.security.Signature class (plus all its dependencies),      * a <code>null</code> value must always be returned.     *      * @return the certificates     */    public final java.security.cert.Certificate[] getCertificates() {	/* return a clone copy, to avoid malicious modification to the	   original object */	if (this.certs != null) {	    return (java.security.cert.Certificate[])this.certs.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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?