KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > content > keyvalues > DSAKeyValue


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.keys.content.keyvalues;
18
19
20
21 import java.math.BigInteger JavaDoc;
22 import java.security.Key JavaDoc;
23 import java.security.KeyFactory JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25 import java.security.PublicKey JavaDoc;
26 import java.security.interfaces.DSAPublicKey JavaDoc;
27 import java.security.spec.DSAPublicKeySpec JavaDoc;
28 import java.security.spec.InvalidKeySpecException JavaDoc;
29
30 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
31 import com.sun.org.apache.xml.internal.security.utils.Constants;
32 import com.sun.org.apache.xml.internal.security.utils.I18n;
33 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
34 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38
39 /**
40  *
41  * @author $Author: raul $
42  */

43 public class DSAKeyValue extends SignatureElementProxy
44         implements KeyValueContent {
45
46    /** {@link java.util.logging} logging facility */
47     static java.util.logging.Logger JavaDoc log =
48         java.util.logging.Logger.getLogger(DSAKeyValue.class.getName());
49
50    /**
51     * Constructor DSAKeyValue
52     *
53     * @param element
54     * @param BaseURI
55     * @throws XMLSecurityException
56     */

57    public DSAKeyValue(Element JavaDoc element, String JavaDoc BaseURI)
58            throws XMLSecurityException {
59       super(element, BaseURI);
60    }
61
62    /**
63     * Constructor DSAKeyValue
64     *
65     * @param doc
66     * @param P
67     * @param Q
68     * @param G
69     * @param Y
70     */

71    public DSAKeyValue(Document JavaDoc doc, BigInteger JavaDoc P, BigInteger JavaDoc Q, BigInteger JavaDoc G,
72                       BigInteger JavaDoc Y) {
73
74       super(doc);
75
76       XMLUtils.addReturnToElement(this._constructionElement);
77       this.addBigIntegerElement(P, Constants._TAG_P);
78       this.addBigIntegerElement(Q, Constants._TAG_Q);
79       this.addBigIntegerElement(G, Constants._TAG_G);
80       this.addBigIntegerElement(Y, Constants._TAG_Y);
81    }
82
83    /**
84     * Constructor DSAKeyValue
85     *
86     * @param doc
87     * @param key
88     * @throws IllegalArgumentException
89     */

90    public DSAKeyValue(Document JavaDoc doc, Key JavaDoc key) throws IllegalArgumentException JavaDoc {
91
92       super(doc);
93
94       XMLUtils.addReturnToElement(this._constructionElement);
95
96       if (key instanceof java.security.interfaces.DSAPublicKey JavaDoc) {
97          this.addBigIntegerElement(((DSAPublicKey JavaDoc) key).getParams().getP(),
98                                    Constants._TAG_P);
99          this.addBigIntegerElement(((DSAPublicKey JavaDoc) key).getParams().getQ(),
100                                    Constants._TAG_Q);
101          this.addBigIntegerElement(((DSAPublicKey JavaDoc) key).getParams().getG(),
102                                    Constants._TAG_G);
103          this.addBigIntegerElement(((DSAPublicKey JavaDoc) key).getY(),
104                                    Constants._TAG_Y);
105       } else {
106          Object JavaDoc exArgs[] = { Constants._TAG_DSAKEYVALUE,
107                              key.getClass().getName() };
108
109          throw new IllegalArgumentException JavaDoc(I18n
110             .translate("KeyValue.IllegalArgument", exArgs));
111       }
112    }
113
114    /** @inheritDoc */
115    public PublicKey JavaDoc getPublicKey() throws XMLSecurityException {
116
117       try {
118          DSAPublicKeySpec JavaDoc pkspec =
119             new DSAPublicKeySpec JavaDoc(this
120                .getBigIntegerFromChildElement(Constants._TAG_Y, Constants
121                .SignatureSpecNS), this
122                   .getBigIntegerFromChildElement(Constants._TAG_P, Constants
123                   .SignatureSpecNS), this
124                      .getBigIntegerFromChildElement(Constants._TAG_Q, Constants
125                      .SignatureSpecNS), this
126                         .getBigIntegerFromChildElement(Constants
127                            ._TAG_G, Constants.SignatureSpecNS));
128          KeyFactory JavaDoc dsaFactory = KeyFactory.getInstance("DSA");
129          PublicKey JavaDoc pk = dsaFactory.generatePublic(pkspec);
130
131          return pk;
132       } catch (NoSuchAlgorithmException JavaDoc ex) {
133          throw new XMLSecurityException("empty", ex);
134       } catch (InvalidKeySpecException JavaDoc ex) {
135          throw new XMLSecurityException("empty", ex);
136       }
137    }
138
139    /** @inheritDoc */
140    public String JavaDoc getBaseLocalName() {
141       return Constants._TAG_DSAKEYVALUE;
142    }
143 }
144
Popular Tags