KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > algorithms > implementations > SignatureBaseRSA


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.algorithms.implementations;
18
19
20
21 import java.security.InvalidAlgorithmParameterException JavaDoc;
22 import java.security.InvalidKeyException JavaDoc;
23 import java.security.Key JavaDoc;
24 import java.security.NoSuchProviderException JavaDoc;
25 import java.security.PrivateKey JavaDoc;
26 import java.security.PublicKey JavaDoc;
27 import java.security.SecureRandom JavaDoc;
28 import java.security.Signature JavaDoc;
29 import java.security.SignatureException JavaDoc;
30 import java.security.spec.AlgorithmParameterSpec JavaDoc;
31
32 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
33 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi;
34 import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
35 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
36
37
38 /**
39  *
40  * @author $Author: raul $
41  */

42 public abstract class SignatureBaseRSA extends SignatureAlgorithmSpi {
43
44    /** {@link java.util.logging} logging facility */
45     static java.util.logging.Logger JavaDoc log =
46         java.util.logging.Logger.getLogger(SignatureBaseRSA.class.getName());
47
48     /** @inheritDoc */
49    public abstract String JavaDoc engineGetURI();
50
51    /** Field algorithm */
52    private java.security.Signature JavaDoc _signatureAlgorithm = null;
53
54    /**
55     * Constructor SignatureRSA
56     *
57     * @throws XMLSignatureException
58     */

59    public SignatureBaseRSA() throws XMLSignatureException {
60
61       String JavaDoc algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
62
63       if (true)
64         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Created SignatureDSA using " + algorithmID);
65       String JavaDoc provider=JCEMapper.getProviderId();
66       try {
67          if (provider==null) {
68             this._signatureAlgorithm = Signature.getInstance(algorithmID);
69          } else {
70             this._signatureAlgorithm = Signature.getInstance(algorithmID,provider);
71          }
72       } catch (java.security.NoSuchAlgorithmException JavaDoc ex) {
73          Object JavaDoc[] exArgs = { algorithmID,
74                              ex.getLocalizedMessage() };
75
76          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
77       } catch (NoSuchProviderException JavaDoc ex) {
78          Object JavaDoc[] exArgs = { algorithmID,
79                              ex.getLocalizedMessage() };
80
81          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
82     }
83    }
84
85    /** @inheritDoc */
86    protected void engineSetParameter(AlgorithmParameterSpec JavaDoc params)
87            throws XMLSignatureException {
88
89       try {
90          this._signatureAlgorithm.setParameter(params);
91       } catch (InvalidAlgorithmParameterException JavaDoc ex) {
92          throw new XMLSignatureException("empty", ex);
93       }
94    }
95
96    /** @inheritDoc */
97    protected boolean engineVerify(byte[] signature)
98            throws XMLSignatureException {
99
100       try {
101          return this._signatureAlgorithm.verify(signature);
102       } catch (SignatureException JavaDoc ex) {
103          throw new XMLSignatureException("empty", ex);
104       }
105    }
106
107    /** @inheritDoc */
108    protected void engineInitVerify(Key JavaDoc publicKey) throws XMLSignatureException {
109
110       if (!(publicKey instanceof PublicKey JavaDoc)) {
111          String JavaDoc supplied = publicKey.getClass().getName();
112          String JavaDoc needed = PublicKey JavaDoc.class.getName();
113          Object JavaDoc exArgs[] = { supplied, needed };
114
115          throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
116                                          exArgs);
117       }
118
119       try {
120          this._signatureAlgorithm.initVerify((PublicKey JavaDoc) publicKey);
121       } catch (InvalidKeyException JavaDoc ex) {
122          throw new XMLSignatureException("empty", ex);
123       }
124    }
125
126    /** @inheritDoc */
127    protected byte[] engineSign() throws XMLSignatureException {
128
129       try {
130          return this._signatureAlgorithm.sign();
131       } catch (SignatureException JavaDoc ex) {
132          throw new XMLSignatureException("empty", ex);
133       }
134    }
135
136    /** @inheritDoc */
137    protected void engineInitSign(Key JavaDoc privateKey, SecureRandom JavaDoc secureRandom)
138            throws XMLSignatureException {
139
140       if (!(privateKey instanceof PrivateKey JavaDoc)) {
141          String JavaDoc supplied = privateKey.getClass().getName();
142          String JavaDoc needed = PrivateKey JavaDoc.class.getName();
143          Object JavaDoc exArgs[] = { supplied, needed };
144
145          throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
146                                          exArgs);
147       }
148
149       try {
150          this._signatureAlgorithm.initSign((PrivateKey JavaDoc) privateKey,
151                                            secureRandom);
152       } catch (InvalidKeyException JavaDoc ex) {
153          throw new XMLSignatureException("empty", ex);
154       }
155    }
156
157    /** @inheritDoc */
158    protected void engineInitSign(Key JavaDoc privateKey) throws XMLSignatureException {
159
160       if (!(privateKey instanceof PrivateKey JavaDoc)) {
161          String JavaDoc supplied = privateKey.getClass().getName();
162          String JavaDoc needed = PrivateKey JavaDoc.class.getName();
163          Object JavaDoc exArgs[] = { supplied, needed };
164
165          throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
166                                          exArgs);
167       }
168
169       try {
170          this._signatureAlgorithm.initSign((PrivateKey JavaDoc) privateKey);
171       } catch (InvalidKeyException JavaDoc ex) {
172          throw new XMLSignatureException("empty", ex);
173       }
174    }
175
176    /** @inheritDoc */
177    protected void engineUpdate(byte[] input) throws XMLSignatureException {
178
179       try {
180          this._signatureAlgorithm.update(input);
181       } catch (SignatureException JavaDoc ex) {
182          throw new XMLSignatureException("empty", ex);
183       }
184    }
185
186    /** @inheritDoc */
187    protected void engineUpdate(byte input) throws XMLSignatureException {
188
189       try {
190          this._signatureAlgorithm.update(input);
191       } catch (SignatureException JavaDoc ex) {
192          throw new XMLSignatureException("empty", ex);
193       }
194    }
195
196    /** @inheritDoc */
197    protected void engineUpdate(byte buf[], int offset, int len)
198            throws XMLSignatureException {
199
200       try {
201          this._signatureAlgorithm.update(buf, offset, len);
202       } catch (SignatureException JavaDoc ex) {
203          throw new XMLSignatureException("empty", ex);
204       }
205    }
206
207    /** @inheritDoc */
208    protected String JavaDoc engineGetJCEAlgorithmString() {
209       return this._signatureAlgorithm.getAlgorithm();
210    }
211
212    /** @inheritDoc */
213    protected String JavaDoc engineGetJCEProviderName() {
214       return this._signatureAlgorithm.getProvider().getName();
215    }
216
217    /** @inheritDoc */
218    protected void engineSetHMACOutputLength(int HMACOutputLength)
219            throws XMLSignatureException {
220       throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC");
221    }
222
223    /** @inheritDoc */
224    protected void engineInitSign(
225            Key JavaDoc signingKey, AlgorithmParameterSpec JavaDoc algorithmParameterSpec)
226               throws XMLSignatureException {
227       throw new XMLSignatureException(
228          "algorithms.CannotUseAlgorithmParameterSpecOnRSA");
229    }
230
231    /**
232     * Class SignatureRSASHA1
233     *
234     * @author $Author: raul $
235     * @version $Revision: 1.12 $
236     */

