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