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;
  22 
  23 import java.security.PublicKey;
  24 
  25 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  26 import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.DSAKeyValue;
  27 import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.RSAKeyValue;
  28 import com.sun.org.apache.xml.internal.security.utils.Constants;
  29 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
  30 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  31 import org.w3c.dom.Document;
  32 import org.w3c.dom.Element;
  33 
  34 /**
  35  * The KeyValue element contains a single public key that may be useful in
  36  * validating the signature. Structured formats for defining DSA (REQUIRED)
  37  * and RSA (RECOMMENDED) public keys are defined in Signature Algorithms
  38  * (section 6.4). The KeyValue element may include externally defined public
  39  * keys values represented as PCDATA or element types from an external
  40  * namespace.
  41  *
  42  * @author $Author: mullan $
  43  */
  44 public class KeyValue extends SignatureElementProxy implements KeyInfoContent {
  45 
  46     /**
  47      * Constructor KeyValue
  48      *
  49      * @param doc
  50      * @param dsaKeyValue
  51      */
  52     public KeyValue(Document doc, DSAKeyValue dsaKeyValue) {
  53 
  54         super(doc);
  55 
  56         XMLUtils.addReturnToElement(this._constructionElement);
  57         this._constructionElement.appendChild(dsaKeyValue.getElement());
  58         XMLUtils.addReturnToElement(this._constructionElement);
  59     }
  60 
  61     /**
  62      * Constructor KeyValue
  63      *
  64      * @param doc
  65      * @param rsaKeyValue
  66      */
  67     public KeyValue(Document doc, RSAKeyValue rsaKeyValue) {
  68 
  69         super(doc);
  70 
  71         XMLUtils.addReturnToElement(this._constructionElement);
  72         this._constructionElement.appendChild(rsaKeyValue.getElement());
  73         XMLUtils.addReturnToElement(this._constructionElement);
  74     }
  75 
  76     /**
  77      * Constructor KeyValue
  78      *
  79      * @param doc
  80      * @param unknownKeyValue
  81      */
  82     public KeyValue(Document doc, Element unknownKeyValue) {
  83 
  84         super(doc);
  85 
  86         XMLUtils.addReturnToElement(this._constructionElement);
  87         this._constructionElement.appendChild(unknownKeyValue);
  88         XMLUtils.addReturnToElement(this._constructionElement);
  89     }
  90 
  91     /**
  92      * Constructor KeyValue
  93      *
  94      * @param doc
  95      * @param pk
  96      */
  97     public KeyValue(Document doc, PublicKey pk) {
  98 
  99         super(doc);
 100 
 101         XMLUtils.addReturnToElement(this._constructionElement);
 102 
 103         if (pk instanceof java.security.interfaces.DSAPublicKey) {
 104             DSAKeyValue dsa = new DSAKeyValue(this._doc, pk);
 105 
 106             this._constructionElement.appendChild(dsa.getElement());
 107             XMLUtils.addReturnToElement(this._constructionElement);
 108         } else if (pk instanceof java.security.interfaces.RSAPublicKey) {
 109             RSAKeyValue rsa = new RSAKeyValue(this._doc, pk);
 110 
 111             this._constructionElement.appendChild(rsa.getElement());
 112             XMLUtils.addReturnToElement(this._constructionElement);
 113         }
 114     }
 115 
 116     /**
 117      * Constructor KeyValue
 118      *
 119      * @param element
 120      * @param BaseURI
 121      * @throws XMLSecurityException
 122      */
 123     public KeyValue(Element element, String BaseURI)
 124            throws XMLSecurityException {
 125         super(element, BaseURI);
 126     }
 127 
 128     /**
 129      * Method getPublicKey
 130      *
 131      * @return the public key
 132      * @throws XMLSecurityException
 133      */
 134     public PublicKey getPublicKey() throws XMLSecurityException {
 135 
 136         Element rsa = XMLUtils.selectDsNode
 137             (this._constructionElement.getFirstChild(),
 138              Constants._TAG_RSAKEYVALUE,0);
 139 
 140         if (rsa != null) {
 141             RSAKeyValue kv = new RSAKeyValue(rsa, this._baseURI);
 142             return kv.getPublicKey();
 143         }
 144 
 145         Element dsa = XMLUtils.selectDsNode
 146             (this._constructionElement.getFirstChild(),
 147              Constants._TAG_DSAKEYVALUE,0);
 148 
 149         if (dsa != null) {
 150             DSAKeyValue kv = new DSAKeyValue(dsa, this._baseURI);
 151             return kv.getPublicKey();
 152         }
 153 
 154         return null;
 155     }
 156 
 157     /** @inheritDoc */
 158     public String getBaseLocalName() {
 159         return Constants._TAG_KEYVALUE;
 160     }
 161 }