View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /**
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements. See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership. The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License. You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied. See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  package com.sun.org.apache.xml.internal.security.keys.content.x509;
24  
25  import java.io.ByteArrayInputStream;
26  import java.security.PublicKey;
27  import java.security.cert.CertificateException;
28  import java.security.cert.CertificateFactory;
29  import java.security.cert.X509Certificate;
30  import java.util.Arrays;
31  
32  import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
33  import com.sun.org.apache.xml.internal.security.utils.Constants;
34  import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Element;
37  
38  public class XMLX509Certificate extends SignatureElementProxy implements XMLX509DataContent {
39  
40      /** Field JCA_CERT_ID */
41      public static final String JCA_CERT_ID = "X.509";
42  
43      /**
44       * Constructor X509Certificate
45       *
46       * @param element
47       * @param BaseURI
48       * @throws XMLSecurityException
49       */
50      public XMLX509Certificate(Element element, String BaseURI) throws XMLSecurityException {
51          super(element, BaseURI);
52      }
53  
54      /**
55       * Constructor X509Certificate
56       *
57       * @param doc
58       * @param certificateBytes
59       */
60      public XMLX509Certificate(Document doc, byte[] certificateBytes) {
61          super(doc);
62  
63          this.addBase64Text(certificateBytes);
64      }
65  
66      /**
67       * Constructor XMLX509Certificate
68       *
69       * @param doc
70       * @param x509certificate
71       * @throws XMLSecurityException
72       */
73      public XMLX509Certificate(Document doc, X509Certificate x509certificate)
74          throws XMLSecurityException {
75          super(doc);
76  
77          try {
78              this.addBase64Text(x509certificate.getEncoded());
79          } catch (java.security.cert.CertificateEncodingException ex) {
80              throw new XMLSecurityException("empty", ex);
81          }
82      }
83  
84      /**
85       * Method getCertificateBytes
86       *
87       * @return the certificate bytes
88       * @throws XMLSecurityException
89       */
90      public byte[] getCertificateBytes() throws XMLSecurityException {
91          return this.getBytesFromTextChild();
92      }
93  
94      /**
95       * Method getX509Certificate
96       *
97       * @return the x509 certificate
98       * @throws XMLSecurityException
99       */
100     public X509Certificate getX509Certificate() throws XMLSecurityException {
101         try {
102             byte certbytes[] = this.getCertificateBytes();
103             CertificateFactory certFact =
104                 CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
105             X509Certificate cert =
106                 (X509Certificate) certFact.generateCertificate(
107                     new ByteArrayInputStream(certbytes)
108                 );
109 
110             if (cert != null) {
111                 return cert;
112             }
113 
114             return null;
115         } catch (CertificateException ex) {
116             throw new XMLSecurityException("empty", ex);
117         }
118     }
119 
120     /**
121      * Method getPublicKey
122      *
123      * @return the publickey
124      * @throws XMLSecurityException
125      */
126     public PublicKey getPublicKey() throws XMLSecurityException {
127         X509Certificate cert = this.getX509Certificate();
128 
129         if (cert != null) {
130             return cert.getPublicKey();
131         }
132 
133         return null;
134     }
135 
136     /** @inheritDoc */
137     public boolean equals(Object obj) {
138         if (!(obj instanceof XMLX509Certificate)) {
139             return false;
140         }
141         XMLX509Certificate other = (XMLX509Certificate) obj;
142         try {
143             return Arrays.equals(other.getCertificateBytes(), this.getCertificateBytes());
144         } catch (XMLSecurityException ex) {
145             return false;
146         }
147     }
148 
149     public int hashCode() {
150         int result = 17;
151         try {
152             byte[] bytes = getCertificateBytes();
153             for (int i = 0; i < bytes.length; i++) {
154                 result = 31 * result + bytes[i];
155             }
156         } catch (XMLSecurityException e) {
157             if (log.isLoggable(java.util.logging.Level.FINE)) {
158                 log.log(java.util.logging.Level.FINE, e.getMessage(), e);
159             }
160         }
161         return result;
162     }
163 
164     /** @inheritDoc */
165     public String getBaseLocalName() {
166         return Constants._TAG_X509CERTIFICATE;
167     }
168 }