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.math.BigInteger;
26  import java.security.cert.X509Certificate;
27  
28  import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
29  import com.sun.org.apache.xml.internal.security.utils.Constants;
30  import com.sun.org.apache.xml.internal.security.utils.RFC2253Parser;
31  import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
32  import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.Element;
35  
36  public class XMLX509IssuerSerial extends SignatureElementProxy implements XMLX509DataContent {
37  
38      /** {@link org.apache.commons.logging} logging facility */
39      private static java.util.logging.Logger log =
40          java.util.logging.Logger.getLogger(XMLX509IssuerSerial.class.getName());
41  
42      /**
43       * Constructor XMLX509IssuerSerial
44       *
45       * @param element
46       * @param baseURI
47       * @throws XMLSecurityException
48       */
49      public XMLX509IssuerSerial(Element element, String baseURI) throws XMLSecurityException {
50          super(element, baseURI);
51      }
52  
53      /**
54       * Constructor XMLX509IssuerSerial
55       *
56       * @param doc
57       * @param x509IssuerName
58       * @param x509SerialNumber
59       */
60      public XMLX509IssuerSerial(Document doc, String x509IssuerName, BigInteger x509SerialNumber) {
61          super(doc);
62          XMLUtils.addReturnToElement(this.constructionElement);
63          addTextElement(x509IssuerName, Constants._TAG_X509ISSUERNAME);
64          addTextElement(x509SerialNumber.toString(), Constants._TAG_X509SERIALNUMBER);
65      }
66  
67      /**
68       * Constructor XMLX509IssuerSerial
69       *
70       * @param doc
71       * @param x509IssuerName
72       * @param x509SerialNumber
73       */
74      public XMLX509IssuerSerial(Document doc, String x509IssuerName, String x509SerialNumber) {
75          this(doc, x509IssuerName, new BigInteger(x509SerialNumber));
76      }
77  
78      /**
79       * Constructor XMLX509IssuerSerial
80       *
81       * @param doc
82       * @param x509IssuerName
83       * @param x509SerialNumber
84       */
85      public XMLX509IssuerSerial(Document doc, String x509IssuerName, int x509SerialNumber) {
86          this(doc, x509IssuerName, new BigInteger(Integer.toString(x509SerialNumber)));
87      }
88  
89      /**
90       * Constructor XMLX509IssuerSerial
91       *
92       * @param doc
93       * @param x509certificate
94       */
95      public XMLX509IssuerSerial(Document doc, X509Certificate x509certificate) {
96          this(
97              doc,
98              x509certificate.getIssuerX500Principal().getName(),
99              x509certificate.getSerialNumber()
100         );
101     }
102 
103     /**
104      * Method getSerialNumber
105      *
106      * @return the serial number
107      */
108     public BigInteger getSerialNumber() {
109         String text =
110             this.getTextFromChildElement(Constants._TAG_X509SERIALNUMBER, Constants.SignatureSpecNS);
111         if (log.isLoggable(java.util.logging.Level.FINE)) {
112             log.log(java.util.logging.Level.FINE, "X509SerialNumber text: " + text);
113         }
114 
115         return new BigInteger(text);
116     }
117 
118     /**
119      * Method getSerialNumberInteger
120      *
121      * @return the serial number as plain int
122      */
123     public int getSerialNumberInteger() {
124         return this.getSerialNumber().intValue();
125     }
126 
127     /**
128      * Method getIssuerName
129      *
130      * @return the issuer name
131      */
132     public String getIssuerName()  {
133         return RFC2253Parser.normalize(
134             this.getTextFromChildElement(Constants._TAG_X509ISSUERNAME, Constants.SignatureSpecNS)
135         );
136     }
137 
138     /** @inheritDoc */
139     public boolean equals(Object obj) {
140         if (!(obj instanceof XMLX509IssuerSerial)) {
141             return false;
142         }
143 
144         XMLX509IssuerSerial other = (XMLX509IssuerSerial) obj;
145 
146         return this.getSerialNumber().equals(other.getSerialNumber())
147             && this.getIssuerName().equals(other.getIssuerName());
148     }
149 
150     public int hashCode() {
151         int result = 17;
152         result = 31 * result + getSerialNumber().hashCode();
153         result = 31 * result + getIssuerName().hashCode();
154         return result;
155     }
156 
157     /** @inheritDoc */
158     public String getBaseLocalName() {
159         return Constants._TAG_X509ISSUERSERIAL;
160     }
161 }