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