KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > SignatureSpi


1 /*
2  * @(#)SignatureSpi.java 1.24 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7   
8 package java.security;
9
10 import java.security.spec.AlgorithmParameterSpec JavaDoc;
11 import java.util.*;
12 import java.io.*;
13
14 import java.nio.ByteBuffer JavaDoc;
15
16 import sun.security.jca.JCAUtil;
17
18 /**
19  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
20  * for the <code>Signature</code> class, which is used to provide the
21  * functionality of a digital signature algorithm. Digital signatures are used
22  * for authentication and integrity assurance of digital data.
23  *.
24  * <p> All the abstract methods in this class must be implemented by each
25  * cryptographic service provider who wishes to supply the implementation
26  * of a particular signature algorithm.
27  *
28  * @author Benjamin Renaud
29  *
30  * @version 1.24, 05/18/04
31  *
32  * @see Signature
33  */

34
35 public abstract class SignatureSpi {
36
37     /**
38      * Application-specified source of randomness.
39      */

40     protected SecureRandom JavaDoc appRandom = null;
41
42     /**
43      * Initializes this signature object with the specified
44      * public key for verification operations.
45      *
46      * @param publicKey the public key of the identity whose signature is
47      * going to be verified.
48      *
49      * @exception InvalidKeyException if the key is improperly
50      * encoded, parameters are missing, and so on.
51      */

52     protected abstract void engineInitVerify(PublicKey JavaDoc publicKey)
53     throws InvalidKeyException JavaDoc;
54
55     /**
56      * Initializes this signature object with the specified
57      * private key for signing operations.
58      *
59      * @param privateKey the private key of the identity whose signature
60      * will be generated.
61      *
62      * @exception InvalidKeyException if the key is improperly
63      * encoded, parameters are missing, and so on.
64      */

65     protected abstract void engineInitSign(PrivateKey JavaDoc privateKey)
66     throws InvalidKeyException JavaDoc;
67
68     /**
69      * Initializes this signature object with the specified
70      * private key and source of randomness for signing operations.
71      *
72      * <p>This concrete method has been added to this previously-defined
73      * abstract class. (For backwards compatibility, it cannot be abstract.)
74      *
75      * @param privateKey the private key of the identity whose signature
76      * will be generated.
77      * @param random the source of randomness
78      *
79      * @exception InvalidKeyException if the key is improperly
80      * encoded, parameters are missing, and so on.
81      */

82     protected void engineInitSign(PrivateKey JavaDoc privateKey,
83                   SecureRandom JavaDoc random)
84     throws InvalidKeyException JavaDoc {
85         this.appRandom = random;
86         engineInitSign(privateKey);
87     }
88
89     /**
90      * Updates the data to be signed or verified
91      * using the specified byte.
92      *
93      * @param b the byte to use for the update.
94      *
95      * @exception SignatureException if the engine is not initialized
96      * properly.
97      */

98     protected abstract void engineUpdate(byte b) throws SignatureException JavaDoc;
99
100     /**
101      * Updates the data to be signed or verified, using the
102      * specified array of bytes, starting at the specified offset.
103      *
104      * @param b the array of bytes
105      * @param off the offset to start from in the array of bytes
106      * @param len the number of bytes to use, starting at offset
107      *
108      * @exception SignatureException if the engine is not initialized
109      * properly
110      */

111     protected abstract void engineUpdate(byte[] b, int off, int len)
112         throws SignatureException JavaDoc;
113
114     /**
115      * Updates the data to be signed or verified using the specified
116      * ByteBuffer. Processes the <code>data.remaining()</code> bytes
117      * starting at at <code>data.position()</code>.
118      * Upon return, the buffer's position will be equal to its limit;
119      * its limit will not have changed.
120      *
121      * @param input the ByteBuffer
122      * @since 1.5
123      */

124     protected void engineUpdate(ByteBuffer JavaDoc input) {
125     if (input.hasRemaining() == false) {
126         return;
127     }
128     try {
129         if (input.hasArray()) {
130         byte[] b = input.array();
131         int ofs = input.arrayOffset();
132         int pos = input.position();
133         int lim = input.limit();
134         engineUpdate(b, ofs + pos, lim - pos);
135         input.position(lim);
136         } else {
137         int len = input.remaining();
138         byte[] b = new byte[JCAUtil.getTempArraySize(len)];
139         while (len > 0) {
140             int chunk = Math.min(len, b.length);
141             input.get(b, 0, chunk);
142             engineUpdate(b, 0, chunk);
143             len -= chunk;
144         }
145         }
146     } catch (SignatureException JavaDoc e) {
147         // is specified to only occur when the engine is not initialized
148
// this case should never occur as it is caught in Signature.java
149
throw new ProviderException JavaDoc("update() failed", e);
150     }
151     }
152
153     /**
154      * Returns the signature bytes of all the data
155      * updated so far.
156      * The format of the signature depends on the underlying
157      * signature scheme.
158      *
159      * @return the signature bytes of the signing operation's result.
160      *
161      * @exception SignatureException if the engine is not
162      * initialized properly or if this signature algorithm is unable to
163      * process the input data provided.
164      */

165     protected abstract byte[] engineSign() throws SignatureException JavaDoc;
166
167     /**
168      * Finishes this signature operation and stores the resulting signature
169      * bytes in the provided buffer <code>outbuf</code>, starting at
170      * <code>offset</code>.
171      * The format of the signature depends on the underlying
172      * signature scheme.
173      *
174      * <p>The signature implementation is reset to its initial state
175      * (the state it was in after a call to one of the
176      * <code>engineInitSign</code> methods)
177      * and can be reused to generate further signatures with the same private
178      * key.
179      *
180      * This method should be abstract, but we leave it concrete for
181      * binary compatibility. Knowledgeable providers should override this
182      * method.
183      *
184      * @param outbuf buffer for the signature result.
185      *
186      * @param offset offset into <code>outbuf</code> where the signature is
187      * stored.
188      *
189      * @param len number of bytes within <code>outbuf</code> allotted for the
190      * signature.
191      * Both this default implementation and the SUN provider do not
192      * return partial digests. If the value of this parameter is less
193      * than the actual signature length, this method will throw a
194      * SignatureException.
195      * This parameter is ignored if its value is greater than or equal to
196      * the actual signature length.
197      *
198      * @return the number of bytes placed into <code>outbuf</code>
199      *
200      * @exception SignatureException if the engine is not
201      * initialized properly, if this signature algorithm is unable to
202      * process the input data provided, or if <code>len</code> is less
203      * than the actual signature length.
204      *
205      * @since 1.2
206      */

207     protected int engineSign(byte[] outbuf, int offset, int len)
208                     throws SignatureException JavaDoc {
209     byte[] sig = engineSign();
210     if (len < sig.length) {
211         throw new SignatureException JavaDoc
212             ("partial signatures not returned");
213     }
214     if (outbuf.length - offset < sig.length) {
215         throw new SignatureException JavaDoc
216             ("insufficient space in the output buffer to store the "
217              + "signature");
218     }
219     System.arraycopy(sig, 0, outbuf, offset, sig.length);
220     return sig.length;
221     }
222
223     /**
224      * Verifies the passed-in signature.
225      *
226      * @param sigBytes the signature bytes to be verified.
227      *
228      * @return true if the signature was verified, false if not.
229      *
230      * @exception SignatureException if the engine is not
231      * initialized properly, the passed-in signature is improperly
232      * encoded or of the wrong type, if this signature algorithm is unable to
233      * process the input data provided, etc.
234      */

235     protected abstract boolean engineVerify(byte[] sigBytes)
236     throws SignatureException JavaDoc;
237
238     /**
239      * Verifies the passed-in signature in the specified array
240      * of bytes, starting at the specified offset.
241      *
242      * <p> Note: Subclasses should overwrite the default implementation.
243      *
244      *
245      * @param sigBytes the signature bytes to be verified.
246      * @param offset the offset to start from in the array of bytes.
247      * @param length the number of bytes to use, starting at offset.
248      *
249      * @return true if the signature was verified, false if not.
250      *
251      * @exception SignatureException if the engine is not
252      * initialized properly, the passed-in signature is improperly
253      * encoded or of the wrong type, if this signature algorithm is unable to
254      * process the input data provided, etc.
255      */

256     protected boolean engineVerify(byte[] sigBytes, int offset, int length)
257     throws SignatureException JavaDoc {
258     byte[] sigBytesCopy = new byte[length];
259     System.arraycopy(sigBytes, offset, sigBytesCopy, 0, length);
260     return engineVerify(sigBytesCopy);
261     }
262
263     /**
264      * Sets the specified algorithm parameter to the specified
265      * value. This method supplies a general-purpose mechanism through
266      * which it is possible to set the various parameters of this object.
267      * A parameter may be any settable parameter for the algorithm, such as
268      * a parameter size, or a source of random bits for signature generation
269      * (if appropriate), or an indication of whether or not to perform
270      * a 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 identifier of the parameter.
275      *
276      * @param value the parameter value.
277      *
278      * @exception InvalidParameterException if <code>param</code> is an
279      * invalid parameter for this signature algorithm engine,
280      * the parameter is already set
281      * and cannot be set again, a security exception occurs, and so on.
282      *
283      * @deprecated Replaced by {@link
284      * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
285      * engineSetParameter}.
286      */

287     @Deprecated JavaDoc
288     protected abstract void engineSetParameter(String JavaDoc param, Object JavaDoc value)
289     throws InvalidParameterException JavaDoc;
290
291     /**
292      * <p>This method is overridden by providers to initialize
293      * this signature engine with the specified parameter set.
294      *
295      * @param params the parameters
296      *
297      * @exception UnsupportedOperationException if this method is not
298      * overridden by a provider
299      *
300      * @exception InvalidAlgorithmParameterException if this method is
301      * overridden by a provider and the the given parameters
302      * are inappropriate for this signature engine
303      */

304     protected void engineSetParameter(AlgorithmParameterSpec JavaDoc params)
305     throws InvalidAlgorithmParameterException JavaDoc {
306         throw new UnsupportedOperationException JavaDoc();
307     }
308
309     /**
310      * <p>This method is overridden by providers to return the
311      * parameters used with this signature engine, or null
312      * if this signature engine does not use any parameters.
313      *
314      * <p>The returned parameters may be the same that were used to initialize
315      * this signature engine, or may contain a combination of default and
316      * randomly generated parameter values used by the underlying signature
317      * implementation if this signature engine requires algorithm parameters
318      * but was not initialized with any.
319      *
320      * @return the parameters used with this signature engine, or null if this
321      * signature engine does not use any parameters
322      *
323      * @exception UnsupportedOperationException if this method is
324      * not overridden by a provider
325      */

326     protected AlgorithmParameters JavaDoc engineGetParameters() {
327     throw new UnsupportedOperationException JavaDoc();
328     }
329
330     /**
331      * Gets the value of the specified algorithm parameter.
332      * This method supplies a general-purpose mechanism through which it
333      * is possible to get the various parameters of this object. A parameter
334      * may be any settable parameter for the algorithm, such as a parameter
335      * size, or a source of random bits for signature generation (if
336      * appropriate), or an indication of whether or not to perform a
337      * specific but optional computation. A uniform algorithm-specific
338      * naming scheme for each parameter is desirable but left unspecified
339      * at this time.
340      *
341      * @param param the string name of the parameter.
342      *
343      * @return the object that represents the parameter value, or null if
344      * there is none.
345      *
346      * @exception InvalidParameterException if <code>param</code> is an
347      * invalid parameter for this engine, or another exception occurs while
348      * trying to get this parameter.
349      *
350      * @deprecated
351      */

352     @Deprecated JavaDoc
353     protected abstract Object JavaDoc engineGetParameter(String JavaDoc param)
354     throws InvalidParameterException JavaDoc;
355
356     /**
357      * Returns a clone if the implementation is cloneable.
358      *
359      * @return a clone if the implementation is cloneable.
360      *
361      * @exception CloneNotSupportedException if this is called
362      * on an implementation that does not support <code>Cloneable</code>.
363      */

364     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
365     if (this instanceof Cloneable JavaDoc) {
366         return super.clone();
367     } else {
368         throw new CloneNotSupportedException JavaDoc();
369     }
370     }
371 }
372     
373         
374
375
376
377         
378         
379     
380
Popular Tags