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 org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 
41 public class RSAKeyValue extends SignatureElementProxy implements KeyValueContent {
42 
43     /**
44      * Constructor RSAKeyValue
45      *
46      * @param element
47      * @param baseURI
48      * @throws XMLSecurityException
49      */
RSAKeyValue(Element element, String baseURI)50     public RSAKeyValue(Element element, String baseURI) throws XMLSecurityException {
51         super(element, baseURI);
52     }
53 
54     /**
55      * Constructor RSAKeyValue
56      *
57      * @param doc
58      * @param modulus
59      * @param exponent
60      */
RSAKeyValue(Document doc, BigInteger modulus, BigInteger exponent)61     public RSAKeyValue(Document doc, BigInteger modulus, BigInteger exponent) {
62         super(doc);
63 
64         addReturnToSelf();
65         this.addBigIntegerElement(modulus, Constants._TAG_MODULUS);
66         this.addBigIntegerElement(exponent, Constants._TAG_EXPONENT);
67     }
68 
69     /**
70      * Constructor RSAKeyValue
71      *
72      * @param doc
73      * @param key
74      * @throws IllegalArgumentException
75      */
RSAKeyValue(Document doc, Key key)76     public RSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
77         super(doc);
78 
79         addReturnToSelf();
80 
81         if (key instanceof RSAPublicKey ) {
82             this.addBigIntegerElement(
83                 ((RSAPublicKey) key).getModulus(), Constants._TAG_MODULUS
84             );
85             this.addBigIntegerElement(
86                 ((RSAPublicKey) key).getPublicExponent(), Constants._TAG_EXPONENT
87             );
88         } else {
89             Object exArgs[] = { Constants._TAG_RSAKEYVALUE, key.getClass().getName() };
90 
91             throw new IllegalArgumentException(I18n.translate("KeyValue.IllegalArgument", exArgs));
92         }
93     }
94 
95     /** {@inheritDoc} */
getPublicKey()96     public PublicKey getPublicKey() throws XMLSecurityException {
97         try {
98             KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
99 
100             RSAPublicKeySpec rsaKeyspec =
101                 new RSAPublicKeySpec(
102                     this.getBigIntegerFromChildElement(
103                         Constants._TAG_MODULUS, Constants.SignatureSpecNS
104                     ),
105                     this.getBigIntegerFromChildElement(
106                         Constants._TAG_EXPONENT, Constants.SignatureSpecNS
107                     )
108                 );
109             PublicKey pk = rsaFactory.generatePublic(rsaKeyspec);
110 
111             return pk;
112         } catch (NoSuchAlgorithmException ex) {
113             throw new XMLSecurityException(ex);
114         } catch (InvalidKeySpecException ex) {
115             throw new XMLSecurityException(ex);
116         }
117     }
118 
119     /** {@inheritDoc} */
getBaseLocalName()120     public String getBaseLocalName() {
121         return Constants._TAG_RSAKEYVALUE;
122     }
123 }
124