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.implementations;
  22 
  23 
  24 
  25 import java.security.PublicKey;
  26 import java.security.cert.X509Certificate;
  27 
  28 
  29 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  30 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI;
  31 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException;
  32 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
  33 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
  34 import com.sun.org.apache.xml.internal.security.utils.Constants;
  35 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  36 import org.w3c.dom.Element;
  37 
  38 
  39 /**
  40  *
  41  *
  42  * @author $Author: mullan $
  43  */
  44 public class X509SKIResolver extends KeyResolverSpi {
  45 
  46    /** {@link java.util.logging} logging facility */
  47     static java.util.logging.Logger log =
  48         java.util.logging.Logger.getLogger(X509SKIResolver.class.getName());
  49 
  50 
  51    /**
  52     * Method engineResolvePublicKey
  53     *
  54     * @param element
  55     * @param BaseURI
  56     * @param storage
  57     * @return null if no {@link PublicKey} could be obtained
  58     * @throws KeyResolverException
  59     */
  60    public PublicKey engineLookupAndResolvePublicKey(
  61            Element element, String BaseURI, StorageResolver storage)
  62               throws KeyResolverException {
  63 
  64       X509Certificate cert = this.engineLookupResolveX509Certificate(element,
  65                                 BaseURI, storage);
  66 
  67       if (cert != null) {
  68          return cert.getPublicKey();
  69       }
  70 
  71       return null;
  72    }
  73 
  74    /**
  75     * Method engineResolveX509Certificate
  76     * @inheritDoc
  77     * @param element
  78     * @param BaseURI
  79     * @param storage
  80     *
  81     * @throws KeyResolverException
  82     */
  83    public X509Certificate engineLookupResolveX509Certificate(
  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            if (!XMLUtils.elementIsInSignatureSpace(element,
  90               Constants._TAG_X509DATA)) {
  91                  log.log(java.util.logging.Level.FINE, "I can't");
  92                  return null;
  93            }
  94            /** Field _x509childObject[] */
  95            XMLX509SKI x509childObject[] = null;
  96 
  97            Element x509childNodes[] = null;
  98            x509childNodes = XMLUtils.selectDsNodes(element.getFirstChild(),
  99                           Constants._TAG_X509SKI);
 100 
 101            if (!((x509childNodes != null)
 102                          && (x509childNodes.length > 0))) {
 103                    log.log(java.util.logging.Level.FINE, "I can't");
 104                 return null;
 105            }
 106            try {
 107          if (storage == null) {
 108             Object exArgs[] = { Constants._TAG_X509SKI };
 109             KeyResolverException ex =
 110                new KeyResolverException("KeyResolver.needStorageResolver",
 111                                         exArgs);
 112 
 113             log.log(java.util.logging.Level.INFO, "", ex);
 114 
 115             throw ex;
 116          }
 117 
 118          x509childObject = new XMLX509SKI[x509childNodes.length];
 119 
 120          for (int i = 0; i < x509childNodes.length; i++) {
 121             x509childObject[i] =
 122                new XMLX509SKI(x509childNodes[i], BaseURI);
 123          }
 124 
 125          while (storage.hasNext()) {
 126             X509Certificate cert = storage.next();
 127             XMLX509SKI certSKI = new XMLX509SKI(element.getOwnerDocument(), cert);
 128 
 129             for (int i = 0; i < x509childObject.length; i++) {
 130                if (certSKI.equals(x509childObject[i])) {
 131                   log.log(java.util.logging.Level.FINE, "Return PublicKey from "
 132                             + cert.getSubjectDN().getName());
 133 
 134                   return cert;
 135                }
 136             }
 137          }
 138       } catch (XMLSecurityException ex) {
 139          throw new KeyResolverException("empty", ex);
 140       }
 141 
 142       return null;
 143    }
 144 
 145    /**
 146     * Method engineResolveSecretKey
 147     * @inheritDoc
 148     * @param element
 149     * @param BaseURI
 150     * @param storage
 151     *
 152     */
 153    public javax.crypto.SecretKey engineLookupAndResolveSecretKey(
 154            Element element, String BaseURI, StorageResolver storage)
 155     {
 156       return null;
 157    }
 158 }