1. /*
  2. * @(#)SignatureSpi.java 1.20 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.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.20, 01/23/03
  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 b 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 1.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. * Verifies the passed-in signature in the specified array
  179. * of bytes, starting at the specified offset.
  180. *
  181. * <p> Note: Subclasses should overwrite the default implementation.
  182. *
  183. *
  184. * @param sigBytes the signature bytes to be verified.
  185. * @param offset the offset to start from in the array of bytes.
  186. * @param length the number of bytes to use, starting at offset.
  187. *
  188. * @return true if the signature was verified, false if not.
  189. *
  190. * @exception SignatureException if the engine is not initialized
  191. * properly, or the passed-in signature is improperly encoded or
  192. * of the wrong type, etc.
  193. */
  194. protected boolean engineVerify(byte[] sigBytes, int offset, int length)
  195. throws SignatureException {
  196. byte[] sigBytesCopy = new byte[length];
  197. System.arraycopy(sigBytes, offset, sigBytesCopy, 0, length);
  198. return engineVerify(sigBytesCopy);
  199. }
  200. /**
  201. * Sets the specified algorithm parameter to the specified
  202. * value. This method supplies a general-purpose mechanism through
  203. * which it is possible to set the various parameters of this object.
  204. * A parameter may be any settable parameter for the algorithm, such as
  205. * a parameter size, or a source of random bits for signature generation
  206. * (if appropriate), or an indication of whether or not to perform
  207. * a specific but optional computation. A uniform algorithm-specific
  208. * naming scheme for each parameter is desirable but left unspecified
  209. * at this time.
  210. *
  211. * @param param the string identifier of the parameter.
  212. *
  213. * @param value the parameter value.
  214. *
  215. * @exception InvalidParameterException if <code>param</code> is an
  216. * invalid parameter for this signature algorithm engine,
  217. * the parameter is already set
  218. * and cannot be set again, a security exception occurs, and so on.
  219. *
  220. * @deprecated Replaced by {@link
  221. * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
  222. * engineSetParameter}.
  223. */
  224. protected abstract void engineSetParameter(String param, Object value)
  225. throws InvalidParameterException;
  226. /**
  227. * <p>This method is overridden by providers to initialize
  228. * this signature engine with the specified parameter set.
  229. *
  230. * @param params the parameters
  231. *
  232. * @exception UnsupportedOperationException if this method is not
  233. * overridden by a provider
  234. *
  235. * @exception InvalidAlgorithmParameterException if this method is
  236. * overridden by a provider and the the given parameters
  237. * are inappropriate for this signature engine
  238. */
  239. protected void engineSetParameter(AlgorithmParameterSpec params)
  240. throws InvalidAlgorithmParameterException {
  241. throw new UnsupportedOperationException();
  242. }
  243. /**
  244. * <p>This method is overridden by providers to return the
  245. * parameters used with this signature engine, or null
  246. * if this signature engine does not use any parameters.
  247. *
  248. * <p>The returned parameters may be the same that were used to initialize
  249. * this signature engine, or may contain a combination of default and
  250. * randomly generated parameter values used by the underlying signature
  251. * implementation if this signature engine requires algorithm parameters
  252. * but was not initialized with any.
  253. *
  254. * @return the parameters used with this signature engine, or null if this
  255. * signature engine does not use any parameters
  256. *
  257. * @exception UnsupportedOperationException if this method is
  258. * not overridden by a provider
  259. */
  260. protected AlgorithmParameters engineGetParameters() {
  261. throw new UnsupportedOperationException();
  262. }
  263. /**
  264. * Gets the value of the specified algorithm parameter.
  265. * This method supplies a general-purpose mechanism through which it
  266. * is possible to get the various parameters of this object. A parameter
  267. * may be any settable parameter for the algorithm, such as a parameter
  268. * size, or a source of random bits for signature generation (if
  269. * appropriate), or an indication of whether or not to perform a
  270. * specific but optional computation. A uniform algorithm-specific
  271. * naming scheme for each parameter is desirable but left unspecified
  272. * at this time.
  273. *
  274. * @param param the string name of the parameter.
  275. *
  276. * @return the object that represents the parameter value, or null if
  277. * there is none.
  278. *
  279. * @exception InvalidParameterException if <code>param</code> is an
  280. * invalid parameter for this engine, or another exception occurs while
  281. * trying to get this parameter.
  282. *
  283. * @deprecated
  284. */
  285. protected abstract Object engineGetParameter(String param)
  286. throws InvalidParameterException;
  287. /**
  288. * Returns a clone if the implementation is cloneable.
  289. *
  290. * @return a clone if the implementation is cloneable.
  291. *
  292. * @exception CloneNotSupportedException if this is called
  293. * on an implementation that does not support <code>Cloneable</code>.
  294. */
  295. public Object clone() throws CloneNotSupportedException {
  296. if (this instanceof Cloneable) {
  297. return super.clone();
  298. } else {
  299. throw new CloneNotSupportedException();
  300. }
  301. }
  302. }