1. /*
  2. * @(#)Timestamp.java 1.2 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.io.Serializable;
  9. import java.security.cert.CertPath;
  10. import java.security.cert.X509Extension;
  11. import java.util.Date;
  12. /**
  13. * This class encapsulates information about a signed timestamp.
  14. * It is immutable.
  15. * It includes the timestamp's date and time as well as information about the
  16. * Timestamping Authority (TSA) which generated and signed the timestamp.
  17. *
  18. * @since 1.5
  19. * @version 1.2, 12/19/03
  20. * @author Vincent Ryan
  21. */
  22. public final class Timestamp implements Serializable {
  23. private static final long serialVersionUID = -5502683707821851294L;
  24. /**
  25. * The timestamp's date and time
  26. *
  27. * @serial
  28. */
  29. private Date timestamp;
  30. /**
  31. * The TSA's certificate path.
  32. *
  33. * @serial
  34. */
  35. private CertPath signerCertPath;
  36. /*
  37. * Hash code for this timestamp.
  38. */
  39. private transient int myhash = -1;
  40. /**
  41. * Constructs a Timestamp.
  42. *
  43. * @param timestamp is the timestamp's date and time. It must not be null.
  44. * @param signerCertPath is the TSA's certificate path. It must not be null.
  45. * @throws NullPointerException if timestamp or signerCertPath is null.
  46. */
  47. public Timestamp(Date timestamp, CertPath signerCertPath) {
  48. if (timestamp == null || signerCertPath == null) {
  49. throw new NullPointerException();
  50. }
  51. this.timestamp = new Date(timestamp.getTime()); // clone
  52. this.signerCertPath = signerCertPath;
  53. }
  54. /**
  55. * Returns the date and time when the timestamp was generated.
  56. *
  57. * @return The timestamp's date and time.
  58. */
  59. public Date getTimestamp() {
  60. return new Date(timestamp.getTime()); // clone
  61. }
  62. /**
  63. * Returns the certificate path for the Timestamping Authority.
  64. *
  65. * @return The TSA's certificate path.
  66. */
  67. public CertPath getSignerCertPath() {
  68. return signerCertPath;
  69. }
  70. /**
  71. * Returns the hash code value for this timestamp.
  72. * The hash code is generated using the date and time of the timestamp
  73. * and the TSA's certificate path.
  74. *
  75. * @return a hash code value for this timestamp.
  76. */
  77. public int hashCode() {
  78. if (myhash == -1) {
  79. myhash = timestamp.hashCode() + signerCertPath.hashCode();
  80. }
  81. return myhash;
  82. }
  83. /**
  84. * Tests for equality between the specified object and this
  85. * timestamp. Two timestamps are considered equal if the date and time of
  86. * their timestamp's and their signer's certificate paths are equal.
  87. *
  88. * @param obj the object to test for equality with this timestamp.
  89. *
  90. * @return true if the timestamp are considered equal, false otherwise.
  91. */
  92. public boolean equals(Object obj) {
  93. if (obj == null || (!(obj instanceof Timestamp))) {
  94. return false;
  95. }
  96. Timestamp that = (Timestamp)obj;
  97. if (this == that) {
  98. return true;
  99. }
  100. return (timestamp.equals(that.getTimestamp()) &&
  101. signerCertPath.equals(that.getSignerCertPath()));
  102. }
  103. /**
  104. * Returns a string describing this timestamp.
  105. *
  106. * @return A string comprising the date and time of the timestamp and
  107. * its signer's certificate.
  108. */
  109. public String toString() {
  110. StringBuffer sb = new StringBuffer();
  111. sb.append("(");
  112. sb.append("timestamp: " + timestamp);
  113. sb.append("TSA: " + signerCertPath.getCertificates().get(0));
  114. sb.append(")");
  115. return sb.toString();
  116. }
  117. }