source: trunk/src/gcc/libjava/java/security/Signature.java@ 192

Last change on this file since 192 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.7 KB
Line 
1/* Signature.java --- Signature Class
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package java.security;
39import java.security.spec.AlgorithmParameterSpec;
40
41/**
42 Signature is used to provide an interface to digital signature
43 algorithms. Digital signatures provide authentication and data
44 integrity of digital data.
45
46 The GNU provider provides the NIST standard DSA which uses DSA
47 and SHA-1. It can be specified by SHA/DSA, SHA-1/DSA or its
48 OID. If the RSA signature algorithm is provided then
49 it could be MD2/RSA. MD5/RSA, or SHA-1/RSA. The algorithm must
50 be specified because there is no default.
51
52 Signature provides implementation-independent algorithms which
53 are requested by the user through getInstance. It can be
54 requested by specifying just the algorithm name or by
55 specifying both the algorithm name and provider name.
56
57 The three phases of using Signature are:
58
59 1. Initialing
60
61 * It must be initialized with a private key for
62 signing.
63 * It must be initialized with a public key for
64 verifying.
65
66 2. Updating
67
68 Update the bytes for signing or verifying with calls
69 to update.
70
71 3. Signing or Verify the signature on the currently stored
72 bytes by calling sign or verify.
73
74 @author Mark Benvenuto <[email protected]>
75 @since JDK 1.1
76 */
77public abstract class Signature extends SignatureSpi
78{
79 /**
80 Possible state variable which signifies if it has not been
81 initialized.
82 */
83 protected static final int UNINITIALIZED = 1;
84
85 /**
86 Possible state variable which signifies if it has been
87 initialized for signing.
88 */
89 protected static final int SIGN = 2;
90
91 /**
92 Possible state variable which signifies if it has been
93 initialized for verifying.
94 */
95 protected static final int VERIFY = 3;
96
97 /**
98 State of this Signature class.
99 */
100 protected int state = UNINITIALIZED;
101
102 private String algorithm;
103 private Provider provider;
104
105 /**
106 Creates a new signature for this algorithm.
107
108 @param algorithm the algorithm to use
109 */
110 protected Signature(String algorithm)
111 {
112 this.algorithm = algorithm;
113 state = UNINITIALIZED;
114 }
115
116 /**
117 Gets an instance of the Signature class representing
118 the specified signature. If the algorithm is not found then,
119 it throws NoSuchAlgorithmException.
120
121 @param algorithm the name of signature algorithm to choose
122 @return a Signature repesenting the desired algorithm
123
124 @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
125 */
126 public static Signature getInstance(String algorithm)
127 throws NoSuchAlgorithmException
128 {
129 String name = "Signature." + algorithm;
130 Provider[] p = Security.getProviders();
131
132 for (int i = 0; i < p.length; i++)
133 {
134 String classname = p[i].getProperty(name);
135 if (classname != null)
136 return getInstance(classname, algorithm, p[i]);
137 }
138
139 throw new NoSuchAlgorithmException(algorithm);
140 }
141
142 /**
143 Gets an instance of the Signature class representing
144 the specified signature from the specified provider. If the
145 algorithm is not found then, it throws NoSuchAlgorithmException.
146 If the provider is not found, then it throws
147 NoSuchProviderException.
148
149 @param algorithm the name of signature algorithm to choose
150 @param provider the name of the provider to find the algorithm in
151 @return a Signature repesenting the desired algorithm
152
153 @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
154 @throws NoSuchProviderException if the provider is not found
155 */
156 public static Signature getInstance(String algorithm, String provider)
157 throws NoSuchAlgorithmException, NoSuchProviderException
158 {
159 Provider p = Security.getProvider(provider);
160 if (p == null)
161 throw new NoSuchProviderException();
162
163 return getInstance(p.getProperty("Signature." + algorithm), algorithm, p);
164 }
165
166 private static Signature getInstance(String classname,
167 String algorithm,
168 Provider provider)
169 throws NoSuchAlgorithmException
170 {
171 try
172 {
173 Object o = Class.forName(classname).newInstance();
174 Signature sig;
175 if (o instanceof SignatureSpi)
176 sig = (Signature) (new DummySignature((SignatureSpi) o, algorithm));
177 else
178 {
179 sig = (Signature) o;
180 sig.algorithm = algorithm;
181 }
182
183 sig.provider = provider;
184 return sig;
185 }
186 catch (ClassNotFoundException cnfe)
187 {
188 throw new NoSuchAlgorithmException("Class not found");
189 }
190 catch (InstantiationException ie)
191 {
192 throw new NoSuchAlgorithmException("Class instantiation failed");
193 }
194 catch (IllegalAccessException iae)
195 {
196 throw new NoSuchAlgorithmException("Illegal Access");
197 }
198 }
199
200 /**
201 Gets the provider that the Signature is from.
202
203 @return the provider the this Signature
204 */
205 public final Provider getProvider()
206 {
207 return provider;
208 }
209
210 /**
211 Initializes this class with the public key for
212 verification purposes.
213
214 @param publicKey the public key to verify with
215
216 @throws InvalidKeyException invalid key
217 */
218 public final void initVerify(PublicKey publicKey) throws InvalidKeyException
219 {
220 state = VERIFY;
221 engineInitVerify(publicKey);
222 }
223
224 /**
225 Verify Signature with a certificate. This is a FIPS 140-1 compatible method
226 since it verifies a signature with a certificate.
227
228 If the certificate is an X.509 certificate, has a KeyUsage parameter and
229 the parameter indicates this key is not to be used for signing then an
230 error is returned.
231
232 @param certificate a certificate containing a public key to verify with
233 */
234 public final void initVerify(java.security.cert.Certificate certificate)
235 throws InvalidKeyException
236 {
237 state = VERIFY;
238 if (certificate.getType().equals("X509"))
239 {
240 java.security.cert.X509Certificate cert =
241 (java.security.cert.X509Certificate) certificate;
242
243 boolean[]array = cert.getKeyUsage();
244 if (array != null && array[0] == false)
245 throw new InvalidKeyException
246 ("KeyUsage of this Certificate indicates it cannot be used for digital signing");
247 }
248 this.initVerify(certificate.getPublicKey());
249 }
250
251 /**
252 Initializes this class with the private key for
253 signing purposes.
254
255 @param privateKey the private key to sign with
256
257 @throws InvalidKeyException invalid key
258 */
259 public final void initSign(PrivateKey privateKey) throws InvalidKeyException
260 {
261 state = SIGN;
262 engineInitSign(privateKey);
263 }
264
265 /**
266 Initializes this class with the private key and source
267 of randomness for signing purposes.
268
269 @param privateKey the private key to sign with
270 @param random Source of randomness
271
272 @throws InvalidKeyException invalid key
273
274 @since JDK 1.2
275 */
276 public final void initSign(PrivateKey privateKey, SecureRandom random)
277 throws InvalidKeyException
278 {
279 state = SIGN;
280 engineInitSign(privateKey, random);
281 }
282
283
284 /**
285 Returns the signature bytes of all the data fed to this class.
286 The format of the output depends on the underlying signature
287 algorithm.
288
289 @return the signature
290
291 @throws SignatureException engine not properly initialized
292 */
293 public final byte[] sign() throws SignatureException
294 {
295 if (state == SIGN)
296 {
297 state = UNINITIALIZED;
298 return engineSign();
299 }
300 else
301 throw new SignatureException();
302 }
303
304 /**
305 Generates signature bytes of all the data fed to this class
306 and outputs it to the passed array. The format of the
307 output depends on the underlying signature algorithm.
308
309 After calling this method, the signature is reset to its
310 initial state and can be used to generate additional
311 signatures.
312
313 @param outbuff array of bytes
314 @param offset the offset to start at in the array
315 @param len the length of the bytes to put into the array.
316 Neither this method or the GNU provider will
317 return partial digests. If len is less than the
318 signature length, this method will throw
319 SignatureException. If it is greater than or equal
320 then it is ignored.
321
322 @return number of bytes in outbuf
323
324 @throws SignatureException engine not properly initialized
325
326 @since JDK 1.2
327 */
328 public final int sign(byte[]outbuf, int offset, int len)
329 throws SignatureException
330 {
331 if (state == SIGN)
332 {
333 state = UNINITIALIZED;
334 return engineSign(outbuf, offset, len);
335 }
336 else
337 throw new SignatureException();
338 }
339
340 /**
341 Verifies the passed signature.
342
343 @param signature the signature bytes to verify
344
345 @return true if verified, false otherwise
346
347 @throws SignatureException engine not properly initialized
348 or wrong signature
349 */
350 public final boolean verify(byte[]signature) throws SignatureException
351 {
352 if (state == VERIFY)
353 {
354 state = UNINITIALIZED;
355 return engineVerify(signature);
356 }
357 else
358 throw new SignatureException();
359 }
360
361 /**
362 Updates the data to be signed or verified with the specified
363 byte.
364
365 @param b byte to update with
366
367 @throws SignatureException Engine not properly initialized
368 */
369 public final void update(byte b) throws SignatureException
370 {
371 if (state != UNINITIALIZED)
372 engineUpdate(b);
373 else
374 throw new SignatureException();
375 }
376
377 /**
378 Updates the data to be signed or verified with the specified
379 bytes.
380
381 @param data array of bytes
382
383 @throws SignatureException engine not properly initialized
384 */
385 public final void update(byte[]data) throws SignatureException
386 {
387 if (state != UNINITIALIZED)
388 engineUpdate(data, 0, data.length);
389 else
390 throw new SignatureException();
391 }
392
393 /**
394 Updates the data to be signed or verified with the specified
395 bytes.
396
397 @param data array of bytes
398 @param off the offset to start at in the array
399 @param len the length of the bytes to use in the array
400
401 @throws SignatureException engine not properly initialized
402 */
403 public final void update(byte[]data, int off, int len)
404 throws SignatureException
405 {
406 if (state != UNINITIALIZED)
407 engineUpdate(data, off, len);
408 else
409 throw new SignatureException();
410 }
411
412 /**
413 Gets the name of the algorithm currently used.
414 The names of algorithms are usually SHA/DSA or SHA/RSA.
415
416 @return name of algorithm.
417 */
418 public final String getAlgorithm()
419 {
420 return algorithm;
421 }
422
423 /**
424 Returns a representation of the Signature as a String
425
426 @return a string representing the signature
427 */
428 public String toString()
429 {
430 return (algorithm + " Signature");
431 }
432
433 /**
434 Sets the specified algorithm parameter to the specified value.
435
436 @param param parameter name
437 @param value parameter value
438
439 @throws InvalidParameterException invalid parameter, parameter
440 already set and cannot set again, a security exception,
441 etc.
442
443 @deprecated use the other setParameter
444 */
445 public final void setParameter(String param, Object value)
446 throws InvalidParameterException
447 {
448 engineSetParameter(param, value);
449 }
450
451 /**
452 Sets the signature engine with the specified
453 AlgorithmParameterSpec;
454
455 By default this always throws UnsupportedOperationException
456 if not overridden;
457
458 @param params the parameters
459
460 @throws InvalidParameterException invalid parameter, parameter
461 already set and cannot set again, a security exception,
462 etc.
463 */
464 public final void setParameter(AlgorithmParameterSpec params)
465 throws InvalidAlgorithmParameterException
466 {
467 engineSetParameter(params);
468 }
469
470 /**
471 Gets the value for the specified algorithm parameter.
472
473 @param param parameter name
474
475 @return parameter value
476
477 @throws InvalidParameterException invalid parameter
478
479 @deprecated use the other getParameter
480 */
481 public final Object getParameter(String param)
482 throws InvalidParameterException
483 {
484 return engineGetParameter(param);
485 }
486
487 /**
488 Returns a clone if cloneable.
489
490 @return a clone if cloneable.
491
492 @throws CloneNotSupportedException if the implementation does
493 not support cloning
494 */
495 public Object clone() throws CloneNotSupportedException
496 {
497 throw new CloneNotSupportedException();
498 }
499}
Note: See TracBrowser for help on using the repository browser.