1. /*
  2. * @(#)SignatureSpi.java 1.10 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.security.spec.AlgorithmParameterSpec;
  9. import java.util.*;
  10. import java.io.*;
  11. /**
  12. * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  13. * for the <code>Signature</code> class, which is used to provide the
  14. * functionality of a digital signature algorithm. Digital signatures are used
  15. * for authentication and integrity assurance of digital data.
  16. *.
  17. * <p> All the abstract methods in this class must be implemented by each
  18. * cryptographic service provider who wishes to supply the implementation
  19. * of a particular signature algorithm.
  20. *
  21. * @author Benjamin Renaud
  22. *
  23. * @version 1.10 01/11/29
  24. *
  25. * @see Signature
  26. */
  27. public abstract class SignatureSpi {
  28. /**
  29. * Application-specified source of randomness.
  30. */
  31. protected SecureRandom appRandom = null;
  32. /**
  33. * Initializes this signature object with the specified
  34. * public key for verification operations.
  35. *
  36. * @param publicKey the public key of the identity whose signature is
  37. * going to be verified.
  38. *
  39. * @exception InvalidKeyException if the key is improperly
  40. * encoded, parameters are missing, and so on.
  41. */
  42. protected abstract void engineInitVerify(PublicKey publicKey)
  43. throws InvalidKeyException;
  44. /**
  45. * Initializes this signature object with the specified
  46. * private key for signing operations.
  47. *
  48. * @param privateKey the private key of the identity whose signature
  49. * will be generated.
  50. *
  51. * @exception InvalidKeyException if the key is improperly
  52. * encoded, parameters are missing, and so on.
  53. */
  54. protected abstract void engineInitSign(PrivateKey privateKey)
  55. throws InvalidKeyException;
  56. /**
  57. * Initializes this signature object with the specified
  58. * private key and source of randomness for signing operations.
  59. *
  60. * <p>This concrete method has been added to this previously-defined
  61. * abstract class. (For backwards compatibility, it cannot be abstract.)
  62. *
  63. * @param privateKey the private key of the identity whose signature
  64. * will be generated.
  65. * @param random the source of randomness
  66. *
  67. * @exception InvalidKeyException if the key is improperly
  68. * encoded, parameters are missing, and so on.
  69. */
  70. protected void engineInitSign(PrivateKey privateKey,
  71. SecureRandom random)
  72. throws InvalidKeyException {
  73. this.appRandom = random;
  74. engineInitSign(privateKey);
  75. }
  76. /**
  77. * Updates the data to be signed or verified
  78. * using the specified byte.
  79. *
  80. * @param b the byte to use for the update.
  81. *
  82. * @exception SignatureException if the engine is not initialized
  83. * properly.
  84. */
  85. protected abstract void engineUpdate(byte b) throws SignatureException;
  86. /**
  87. * Updates the data to be signed or verified, using the
  88. * specified array of bytes, starting at the specified offset.
  89. *
  90. * @param data the array of bytes.
  91. * @param off the offset to start from in the array of bytes.
  92. * @param len the number of bytes to use, starting at offset.
  93. *
  94. * @exception SignatureException if the engine is not initialized
  95. * properly.
  96. */
  97. protected abstract void engineUpdate(byte[] b, int off, int len)
  98. throws SignatureException;
  99. /**
  100. * Returns the signature bytes of all the data
  101. * updated so far.
  102. * The format of the signature depends on the underlying
  103. * signature scheme.
  104. *
  105. * @return the signature bytes of the signing operation's result.
  106. *
  107. * @exception SignatureException if the engine is not
  108. * initialized properly.
  109. */
  110. protected abstract byte[] engineSign() throws SignatureException;
  111. /**
  112. * Finishes this signature operation and stores the resulting signature
  113. * bytes in the provided buffer <code>outbuf</code>, starting at
  114. * <code>offset</code>.
  115. * The format of the signature depends on the underlying
  116. * signature scheme.
  117. *
  118. * <p>The signature implementation is reset to its initial state
  119. * (the state it was in after a call to one of the
  120. * <code>engineInitSign</code> methods)
  121. * and can be reused to generate further signatures with the same private
  122. * key.
  123. *
  124. * This method should be abstract, but we leave it concrete for
  125. * binary compatibility. Knowledgeable providers should override this
  126. * method.
  127. *
  128. * @param outbuf buffer for the signature result.
  129. *
  130. * @param offset offset into <code>outbuf</code> where the signature is
  131. * stored.
  132. *
  133. * @param len number of bytes within <code>outbuf</code> allotted for the
  134. * signature.
  135. * Both this default implementation and the SUN provider do not
  136. * return partial digests. If the value of this parameter is less
  137. * than the actual signature length, this method will throw a
  138. * SignatureException.
  139. * This parameter is ignored if its value is greater than or equal to
  140. * the actual signature length.
  141. *
  142. * @return the number of bytes placed into <code>outbuf</code>
  143. *
  144. * @exception SignatureException if an error occurs or <code>len</code>
  145. * is less than the actual signature length.
  146. *
  147. * @since JDK1.2
  148. */
  149. protected int engineSign(byte[] outbuf, int offset, int len)
  150. throws SignatureException {
  151. byte[] sig = engineSign();
  152. if (len < sig.length) {
  153. throw new SignatureException
  154. ("partial signatures not returned");
  155. }
  156. if (outbuf.length - offset < sig.length) {
  157. throw new SignatureException
  158. ("insufficient space in the output buffer to store the "
  159. + "signature");
  160. }
  161. System.arraycopy(sig, 0, outbuf, offset, sig.length);
  162. return sig.length;
  163. }
  164. /**
  165. * Verifies the passed-in signature.
  166. *
  167. * @param sigBytes the signature bytes to be verified.
  168. *
  169. * @return true if the signature was verified, false if not.
  170. *
  171. * @exception SignatureException if the engine is not initialized
  172. * properly, or the passed-in signature is improperly encoded or
  173. * of the wrong type, etc.
  174. */
  175. protected abstract boolean engineVerify(byte[] sigBytes)
  176. throws SignatureException;
  177. /**
  178. * Sets the specified algorithm parameter to the specified
  179. * value. This method supplies a general-purpose mechanism through
  180. * which it is possible to set the various parameters of this object.
  181. * A parameter may be any settable parameter for the algorithm, such as
  182. * a parameter size, or a source of random bits for signature generation
  183. * (if appropriate), or an indication of whether or not to perform
  184. * a specific but optional computation. A uniform algorithm-specific
  185. * naming scheme for each parameter is desirable but left unspecified
  186. * at this time.
  187. *
  188. * @param param the string identifier of the parameter.
  189. *
  190. * @param value the parameter value.
  191. *
  192. * @exception InvalidParameterException if <code>param</code> is an
  193. * invalid parameter for this signature algorithm engine,
  194. * the parameter is already set
  195. * and cannot be set again, a security exception occurs, and so on.
  196. *
  197. * @deprecated Replaced by <a href =
  198. * "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
  199. * engineSetParameter</a>.
  200. */
  201. protected abstract void engineSetParameter(String param, Object value)
  202. throws InvalidParameterException;
  203. /**
  204. * Initializes this signature engine with the specified parameter set.
  205. *
  206. * This concrete method has been added to this previously-defined
  207. * abstract class. (For backwards compatibility, it cannot be abstract.)
  208. * It may be overridden by a provider to set the algorithm parameters
  209. * using the specified <code>params</code>. Such an override
  210. * is expected to throw an InvalidAlgorithmParameterException if
  211. * a parameter is invalid.
  212. * If this method is not overridden, it always throws an
  213. * UnsupportedOperationException.
  214. *
  215. * @param params the parameters
  216. *
  217. * @exception InvalidAlgorithmParameterException if the given parameters
  218. * are inappropriate for this signature engine
  219. */
  220. protected void engineSetParameter(AlgorithmParameterSpec params)
  221. throws InvalidAlgorithmParameterException {
  222. throw new UnsupportedOperationException();
  223. }
  224. /**
  225. * Gets the value of the specified algorithm parameter.
  226. * This method supplies a general-purpose mechanism through which it
  227. * is possible to get the various parameters of this object. A parameter
  228. * may be any settable parameter for the algorithm, such as a parameter
  229. * size, or a source of random bits for signature generation (if
  230. * appropriate), or an indication of whether or not to perform a
  231. * specific but optional computation. A uniform algorithm-specific
  232. * naming scheme for each parameter is desirable but left unspecified
  233. * at this time.
  234. *
  235. * @param param the string name of the parameter.
  236. *
  237. * @return the object that represents the parameter value, or null if
  238. * there is none.
  239. *
  240. * @exception InvalidParameterException if <code>param</code> is an
  241. * invalid parameter for this engine, or another exception occurs while
  242. * trying to get this parameter.
  243. *
  244. * @deprecated
  245. */
  246. protected abstract Object engineGetParameter(String param)
  247. throws InvalidParameterException;
  248. /**
  249. * Returns a clone if the implementation is cloneable.
  250. *
  251. * @return a clone if the implementation is cloneable.
  252. *
  253. * @exception CloneNotSupportedException if this is called
  254. * on an implementation that does not support <code>Cloneable</code>.
  255. */
  256. public Object clone() throws CloneNotSupportedException {
  257. if (this instanceof Cloneable) {
  258. return super.clone();
  259. } else {
  260. throw new CloneNotSupportedException();
  261. }
  262. }
  263. }