SignatureSpi.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* SignatureSpi.java --- Signature Service Provider Interface
  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. import java.nio.ByteBuffer;
  33. import java.security.spec.AlgorithmParameterSpec;
  34. /**
  35. * <code>SignatureSpi</code> defines the Service Provider Interface (SPI) for
  36. * the {@link Signature} class. The signature class provides an interface to a
  37. * digital signature algorithm. Digital signatures are used for authentication
  38. * and integrity of data.
  39. *
  40. * @author Mark Benvenuto (ivymccough@worldnet.att.net)
  41. * @since 1.2
  42. * @see Signature
  43. */
  44. public abstract class SignatureSpi
  45. {
  46. /** Source of randomness. */
  47. protected SecureRandom appRandom;
  48. /**
  49. * Creates a new instance of <code>SignatureSpi</code>.
  50. */
  51. public SignatureSpi()
  52. {
  53. appRandom = null;
  54. }
  55. /**
  56. * Initializes this instance with the public key for verification purposes.
  57. *
  58. * @param publicKey
  59. * the public key to verify with.
  60. * @throws InvalidKeyException
  61. * if the key is invalid.
  62. */
  63. protected abstract void engineInitVerify(PublicKey publicKey)
  64. throws InvalidKeyException;
  65. /**
  66. * Initializes this instance with the private key for signing purposes.
  67. *
  68. * @param privateKey
  69. * the private key to sign with.
  70. * @throws InvalidKeyException
  71. * if the key is invalid.
  72. */
  73. protected abstract void engineInitSign(PrivateKey privateKey)
  74. throws InvalidKeyException;
  75. /**
  76. * Initializes this instance with the private key and source of randomness for
  77. * signing purposes.
  78. *
  79. * <p>This method cannot be abstract for backward compatibility reasons.</p>
  80. *
  81. * @param privateKey
  82. * the private key to sign with.
  83. * @param random
  84. * the {@link SecureRandom} to use.
  85. * @throws InvalidKeyException
  86. * if the key is invalid.
  87. * @since 1.2
  88. */
  89. protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
  90. throws InvalidKeyException
  91. {
  92. appRandom = random;
  93. engineInitSign(privateKey);
  94. }
  95. /**
  96. * Updates the data to be signed or verified with the specified byte.
  97. *
  98. * @param b
  99. * byte to update with.
  100. * @throws SignatureException
  101. * if the engine is not properly initialized.
  102. */
  103. protected abstract void engineUpdate(byte b) throws SignatureException;
  104. /**
  105. * Updates the data to be signed or verified with the specified bytes.
  106. *
  107. * @param b
  108. * the array of bytes to use.
  109. * @param off
  110. * the offset to start at in the array.
  111. * @param len
  112. * the number of the bytes to use from the array.
  113. * @throws SignatureException
  114. * if the engine is not properly initialized.
  115. */
  116. protected abstract void engineUpdate(byte[] b, int off, int len)
  117. throws SignatureException;
  118. /**
  119. * Update this signature with the {@link java.nio.Buffer#remaining()}
  120. * bytes of the given buffer.
  121. *
  122. * @param input The input buffer.
  123. * @throws IllegalStateException if the engine is not properly initialized.
  124. */
  125. protected void engineUpdate(ByteBuffer input)
  126. {
  127. byte[] buf = new byte[4096];
  128. while (input.hasRemaining())
  129. {
  130. int l = Math.min(input.remaining(), buf.length);
  131. input.get(buf, 0, l);
  132. try
  133. {
  134. engineUpdate(buf, 0, l);
  135. }
  136. catch (SignatureException se)
  137. {
  138. throw new IllegalStateException(se);
  139. }
  140. }
  141. }
  142. /**
  143. * Returns the signature bytes of all the data fed to this instance. The
  144. * format of the output depends on the underlying signature algorithm.
  145. *
  146. * @return the signature bytes.
  147. * @throws SignatureException
  148. * if the engine is not properly initialized.
  149. */
  150. protected abstract byte[] engineSign() throws SignatureException;
  151. /**
  152. * Generates signature bytes of all the data fed to this instance and stores
  153. * the result in the designated array. The format of the output depends on
  154. * the underlying signature algorithm.
  155. *
  156. * <p>This method cannot be abstract for backward compatibility reasons.
  157. * After calling this method, the signature is reset to its initial state and
  158. * can be used to generate additional signatures.</p>
  159. *
  160. * <p><b>IMPLEMENTATION NOTE:</b>: Neither this method nor the GNU provider
  161. * will return partial digests. If <code>len</code> is less than the
  162. * signature length, this method will throw a {@link SignatureException}. If
  163. * it is greater than or equal then it is ignored.</p>
  164. *
  165. * @param outbuf
  166. * the array of bytes to store the result in.
  167. * @param offset
  168. * the offset to start at in the array.
  169. * @param len
  170. * the number of the bytes to use in the array.
  171. * @return the real number of bytes used.
  172. * @throws SignatureException
  173. * if the engine is not properly initialized.
  174. * @since 1.2
  175. */
  176. protected int engineSign(byte[] outbuf, int offset, int len)
  177. throws SignatureException
  178. {
  179. byte[] tmp = engineSign();
  180. if (tmp.length > len)
  181. throw new SignatureException("Invalid Length");
  182. System.arraycopy(outbuf, offset, tmp, 0, tmp.length);
  183. return tmp.length;
  184. }
  185. /**
  186. * Verifies a designated signature.
  187. *
  188. * @param sigBytes
  189. * the signature bytes to verify.
  190. * @return <code>true</code> if verified, <code>false</code> otherwise.
  191. * @throws SignatureException
  192. * if the engine is not properly initialized or if it is the wrong
  193. * signature.
  194. */
  195. protected abstract boolean engineVerify(byte[] sigBytes)
  196. throws SignatureException;
  197. /**
  198. * Convenience method which calls the method with the same name and one
  199. * argument after copying the designated bytes into a temporary byte array.
  200. * Subclasses may override this method for performance reasons.
  201. *
  202. * @param sigBytes
  203. * the array of bytes to use.
  204. * @param offset
  205. * the offset to start from in the array of bytes.
  206. * @param length
  207. * the number of bytes to use, starting at offset.
  208. * @return <code>true</code> if verified, <code>false</code> otherwise.
  209. * @throws SignatureException
  210. * if the engine is not properly initialized.
  211. */
  212. protected boolean engineVerify(byte[] sigBytes, int offset, int length)
  213. throws SignatureException
  214. {
  215. byte[] tmp = new byte[length];
  216. System.arraycopy(sigBytes, offset, tmp, 0, length);
  217. return engineVerify(tmp);
  218. }
  219. /**
  220. * Sets the specified algorithm parameter to the specified value.
  221. *
  222. * @param param
  223. * the parameter name.
  224. * @param value
  225. * the parameter value.
  226. * @throws InvalidParameterException
  227. * if the parameter invalid, the parameter is already set and
  228. * cannot be changed, a security exception occured, etc.
  229. * @deprecated use the other setParameter.
  230. */
  231. protected abstract void engineSetParameter(String param, Object value)
  232. throws InvalidParameterException;
  233. /**
  234. * Sets the signature engine with the specified {@link AlgorithmParameterSpec}.
  235. *
  236. * <p>This method cannot be abstract for backward compatibility reasons. By
  237. * default it always throws {@link UnsupportedOperationException} unless
  238. * overridden.</p>
  239. *
  240. * @param params
  241. * the parameters.
  242. * @throws InvalidParameterException
  243. * if the parameter is invalid, the parameter is already set and
  244. * cannot be changed, a security exception occured, etc.
  245. */
  246. protected void engineSetParameter(AlgorithmParameterSpec params)
  247. throws InvalidAlgorithmParameterException
  248. {
  249. throw new UnsupportedOperationException();
  250. }
  251. /**
  252. * The default implementaion of this method always throws a
  253. * {@link UnsupportedOperationException}. It MUST be overridden by concrete
  254. * implementations to return the appropriate {@link AlgorithmParameters} for
  255. * this signature engine (or <code>null</code> when that engine does not use
  256. * any parameters.
  257. *
  258. * @return the parameters used with this signature engine, or
  259. * <code>null</code> if it does not use any parameters.
  260. * @throws UnsupportedOperationException
  261. * always.
  262. */
  263. protected AlgorithmParameters engineGetParameters()
  264. {
  265. throw new UnsupportedOperationException();
  266. }
  267. /**
  268. * Returns the value for the specified algorithm parameter.
  269. *
  270. * @param param
  271. * the parameter name.
  272. * @return the parameter value.
  273. * @throws InvalidParameterException
  274. * if the parameter is invalid.
  275. * @deprecated use the other getParameter
  276. */
  277. protected abstract Object engineGetParameter(String param)
  278. throws InvalidParameterException;
  279. /**
  280. * Returns a clone of this instance.
  281. *
  282. * @return a clone of this instance.
  283. * @throws CloneNotSupportedException
  284. * if the implementation does not support cloning.
  285. */
  286. public Object clone() throws CloneNotSupportedException
  287. {
  288. return super.clone();
  289. }
  290. }