1. /*
  2. * @(#)CodeSigner.java 1.4 04/04/26
  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. /**
  11. * This class encapsulates information about a code signer.
  12. * It is immutable.
  13. *
  14. * @since 1.5
  15. * @version 1.4, 04/26/04
  16. * @author Vincent Ryan
  17. */
  18. public final class CodeSigner implements Serializable {
  19. private static final long serialVersionUID = 6819288105193937581L;
  20. /**
  21. * The signer's certificate path.
  22. *
  23. * @serial
  24. */
  25. private CertPath signerCertPath;
  26. /*
  27. * The signature timestamp.
  28. *
  29. * @serial
  30. */
  31. private Timestamp timestamp;
  32. /*
  33. * Hash code for this code signer.
  34. */
  35. private transient int myhash = -1;
  36. /**
  37. * Constructs a CodeSigner object.
  38. *
  39. * @param signerCertPath The signer's certificate path.
  40. * It must not be <code>null</code>.
  41. * @param timestamp A signature timestamp.
  42. * If <code>null</code> then no timestamp was generated
  43. * for the signature.
  44. * @throws NullPointerException if <code>signerCertPath</code> is
  45. * <code>null</code>.
  46. */
  47. public CodeSigner(CertPath signerCertPath, Timestamp timestamp) {
  48. if (signerCertPath == null) {
  49. throw new NullPointerException();
  50. }
  51. this.signerCertPath = signerCertPath;
  52. this.timestamp = timestamp;
  53. }
  54. /**
  55. * Returns the signer's certificate path.
  56. *
  57. * @return A certificate path.
  58. */
  59. public CertPath getSignerCertPath() {
  60. return signerCertPath;
  61. }
  62. /**
  63. * Returns the signature timestamp.
  64. *
  65. * @return The timestamp or <code>null</code> if none is present.
  66. */
  67. public Timestamp getTimestamp() {
  68. return timestamp;
  69. }
  70. /**
  71. * Returns the hash code value for this code signer.
  72. * The hash code is generated using the signer's certificate path and the
  73. * timestamp, if present.
  74. *
  75. * @return a hash code value for this code signer.
  76. */
  77. public int hashCode() {
  78. if (myhash == -1) {
  79. if (timestamp == null) {
  80. myhash = signerCertPath.hashCode();
  81. } else {
  82. myhash = signerCertPath.hashCode() + timestamp.hashCode();
  83. }
  84. }
  85. return myhash;
  86. }
  87. /**
  88. * Tests for equality between the specified object and this
  89. * code signer. Two code signers are considered equal if their
  90. * signer certificate paths are equal and if their timestamps are equal,
  91. * if present in both.
  92. *
  93. * @param obj the object to test for equality with this object.
  94. *
  95. * @return true if the objects are considered equal, false otherwise.
  96. */
  97. public boolean equals(Object obj) {
  98. if (obj == null || (!(obj instanceof CodeSigner))) {
  99. return false;
  100. }
  101. CodeSigner that = (CodeSigner)obj;
  102. if (this == that) {
  103. return true;
  104. }
  105. Timestamp thatTimestamp = that.getTimestamp();
  106. if (timestamp == null) {
  107. if (thatTimestamp != null) {
  108. return false;
  109. }
  110. } else {
  111. if (thatTimestamp == null ||
  112. (! timestamp.equals(thatTimestamp))) {
  113. return false;
  114. }
  115. }
  116. return signerCertPath.equals(that.getSignerCertPath());
  117. }
  118. /**
  119. * Returns a string describing this code signer.
  120. *
  121. * @return A string comprising the signer's certificate and a timestamp,
  122. * if present.
  123. */
  124. public String toString() {
  125. StringBuffer sb = new StringBuffer();
  126. sb.append("(");
  127. sb.append("Signer: " + signerCertPath.getCertificates().get(0));
  128. if (timestamp != null) {
  129. sb.append("timestamp: " + timestamp);
  130. }
  131. sb.append(")");
  132. return sb.toString();
  133. }
  134. }