KickJava   Java API By Example, From Geeks To Geeks.

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


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;
18
19
20 import java.security.Key JavaDoc;
21 import java.security.SecureRandom JavaDoc;
22 import java.security.spec.AlgorithmParameterSpec JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import com.sun.org.apache.xml.internal.security.algorithms.implementations.IntegrityHmac;
26 import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException;
27 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
29 import com.sun.org.apache.xml.internal.security.utils.Constants;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33
34 /**
35  * Allows selection of digital signature's algorithm, private keys, other security parameters, and algorithm's ID.
36  *
37  * @author Christian Geuer-Pollmann
38  */

39 public class SignatureAlgorithm extends Algorithm {
40
41    /** {@link java.util.logging} logging facility */
42     static java.util.logging.Logger JavaDoc log =
43         java.util.logging.Logger.getLogger(SignatureAlgorithm.class.getName());
44
45    /** Field _alreadyInitialized */
46    static boolean _alreadyInitialized = false;
47
48    /** All available algorithm classes are registered here */
49    static HashMap JavaDoc _algorithmHash = null;
50
51    /** Field _signatureAlgorithm */
52    protected SignatureAlgorithmSpi _signatureAlgorithm = null;
53
54    /**
55     * Constructor SignatureAlgorithm
56     *
57     * @param doc
58     * @param algorithmURI
59     * @throws XMLSecurityException
60     */

61    public SignatureAlgorithm(Document JavaDoc doc, String JavaDoc algorithmURI)
62            throws XMLSecurityException {
63
64       super(doc, algorithmURI);
65
66       try {
67          Class JavaDoc implementingClass =
68             SignatureAlgorithm.getImplementingClass(algorithmURI);
69          if (true)
70             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Create URI \"" + algorithmURI + "\" class \""
71                    + implementingClass + "\"");
72
73          this._signatureAlgorithm =
74             (SignatureAlgorithmSpi) implementingClass.newInstance();
75       } catch (IllegalAccessException JavaDoc ex) {
76          Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
77
78          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
79                                          ex);
80       } catch (InstantiationException JavaDoc ex) {
81          Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
82
83          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
84                                          ex);
85       } catch (NullPointerException JavaDoc ex) {
86          Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
87
88          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
89                                          ex);
90       }
91    }
92
93    /**
94     * Constructor SignatureAlgorithm
95     *
96     * @param doc
97     * @param algorithmURI
98     * @param HMACOutputLength
99     * @throws XMLSecurityException
100     */

101    public SignatureAlgorithm(
102            Document JavaDoc doc, String JavaDoc algorithmURI, int HMACOutputLength)
103               throws XMLSecurityException {
104
105       this(doc, algorithmURI);
106
107       this._signatureAlgorithm.engineSetHMACOutputLength(HMACOutputLength);
108       ((IntegrityHmac)this._signatureAlgorithm)
109          .engineAddContextToElement(this._constructionElement);
110    }
111
112    /**
113     * Constructor SignatureAlgorithm
114     *
115     * @param element
116     * @param BaseURI
117     * @throws XMLSecurityException
118     */

119    public SignatureAlgorithm(Element JavaDoc element, String JavaDoc BaseURI)
120            throws XMLSecurityException {
121
122       super(element, BaseURI);
123
124       String JavaDoc algorithmURI = this.getURI();
125
126       try {
127          Class JavaDoc implementingClass =
128             SignatureAlgorithm.getImplementingClass(algorithmURI);
129          if (true)
130             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Create URI \"" + algorithmURI + "\" class \""
131                    + implementingClass + "\"");
132
133          this._signatureAlgorithm =
134             (SignatureAlgorithmSpi) implementingClass.newInstance();
135
136          this._signatureAlgorithm
137             .engineGetContextFromElement(this._constructionElement);
138       } catch (IllegalAccessException JavaDoc ex) {
139          Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
140
141          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
142                                          ex);
143       } catch (InstantiationException JavaDoc ex) {
144          Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
145
146          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
147                                          ex);
148       } catch (NullPointerException JavaDoc ex) {
149          Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
150
151          throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
152                                          ex);
153       }
154    }
155
156    /**
157     * Proxy method for {@link java.security.Signature#sign()}
158     * which is executed on the internal {@link java.security.Signature} object.
159     *
160     * @return the result of the {@link java.security.Signature#sign()} method
161     * @throws XMLSignatureException
162     */

