SignatureSpi.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* SignatureSpi.java --- Signature Service Provider Interface
  2. Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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. import java.security.spec.AlgorithmParameterSpec;
  33. /**
  34. SignatureSpi defines the Service Provider Interface (SPI)
  35. for the Signature class. The signature class provides an
  36. interface to a digital signature algorithm. Digital signatures
  37. are used for authentication and integrity of data.
  38. @author Mark Benvenuto <ivymccough@worldnet.att.net>
  39. @since JDK 1.2
  40. */
  41. public abstract class SignatureSpi
  42. {
  43. /**
  44. Source of randomness
  45. */
  46. protected SecureRandom appRandom;
  47. /**
  48. Creates a new instance of SignatureSpi.
  49. */
  50. public SignatureSpi()
  51. {
  52. appRandom = null;
  53. }
  54. /**
  55. Initializes this class with the public key for
  56. verification purposes.
  57. @param publicKey the public key to verify with
  58. @throws InvalidKeyException invalid key
  59. */
  60. protected abstract void engineInitVerify(PublicKey publicKey)
  61. throws InvalidKeyException;
  62. /**
  63. Initializes this class with the private key for
  64. signing purposes.
  65. @param privateKey the private key to sign with
  66. @throws InvalidKeyException invalid key
  67. */
  68. protected abstract void engineInitSign(PrivateKey privateKey)
  69. throws InvalidKeyException;
  70. /**
  71. Initializes this class with the private key and source
  72. of randomness for signing purposes.
  73. This cannot be abstract backward compatibility reasons
  74. @param privateKey the private key to sign with
  75. @param random Source of randomness
  76. @throws InvalidKeyException invalid key
  77. @since JDK 1.2
  78. */
  79. protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
  80. throws InvalidKeyException
  81. {
  82. appRandom = random;
  83. engineInitSign(privateKey);
  84. }
  85. /**
  86. Updates the data to be signed or verified with the specified
  87. byte.
  88. @param b byte to update with
  89. @throws SignatureException Engine not properly initialized
  90. */
  91. protected abstract void engineUpdate(byte b) throws SignatureException;
  92. /**
  93. Updates the data to be signed or verified with the specified
  94. bytes.
  95. @param b array of bytes
  96. @param off the offset to start at in the array
  97. @param len the length of the bytes to use in the array
  98. @throws SignatureException engine not properly initialized
  99. */
  100. protected abstract void engineUpdate(byte[] b, int off, int len)
  101. throws SignatureException;
  102. /**
  103. Returns the signature bytes of all the data fed to this class.
  104. The format of the output depends on the underlying signature
  105. algorithm.
  106. @return the signature
  107. @throws SignatureException engine not properly initialized
  108. */
  109. protected abstract byte[] engineSign() throws SignatureException;
  110. /**
  111. Generates signature bytes of all the data fed to this class
  112. and outputs it to the passed array. The format of the
  113. output depends on the underlying signature algorithm.
  114. This cannot be abstract backward compatibility reasons.
  115. After calling this method, the signature is reset to its
  116. initial state and can be used to generate additional
  117. signatures.
  118. @param outbuff array of bytes
  119. @param offset the offset to start at in the array
  120. @param len the length of the bytes to put into the array.
  121. Neither this method or the GNU provider will
  122. return partial digests. If len is less than the
  123. signature length, this method will throw
  124. SignatureException. If it is greater than or equal
  125. then it is ignored.
  126. @return number of bytes in outbuf
  127. @throws SignatureException engine not properly initialized
  128. @since JDK 1.2
  129. */
  130. protected int engineSign(byte[] outbuf, int offset, int len)
  131. throws SignatureException
  132. {
  133. byte tmp[] = engineSign();
  134. if (tmp.length > len)
  135. throw new SignatureException("Invalid Length");
  136. System.arraycopy(outbuf, offset, tmp, 0, tmp.length);
  137. return tmp.length;
  138. }
  139. /**
  140. Verifies the passed signature.
  141. @param sigBytes the signature bytes to verify
  142. @return true if verified, false otherwise
  143. @throws SignatureException engine not properly initialized
  144. or wrong signature
  145. */
  146. protected abstract boolean engineVerify(byte[] sigBytes)
  147. throws SignatureException;
  148. /**
  149. Sets the specified algorithm parameter to the specified value.
  150. @param param parameter name
  151. @param value parameter value
  152. @throws InvalidParameterException invalid parameter, parameter
  153. already set and cannot set again, a security exception,
  154. etc.
  155. @deprecated use the other setParameter
  156. */
  157. protected abstract void engineSetParameter(String param, Object value)
  158. throws InvalidParameterException;
  159. /**
  160. Sets the signature engine with the specified
  161. AlgorithmParameterSpec;
  162. This cannot be abstract backward compatibility reasons
  163. By default this always throws UnsupportedOperationException
  164. if not overridden;
  165. @param params the parameters
  166. @throws InvalidParameterException invalid parameter, parameter
  167. already set and cannot set again, a security exception,
  168. etc.
  169. */
  170. protected void engineSetParameter(AlgorithmParameterSpec params)
  171. throws InvalidAlgorithmParameterException
  172. {
  173. throw new UnsupportedOperationException();
  174. }
  175. /**
  176. Gets the value for the specified algorithm parameter.
  177. @param param parameter name
  178. @return parameter value
  179. @throws InvalidParameterException invalid parameter
  180. @deprecated use the other getParameter
  181. */
  182. protected abstract Object engineGetParameter(String param)
  183. throws InvalidParameterException;
  184. /**
  185. Returns a clone if cloneable.
  186. @return a clone if cloneable.
  187. @throws CloneNotSupportedException if the implementation does
  188. not support cloning
  189. */
  190. public Object clone() throws CloneNotSupportedException
  191. {
  192. throw new CloneNotSupportedException();
  193. }
  194. }