237    public static class SignatureRSASHA1 extends SignatureBaseRSA {
238
239       /**
240        * Constructor SignatureRSASHA1
241        *
242        * @throws XMLSignatureException
243        */

244       public SignatureRSASHA1() throws XMLSignatureException {
245          super();
246       }
247
248       /** @inheritDoc */
249       public String JavaDoc engineGetURI() {
250          return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
251       }
252    }
253
254    /**
255     * Class SignatureRSASHA256
256     *
257     * @author $Author: raul $
258     * @version $Revision: 1.12 $
259     */

260    public static class SignatureRSASHA256 extends SignatureBaseRSA {
261
262       /**
263        * Constructor SignatureRSASHA256
264        *
265        * @throws XMLSignatureException
266        */

267       public SignatureRSASHA256() throws XMLSignatureException {
268          super();
269       }
270
271       /** @inheritDoc */
272       public String JavaDoc engineGetURI() {
273          return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256;
274       }
275    }
276
277    /**
278     * Class SignatureRSASHA384
279     *
280     * @author $Author: raul $
281     * @version $Revision: 1.12 $
282     */

283    public static class SignatureRSASHA384 extends SignatureBaseRSA {
284
285       /**
286        * Constructor SignatureRSASHA384
287        *
288        * @throws XMLSignatureException
289        */

290       public SignatureRSASHA384() throws XMLSignatureException {
291          super();
292       }
293
294       /** @inheritDoc */
295       public String JavaDoc engineGetURI() {
296          return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA384;
297       }
298    }
299
300    /**
301     * Class SignatureRSASHA512
302     *
303     * @author $Author: raul $
304     * @version $Revision: 1.12 $
305     */

306    public static class SignatureRSASHA512 extends SignatureBaseRSA {
307
308       /**
309        * Constructor SignatureRSASHA512
310        *
311        * @throws XMLSignatureException
312        */

313       public SignatureRSASHA512() throws XMLSignatureException {
314          super();
315       }
316
317       /** @inheritDoc */
318       public String JavaDoc engineGetURI() {
319          return XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA512;
320       }
321    }
322
323    /**
324     * Class SignatureRSARIPEMD160
325     *
326     * @author $Author: raul $
327     * @version $Revision: 1.12 $
328     */

329    public static class SignatureRSARIPEMD160 extends SignatureBaseRSA {
330
331       /**
332        * Constructor SignatureRSARIPEMD160
333        *
334        * @throws XMLSignatureException
335        */

336       public SignatureRSARIPEMD160() throws XMLSignatureException {
337          super();
338       }
339
340       /** @inheritDoc */
341       public String JavaDoc engineGetURI() {
342          return XMLSignature.ALGO_ID_SIGNATURE_RSA_RIPEMD160;
343       }
344    }
345
346    /**
347     * Class SignatureRSAMD5
348     *
349     * @author $Author: raul $
350     * @version $Revision: 1.12 $
351     */

352    public static class SignatureRSAMD5 extends SignatureBaseRSA {
353
354       /**
355        * Constructor SignatureRSAMD5
356        *
357        * @throws XMLSignatureException
358        */

359       public SignatureRSAMD5() throws XMLSignatureException {
360          super();
361       }
362
363       /** @inheritDoc */
364       public String JavaDoc engineGetURI() {
365          return XMLSignature.ALGO_ID_SIGNATURE_NOT_RECOMMENDED_RSA_MD5;
366       }
367    }
368 }
369
Popular Tags