163    public byte[] sign() throws XMLSignatureException {
164       return this._signatureAlgorithm.engineSign();
165    }
166
167    /**
168     * Proxy method for {@link java.security.Signature#getAlgorithm}
169     * which is executed on the internal {@link java.security.Signature} object.
170     *
171     * @return the result of the {@link java.security.Signature#getAlgorithm} method
172     */

173    public String JavaDoc getJCEAlgorithmString() {
174       return this._signatureAlgorithm.engineGetJCEAlgorithmString();
175    }
176
177    /**
178     * Method getJCEProviderName
179     *
180     * @return The Provider of this Signature Alogrithm
181     */

182    public String JavaDoc getJCEProviderName() {
183       return this._signatureAlgorithm.engineGetJCEProviderName();
184    }
185
186    /**
187     * Proxy method for {@link java.security.Signature#update(byte[])}
188     * which is executed on the internal {@link java.security.Signature} object.
189     *
190     * @param input
191     * @throws XMLSignatureException
192     */

193    public void update(byte[] input) throws XMLSignatureException {
194       this._signatureAlgorithm.engineUpdate(input);
195    }
196
197    /**
198     * Proxy method for {@link java.security.Signature#update(byte)}
199     * which is executed on the internal {@link java.security.Signature} object.
200     *
201     * @param input
202     * @throws XMLSignatureException
203     */

204    public void update(byte input) throws XMLSignatureException {
205       this._signatureAlgorithm.engineUpdate(input);
206    }
207
208    /**
209     * Proxy method for {@link java.security.Signature#update(byte[], int, int)}
210     * which is executed on the internal {@link java.security.Signature} object.
211     *
212     * @param buf
213     * @param offset
214     * @param len
215     * @throws XMLSignatureException
216     */

217    public void update(byte buf[], int offset, int len)
218            throws XMLSignatureException {
219       this._signatureAlgorithm.engineUpdate(buf, offset, len);
220    }
221
222    /**
223     * Proxy method for {@link java.security.Signature#initSign(java.security.PrivateKey)}
224     * which is executed on the internal {@link java.security.Signature} object.
225     *
226     * @param signingKey
227     * @throws XMLSignatureException
228     */

229    public void initSign(Key JavaDoc signingKey) throws XMLSignatureException {
230       this._signatureAlgorithm.engineInitSign(signingKey);
231    }
232
233    /**
234     * Proxy method for {@link java.security.Signature#initSign(java.security.PrivateKey, java.security.SecureRandom)}
235     * which is executed on the internal {@link java.security.Signature} object.
236     *
237     * @param signingKey
238     * @param secureRandom
239     * @throws XMLSignatureException
240     */

241    public void initSign(Key JavaDoc signingKey, SecureRandom JavaDoc secureRandom)
242            throws XMLSignatureException {
243       this._signatureAlgorithm.engineInitSign(signingKey, secureRandom);
244    }
245
246    /**
247     * Proxy method for {@link java.security.Signature#initSign(java.security.PrivateKey)}
248     * which is executed on the internal {@link java.security.Signature} object.
249     *
250     * @param signingKey
251     * @param algorithmParameterSpec
252     * @throws XMLSignatureException
253     */

254    public void initSign(
255            Key JavaDoc signingKey, AlgorithmParameterSpec JavaDoc algorithmParameterSpec)
256               throws XMLSignatureException {
257       this._signatureAlgorithm.engineInitSign(signingKey,
258                                               algorithmParameterSpec);
259    }
260
261    /**
262     * Proxy method for {@link java.security.Signature#setParameter(java.security.spec.AlgorithmParameterSpec)}
263     * which is executed on the internal {@link java.security.Signature} object.
264     *
265     * @param params
266     * @throws XMLSignatureException
267     */

268    public void setParameter(AlgorithmParameterSpec JavaDoc params)
269            throws XMLSignatureException {
270       this._signatureAlgorithm.engineSetParameter(params);
271    }
272
273    /**
274     * Proxy method for {@link java.security.Signature#initVerify(java.security.PublicKey)}
275     * which is executed on the internal {@link java.security.Signature} object.
276     *
277     * @param verificationKey
278     * @throws XMLSignatureException
279     */

