blob: 6fc33aded203f25c290fc92041818dfc0642f2b7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security.keys.content.keyvalues;
22
23
24
25import java.math.BigInteger;
26import java.security.Key;
27import java.security.KeyFactory;
28import java.security.NoSuchAlgorithmException;
29import java.security.PublicKey;
30import java.security.interfaces.RSAPublicKey;
31import java.security.spec.InvalidKeySpecException;
32import java.security.spec.RSAPublicKeySpec;
33
34import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
35import com.sun.org.apache.xml.internal.security.utils.Constants;
36import com.sun.org.apache.xml.internal.security.utils.I18n;
37import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
38import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
39import org.w3c.dom.Document;
40import org.w3c.dom.Element;
41
42
43/**
44 *
45 * @author $Author: raul $
46 */
47public class RSAKeyValue extends SignatureElementProxy
48 implements KeyValueContent {
49
50 /** {@link java.util.logging} logging facility */
51 static java.util.logging.Logger log =
52 java.util.logging.Logger.getLogger(
53 RSAKeyValue.class.getName());
54
55 /**
56 * Constructor RSAKeyValue
57 *
58 * @param element
59 * @param BaseURI
60 * @throws XMLSecurityException
61 */
62 public RSAKeyValue(Element element, String BaseURI)
63 throws XMLSecurityException {
64 super(element, BaseURI);
65 }
66
67 /**
68 * Constructor RSAKeyValue
69 *
70 * @param doc
71 * @param modulus
72 * @param exponent
73 */
74 public RSAKeyValue(Document doc, BigInteger modulus, BigInteger exponent) {
75
76 super(doc);
77
78 XMLUtils.addReturnToElement(this._constructionElement);
79 this.addBigIntegerElement(modulus, Constants._TAG_MODULUS);
80 this.addBigIntegerElement(exponent, Constants._TAG_EXPONENT);
81 }
82
83 /**
84 * Constructor RSAKeyValue
85 *
86 * @param doc
87 * @param key
88 * @throws IllegalArgumentException
89 */
90 public RSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
91
92 super(doc);
93
94 XMLUtils.addReturnToElement(this._constructionElement);
95
96 if (key instanceof java.security.interfaces.RSAPublicKey ) {
97 this.addBigIntegerElement(((RSAPublicKey) key).getModulus(),
98 Constants._TAG_MODULUS);
99 this.addBigIntegerElement(((RSAPublicKey) key).getPublicExponent(),
100 Constants._TAG_EXPONENT);
101 } else {
102 Object exArgs[] = { Constants._TAG_RSAKEYVALUE,
103 key.getClass().getName() };
104
105 throw new IllegalArgumentException(I18n
106 .translate("KeyValue.IllegalArgument", exArgs));
107 }
108 }
109
110 /** @inheritDoc */
111 public PublicKey getPublicKey() throws XMLSecurityException {
112
113 try {
114 KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
115
116 // KeyFactory rsaFactory = KeyFactory.getInstance(JCE_RSA);
117 RSAPublicKeySpec rsaKeyspec =
118 new RSAPublicKeySpec(this
119 .getBigIntegerFromChildElement(Constants._TAG_MODULUS, Constants
120 .SignatureSpecNS), this
121 .getBigIntegerFromChildElement(Constants
122 ._TAG_EXPONENT, Constants.SignatureSpecNS));
123 PublicKey pk = rsaFactory.generatePublic(rsaKeyspec);
124
125 return pk;
126 } catch (NoSuchAlgorithmException ex) {
127 throw new XMLSecurityException("empty", ex);
128 } catch (InvalidKeySpecException ex) {
129 throw new XMLSecurityException("empty", ex);
130 }
131 }
132
133 /** @inheritDoc */
134 public String getBaseLocalName() {
135 return Constants._TAG_RSAKEYVALUE;
136 }
137}