J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 | |
| 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 | |
| 43 | /** |
| 44 | * |
| 45 | * @author $Author: raul $ |
| 46 | */ |
| 47 | public 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 | } |