PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc-3.2.3-20040701/libjava/java/security/Signature.java

#
Java | 499 lines | 184 code | 33 blank | 282 comment | 23 complexity | 974b0395ddab69ef4517818b86887a53 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, CC-BY-SA-3.0
  1. /* Signature.java --- Signature Class
  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. Signature is used to provide an interface to digital signature
  35. algorithms. Digital signatures provide authentication and data
  36. integrity of digital data.
  37. The GNU provider provides the NIST standard DSA which uses DSA
  38. and SHA-1. It can be specified by SHA/DSA, SHA-1/DSA or its
  39. OID. If the RSA signature algorithm is provided then
  40. it could be MD2/RSA. MD5/RSA, or SHA-1/RSA. The algorithm must
  41. be specified because there is no default.
  42. Signature provides implementation-independent algorithms which
  43. are requested by the user through getInstance. It can be
  44. requested by specifying just the algorithm name or by
  45. specifying both the algorithm name and provider name.
  46. The three phases of using Signature are:
  47. 1. Initialing
  48. * It must be initialized with a private key for
  49. signing.
  50. * It must be initialized with a public key for
  51. verifying.
  52. 2. Updating
  53. Update the bytes for signing or verifying with calls
  54. to update.
  55. 3. Signing or Verify the signature on the currently stored
  56. bytes by calling sign or verify.
  57. @author Mark Benvenuto <ivymccough@worldnet.att.net>
  58. @since JDK 1.1
  59. */
  60. public abstract class Signature extends SignatureSpi
  61. {
  62. /**
  63. Possible state variable which signifies if it has not been
  64. initialized.
  65. */
  66. protected static final int UNINITIALIZED = 1;
  67. /**
  68. Possible state variable which signifies if it has been
  69. initialized for signing.
  70. */
  71. protected static final int SIGN = 2;
  72. /**
  73. Possible state variable which signifies if it has been
  74. initialized for verifying.
  75. */
  76. protected static final int VERIFY = 3;
  77. /**
  78. State of this Signature class.
  79. */
  80. protected int state = UNINITIALIZED;
  81. private String algorithm;
  82. private Provider provider;
  83. /**
  84. Creates a new signature for this algorithm.
  85. @param algorithm the algorithm to use
  86. */
  87. protected Signature(String algorithm)
  88. {
  89. this.algorithm = algorithm;
  90. state = UNINITIALIZED;
  91. }
  92. /**
  93. Gets an instance of the Signature class representing
  94. the specified signature. If the algorithm is not found then,
  95. it throws NoSuchAlgorithmException.
  96. @param algorithm the name of signature algorithm to choose
  97. @return a Signature repesenting the desired algorithm
  98. @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
  99. */
  100. public static Signature getInstance(String algorithm)
  101. throws NoSuchAlgorithmException
  102. {
  103. String name = "Signature." + algorithm;
  104. Provider[] p = Security.getProviders();
  105. for (int i = 0; i < p.length; i++)
  106. {
  107. String classname = p[i].getProperty(name);
  108. if (classname != null)
  109. return getInstance(classname, algorithm, p[i]);
  110. }
  111. throw new NoSuchAlgorithmException(algorithm);
  112. }
  113. /**
  114. Gets an instance of the Signature class representing
  115. the specified signature from the specified provider. If the
  116. algorithm is not found then, it throws NoSuchAlgorithmException.
  117. If the provider is not found, then it throws
  118. NoSuchProviderException.
  119. @param algorithm the name of signature algorithm to choose
  120. @param provider the name of the provider to find the algorithm in
  121. @return a Signature repesenting the desired algorithm
  122. @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
  123. @throws NoSuchProviderException if the provider is not found
  124. */
  125. public static Signature getInstance(String algorithm, String provider)
  126. throws NoSuchAlgorithmException, NoSuchProviderException
  127. {
  128. Provider p = Security.getProvider(provider);
  129. if (p == null)
  130. throw new NoSuchProviderException();
  131. return getInstance(p.getProperty("Signature." + algorithm), algorithm, p);
  132. }
  133. private static Signature getInstance(String classname,
  134. String algorithm,
  135. Provider provider)
  136. throws NoSuchAlgorithmException
  137. {
  138. try
  139. {
  140. Object o = Class.forName(classname).newInstance();
  141. Signature sig;
  142. if (o instanceof SignatureSpi)
  143. sig = (Signature) (new DummySignature((SignatureSpi) o, algorithm));
  144. else
  145. {
  146. sig = (Signature) o;
  147. sig.algorithm = algorithm;
  148. }
  149. sig.provider = provider;
  150. return sig;
  151. }
  152. catch (ClassNotFoundException cnfe)
  153. {
  154. throw new NoSuchAlgorithmException("Class not found");
  155. }
  156. catch (InstantiationException ie)
  157. {
  158. throw new NoSuchAlgorithmException("Class instantiation failed");
  159. }
  160. catch (IllegalAccessException iae)
  161. {
  162. throw new NoSuchAlgorithmException("Illegal Access");
  163. }
  164. }
  165. /**
  166. Gets the provider that the Signature is from.
  167. @return the provider the this Signature
  168. */
  169. public final Provider getProvider()
  170. {
  171. return provider;
  172. }
  173. /**
  174. Initializes this class with the public key for
  175. verification purposes.
  176. @param publicKey the public key to verify with
  177. @throws InvalidKeyException invalid key
  178. */
  179. public final void initVerify(PublicKey publicKey) throws InvalidKeyException
  180. {
  181. state = VERIFY;
  182. engineInitVerify(publicKey);
  183. }
  184. /**
  185. Verify Signature with a certificate. This is a FIPS 140-1 compatible method
  186. since it verifies a signature with a certificate.
  187. If the certificate is an X.509 certificate, has a KeyUsage parameter and
  188. the parameter indicates this key is not to be used for signing then an
  189. error is returned.
  190. @param certificate a certificate containing a public key to verify with
  191. */
  192. public final void initVerify(java.security.cert.Certificate certificate)
  193. throws InvalidKeyException
  194. {
  195. state = VERIFY;
  196. if (certificate.getType().equals("X509"))
  197. {
  198. java.security.cert.X509Certificate cert =
  199. (java.security.cert.X509Certificate) certificate;
  200. boolean[]array = cert.getKeyUsage();
  201. if (array != null && array[0] == false)
  202. throw new InvalidKeyException
  203. ("KeyUsage of this Certificate indicates it cannot be used for digital signing");
  204. }
  205. this.initVerify(certificate.getPublicKey());
  206. }
  207. /**
  208. Initializes this class with the private key for
  209. signing purposes.
  210. @param privateKey the private key to sign with
  211. @throws InvalidKeyException invalid key
  212. */
  213. public final void initSign(PrivateKey privateKey) throws InvalidKeyException
  214. {
  215. state = SIGN;
  216. engineInitSign(privateKey);
  217. }
  218. /**
  219. Initializes this class with the private key and source
  220. of randomness for signing purposes.
  221. @param privateKey the private key to sign with
  222. @param random Source of randomness
  223. @throws InvalidKeyException invalid key
  224. @since JDK 1.2
  225. */
  226. public final void initSign(PrivateKey privateKey, SecureRandom random)
  227. throws InvalidKeyException
  228. {
  229. state = SIGN;
  230. engineInitSign(privateKey, random);
  231. }
  232. /**
  233. Returns the signature bytes of all the data fed to this class.
  234. The format of the output depends on the underlying signature
  235. algorithm.
  236. @return the signature
  237. @throws SignatureException engine not properly initialized
  238. */
  239. public final byte[] sign() throws SignatureException
  240. {
  241. if (state == SIGN)
  242. {
  243. state = UNINITIALIZED;
  244. return engineSign();
  245. }
  246. else
  247. throw new SignatureException();
  248. }
  249. /**
  250. Generates signature bytes of all the data fed to this class
  251. and outputs it to the passed array. The format of the
  252. output depends on the underlying signature algorithm.
  253. After calling this method, the signature is reset to its
  254. initial state and can be used to generate additional
  255. signatures.
  256. @param outbuff array of bytes
  257. @param offset the offset to start at in the array
  258. @param len the length of the bytes to put into the array.
  259. Neither this method or the GNU provider will
  260. return partial digests. If len is less than the
  261. signature length, this method will throw
  262. SignatureException. If it is greater than or equal
  263. then it is ignored.
  264. @return number of bytes in outbuf
  265. @throws SignatureException engine not properly initialized
  266. @since JDK 1.2
  267. */
  268. public final int sign(byte[]outbuf, int offset, int len)
  269. throws SignatureException
  270. {
  271. if (state == SIGN)
  272. {
  273. state = UNINITIALIZED;
  274. return engineSign(outbuf, offset, len);
  275. }
  276. else
  277. throw new SignatureException();
  278. }
  279. /**
  280. Verifies the passed signature.
  281. @param signature the signature bytes to verify
  282. @return true if verified, false otherwise
  283. @throws SignatureException engine not properly initialized
  284. or wrong signature
  285. */
  286. public final boolean verify(byte[]signature) throws SignatureException
  287. {
  288. if (state == VERIFY)
  289. {
  290. state = UNINITIALIZED;
  291. return engineVerify(signature);
  292. }
  293. else
  294. throw new SignatureException();
  295. }
  296. /**
  297. Updates the data to be signed or verified with the specified
  298. byte.
  299. @param b byte to update with
  300. @throws SignatureException Engine not properly initialized
  301. */
  302. public final void update(byte b) throws SignatureException
  303. {
  304. if (state != UNINITIALIZED)
  305. engineUpdate(b);
  306. else
  307. throw new SignatureException();
  308. }
  309. /**
  310. Updates the data to be signed or verified with the specified
  311. bytes.
  312. @param data array of bytes
  313. @throws SignatureException engine not properly initialized
  314. */
  315. public final void update(byte[]data) throws SignatureException
  316. {
  317. if (state != UNINITIALIZED)
  318. engineUpdate(data, 0, data.length);
  319. else
  320. throw new SignatureException();
  321. }
  322. /**
  323. Updates the data to be signed or verified with the specified
  324. bytes.
  325. @param data array of bytes
  326. @param off the offset to start at in the array
  327. @param len the length of the bytes to use in the array
  328. @throws SignatureException engine not properly initialized
  329. */
  330. public final void update(byte[]data, int off, int len)
  331. throws SignatureException
  332. {
  333. if (state != UNINITIALIZED)
  334. engineUpdate(data, off, len);
  335. else
  336. throw new SignatureException();
  337. }
  338. /**
  339. Gets the name of the algorithm currently used.
  340. The names of algorithms are usually SHA/DSA or SHA/RSA.
  341. @return name of algorithm.
  342. */
  343. public final String getAlgorithm()
  344. {
  345. return algorithm;
  346. }
  347. /**
  348. Returns a representation of the Signature as a String
  349. @return a string representing the signature
  350. */
  351. public String toString()
  352. {
  353. return (algorithm + " Signature");
  354. }
  355. /**
  356. Sets the specified algorithm parameter to the specified value.
  357. @param param parameter name
  358. @param value parameter value
  359. @throws InvalidParameterException invalid parameter, parameter
  360. already set and cannot set again, a security exception,
  361. etc.
  362. @deprecated use the other setParameter
  363. */
  364. public final void setParameter(String param, Object value)
  365. throws InvalidParameterException
  366. {
  367. engineSetParameter(param, value);
  368. }
  369. /**
  370. Sets the signature engine with the specified
  371. AlgorithmParameterSpec;
  372. By default this always throws UnsupportedOperationException
  373. if not overridden;
  374. @param params the parameters
  375. @throws InvalidParameterException invalid parameter, parameter
  376. already set and cannot set again, a security exception,
  377. etc.
  378. */
  379. public final void setParameter(AlgorithmParameterSpec params)
  380. throws InvalidAlgorithmParameterException
  381. {
  382. engineSetParameter(params);
  383. }
  384. /**
  385. Gets the value for the specified algorithm parameter.
  386. @param param parameter name
  387. @return parameter value
  388. @throws InvalidParameterException invalid parameter
  389. @deprecated use the other getParameter
  390. */
  391. public final Object getParameter(String param)
  392. throws InvalidParameterException
  393. {
  394. return engineGetParameter(param);
  395. }
  396. /**
  397. Returns a clone if cloneable.
  398. @return a clone if cloneable.
  399. @throws CloneNotSupportedException if the implementation does
  400. not support cloning
  401. */
  402. public Object clone() throws CloneNotSupportedException
  403. {
  404. throw new CloneNotSupportedException();
  405. }
  406. }