/*
 * Copyright  1999-2004 The Apache Software Foundation.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.xml.security.keys.content.x509;



import java.io.ByteArrayInputStream;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.utils.Constants;
import org.apache.xml.security.utils.SignatureElementProxy;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


/**
 *
 * @author $Author$
 */
public class XMLX509Certificate extends SignatureElementProxy
        implements XMLX509DataContent {

   /** {@link org.apache.commons.logging} logging facility */
    static org.apache.commons.logging.Log log = 
        org.apache.commons.logging.LogFactory.getLog(XMLX509Certificate.class.getName());

   /** Field JCA_CERT_ID */
   public static final String JCA_CERT_ID = "X.509";

   /**
    * Constructor X509Certificate
    *
    * @param element
    * @param BaseURI
    * @throws XMLSecurityException
    */
   public XMLX509Certificate(Element element, String BaseURI)
           throws XMLSecurityException {
      super(element, BaseURI);
   }

   /**
    * Constructor X509Certificate
    *
    * @param doc
    * @param certificateBytes
    */
   public XMLX509Certificate(Document doc, byte[] certificateBytes) {

      super(doc);

      this.addBase64Text(certificateBytes);
   }

   /**
    * Constructor XMLX509Certificate
    *
    * @param doc
    * @param x509certificate
    * @throws XMLSecurityException
    */
   public XMLX509Certificate(Document doc, X509Certificate x509certificate)
           throws XMLSecurityException {

      super(doc);

      try {
         this.addBase64Text(x509certificate.getEncoded());
      } catch (java.security.cert.CertificateEncodingException ex) {
         throw new XMLSecurityException("empty", ex);
      }
   }

   /**
    * Method getCertificateBytes
    *
    * @return the certificate bytes
    * @throws XMLSecurityException
    */
   public byte[] getCertificateBytes() throws XMLSecurityException {
      return this.getBytesFromTextChild();
   }

   /**
    * Method getX509Certificate
    *
    * @return the x509 certificate
    * @throws XMLSecurityException
    */
   public X509Certificate getX509Certificate() throws XMLSecurityException {

      try {
         byte certbytes[] = this.getCertificateBytes();
         CertificateFactory certFact =
            CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
         X509Certificate cert =
            (X509Certificate) certFact
               .generateCertificate(new ByteArrayInputStream(certbytes));

         if (cert != null) {
            return cert;
         }

         return null;
      } catch (CertificateException ex) {
         throw new XMLSecurityException("empty", ex);
      }
   }

   /**
    * Method getPublicKey
    *
    * @return teh publickey
    * @throws XMLSecurityException
    */
   public PublicKey getPublicKey() throws XMLSecurityException {

      X509Certificate cert = this.getX509Certificate();

      if (cert != null) {
         return cert.getPublicKey();
      }

      return null;
   }

   /** @inheritDoc */
   public boolean equals(Object obj) {

      try {
         if (!obj.getClass().getName().equals(this.getClass().getName())) {
            return false;
         }

         XMLX509Certificate other = (XMLX509Certificate) obj;

         /** $todo$ or should be create X509Certificates and use the equals() from the Certs */
         return java.security.MessageDigest.isEqual(other.getCertificateBytes(),
                                        this.getCertificateBytes());
      } catch (XMLSecurityException ex) {
         return false;
      }
   }

   /** @inheritDoc */
   public String getBaseLocalName() {
      return Constants._TAG_X509CERTIFICATE;
   }
}

@@ -678,18 +678,16 @@
 .x509;%0A%0A
-%0A%0A
 import j
@@ -1115,17 +1115,16 @@
 ement;%0A%0A
-%0A
 /**%0A *%0A 
@@ -1253,210 +1253,8 @@
  %7B%0A%0A
-   /** %7B@link org.apache.commons.logging%7D logging facility */%0A    static org.apache.commons.logging.Log log = %0A        org.apache.commons.logging.LogFactory.getLog(XMLX509Certificate.class.getName());%0A%0A
    /
@@ -3619,32 +3619,33 @@
  null;%0A   %7D%0A%0A   
+ 
 /** @inheritDoc 
@@ -3639,32 +3639,33 @@
  @inheritDoc */%0A
+ 
    public boolea
@@ -3686,39 +3686,82 @@
 t obj) %7B%0A%0A      
-try %7B%0A 
+  if (obj == null) %7B%0A%09    return false;%0A        %7D%0A
         if (!obj
@@ -3757,19 +3757,20 @@
    if (!
-obj
+this
 .getClas
@@ -3790,20 +3790,19 @@
 .equals(
-this
+obj
 .getClas
@@ -3845,37 +3845,34 @@
  false;%0A        
- 
 %7D%0A
-%0A 
         XMLX509C
@@ -3916,17 +3916,34 @@
 e) obj;%0A
-%0A
+        try %7B%0A%0A   
         
@@ -4035,24 +4035,27 @@
 */%0A         
+   
 return java.
@@ -4084,16 +4084,19 @@
 .isEqual
+%0A%09%09
 (other.g
@@ -4120,48 +4120,8 @@
 s(),
-%0A                                       
  thi
@@ -4142,24 +4142,26 @@
 teBytes());%0A
+  
       %7D catc
@@ -4182,32 +4182,35 @@
 Exception ex) %7B%0A
+   
          return 
@@ -4214,34 +4214,167 @@
 rn false;%0A      
-%7D%0A
+  %7D%0A    %7D%0A%0A    public int hashCode() %7B%0A%09// Uncomment when JDK 1.4 is required%0A%09// assert false : %22hashCode not designed%22;%0A%09return 72;%0A 
    %7D%0A%0A   /** @in

Fix findbugs bugs: XMLX509Certificate defines equals and uses Object.hashCode() Possible null pointer dereference in equals(). Unread field: XMLX509Certificate.log