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