2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / security / Signature.java
blob1e99a7bb3e0552aba0800e111b39ec011f82ff72
1 /* Signature.java --- Signature Class
2 Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.security;
40 import java.security.cert.Certificate;
41 import java.security.cert.X509Certificate;
42 import java.security.spec.AlgorithmParameterSpec;
44 import gnu.java.security.Engine;
46 /**
47 * <p>This <code>Signature</code> class is used to provide applications the
48 * functionality of a digital signature algorithm. Digital signatures are used
49 * for authentication and integrity assurance of digital data.</p>
51 * <p>The signature algorithm can be, among others, the NIST standard <i>DSS</i>,
52 * using <i>DSA</i> and <i>SHA-1</i>. The <i>DSA</i> algorithm using the
53 * <i>SHA-1</i> message digest algorithm can be specified as <code>SHA1withDSA
54 * </code>. In the case of <i>RSA</i>, there are multiple choices for the
55 * message digest algorithm, so the signing algorithm could be specified as, for
56 * example, <code>MD2withRSA</code>, <code>MD5withRSA</code>, or
57 * <code>SHA1withRSA</code>. The algorithm name must be specified, as there is
58 * no default.</p>
60 * <p>Like other algorithm-based classes in Java Security, <code>Signature</code>
61 * provides implementation-independent algorithms, whereby a caller (application
62 * code) requests a particular signature algorithm and is handed back a properly
63 * initialized <code>Signature</code> object. It is also possible, if desired,
64 * to request a particular algorithm from a particular provider. See the
65 * <code>getInstance()</code> methods.</p>
67 * <p>Thus, there are two ways to request a <code>Signature</code> algorithm
68 * object: by specifying either just an algorithm name, or both an algorithm
69 * name and a package provider.</p>
71 * <p>If just an algorithm name is specified, the system will determine if there
72 * is an implementation of the algorithm requested available in the environment,
73 * and if there is more than one, if there is a preferred one.</p>
75 * <p>If both an algorithm name and a package provider are specified, the system
76 * will determine if there is an implementation of the algorithm in the package
77 * requested, and throw an exception if there is not.</p>
79 * <p>A <code>Signature</code> object can be used to generate and verify digital
80 * signatures.</p>
82 * <p>There are three phases to the use of a <code>Signature</code> object for
83 * either signing data or verifying a signature:</p>
85 * <ol>
86 * <li>Initialization, with either
87 * <ul>
88 * <li>a public key, which initializes the signature for verification
89 * (see <code>initVerify()</code>), or</li>
90 * <li>a private key (and optionally a Secure Random Number Generator),
91 * which initializes the signature for signing (see
92 * {@link #initSign(PrivateKey)} and {@link #initSign(PrivateKey, SecureRandom)}
93 * ).</li>
94 * </ul></li>
95 * <li>Updating<br/>
96 * Depending on the type of initialization, this will update the bytes to
97 * be signed or verified. See the update methods.<br/></li>
98 * <li>Signing or Verifying a signature on all updated bytes. See the
99 * <code>sign()</code> methods and the <code>verify()</code> method.</li>
100 * </ol>
102 * <p>Note that this class is abstract and extends from {@link SignatureSpi} for
103 * historical reasons. Application developers should only take notice of the
104 * methods defined in this <code>Signature</code> class; all the methods in the
105 * superclass are intended for cryptographic service providers who wish to
106 * supply their own implementations of digital signature algorithms.
108 * @author Mark Benvenuto <ivymccough@worldnet.att.net>
110 public abstract class Signature extends SignatureSpi
112 /** Service name for signatures. */
113 private static final String SIGNATURE = "Signature";
116 * Possible <code>state</code> value, signifying that this signature object
117 * has not yet been initialized.
119 protected static final int UNINITIALIZED = 0;
121 // Constructor.
122 // ------------------------------------------------------------------------
125 * Possible <code>state</code> value, signifying that this signature object
126 * has been initialized for signing.
128 protected static final int SIGN = 2;
131 * Possible <code>state</code> value, signifying that this signature object
132 * has been initialized for verification.
134 protected static final int VERIFY = 3;
136 /** Current state of this signature object. */
137 protected int state = UNINITIALIZED;
139 private String algorithm;
140 Provider provider;
143 * Creates a <code>Signature</code> object for the specified algorithm.
145 * @param algorithm the standard string name of the algorithm. See Appendix A
146 * in the Java Cryptography Architecture API Specification &amp; Reference for
147 * information about standard algorithm names.
149 protected Signature(String algorithm)
151 this.algorithm = algorithm;
152 state = UNINITIALIZED;
156 * Generates a <code>Signature</code> object that implements the specified
157 * digest algorithm. If the default provider package provides an
158 * implementation of the requested digest algorithm, an instance of
159 * <code>Signature</code> containing that implementation is returned. If the
160 * algorithm is not available in the default package, other packages are
161 * searched.
163 * @param algorithm the standard name of the algorithm requested. See Appendix
164 * A in the Java Cryptography Architecture API Specification &amp; Reference
165 * for information about standard algorithm names.
166 * @return the new Signature object.
167 * @throws NoSuchAlgorithmException if the algorithm is not available in the
168 * environment.
170 public static Signature getInstance(String algorithm)
171 throws NoSuchAlgorithmException
173 Provider[] p = Security.getProviders();
174 for (int i = 0; i < p.length; i++)
178 return getInstance(algorithm, p[i]);
180 catch (NoSuchAlgorithmException ignored) {}
183 throw new NoSuchAlgorithmException(algorithm);
187 * Generates a <code>Signature</code> object implementing the specified
188 * algorithm, as supplied from the specified provider, if such an algorithm
189 * is available from the provider.
191 * @param algorithm the name of the algorithm requested. See Appendix A in
192 * the Java Cryptography Architecture API Specification &amp; Reference for
193 * information about standard algorithm names.
194 * @param provider the name of the provider.
195 * @return the new <code>Signature</code> object.
196 * @throws NoSuchAlgorithmException if the algorithm is not available in the
197 * package supplied by the requested provider.
198 * @throws NoSuchProviderException if the provider is not available in the
199 * environment.
200 * @throws IllegalArgumentException if the provider name is <code>null</code>
201 * or empty.
202 * @see Provider
204 public static Signature getInstance(String algorithm, String provider)
205 throws NoSuchAlgorithmException, NoSuchProviderException
207 if (provider == null || provider.length() == 0)
208 throw new IllegalArgumentException("Illegal provider");
210 Provider p = Security.getProvider(provider);
211 if (p == null)
212 throw new NoSuchProviderException(provider);
214 return getInstance(algorithm, p);
218 * Generates a <code>Signature</code> object implementing the specified
219 * algorithm, as supplied from the specified provider, if such an algorithm
220 * is available from the provider. Note: the provider doesn't have to be
221 * registered.
223 * @param algorithm the name of the algorithm requested. See Appendix A in
224 * the Java Cryptography Architecture API Specification &amp; Reference for
225 * information about standard algorithm names.
226 * @param provider the provider.
227 * @return the new <code>Signature</code> object.
228 * @throws NoSuchAlgorithmException if the <code>algorithm</code> is not
229 * available in the package supplied by the requested <code>provider</code>.
230 * @throws IllegalArgumentException if the <code>provider</code> is
231 * <code>null</code>.
232 * @since 1.4
233 * @see Provider
235 public static Signature getInstance(String algorithm, Provider provider)
236 throws NoSuchAlgorithmException
238 if (provider == null)
239 throw new IllegalArgumentException("Illegal provider");
241 Signature result = null;
242 Object o = null;
245 o = Engine.getInstance(SIGNATURE, algorithm, provider);
247 catch (java.lang.reflect.InvocationTargetException ite)
249 throw new NoSuchAlgorithmException(algorithm);
252 if (o instanceof SignatureSpi)
254 result = new DummySignature((SignatureSpi) o, algorithm);
256 else if (o instanceof Signature)
258 result = (Signature) o;
259 result.algorithm = algorithm;
261 else
263 throw new NoSuchAlgorithmException(algorithm);
265 result.provider = provider;
266 return result;
270 * Returns the provider of this signature object.
272 * @return the provider of this signature object.
274 public final Provider getProvider()
276 return provider;
280 * Initializes this object for verification. If this method is called again
281 * with a different argument, it negates the effect of this call.
283 * @param publicKey the public key of the identity whose signature is going
284 * to be verified.
285 * @throws InvalidKeyException if the key is invalid.
287 public final void initVerify(PublicKey publicKey) throws InvalidKeyException
289 state = VERIFY;
290 engineInitVerify(publicKey);
294 * <p>Initializes this object for verification, using the public key from the
295 * given certificate.</p>
297 * <p>If the certificate is of type <i>X.509</i> and has a <i>key usage</i>
298 * extension field marked as <i>critical</i>, and the value of the <i>key
299 * usage</i> extension field implies that the public key in the certificate
300 * and its corresponding private key are not supposed to be used for digital
301 * signatures, an {@link InvalidKeyException} is thrown.</p>
303 * @param certificate the certificate of the identity whose signature is
304 * going to be verified.
305 * @throws InvalidKeyException if the public key in the certificate is not
306 * encoded properly or does not include required parameter information or
307 * cannot be used for digital signature purposes.
309 public final void initVerify(Certificate certificate)
310 throws InvalidKeyException
312 state = VERIFY;
313 if (certificate.getType().equals("X509"))
315 X509Certificate cert = (X509Certificate) certificate;
316 boolean[]array = cert.getKeyUsage();
317 if (array != null && array[0] == false)
318 throw new InvalidKeyException(
319 "KeyUsage of this Certificate indicates it cannot be used for digital signing");
321 this.initVerify(certificate.getPublicKey());
325 * Initialize this object for signing. If this method is called again with a
326 * different argument, it negates the effect of this call.
328 * @param privateKey the private key of the identity whose signature is going
329 * to be generated.
330 * @throws InvalidKeyException if the key is invalid.
332 public final void initSign(PrivateKey privateKey) throws InvalidKeyException
334 state = SIGN;
335 engineInitSign(privateKey);
339 * Initialize this object for signing. If this method is called again with a
340 * different argument, it negates the effect of this call.
342 * @param privateKey the private key of the identity whose signature is going
343 * to be generated.
344 * @param random the source of randomness for this signature.
345 * @throws InvalidKeyException if the key is invalid.
347 public final void initSign(PrivateKey privateKey, SecureRandom random)
348 throws InvalidKeyException
350 state = SIGN;
351 engineInitSign(privateKey, random);
355 * <p>Returns the signature bytes of all the data updated. The format of the
356 * signature depends on the underlying signature scheme.</p>
358 * <p>A call to this method resets this signature object to the state it was
359 * in when previously initialized for signing via a call to
360 * <code>initSign(PrivateKey)</code>. That is, the object is reset and
361 * available to generate another signature from the same signer, if desired,
362 * via new calls to <code>update()</code> and <code>sign()</code>.</p>
364 * @return the signature bytes of the signing operation's result.
365 * @throws SignatureException if this signature object is not initialized
366 * properly.
368 public final byte[] sign() throws SignatureException
370 if (state == SIGN)
372 state = UNINITIALIZED;
373 return engineSign();
375 else
376 throw new SignatureException();
380 * <p>Finishes the signature operation and stores the resulting signature
381 * bytes in the provided buffer <code>outbuf</code>, starting at <code>offset
382 * </code>. The format of the signature depends on the underlying signature
383 * scheme.</p>
385 * <p>This signature object is reset to its initial state (the state it was
386 * in after a call to one of the <code>initSign()</code> methods) and can be
387 * reused to generate further signatures with the same private key.</p>
389 * @param outbuf buffer for the signature result.
390 * @param offset offset into outbuf where the signature is stored.
391 * @param len number of bytes within outbuf allotted for the signature.
392 * @return the number of bytes placed into outbuf.
393 * @throws SignatureException if an error occurs or len is less than the
394 * actual signature length.
395 * @since 1.2
397 public final int sign(byte[] outbuf, int offset, int len)
398 throws SignatureException
400 if (state == SIGN)
402 state = UNINITIALIZED;
403 return engineSign(outbuf, offset, len);
405 else
406 throw new SignatureException();
410 * <p>Verifies the passed-in signature.</p>
412 * <p>A call to this method resets this signature object to the state it was
413 * in when previously initialized for verification via a call to
414 * <code>initVerify(PublicKey)</code>. That is, the object is reset and
415 * available to verify another signature from the identity whose public key
416 * was specified in the call to <code>initVerify()</code>.</p>
418 * @param signature the signature bytes to be verified.
419 * @return <code>true</code> if the signature was verified, <code>false</code>
420 * if not.
421 * @throws SignatureException if this signature object is not initialized
422 * properly, or the passed-in signature is improperly encoded or of the wrong
423 * type, etc.
425 public final boolean verify(byte[]signature) throws SignatureException
427 if (state == VERIFY)
429 state = UNINITIALIZED;
430 return engineVerify(signature);
432 else
433 throw new SignatureException();
437 * <p>Verifies the passed-in <code>signature</code> in the specified array of
438 * bytes, starting at the specified <code>offset</code>.</p>
440 * <p>A call to this method resets this signature object to the state it was
441 * in when previously initialized for verification via a call to
442 * <code>initVerify(PublicKey)</code>. That is, the object is reset and
443 * available to verify another signature from the identity whose public key
444 * was specified in the call to <code>initVerify()</code>.</p>
446 * @param signature the signature bytes to be verified.
447 * @param offset the offset to start from in the array of bytes.
448 * @param length the number of bytes to use, starting at offset.
449 * @return <code>true</code> if the signature was verified, <code>false</code>
450 * if not.
451 * @throws SignatureException if this signature object is not initialized
452 * properly, or the passed-in <code>signature</code> is improperly encoded or
453 * of the wrong type, etc.
454 * @throws IllegalArgumentException if the <code>signature</code> byte array
455 * is <code>null</code>, or the <code>offset</code> or <code>length</code> is
456 * less than <code>0</code>, or the sum of the <code>offset</code> and
457 * <code>length</code> is greater than the length of the <code>signature</code>
458 * byte array.
460 public final boolean verify(byte[] signature, int offset, int length)
461 throws SignatureException
463 if (state != VERIFY)
464 throw new SignatureException("illegal state");
466 if (signature == null)
467 throw new IllegalArgumentException("signaure is null");
468 if (offset < 0)
469 throw new IllegalArgumentException("offset is less than 0");
470 if (length < 0)
471 throw new IllegalArgumentException("length is less than 0");
472 if (offset + length < signature.length)
473 throw new IllegalArgumentException("range is out of bounds");
475 state = UNINITIALIZED;
476 return engineVerify(signature, offset, length);
480 * Updates the data to be signed or verified by a byte.
482 * @param b the byte to use for the update.
483 * @throws SignatureException if this signature object is not initialized
484 * properly.
486 public final void update(byte b) throws SignatureException
488 if (state != UNINITIALIZED)
489 engineUpdate(b);
490 else
491 throw new SignatureException();
495 * Updates the data to be signed or verified, using the specified array of
496 * bytes.
498 * @param data the byte array to use for the update.
499 * @throws SignatureException if this signature object is not initialized
500 * properly.
502 public final void update(byte[]data) throws SignatureException
504 if (state != UNINITIALIZED)
505 engineUpdate(data, 0, data.length);
506 else
507 throw new SignatureException();
511 * Updates the data to be signed or verified, using the specified array of
512 * bytes, starting at the specified offset.
514 * @param data the array of bytes.
515 * @param off the offset to start from in the array of bytes.
516 * @param len the number of bytes to use, starting at offset.
517 * @throws SignatureException if this signature object is not initialized
518 * properly.
520 public final void update(byte[]data, int off, int len)
521 throws SignatureException
523 if (state != UNINITIALIZED)
524 engineUpdate(data, off, len);
525 else
526 throw new SignatureException();
530 * Returns the name of the algorithm for this signature object.
532 * @return the name of the algorithm for this signature object.
534 public final String getAlgorithm()
536 return algorithm;
540 * Returns a string representation of this signature object, providing
541 * information that includes the state of the object and the name of the
542 * algorithm used.
544 * @return a string representation of this signature object.
546 public String toString()
548 return (algorithm + " Signature");
552 * Sets the specified algorithm parameter to the specified value. This method
553 * supplies a general-purpose mechanism through which it is possible to set
554 * the various parameters of this object. A parameter may be any settable
555 * parameter for the algorithm, such as a parameter size, or a source of
556 * random bits for signature generation (if appropriate), or an indication of
557 * whether or not to perform a specific but optional computation. A uniform
558 * algorithm-specific naming scheme for each parameter is desirable but left
559 * unspecified at this time.
561 * @param param the string identifier of the parameter.
562 * @param value the parameter value.
563 * @throws InvalidParameterException if param is an invalid parameter for this
564 * signature algorithm engine, the parameter is already set and cannot be set
565 * again, a security exception occurs, and so on.
566 * @see #getParameter(String)
567 * @deprecated Use setParameter(AlgorithmParameterSpec).
569 public final void setParameter(String param, Object value)
570 throws InvalidParameterException
572 engineSetParameter(param, value);
576 * Initializes this signature engine with the specified parameter set.
578 * @param params the parameters.
579 * @throws InvalidAlgorithmParameterException if the given parameters are
580 * inappropriate for this signature engine.
581 * @see #getParameters()
583 public final void setParameter(AlgorithmParameterSpec params)
584 throws InvalidAlgorithmParameterException
586 engineSetParameter(params);
590 * <p>Returns the parameters used with this signature object.</p>
592 * <p>The returned parameters may be the same that were used to initialize
593 * this signature, or may contain a combination of default and randomly
594 * generated parameter values used by the underlying signature implementation
595 * if this signature requires algorithm parameters but was not initialized
596 * with any.
598 * @return the parameters used with this signature, or <code>null</code> if
599 * this signature does not use any parameters.
600 * @see #setParameter(AlgorithmParameterSpec)
602 public final AlgorithmParameters getParameters()
604 return engineGetParameters();
608 * Gets the value of the specified algorithm parameter. This method supplies
609 * a general-purpose mechanism through which it is possible to get the various
610 * parameters of this object. A parameter may be any settable parameter for
611 * the algorithm, such as a parameter size, or a source of random bits for
612 * signature generation (if appropriate), or an indication of whether or not
613 * to perform a specific but optional computation. A uniform
614 * algorithm-specific naming scheme for each parameter is desirable but left
615 * unspecified at this time.
617 * @param param the string name of the parameter.
618 * @return the object that represents the parameter value, or null if there
619 * is none.
620 * @throws InvalidParameterException if param is an invalid parameter for this
621 * engine, or another exception occurs while trying to get this parameter.
622 * @see #setParameter(String, Object)
623 * @deprecated
625 public final Object getParameter(String param)
626 throws InvalidParameterException
628 return engineGetParameter(param);
632 * Returns a clone if the implementation is cloneable.
634 * @return a clone if the implementation is cloneable.
635 * @throws CloneNotSupportedException if this is called on an implementation
636 * that does not support {@link Cloneable}.
638 public Object clone() throws CloneNotSupportedException
640 throw new CloneNotSupportedException();