1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 package com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations;
   6 
   7 import java.security.PrivateKey;
   8 import java.security.PublicKey;
   9 import java.security.cert.X509Certificate;
  10 import javax.crypto.SecretKey;
  11 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException;
  12 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
  13 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
  14 import com.sun.org.apache.xml.internal.security.utils.Constants;
  15 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  16 import org.w3c.dom.Element;
  17 
  18 /**
  19  * Resolves a single Key based on the KeyName.
  20  */
  21 public class SingleKeyResolver extends KeyResolverSpi
  22 {
  23     /** {@link org.apache.commons.logging} logging facility */
  24     private static java.util.logging.Logger log = 
  25         java.util.logging.Logger.getLogger(SingleKeyResolver.class.getName());
  26 
  27     private String keyName;
  28     private PublicKey publicKey;
  29     private PrivateKey privateKey;
  30     private SecretKey secretKey;
  31 
  32     /**
  33      * Constructor.
  34      * @param keyName
  35      * @param publicKey
  36      */
  37     public SingleKeyResolver(String keyName, PublicKey publicKey) {
  38         this.keyName = keyName;
  39         this.publicKey = publicKey;
  40     }
  41 
  42     /**
  43      * Constructor.
  44      * @param keyName
  45      * @param privateKey
  46      */
  47     public SingleKeyResolver(String keyName, PrivateKey privateKey) {
  48         this.keyName = keyName;
  49         this.privateKey = privateKey;
  50     }
  51 
  52     /**
  53      * Constructor.
  54      * @param keyName
  55      * @param secretKey
  56      */
  57     public SingleKeyResolver(String keyName, SecretKey secretKey) {
  58         this.keyName = keyName;
  59         this.secretKey = secretKey;
  60     }
  61 
  62     /**
  63      * This method returns whether the KeyResolverSpi is able to perform the requested action.
  64      *
  65      * @param element
  66      * @param BaseURI
  67      * @param storage
  68      * @return whether the KeyResolverSpi is able to perform the requested action.
  69      */
  70     public boolean engineCanResolve(Element element, String baseURI, StorageResolver storage) {
  71         return XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME);
  72     }
  73 
  74     /**
  75      * Method engineLookupAndResolvePublicKey
  76      *
  77      * @param element
  78      * @param baseURI
  79      * @param storage
  80      * @return null if no {@link PublicKey} could be obtained
  81      * @throws KeyResolverException
  82      */
  83     public PublicKey engineLookupAndResolvePublicKey(
  84         Element element, String baseURI, StorageResolver storage
  85     ) throws KeyResolverException {
  86         if (log.isLoggable(java.util.logging.Level.FINE)) {
  87             log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?");
  88         }
  89 
  90         if (publicKey != null 
  91             && XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME)) {
  92             String name = element.getFirstChild().getNodeValue();
  93             if (keyName.equals(name)) {
  94                 return publicKey;
  95             }
  96         }
  97 
  98         log.log(java.util.logging.Level.FINE, "I can't");
  99         return null;
 100     }
 101 
 102     /**
 103      * Method engineResolveX509Certificate
 104      * @inheritDoc
 105      * @param element
 106      * @param baseURI
 107      * @param storage
 108      * @throws KeyResolverException
 109      */
 110     public X509Certificate engineLookupResolveX509Certificate(
 111         Element element, String baseURI, StorageResolver storage
 112     ) throws KeyResolverException {
 113         return null;
 114     }
 115 
 116     /**
 117      * Method engineResolveSecretKey
 118      *
 119      * @param element
 120      * @param baseURI
 121      * @param storage
 122      * @return resolved SecretKey key or null if no {@link SecretKey} could be obtained
 123      *
 124      * @throws KeyResolverException
 125      */
 126     public SecretKey engineResolveSecretKey(
 127         Element element, String baseURI, StorageResolver storage
 128     ) throws KeyResolverException {
 129         if (log.isLoggable(java.util.logging.Level.FINE)) {
 130             log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?");
 131         }
 132 
 133         if (secretKey != null
 134             && XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME)) {
 135             String name = element.getFirstChild().getNodeValue();
 136             if (keyName.equals(name)) {
 137                 return secretKey;
 138             }
 139         }
 140 
 141         log.log(java.util.logging.Level.FINE, "I can't");
 142         return null;
 143     }
 144 
 145     /**
 146      * Method engineResolvePrivateKey
 147      * @inheritDoc
 148      * @param element
 149      * @param baseURI
 150      * @param storage
 151      * @return resolved PrivateKey key or null if no {@link PrivateKey} could be obtained
 152      * @throws KeyResolverException
 153      */
 154     public PrivateKey engineLookupAndResolvePrivateKey(
 155         Element element, String baseURI, StorageResolver storage
 156     ) throws KeyResolverException {
 157         if (log.isLoggable(java.util.logging.Level.FINE)) {
 158             log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?");
 159         }
 160 
 161         if (privateKey != null
 162             && XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME)) {
 163             String name = element.getFirstChild().getNodeValue();
 164             if (keyName.equals(name)) {
 165                 return privateKey;
 166             }
 167         }
 168 
 169         log.log(java.util.logging.Level.FINE, "I can't");
 170         return null;
 171     }
 172 }