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.keyresolver; 24 25 import java.security.PrivateKey; 26 import java.security.PublicKey; 27 import java.security.cert.X509Certificate; 28 import java.util.HashMap; 29 30 import javax.crypto.SecretKey; 31 32 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; 33 import org.w3c.dom.Element; 34 35 /** 36 * This class is an abstract class for a child KeyInfo Element. 37 * 38 * If you want the your KeyResolver, at firstly you must extend this class, and register 39 * as following in config.xml 40 * <PRE> 41 * <KeyResolver URI="http://www.w3.org/2000/09/xmldsig#KeyValue" 42 * JAVACLASS="MyPackage.MyKeyValueImpl"//gt; 43 * </PRE> 44 */ 45 public abstract class KeyResolverSpi { 46 47 /** Field properties */ 48 protected java.util.Map<String, String> properties = null; 49 50 protected boolean globalResolver = false; 51 52 protected boolean secureValidation; 53 54 /** 55 * Set whether secure validation is enabled or not. The default is false. 56 */ 57 public void setSecureValidation(boolean secureValidation) { 58 this.secureValidation = secureValidation; 59 } 60 61 /** 62 * This method returns whether the KeyResolverSpi is able to perform the requested action. 63 * 64 * @param element 65 * @param baseURI 66 * @param storage 67 * @return whether the KeyResolverSpi is able to perform the requested action. 68 */ 69 public boolean engineCanResolve(Element element, String baseURI, StorageResolver storage) { 70 throw new UnsupportedOperationException(); 71 } 72 73 /** 74 * Method engineResolvePublicKey 75 * 76 * @param element 77 * @param baseURI 78 * @param storage 79 * @return resolved public key from the registered from the element. 80 * 81 * @throws KeyResolverException 82 */ 83 public PublicKey engineResolvePublicKey( 84 Element element, String baseURI, StorageResolver storage 85 ) throws KeyResolverException { 86 throw new UnsupportedOperationException(); 87 }; 88 89 /** 90 * Method engineLookupAndResolvePublicKey 91 * 92 * @param element 93 * @param baseURI 94 * @param storage 95 * @return resolved public key from the registered from the element. 96 * 97 * @throws KeyResolverException 98 */ 99 public PublicKey engineLookupAndResolvePublicKey( 100 Element element, String baseURI, StorageResolver storage 101 ) throws KeyResolverException { 102 KeyResolverSpi tmp = cloneIfNeeded(); 103 if (!tmp.engineCanResolve(element, baseURI, storage)) { 104 return null; 105 } 106 return tmp.engineResolvePublicKey(element, baseURI, storage); 107 } 108 109 private KeyResolverSpi cloneIfNeeded() throws KeyResolverException { 110 KeyResolverSpi tmp = this; 111 if (globalResolver) { 112 try { 113 tmp = getClass().newInstance(); 114 } catch (InstantiationException e) { 115 throw new KeyResolverException("", e); 116 } catch (IllegalAccessException e) { 117 throw new KeyResolverException("", e); 118 } 119 } 120 return tmp; 121 } 122 123 /** 124 * Method engineResolveCertificate 125 * 126 * @param element 127 * @param baseURI 128 * @param storage 129 * @return resolved X509Certificate key from the registered from the elements 130 * 131 * @throws KeyResolverException 132 */ 133 public X509Certificate engineResolveX509Certificate( 134 Element element, String baseURI, StorageResolver storage 135 ) throws KeyResolverException{ 136 throw new UnsupportedOperationException(); 137 }; 138 139 /** 140 * Method engineLookupResolveX509Certificate 141 * 142 * @param element 143 * @param baseURI 144 * @param storage 145 * @return resolved X509Certificate key from the registered from the elements 146 * 147 * @throws KeyResolverException 148 */ 149 public X509Certificate engineLookupResolveX509Certificate( 150 Element element, String baseURI, StorageResolver storage 151 ) throws KeyResolverException { 152 KeyResolverSpi tmp = cloneIfNeeded(); 153 if (!tmp.engineCanResolve(element, baseURI, storage)) { 154 return null; 155 } 156 return tmp.engineResolveX509Certificate(element, baseURI, storage); 157 158 } 159 /** 160 * Method engineResolveSecretKey 161 * 162 * @param element 163 * @param baseURI 164 * @param storage 165 * @return resolved SecretKey key from the registered from the elements 166 * 167 * @throws KeyResolverException 168 */ 169 public SecretKey engineResolveSecretKey( 170 Element element, String baseURI, StorageResolver storage 171 ) throws KeyResolverException{ 172 throw new UnsupportedOperationException(); 173 }; 174 175 /** 176 * Method engineLookupAndResolveSecretKey 177 * 178 * @param element 179 * @param baseURI 180 * @param storage 181 * @return resolved SecretKey key from the registered from the elements 182 * 183 * @throws KeyResolverException 184 */ 185 public SecretKey engineLookupAndResolveSecretKey( 186 Element element, String baseURI, StorageResolver storage 187 ) throws KeyResolverException { 188 KeyResolverSpi tmp = cloneIfNeeded(); 189 if (!tmp.engineCanResolve(element, baseURI, storage)) { 190 return null; 191 } 192 return tmp.engineResolveSecretKey(element, baseURI, storage); 193 } 194 195 /** 196 * Method engineLookupAndResolvePrivateKey 197 * 198 * @param element 199 * @param baseURI 200 * @param storage 201 * @return resolved PrivateKey key from the registered from the elements 202 * 203 * @throws KeyResolverException 204 */ 205 public PrivateKey engineLookupAndResolvePrivateKey( 206 Element element, String baseURI, StorageResolver storage 207 ) throws KeyResolverException { 208 // This method was added later, it has no equivalent 209 // engineResolvePrivateKey() in the old API. 210 // We cannot throw UnsupportedOperationException because 211 // KeyResolverSpi implementations who don't know about 212 // this method would stop the search too early. 213 return null; 214 } 215 216 /** 217 * Method engineSetProperty 218 * 219 * @param key 220 * @param value 221 */ 222 public void engineSetProperty(String key, String value) { 223 if (properties == null) { 224 properties = new HashMap<String, String>(); 225 } 226 properties.put(key, value); 227 } 228 229 /** 230 * Method engineGetProperty 231 * 232 * @param key 233 * @return obtain the property appointed by key 234 */ 235 public String engineGetProperty(String key) { 236 if (properties == null) { 237 return null; 238 } 239 240 return properties.get(key); 241 } 242 243 /** 244 * Method understandsProperty 245 * 246 * @param propertyToTest 247 * @return true if understood the property 248 */ 249 public boolean understandsProperty(String propertyToTest) { 250 if (properties == null) { 251 return false; 252 } 253 254 return properties.get(propertyToTest) != null; 255 } 256 257 public void setGlobalResolver(boolean globalResolver) { 258 this.globalResolver = globalResolver; 259 } 260 261 }