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 @SuppressWarnings("deprecation") 114 KeyResolverSpi krs = getClass().newInstance(); 115 tmp = krs; 116 } catch (InstantiationException e) { 117 throw new KeyResolverException("", e); 118 } catch (IllegalAccessException e) { 119 throw new KeyResolverException("", e); 120 } 121 } 122 return tmp; 123 } 124 125 /** 126 * Method engineResolveCertificate 127 * 128 * @param element 129 * @param baseURI 130 * @param storage 131 * @return resolved X509Certificate key from the registered from the elements 132 * 133 * @throws KeyResolverException 134 */ 135 public X509Certificate engineResolveX509Certificate( 136 Element element, String baseURI, StorageResolver storage 137 ) throws KeyResolverException{ 138 throw new UnsupportedOperationException(); 139 }; 140 141 /** 142 * Method engineLookupResolveX509Certificate 143 * 144 * @param element 145 * @param baseURI 146 * @param storage 147 * @return resolved X509Certificate key from the registered from the elements 148 * 149 * @throws KeyResolverException 150 */ 151 public X509Certificate engineLookupResolveX509Certificate( 152 Element element, String baseURI, StorageResolver storage 153 ) throws KeyResolverException { 154 KeyResolverSpi tmp = cloneIfNeeded(); 155 if (!tmp.engineCanResolve(element, baseURI, storage)) { 156 return null; 157 } 158 return tmp.engineResolveX509Certificate(element, baseURI, storage); 159 160 } 161 /** 162 * Method engineResolveSecretKey 163 * 164 * @param element 165 * @param baseURI 166 * @param storage 167 * @return resolved SecretKey key from the registered from the elements 168 * 169 * @throws KeyResolverException 170 */ 171 public SecretKey engineResolveSecretKey( 172 Element element, String baseURI, StorageResolver storage 173 ) throws KeyResolverException{ 174 throw new UnsupportedOperationException(); 175 }; 176 177 /** 178 * Method engineLookupAndResolveSecretKey 179 * 180 * @param element 181 * @param baseURI 182 * @param storage 183 * @return resolved SecretKey key from the registered from the elements 184 * 185 * @throws KeyResolverException 186 */ 187 public SecretKey engineLookupAndResolveSecretKey( 188 Element element, String baseURI, StorageResolver storage 189 ) throws KeyResolverException { 190 KeyResolverSpi tmp = cloneIfNeeded(); 191 if (!tmp.engineCanResolve(element, baseURI, storage)) { 192 return null; 193 } 194 return tmp.engineResolveSecretKey(element, baseURI, storage); 195 } 196 197 /** 198 * Method engineLookupAndResolvePrivateKey 199 * 200 * @param element 201 * @param baseURI 202 * @param storage 203 * @return resolved PrivateKey key from the registered from the elements 204 * 205 * @throws KeyResolverException 206 */ 207 public PrivateKey engineLookupAndResolvePrivateKey( 208 Element element, String baseURI, StorageResolver storage 209 ) throws KeyResolverException { 210 // This method was added later, it has no equivalent 211 // engineResolvePrivateKey() in the old API. 212 // We cannot throw UnsupportedOperationException because 213 // KeyResolverSpi implementations who don't know about 214 // this method would stop the search too early. 215 return null; 216 } 217 218 /** 219 * Method engineSetProperty 220 * 221 * @param key 222 * @param value 223 */ 224 public void engineSetProperty(String key, String value) { 225 if (properties == null) { 226 properties = new HashMap<String, String>(); 227 } 228 properties.put(key, value); 229 } 230 231 /** 232 * Method engineGetProperty 233 * 234 * @param key 235 * @return obtain the property appointed by key 236 */ 237 public String engineGetProperty(String key) { 238 if (properties == null) { 239 return null; 240 } 241 242 return properties.get(key); 243 } 244 245 /** 246 * Method understandsProperty 247 * 248 * @param propertyToTest 249 * @return true if understood the property 250 */ 251 public boolean understandsProperty(String propertyToTest) { 252 if (properties == null) { 253 return false; 254 } 255 256 return properties.get(propertyToTest) != null; 257 } 258 259 public void setGlobalResolver(boolean globalResolver) { 260 this.globalResolver = globalResolver; 261 } 262 263 }