001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package java.security;
027:
028: import java.io.Serializable;
029: import java.security.cert.CertPath;
030:
031: /**
032: * This class encapsulates information about a code signer.
033: * It is immutable.
034: *
035: * @since 1.5
036: * @version 1.11, 05/05/07
037: * @author Vincent Ryan
038: */
039:
040: public final class CodeSigner implements Serializable {
041:
042: private static final long serialVersionUID = 6819288105193937581L;
043:
044: /**
045: * The signer's certificate path.
046: *
047: * @serial
048: */
049: private CertPath signerCertPath;
050:
051: /*
052: * The signature timestamp.
053: *
054: * @serial
055: */
056: private Timestamp timestamp;
057:
058: /*
059: * Hash code for this code signer.
060: */
061: private transient int myhash = -1;
062:
063: /**
064: * Constructs a CodeSigner object.
065: *
066: * @param signerCertPath The signer's certificate path.
067: * It must not be <code>null</code>.
068: * @param timestamp A signature timestamp.
069: * If <code>null</code> then no timestamp was generated
070: * for the signature.
071: * @throws NullPointerException if <code>signerCertPath</code> is
072: * <code>null</code>.
073: */
074: public CodeSigner(CertPath signerCertPath, Timestamp timestamp) {
075: if (signerCertPath == null) {
076: throw new NullPointerException();
077: }
078: this .signerCertPath = signerCertPath;
079: this .timestamp = timestamp;
080: }
081:
082: /**
083: * Returns the signer's certificate path.
084: *
085: * @return A certificate path.
086: */
087: public CertPath getSignerCertPath() {
088: return signerCertPath;
089: }
090:
091: /**
092: * Returns the signature timestamp.
093: *
094: * @return The timestamp or <code>null</code> if none is present.
095: */
096: public Timestamp getTimestamp() {
097: return timestamp;
098: }
099:
100: /**
101: * Returns the hash code value for this code signer.
102: * The hash code is generated using the signer's certificate path and the
103: * timestamp, if present.
104: *
105: * @return a hash code value for this code signer.
106: */
107: public int hashCode() {
108: if (myhash == -1) {
109: if (timestamp == null) {
110: myhash = signerCertPath.hashCode();
111: } else {
112: myhash = signerCertPath.hashCode()
113: + timestamp.hashCode();
114: }
115: }
116: return myhash;
117: }
118:
119: /**
120: * Tests for equality between the specified object and this
121: * code signer. Two code signers are considered equal if their
122: * signer certificate paths are equal and if their timestamps are equal,
123: * if present in both.
124: *
125: * @param obj the object to test for equality with this object.
126: *
127: * @return true if the objects are considered equal, false otherwise.
128: */
129: public boolean equals(Object obj) {
130: if (obj == null || (!(obj instanceof CodeSigner))) {
131: return false;
132: }
133: CodeSigner that = (CodeSigner) obj;
134:
135: if (this == that) {
136: return true;
137: }
138: Timestamp thatTimestamp = that.getTimestamp();
139: if (timestamp == null) {
140: if (thatTimestamp != null) {
141: return false;
142: }
143: } else {
144: if (thatTimestamp == null
145: || (!timestamp.equals(thatTimestamp))) {
146: return false;
147: }
148: }
149: return signerCertPath.equals(that.getSignerCertPath());
150: }
151:
152: /**
153: * Returns a string describing this code signer.
154: *
155: * @return A string comprising the signer's certificate and a timestamp,
156: * if present.
157: */
158: public String toString() {
159: StringBuffer sb = new StringBuffer();
160: sb.append("(");
161: sb.append("Signer: " + signerCertPath.getCertificates().get(0));
162: if (timestamp != null) {
163: sb.append("timestamp: " + timestamp);
164: }
165: sb.append(")");
166: return sb.toString();
167: }
168: }
|