1 /*
   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  */
  21 package com.sun.org.apache.xml.internal.security.keys.content.keyvalues;
  22 
  23 import java.math.BigInteger;
  24 import java.security.Key;
  25 import java.security.KeyFactory;
  26 import java.security.NoSuchAlgorithmException;
  27 import java.security.PublicKey;
  28 import java.security.interfaces.RSAPublicKey;
  29 import java.security.spec.InvalidKeySpecException;
  30 import java.security.spec.RSAPublicKeySpec;
  31 
  32 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  33 import com.sun.org.apache.xml.internal.security.utils.Constants;
  34 import com.sun.org.apache.xml.internal.security.utils.I18n;
  35 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
  36 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  37 import org.w3c.dom.Document;
  38 import org.w3c.dom.Element;
  39 
  40 /**
  41  *
  42  * @author $Author: mullan $
  43  */
  44 public class RSAKeyValue extends SignatureElementProxy
  45         implements KeyValueContent {
  46 
  47    /**
  48     * Constructor RSAKeyValue
  49     *
  50     * @param element
  51     * @param BaseURI
  52     * @throws XMLSecurityException
  53     */
  54    public RSAKeyValue(Element element, String BaseURI)
  55            throws XMLSecurityException {
  56       super(element, BaseURI);
  57    }
  58 
  59    /**
  60     * Constructor RSAKeyValue
  61     *
  62     * @param doc
  63     * @param modulus
  64     * @param exponent
  65     */
  66    public RSAKeyValue(Document doc, BigInteger modulus, BigInteger exponent) {
  67 
  68       super(doc);
  69 
  70       XMLUtils.addReturnToElement(this._constructionElement);
  71       this.addBigIntegerElement(modulus, Constants._TAG_MODULUS);
  72       this.addBigIntegerElement(exponent, Constants._TAG_EXPONENT);
  73    }
  74 
  75    /**
  76     * Constructor RSAKeyValue
  77     *
  78     * @param doc
  79     * @param key
  80     * @throws IllegalArgumentException
  81     */
  82    public RSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
  83 
  84       super(doc);
  85 
  86       XMLUtils.addReturnToElement(this._constructionElement);
  87 
  88       if (key instanceof java.security.interfaces.RSAPublicKey ) {
  89          this.addBigIntegerElement(((RSAPublicKey) key).getModulus(),
  90                                    Constants._TAG_MODULUS);
  91          this.addBigIntegerElement(((RSAPublicKey) key).getPublicExponent(),
  92                                    Constants._TAG_EXPONENT);
  93       } else {
  94          Object exArgs[] = { Constants._TAG_RSAKEYVALUE,
  95                              key.getClass().getName() };
  96 
  97          throw new IllegalArgumentException(I18n
  98             .translate("KeyValue.IllegalArgument", exArgs));
  99       }
 100    }
 101 
 102    /** @inheritDoc */
 103    public PublicKey getPublicKey() throws XMLSecurityException {
 104 
 105       try {
 106          KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
 107 
 108          // KeyFactory rsaFactory = KeyFactory.getInstance(JCE_RSA);
 109          RSAPublicKeySpec rsaKeyspec =
 110             new RSAPublicKeySpec(this
 111                .getBigIntegerFromChildElement(Constants._TAG_MODULUS, Constants
 112                .SignatureSpecNS), this
 113                   .getBigIntegerFromChildElement(Constants
 114                      ._TAG_EXPONENT, Constants.SignatureSpecNS));
 115          PublicKey pk = rsaFactory.generatePublic(rsaKeyspec);
 116 
 117          return pk;
 118       } catch (NoSuchAlgorithmException ex) {
 119          throw new XMLSecurityException("empty", ex);
 120       } catch (InvalidKeySpecException ex) {
 121          throw new XMLSecurityException("empty", ex);
 122       }
 123    }
 124 
 125    /** @inheritDoc */
 126    public String getBaseLocalName() {
 127       return Constants._TAG_RSAKEYVALUE;
 128    }
 129 }