KickJava   Java API By Example, From Geeks To Geeks.

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


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.RSAPublicKey JavaDoc;
27 import java.security.spec.InvalidKeySpecException JavaDoc;
28 import java.security.spec.RSAPublicKeySpec 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 RSAKeyValue 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(
49             RSAKeyValue.class.getName());
50
51    /**
52     * Constructor RSAKeyValue
53     *
54     * @param element
55     * @param BaseURI
56     * @throws XMLSecurityException
57     */

58    public RSAKeyValue(Element JavaDoc element, String JavaDoc BaseURI)
59            throws XMLSecurityException {
60       super(element, BaseURI);
61    }
62
63    /**
64     * Constructor RSAKeyValue
65     *
66     * @param doc
67     * @param modulus
68     * @param exponent
69     */

70    public RSAKeyValue(Document JavaDoc doc, BigInteger JavaDoc modulus, BigInteger JavaDoc exponent) {
71
72       super(doc);
73
74       XMLUtils.addReturnToElement(this._constructionElement);
75       this.addBigIntegerElement(modulus, Constants._TAG_MODULUS);
76       this.addBigIntegerElement(exponent, Constants._TAG_EXPONENT);
77    }
78
79    /**
80     * Constructor RSAKeyValue
81     *
82     * @param doc
83     * @param key
84     * @throws IllegalArgumentException
85     */

86    public RSAKeyValue(Document JavaDoc doc, Key JavaDoc key) throws IllegalArgumentException JavaDoc {
87
88       super(doc);
89
90       XMLUtils.addReturnToElement(this._constructionElement);
91
92       if (key instanceof java.security.interfaces.RSAPublicKey JavaDoc ) {
93          this.addBigIntegerElement(((RSAPublicKey JavaDoc) key).getModulus(),
94                                    Constants._TAG_MODULUS);
95          this.addBigIntegerElement(((RSAPublicKey JavaDoc) key).getPublicExponent(),
96                                    Constants._TAG_EXPONENT);
97       } else {
98          Object JavaDoc exArgs[] = { Constants._TAG_RSAKEYVALUE,
99                              key.getClass().getName() };
100
101          throw new IllegalArgumentException JavaDoc(I18n
102             .translate("KeyValue.IllegalArgument", exArgs));
103       }
104    }
105
106    /** @inheritDoc */
107    public PublicKey JavaDoc getPublicKey() throws XMLSecurityException {
108
109       try {
110          KeyFactory JavaDoc rsaFactory = KeyFactory.getInstance("RSA");
111
112          // KeyFactory rsaFactory = KeyFactory.getInstance(JCE_RSA);
113
RSAPublicKeySpec JavaDoc rsaKeyspec =
114             new RSAPublicKeySpec JavaDoc(this
115                .getBigIntegerFromChildElement(Constants._TAG_MODULUS, Constants
116                .SignatureSpecNS), this
117                   .getBigIntegerFromChildElement(Constants
118                      ._TAG_EXPONENT, Constants.SignatureSpecNS));
119          PublicKey JavaDoc pk = rsaFactory.generatePublic(rsaKeyspec);
120
121          return pk;
122       } catch (NoSuchAlgorithmException JavaDoc ex) {
123          throw new XMLSecurityException("empty", ex);
124       } catch (InvalidKeySpecException JavaDoc ex) {
125          throw new XMLSecurityException("empty", ex);
126       }
127    }
128
129    /** @inheritDoc */
130    public String JavaDoc getBaseLocalName() {
131       return Constants._TAG_RSAKEYVALUE;
132    }
133 }
134
Popular Tags