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