280    public void initVerify(Key JavaDoc verificationKey) throws XMLSignatureException {
281       this._signatureAlgorithm.engineInitVerify(verificationKey);
282    }
283
284    /**
285     * Proxy method for {@link java.security.Signature#verify(byte[])}
286     * which is executed on the internal {@link java.security.Signature} object.
287     *
288     * @param signature
289     * @return true if if the signature is valid.
290     *
291     * @throws XMLSignatureException
292     */

293    public boolean verify(byte[] signature) throws XMLSignatureException {
294       return this._signatureAlgorithm.engineVerify(signature);
295    }
296
297    /**
298     * Returns the URI representation of Transformation algorithm
299     *
300     * @return the URI representation of Transformation algorithm
301     */

302    public final String JavaDoc getURI() {
303       return this._constructionElement.getAttributeNS(null,
304               Constants._ATT_ALGORITHM);
305    }
306
307    /**
308     * Initalizes for this {@link com.sun.org.apache.xml.internal.security.transforms.Transform}
309     *
310     */

311    public static void providerInit() {
312
313       if (SignatureAlgorithm.log == null) {
314          SignatureAlgorithm.log =
315             java.util.logging.Logger
316                .getLogger(SignatureAlgorithm.class.getName());
317       }
318
319       if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Init() called");
320
321       if (!SignatureAlgorithm._alreadyInitialized) {
322          SignatureAlgorithm._algorithmHash = new HashMap JavaDoc(10);
323          SignatureAlgorithm._alreadyInitialized = true;
324       }
325    }
326
327    /**
328     * Registers implementing class of the Transform algorithm with algorithmURI
329     *
330     * @param algorithmURI algorithmURI URI representation of <code>Transform algorithm</code>.
331     * @param implementingClass <code>implementingClass</code> the implementing class of {@link SignatureAlgorithmSpi}
332     * @throws AlgorithmAlreadyRegisteredException if specified algorithmURI is already registered
333     * @throws XMLSignatureException
334     */

335    public static void register(String JavaDoc algorithmURI, String JavaDoc implementingClass)
336            throws AlgorithmAlreadyRegisteredException,XMLSignatureException {
337
338       {
339          if (true)
340             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Try to register " + algorithmURI + " " + implementingClass);
341
342          // are we already registered?
343
Class JavaDoc registeredClassClass =
344             SignatureAlgorithm.getImplementingClass(algorithmURI);
345          if (registeredClassClass!=null) {
346              String JavaDoc registeredClass = registeredClassClass.getName();
347
348              if ((registeredClass != null) && (registeredClass.length() != 0)) {
349                  Object JavaDoc exArgs[] = { algorithmURI, registeredClass };
350
351                  throw new AlgorithmAlreadyRegisteredException(
352                          "algorithm.alreadyRegistered", exArgs);
353              }
354          }
355          try {
356              SignatureAlgorithm._algorithmHash.put(algorithmURI, Class.forName(implementingClass));
357           } catch (ClassNotFoundException JavaDoc ex) {
358              Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
359
360              throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
361                                              ex);
362           } catch (NullPointerException JavaDoc ex) {
363              Object JavaDoc exArgs[] = { algorithmURI, ex.getMessage() };
364
365              throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs,
366                                              ex);
367           }
368          
369       }
370    }
371
372    /**
373     * Method getImplementingClass
374     *
375     * @param URI
376     * @return the class that implements the URI
377     */

378    private static Class JavaDoc getImplementingClass(String JavaDoc URI) {
379
380       if (SignatureAlgorithm._algorithmHash == null) {
381          return null;
382       }
383
384       return (Class JavaDoc) SignatureAlgorithm._algorithmHash.get(URI);
385    }
386
387    /**
388     * Method getBaseNamespace
389     *
390     * @return URI of this element
391     */

392    public String JavaDoc getBaseNamespace() {
393       return Constants.SignatureSpecNS;
394    }
395
396    /**
397     * Method getBaseLocalName
398     *
399     * @return Local name
400     */

401    public String JavaDoc getBaseLocalName() {
402       return Constants._TAG_SIGNATUREMETHOD;
403    }
404 }
405
Popular Tags