|
1 /* |
|
2 * Copyright 2003 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package java.security; |
|
27 |
|
28 import java.io.Serializable; |
|
29 import java.security.cert.CertPath; |
|
30 import java.security.cert.X509Extension; |
|
31 import java.util.Date; |
|
32 |
|
33 /** |
|
34 * This class encapsulates information about a signed timestamp. |
|
35 * It is immutable. |
|
36 * It includes the timestamp's date and time as well as information about the |
|
37 * Timestamping Authority (TSA) which generated and signed the timestamp. |
|
38 * |
|
39 * @since 1.5 |
|
40 * @author Vincent Ryan |
|
41 */ |
|
42 |
|
43 public final class Timestamp implements Serializable { |
|
44 |
|
45 private static final long serialVersionUID = -5502683707821851294L; |
|
46 |
|
47 /** |
|
48 * The timestamp's date and time |
|
49 * |
|
50 * @serial |
|
51 */ |
|
52 private Date timestamp; |
|
53 |
|
54 /** |
|
55 * The TSA's certificate path. |
|
56 * |
|
57 * @serial |
|
58 */ |
|
59 private CertPath signerCertPath; |
|
60 |
|
61 /* |
|
62 * Hash code for this timestamp. |
|
63 */ |
|
64 private transient int myhash = -1; |
|
65 |
|
66 /** |
|
67 * Constructs a Timestamp. |
|
68 * |
|
69 * @param timestamp is the timestamp's date and time. It must not be null. |
|
70 * @param signerCertPath is the TSA's certificate path. It must not be null. |
|
71 * @throws NullPointerException if timestamp or signerCertPath is null. |
|
72 */ |
|
73 public Timestamp(Date timestamp, CertPath signerCertPath) { |
|
74 if (timestamp == null || signerCertPath == null) { |
|
75 throw new NullPointerException(); |
|
76 } |
|
77 this.timestamp = new Date(timestamp.getTime()); // clone |
|
78 this.signerCertPath = signerCertPath; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Returns the date and time when the timestamp was generated. |
|
83 * |
|
84 * @return The timestamp's date and time. |
|
85 */ |
|
86 public Date getTimestamp() { |
|
87 return new Date(timestamp.getTime()); // clone |
|
88 } |
|
89 |
|
90 /** |
|
91 * Returns the certificate path for the Timestamping Authority. |
|
92 * |
|
93 * @return The TSA's certificate path. |
|
94 */ |
|
95 public CertPath getSignerCertPath() { |
|
96 return signerCertPath; |
|
97 } |
|
98 |
|
99 /** |
|
100 * Returns the hash code value for this timestamp. |
|
101 * The hash code is generated using the date and time of the timestamp |
|
102 * and the TSA's certificate path. |
|
103 * |
|
104 * @return a hash code value for this timestamp. |
|
105 */ |
|
106 public int hashCode() { |
|
107 if (myhash == -1) { |
|
108 myhash = timestamp.hashCode() + signerCertPath.hashCode(); |
|
109 } |
|
110 return myhash; |
|
111 } |
|
112 |
|
113 /** |
|
114 * Tests for equality between the specified object and this |
|
115 * timestamp. Two timestamps are considered equal if the date and time of |
|
116 * their timestamp's and their signer's certificate paths are equal. |
|
117 * |
|
118 * @param obj the object to test for equality with this timestamp. |
|
119 * |
|
120 * @return true if the timestamp are considered equal, false otherwise. |
|
121 */ |
|
122 public boolean equals(Object obj) { |
|
123 if (obj == null || (!(obj instanceof Timestamp))) { |
|
124 return false; |
|
125 } |
|
126 Timestamp that = (Timestamp)obj; |
|
127 |
|
128 if (this == that) { |
|
129 return true; |
|
130 } |
|
131 return (timestamp.equals(that.getTimestamp()) && |
|
132 signerCertPath.equals(that.getSignerCertPath())); |
|
133 } |
|
134 |
|
135 /** |
|
136 * Returns a string describing this timestamp. |
|
137 * |
|
138 * @return A string comprising the date and time of the timestamp and |
|
139 * its signer's certificate. |
|
140 */ |
|
141 public String toString() { |
|
142 StringBuffer sb = new StringBuffer(); |
|
143 sb.append("("); |
|
144 sb.append("timestamp: " + timestamp); |
|
145 sb.append("TSA: " + signerCertPath.getCertificates().get(0)); |
|
146 sb.append(")"); |
|
147 return sb.toString(); |
|
148 } |
|
149 } |