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.XMLX509Certificate;
  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  * Resolves Certificates which are directly contained inside a
  41  * <CODE>ds:X509Certificate</CODE> Element.
  42  *
  43  * @author $Author: mullan $
  44  */
  45 public class X509CertificateResolver extends KeyResolverSpi {
  46 
  47    /** {@link java.util.logging} logging facility */
  48     static java.util.logging.Logger log =
  49         java.util.logging.Logger.getLogger(X509CertificateResolver.class.getName());
  50 
  51 
  52 
  53    /**
  54     * Method engineResolvePublicKey
  55     * @inheritDoc
  56     * @param element
  57     * @param BaseURI
  58     * @param storage
  59     *
  60     * @throws KeyResolverException
  61     */
  62    public PublicKey engineLookupAndResolvePublicKey(
  63            Element element, String BaseURI, StorageResolver storage)
  64               throws KeyResolverException {
  65 
  66       X509Certificate cert = this.engineLookupResolveX509Certificate(element,
  67                                 BaseURI, storage);
  68 
  69       if (cert != null) {
  70          return cert.getPublicKey();
  71       }
  72 
  73       return null;
  74    }
  75 
  76    /**
  77     * Method engineResolveX509Certificate
  78     * @inheritDoc
  79     * @param element
  80     * @param BaseURI
  81     * @param storage
  82     *
  83     * @throws KeyResolverException
  84     */
  85    public X509Certificate engineLookupResolveX509Certificate(
  86            Element element, String BaseURI, StorageResolver storage)
  87               throws KeyResolverException {
  88 
  89       try {
  90           Element[] els=XMLUtils.selectDsNodes(element.getFirstChild(),
  91                   Constants._TAG_X509CERTIFICATE);
  92          if ((els == null) || (els.length == 0)) {
  93                  Element el=XMLUtils.selectDsNode(element.getFirstChild(),
  94                      Constants._TAG_X509DATA,0);
  95              if (el!=null) {
  96                  return engineLookupResolveX509Certificate(el, BaseURI, storage);
  97              }
  98                  return null;
  99          }
 100 
 101          // populate Object array
 102          for (int i = 0; i < els.length; i++) {
 103                  XMLX509Certificate xmlCert=new XMLX509Certificate(els[i], BaseURI);
 104                  X509Certificate cert = xmlCert.getX509Certificate();
 105             if (cert!=null) {
 106                 return cert;
 107             }
 108          }
 109          return null;
 110       } catch (XMLSecurityException ex) {
 111          log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex);
 112 
 113          throw new KeyResolverException("generic.EmptyMessage", ex);
 114       }
 115    }
 116 
 117    /**
 118     * Method engineResolveSecretKey
 119     * @inheritDoc
 120     * @param element
 121     * @param BaseURI
 122     * @param storage
 123     *
 124     */
 125    public javax.crypto.SecretKey engineLookupAndResolveSecretKey(
 126            Element element, String BaseURI, StorageResolver storage)
 127    {
 128       return null;
 129    }
 130 }