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.keyresolver;
  22 
  23 import java.security.PublicKey;
  24 import java.security.cert.X509Certificate;
  25 import java.util.HashMap;
  26 
  27 import javax.crypto.SecretKey;
  28 
  29 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
  30 import org.w3c.dom.Element;
  31 
  32 /**
  33  * This class is abstract class for a child KeyInfo Elemnet.
  34  *
  35  * If you want your KeyResolver, at first you must extend this class, and register
  36  * as following in config.xml
  37  * <PRE>
  38  *  &lt;KeyResolver URI="http://www.w3.org/2000/09/xmldsig#KeyValue"
  39  *   JAVACLASS="MyPackage.MyKeyValueImpl"//gt;
  40  * </PRE>
  41  *
  42  * @author $Author: mullan $
  43  * @version $Revision: 1.5 $
  44  */
  45 public abstract class KeyResolverSpi {
  46    /**
  47     * This method helps the {@link com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver} to decide whether a
  48     * {@link com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi} is able to perform the requested action.
  49     *
  50     * @param element
  51     * @param BaseURI
  52     * @param storage
  53     * @return
  54     */
  55    public boolean engineCanResolve(Element element, String BaseURI,
  56                                                     StorageResolver storage) {
  57            throw new UnsupportedOperationException();
  58    }
  59 
  60    /**
  61     * Method engineResolvePublicKey
  62     *
  63     * @param element
  64     * @param BaseURI
  65     * @param storage
  66     * @return resolved public key from the registered from the element.
  67     *
  68     * @throws KeyResolverException
  69     */
  70    public PublicKey engineResolvePublicKey(
  71       Element element, String BaseURI, StorageResolver storage)
  72          throws KeyResolverException {
  73            throw new UnsupportedOperationException();
  74     };
  75 
  76    /**
  77     * Method engineResolvePublicKey
  78     *
  79     * @param element
  80     * @param BaseURI
  81     * @param storage
  82     * @return resolved public key from the registered from the element.
  83     *
  84     * @throws KeyResolverException
  85     */
  86     public PublicKey engineLookupAndResolvePublicKey(
  87       Element element, String BaseURI, StorageResolver storage)
  88          throws KeyResolverException {
  89         KeyResolverSpi tmp = cloneIfNeeded();
  90         if (!tmp.engineCanResolve(element, BaseURI, storage))
  91                 return null;
  92             return tmp.engineResolvePublicKey(element, BaseURI, storage);
  93     }
  94 
  95     private KeyResolverSpi cloneIfNeeded() throws KeyResolverException {
  96         KeyResolverSpi tmp=this;
  97         if (globalResolver) {
  98                 try {
  99                         tmp = (KeyResolverSpi) getClass().newInstance();
 100                 } catch (InstantiationException e) {
 101                         throw new KeyResolverException("",e);
 102                 } catch (IllegalAccessException e) {
 103                         throw new KeyResolverException("",e);
 104                 }
 105         }
 106         return tmp;
 107     }
 108 
 109     /**
 110      * Method engineResolveCertificate
 111      *
 112      * @param element
 113      * @param BaseURI
 114      * @param storage
 115      * @return resolved X509Certificate key from the registered from the elements
 116      *
 117      * @throws KeyResolverException
 118      */
 119     public X509Certificate engineResolveX509Certificate(
 120        Element element, String BaseURI, StorageResolver storage)
 121           throws KeyResolverException{
 122                    throw new UnsupportedOperationException();
 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 engineLookupResolveX509Certificate(
 136       Element element, String BaseURI, StorageResolver storage)
 137          throws KeyResolverException {
 138         KeyResolverSpi tmp = cloneIfNeeded();
 139         if (!tmp.engineCanResolve(element, BaseURI, storage))
 140                 return null;
 141         return tmp.engineResolveX509Certificate(element, BaseURI, storage);
 142 
 143     }
 144     /**
 145      * Method engineResolveSecretKey
 146      *
 147      * @param element
 148      * @param BaseURI
 149      * @param storage
 150      * @return resolved SecretKey key from the registered from the elements
 151      *
 152      * @throws KeyResolverException
 153      */
 154     public SecretKey engineResolveSecretKey(
 155        Element element, String BaseURI, StorageResolver storage)
 156           throws KeyResolverException{
 157                    throw new UnsupportedOperationException();
 158     };
 159 
 160    /**
 161     * Method engineResolveSecretKey
 162     *
 163     * @param element
 164     * @param BaseURI
 165     * @param storage
 166     * @return resolved SecretKey key from the registered from the elements
 167     *
 168     * @throws KeyResolverException
 169     */
 170    public SecretKey engineLookupAndResolveSecretKey(
 171       Element element, String BaseURI, StorageResolver storage)
 172          throws KeyResolverException {
 173            KeyResolverSpi tmp = cloneIfNeeded();
 174            if (!tmp.engineCanResolve(element, BaseURI, storage))
 175                    return null;
 176                 return tmp.engineResolveSecretKey(element, BaseURI, storage);
 177    }
 178 
 179    /** Field _properties */
 180    protected java.util.Map<String,String> _properties = null;
 181 
 182    protected boolean globalResolver=false;
 183 
 184    /**
 185     * Method engineSetProperty
 186     *
 187     * @param key
 188     * @param value
 189     */
 190    public void engineSetProperty(String key, String value) {
 191            if (_properties==null)
 192                    _properties=new HashMap<String,String>();
 193       this._properties.put(key, value);
 194    }
 195 
 196    /**
 197     * Method engineGetProperty
 198     *
 199     * @param key
 200     * @return obtain the property appointed by key
 201     */
 202    public String engineGetProperty(String key) {
 203            if (_properties==null)
 204                    return null;
 205 
 206       return this._properties.get(key);
 207    }
 208 
 209    /**
 210     * Method understandsProperty
 211     *
 212     * @param propertyToTest
 213     * @return true if understood the property
 214     */
 215    public boolean understandsProperty(String propertyToTest) {
 216            if (_properties==null)
 217                    return false;
 218 
 219       return  this._properties.get(propertyToTest)!=null;
 220    }
 221    public void setGlobalResolver(boolean globalResolver) {
 222         this.globalResolver = globalResolver;
 223    }
 224 
 225 }