1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.security;
27
28 import java.io.*;
29 import java.security.cert.CertPath;
30
31 /**
32 * This class encapsulates information about a code signer.
33 * It is immutable.
34 *
35 * @since 1.5
36 * @author Vincent Ryan
37 */
38
39 public final class CodeSigner implements Serializable {
40
41 private static final long serialVersionUID = 6819288105193937581L;
42
43 /**
44 * The signer's certificate path.
45 *
46 * @serial
47 */
48 private CertPath signerCertPath;
49
50 /*
51 * The signature timestamp.
52 *
53 * @serial
54 */
55 private Timestamp timestamp;
56
57 /*
58 * Hash code for this code signer.
59 */
60 private transient int myhash = -1;
61
62 /**
63 * Constructs a CodeSigner object.
64 *
65 * @param signerCertPath The signer's certificate path.
66 * It must not be {@code null}.
67 * @param timestamp A signature timestamp.
68 * If {@code null} then no timestamp was generated
69 * for the signature.
70 * @throws NullPointerException if {@code signerCertPath} is
71 * {@code null}.
72 */
73 public CodeSigner(CertPath signerCertPath, Timestamp timestamp) {
74 if (signerCertPath == null) {
75 throw new NullPointerException();
76 }
77 this.signerCertPath = signerCertPath;
78 this.timestamp = timestamp;
79 }
80
81 /**
82 * Returns the signer's certificate path.
83 *
84 * @return A certificate path.
85 */
86 public CertPath getSignerCertPath() {
87 return signerCertPath;
88 }
89
90 /**
91 * Returns the signature timestamp.
92 *
93 * @return The timestamp or {@code null} if none is present.
94 */
95 public Timestamp getTimestamp() {
96 return timestamp;
97 }
98
99 /**
100 * Returns the hash code value for this code signer.
101 * The hash code is generated using the signer's certificate path and the
102 * timestamp, if present.
103 *
104 * @return a hash code value for this code signer.
105 */
106 public int hashCode() {
107 if (myhash == -1) {
108 if (timestamp == null) {
109 myhash = signerCertPath.hashCode();
110 } else {
111 myhash = signerCertPath.hashCode() + timestamp.hashCode();
112 }
113 }
114 return myhash;
115 }
116
117 /**
118 * Tests for equality between the specified object and this
119 * code signer. Two code signers are considered equal if their
120 * signer certificate paths are equal and if their timestamps are equal,
121 * if present in both.
122 *
123 * @param obj the object to test for equality with this object.
124 *
125 * @return true if the objects are considered equal, false otherwise.
126 */
127 public boolean equals(Object obj) {
128 if (obj == null || (!(obj instanceof CodeSigner))) {
129 return false;
130 }
131 CodeSigner that = (CodeSigner)obj;
132
133 if (this == that) {
134 return true;
135 }
136 Timestamp thatTimestamp = that.getTimestamp();
137 if (timestamp == null) {
138 if (thatTimestamp != null) {
139 return false;
140 }
141 } else {
142 if (thatTimestamp == null ||
143 (! timestamp.equals(thatTimestamp))) {
144 return false;
145 }
146 }
147 return signerCertPath.equals(that.getSignerCertPath());
148 }
149
150 /**
151 * Returns a string describing this code signer.
152 *
153 * @return A string comprising the signer's certificate and a timestamp,
154 * if present.
155 */
156 public String toString() {
157 StringBuilder sb = new StringBuilder();
158 sb.append("(");
159 sb.append("Signer: " + signerCertPath.getCertificates().get(0));
160 if (timestamp != null) {
161 sb.append("timestamp: " + timestamp);
162 }
163 sb.append(")");
164 return sb.toString();
165 }
166
167 // Explicitly reset hash code value to -1
168 private void readObject(ObjectInputStream ois)
169 throws IOException, ClassNotFoundException {
170 ois.defaultReadObject();
171 myhash = -1;
172 }
173 }
174