KickJava   Java API By Example, From Geeks To Geeks.

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


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;
18
19
20
21 import java.security.PublicKey JavaDoc;
22
23
24 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
25 import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.DSAKeyValue;
26 import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.RSAKeyValue;
27 import com.sun.org.apache.xml.internal.security.utils.Constants;
28 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
29 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33
34 /**
35  * The KeyValue element contains a single public key that may be useful in
36  * validating the signature. Structured formats for defining DSA (REQUIRED)
37  * and RSA (RECOMMENDED) public keys are defined in Signature Algorithms
38  * (section 6.4). The KeyValue element may include externally defined public
39  * keys values represented as PCDATA or element types from an external namespace.
40  *
41  * @author $Author: vishal $
42  */

43 public class KeyValue extends SignatureElementProxy implements KeyInfoContent {
44
45    /** {@link java.util.logging} logging facility */
46     static java.util.logging.Logger JavaDoc log =
47         java.util.logging.Logger.getLogger(KeyValue.class.getName());
48
49    /**
50     * Constructor KeyValue
51     *
52     * @param doc
53     * @param dsaKeyValue
54     */

55    public KeyValue(Document JavaDoc doc, DSAKeyValue dsaKeyValue) {
56
57       super(doc);
58
59       XMLUtils.addReturnToElement(this._constructionElement);
60       this._constructionElement.appendChild(dsaKeyValue.getElement());
61       XMLUtils.addReturnToElement(this._constructionElement);
62    }
63
64    /**
65     * Constructor KeyValue
66     *
67     * @param doc
68     * @param rsaKeyValue
69     */

70    public KeyValue(Document JavaDoc doc, RSAKeyValue rsaKeyValue) {
71
72       super(doc);
73
74       XMLUtils.addReturnToElement(this._constructionElement);
75       this._constructionElement.appendChild(rsaKeyValue.getElement());
76       XMLUtils.addReturnToElement(this._constructionElement);
77    }
78
79    /**
80     * Constructor KeyValue
81     *
82     * @param doc
83     * @param unknownKeyValue
84     */

85    public KeyValue(Document JavaDoc doc, Element JavaDoc unknownKeyValue) {
86
87       super(doc);
88
89       XMLUtils.addReturnToElement(this._constructionElement);
90       this._constructionElement.appendChild(unknownKeyValue);
91       XMLUtils.addReturnToElement(this._constructionElement);
92    }
93
94    /**
95     * Constructor KeyValue
96     *
97     * @param doc
98     * @param pk
99     */

100    public KeyValue(Document JavaDoc doc, PublicKey JavaDoc pk) {
101
102       super(doc);
103
104       XMLUtils.addReturnToElement(this._constructionElement);
105
106       if (pk instanceof java.security.interfaces.DSAPublicKey JavaDoc) {
107          DSAKeyValue dsa = new DSAKeyValue(this._doc, pk);
108
109          this._constructionElement.appendChild(dsa.getElement());
110          XMLUtils.addReturnToElement(this._constructionElement);
111       } else if (pk instanceof java.security.interfaces.RSAPublicKey JavaDoc) {
112          RSAKeyValue rsa = new RSAKeyValue(this._doc, pk);
113
114          this._constructionElement.appendChild(rsa.getElement());
115          XMLUtils.addReturnToElement(this._constructionElement);
116       }
117    }
118
119    /**
120     * Constructor KeyValue
121     *
122     * @param element
123     * @param BaseURI
124     * @throws XMLSecurityException
125     */

126    public KeyValue(Element JavaDoc element, String JavaDoc BaseURI)
127            throws XMLSecurityException {
128       super(element, BaseURI);
129    }
130
131    /**
132     * Method getPublicKey
133     *
134     * @return the public key
135     * @throws XMLSecurityException
136     */

137    public PublicKey JavaDoc getPublicKey() throws XMLSecurityException {
138
139       
140          Element JavaDoc rsa = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
141                         Constants._TAG_RSAKEYVALUE,0);
142          
143          if (rsa != null) {
144             RSAKeyValue kv = new RSAKeyValue(rsa,
145                                              this._baseURI);
146
147             return kv.getPublicKey();
148          }
149
150          Element JavaDoc dsa = XMLUtils.selectDsNode(this._constructionElement,
151                  Constants._TAG_DSAKEYVALUE,0);
152             
153
154          if (dsa != null) {
155             DSAKeyValue kv = new DSAKeyValue(dsa,
156                                              this._baseURI);
157
158             return kv.getPublicKey();
159          }
160       
161
162       return null;
163    }
164
165    /** @inheritDoc */
166    public String JavaDoc getBaseLocalName() {
167       return Constants._TAG_KEYVALUE;
168    }
169 }
170
Popular Tags