Signer.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Signer.java --- Signer Class
  2. Copyright (C) 1999, 2003, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. /**
  33. * <code>Signer</code> is a subclass of {@link Identity}. It is used to store a
  34. * digital signature key with an <i>Identity</i>.
  35. *
  36. * @author Mark Benvenuto (ivymccough@worldnet.att.net)
  37. * @deprecated Replaced by <code>java.security.KeyStore</code>, the
  38. * <code>java.security.cert</code> package, and <code>java.security.Principal</code>.
  39. */
  40. public abstract class Signer extends Identity
  41. {
  42. private static final long serialVersionUID = -1763464102261361480L;
  43. private PrivateKey privateKey = null;
  44. /** Trivial constructor for serialization purposes. */
  45. protected Signer()
  46. {
  47. }
  48. /**
  49. * Constructs a new instance of <code>Signer</code> with the specified
  50. * identity name.
  51. *
  52. * @param name
  53. * the name of the identity to use.
  54. */
  55. public Signer(String name)
  56. {
  57. super(name);
  58. }
  59. /**
  60. * Constructs a new instance of <code>Signer</code> with the specified
  61. * identity name and {@link IdentityScope}.
  62. *
  63. * @param name
  64. * the name of the the identity to use.
  65. * @param scope
  66. * the {@link IdentityScope} to use.
  67. * @throws KeyManagementException
  68. * if a duplicate identity <code>name</code> exists within
  69. * <code>scope</code>.
  70. */
  71. public Signer(String name, IdentityScope scope) throws KeyManagementException
  72. {
  73. super(name, scope);
  74. }
  75. /**
  76. * Returns the private key of this <code>Signer</code>.
  77. *
  78. * @returns the private key of this <code>Signer</code>.
  79. * @throws SecurityException
  80. * if a {@link SecurityManager} is installed which disallows this
  81. * operation.
  82. */
  83. public PrivateKey getPrivateKey()
  84. {
  85. SecurityManager sm = System.getSecurityManager();
  86. if (sm != null)
  87. sm.checkSecurityAccess("getSignerPrivateKey");
  88. return privateKey;
  89. }
  90. /**
  91. * Specifies the {@link KeyPair} associated with this <code>Signer</code>.
  92. *
  93. * @param pair
  94. * the {@link KeyPair} to use.
  95. * @throws InvalidParameterException
  96. * if the key-pair is invalid.
  97. * @throws KeyException
  98. * if any another key-related error occurs.
  99. * @throws SecurityException
  100. * if a {@link SecurityManager} is installed which disallows this
  101. * operation.
  102. */
  103. public final void setKeyPair(KeyPair pair)
  104. throws InvalidParameterException, KeyException
  105. {
  106. SecurityManager sm = System.getSecurityManager();
  107. if (sm != null)
  108. sm.checkSecurityAccess("setSignerKeyPair");
  109. try
  110. {
  111. if (pair.getPublic() != null)
  112. setPublicKey(pair.getPublic());
  113. else
  114. throw new InvalidParameterException();
  115. }
  116. catch (KeyManagementException kme)
  117. {
  118. throw new KeyException();
  119. }
  120. if (pair.getPrivate() != null)
  121. privateKey = pair.getPrivate();
  122. else
  123. throw new InvalidParameterException();
  124. }
  125. /** @returns a string representing this <code>Signer</code>. */
  126. public String toString()
  127. {
  128. return (getName() + ": " + privateKey);
  129. }
  130. }