1. /*
  2. * @(#)Signer.java 1.37 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.security;
  11. import java.io.*;
  12. /**
  13. * This class is used to represent an Identity that can also digitally
  14. * sign data.
  15. *
  16. * <p>The management of a signer's private keys is an important and
  17. * sensitive issue that should be handled by subclasses as appropriate
  18. * to their intended use.
  19. *
  20. * @see Identity
  21. *
  22. * @version 1.37 00/02/02
  23. * @author Benjamin Renaud
  24. *
  25. * @deprecated This class is no longer used. Its functionality has been
  26. * replaced by <code>java.security.KeyStore</code>, the
  27. * <code>java.security.cert</code> package, and
  28. * <code>java.security.Principal</code>.
  29. */
  30. public abstract class Signer extends Identity {
  31. /**
  32. * The signer's private key.
  33. *
  34. * @serial
  35. */
  36. private PrivateKey privateKey;
  37. /**
  38. * Creates a signer. This constructor should only be used for
  39. * serialization.
  40. */
  41. protected Signer() {
  42. super();
  43. }
  44. /**
  45. * Creates a signer with the specified identity name.
  46. *
  47. * @param name the identity name.
  48. */
  49. public Signer(String name) {
  50. super(name);
  51. }
  52. /**
  53. * Creates a signer with the specified identity name and scope.
  54. *
  55. * @param name the identity name.
  56. *
  57. * @param scope the scope of the identity.
  58. *
  59. * @exception KeyManagementException if there is already an identity
  60. * with the same name in the scope.
  61. */
  62. public Signer(String name, IdentityScope scope)
  63. throws KeyManagementException {
  64. super(name, scope);
  65. }
  66. /**
  67. * Returns this signer's private key.
  68. *
  69. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  70. * method is called with <code>"getSignerPrivateKey"</code>
  71. * as its argument to see if it's ok to return the private key.
  72. *
  73. * @return this signer's private key, or null if the private key has
  74. * not yet been set.
  75. *
  76. * @exception SecurityException if a security manager exists and its
  77. * <code>checkSecurityAccess</code> method doesn't allow
  78. * returning the private key.
  79. *
  80. * @see SecurityManager#checkSecurityAccess
  81. */
  82. public PrivateKey getPrivateKey() {
  83. check("getSignerPrivateKey");
  84. return privateKey;
  85. }
  86. /**
  87. * Sets the key pair (public key and private key) for this signer.
  88. *
  89. * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
  90. * method is called with <code>"setSignerKeyPair"</code>
  91. * as its argument to see if it's ok to set the key pair.
  92. *
  93. * @param pair an initialized key pair.
  94. *
  95. * @exception InvalidParameterException if the key pair is not
  96. * properly initialized.
  97. * @exception KeyException if the key pair cannot be set for any
  98. * other reason.
  99. * @exception SecurityException if a security manager exists and its
  100. * <code>checkSecurityAccess</code> method doesn't allow
  101. * setting the key pair.
  102. *
  103. * @see SecurityManager#checkSecurityAccess
  104. */
  105. public final void setKeyPair(KeyPair pair)
  106. throws InvalidParameterException, KeyException {
  107. check("setSignerKeyPair");
  108. final PublicKey pub = pair.getPublic();
  109. PrivateKey priv = pair.getPrivate();
  110. if (pub == null || priv == null) {
  111. throw new InvalidParameterException();
  112. }
  113. try {
  114. AccessController.doPrivileged(new PrivilegedExceptionAction() {
  115. public Object run() throws KeyManagementException {
  116. setPublicKey(pub);
  117. return null;
  118. }
  119. });
  120. } catch (PrivilegedActionException pae) {
  121. throw (KeyManagementException) pae.getException();
  122. }
  123. privateKey = priv;
  124. }
  125. String printKeys() {
  126. String keys = "";
  127. PublicKey publicKey = getPublicKey();
  128. if (publicKey != null && privateKey != null) {
  129. keys = "\tpublic and private keys initialized";
  130. } else {
  131. keys = "\tno keys";
  132. }
  133. return keys;
  134. }
  135. /**
  136. * Returns a string of information about the signer.
  137. *
  138. * @return a string of information about the signer.
  139. */
  140. public String toString() {
  141. return "[Signer]" + super.toString();
  142. }
  143. private static void check(String directive) {
  144. SecurityManager security = System.getSecurityManager();
  145. if (security != null) {
  146. security.checkSecurityAccess(directive);
  147. }
  148. }
  149. }