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;
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.keys.content.x509.XMLX509CRL;
30  import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate;
31  import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Digest;
32  import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509IssuerSerial;
33  import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI;
34  import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SubjectName;
35  import com.sun.org.apache.xml.internal.security.utils.Constants;
36  import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
37  import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  import org.w3c.dom.Node;
41  
42  public class X509Data extends SignatureElementProxy implements KeyInfoContent {
43  
44      /** {@link org.apache.commons.logging} logging facility */
45      private static java.util.logging.Logger log =
46          java.util.logging.Logger.getLogger(X509Data.class.getName());
47  
48      /**
49       * Constructor X509Data
50       *
51       * @param doc
52       */
53      public X509Data(Document doc) {
54          super(doc);
55  
56          XMLUtils.addReturnToElement(this.constructionElement);
57      }
58  
59      /**
60       * Constructor X509Data
61       *
62       * @param element
63       * @param baseURI
64       * @throws XMLSecurityException
65       */
66      public X509Data(Element element, String baseURI) throws XMLSecurityException {
67          super(element, baseURI);
68  
69          Node sibling = this.constructionElement.getFirstChild();
70          while (sibling != null) {
71              if (sibling.getNodeType() != Node.ELEMENT_NODE) {
72                  sibling = sibling.getNextSibling();
73                  continue;
74              }
75              return;
76          }
77          /* No Elements found */
78          Object exArgs[] = { "Elements", Constants._TAG_X509DATA };
79          throw new XMLSecurityException("xml.WrongContent", exArgs);
80      }
81  
82      /**
83       * Method addIssuerSerial
84       *
85       * @param X509IssuerName
86       * @param X509SerialNumber
87       */
88      public void addIssuerSerial(String X509IssuerName, BigInteger X509SerialNumber) {
89          this.add(new XMLX509IssuerSerial(this.doc, X509IssuerName, X509SerialNumber));
90      }
91  
92      /**
93       * Method addIssuerSerial
94       *
95       * @param X509IssuerName
96       * @param X509SerialNumber
97       */
98      public void addIssuerSerial(String X509IssuerName, String X509SerialNumber) {
99          this.add(new XMLX509IssuerSerial(this.doc, X509IssuerName, X509SerialNumber));
100     }
101 
102     /**
103      * Method addIssuerSerial
104      *
105      * @param X509IssuerName
106      * @param X509SerialNumber
107      */
108     public void addIssuerSerial(String X509IssuerName, int X509SerialNumber) {
109         this.add(new XMLX509IssuerSerial(this.doc, X509IssuerName, X509SerialNumber));
110     }
111 
112     /**
113      * Method add
114      *
115      * @param xmlX509IssuerSerial
116      */
117     public void add(XMLX509IssuerSerial xmlX509IssuerSerial) {
118 
119         this.constructionElement.appendChild(xmlX509IssuerSerial.getElement());
120         XMLUtils.addReturnToElement(this.constructionElement);
121     }
122 
123     /**
124      * Method addSKI
125      *
126      * @param skiBytes
127      */
128     public void addSKI(byte[] skiBytes) {
129         this.add(new XMLX509SKI(this.doc, skiBytes));
130     }
131 
132     /**
133      * Method addSKI
134      *
135      * @param x509certificate
136      * @throws XMLSecurityException
137      */
138     public void addSKI(X509Certificate x509certificate)
139         throws XMLSecurityException {
140         this.add(new XMLX509SKI(this.doc, x509certificate));
141     }
142 
143     /**
144      * Method add
145      *
146      * @param xmlX509SKI
147      */
148     public void add(XMLX509SKI xmlX509SKI) {
149         this.constructionElement.appendChild(xmlX509SKI.getElement());
150         XMLUtils.addReturnToElement(this.constructionElement);
151     }
152 
153     /**
154      * Method addSubjectName
155      *
156      * @param subjectName
157      */
158     public void addSubjectName(String subjectName) {
159         this.add(new XMLX509SubjectName(this.doc, subjectName));
160     }
161 
162     /**
163      * Method addSubjectName
164      *
165      * @param x509certificate
166      */
167     public void addSubjectName(X509Certificate x509certificate) {
168         this.add(new XMLX509SubjectName(this.doc, x509certificate));
169     }
170 
171     /**
172      * Method add
173      *
174      * @param xmlX509SubjectName
175      */
176     public void add(XMLX509SubjectName xmlX509SubjectName) {
177         this.constructionElement.appendChild(xmlX509SubjectName.getElement());
178         XMLUtils.addReturnToElement(this.constructionElement);
179     }
180 
181     /**
182      * Method addCertificate
183      *
184      * @param x509certificate
185      * @throws XMLSecurityException
186      */
187     public void addCertificate(X509Certificate x509certificate)
188         throws XMLSecurityException {
189         this.add(new XMLX509Certificate(this.doc, x509certificate));
190     }
191 
192     /**
193      * Method addCertificate
194      *
195      * @param x509certificateBytes
196      */
197     public void addCertificate(byte[] x509certificateBytes) {
198         this.add(new XMLX509Certificate(this.doc, x509certificateBytes));
199     }
200 
201     /**
202      * Method add
203      *
204      * @param xmlX509Certificate
205      */
206     public void add(XMLX509Certificate xmlX509Certificate) {
207         this.constructionElement.appendChild(xmlX509Certificate.getElement());
208         XMLUtils.addReturnToElement(this.constructionElement);
209     }
210 
211     /**
212      * Method addCRL
213      *
214      * @param crlBytes
215      */
216     public void addCRL(byte[] crlBytes) {
217         this.add(new XMLX509CRL(this.doc, crlBytes));
218     }
219 
220     /**
221      * Method add
222      *
223      * @param xmlX509CRL
224      */
225     public void add(XMLX509CRL xmlX509CRL) {
226         this.constructionElement.appendChild(xmlX509CRL.getElement());
227         XMLUtils.addReturnToElement(this.constructionElement);
228     }
229 
230     /**
231      * Method addDigest
232      *
233      * @param x509certificate
234      * @param algorithmURI
235      * @throws XMLSecurityException
236      */
237     public void addDigest(X509Certificate x509certificate, String algorithmURI)
238         throws XMLSecurityException {
239         this.add(new XMLX509Digest(this.doc, x509certificate, algorithmURI));
240     }
241 
242     /**
243      * Method addDigest
244      *
245      * @param x509CertificateDigestByes
246      * @param algorithmURI
247      */
248     public void addDigest(byte[] x509certificateDigestBytes, String algorithmURI) {
249         this.add(new XMLX509Digest(this.doc, x509certificateDigestBytes, algorithmURI));
250     }
251 
252     /**
253      * Method add
254      *
255      * @param XMLX509Digest
256      */
257     public void add(XMLX509Digest xmlX509Digest) {
258         this.constructionElement.appendChild(xmlX509Digest.getElement());
259         XMLUtils.addReturnToElement(this.constructionElement);
260     }
261 
262     /**
263      * Method addUnknownElement
264      *
265      * @param element
266      */
267     public void addUnknownElement(Element element) {
268         this.constructionElement.appendChild(element);
269         XMLUtils.addReturnToElement(this.constructionElement);
270     }
271 
272     /**
273      * Method lengthIssuerSerial
274      *
275      * @return the number of IssuerSerial elements in this X509Data
276      */
277     public int lengthIssuerSerial() {
278         return this.length(Constants.SignatureSpecNS, Constants._TAG_X509ISSUERSERIAL);
279     }
280 
281     /**
282      * Method lengthSKI
283      *
284      * @return the number of SKI elements in this X509Data
285      */
286     public int lengthSKI() {
287         return this.length(Constants.SignatureSpecNS, Constants._TAG_X509SKI);
288     }
289 
290     /**
291      * Method lengthSubjectName
292      *
293      * @return the number of SubjectName elements in this X509Data
294      */
295     public int lengthSubjectName() {
296         return this.length(Constants.SignatureSpecNS, Constants._TAG_X509SUBJECTNAME);
297     }
298 
299     /**
300      * Method lengthCertificate
301      *
302      * @return the number of Certificate elements in this X509Data
303      */
304     public int lengthCertificate() {
305         return this.length(Constants.SignatureSpecNS, Constants._TAG_X509CERTIFICATE);
306     }
307 
308     /**
309      * Method lengthCRL
310      *
311      * @return the number of CRL elements in this X509Data
312      */
313     public int lengthCRL() {
314         return this.length(Constants.SignatureSpecNS, Constants._TAG_X509CRL);
315     }
316 
317     /**
318      * Method lengthDigest
319      *
320      * @return the number of X509Digest elements in this X509Data
321      */
322     public int lengthDigest() {
323         return this.length(Constants.SignatureSpec11NS, Constants._TAG_X509DIGEST);
324     }
325 
326     /**
327      * Method lengthUnknownElement
328      *
329      * @return the number of UnknownElement elements in this X509Data
330      */
331     public int lengthUnknownElement() {
332         int result = 0;
333         Node n = this.constructionElement.getFirstChild();
334         while (n != null){
335             if ((n.getNodeType() == Node.ELEMENT_NODE)
336                 && !n.getNamespaceURI().equals(Constants.SignatureSpecNS)) {
337                 result++;
338             }
339             n = n.getNextSibling();
340         }
341 
342         return result;
343     }
344 
345     /**
346      * Method itemIssuerSerial
347      *
348      * @param i
349      * @return the X509IssuerSerial, null if not present
350      * @throws XMLSecurityException
351      */
352     public XMLX509IssuerSerial itemIssuerSerial(int i) throws XMLSecurityException {
353         Element e =
354             XMLUtils.selectDsNode(
355                 this.constructionElement.getFirstChild(), Constants._TAG_X509ISSUERSERIAL, i);
356 
357         if (e != null) {
358             return new XMLX509IssuerSerial(e, this.baseURI);
359         }
360         return null;
361     }
362 
363     /**
364      * Method itemSKI
365      *
366      * @param i
367      * @return the X509SKI, null if not present
368      * @throws XMLSecurityException
369      */
370     public XMLX509SKI itemSKI(int i) throws XMLSecurityException {
371 
372         Element e =
373             XMLUtils.selectDsNode(
374                 this.constructionElement.getFirstChild(), Constants._TAG_X509SKI, i);
375 
376         if (e != null) {
377             return new XMLX509SKI(e, this.baseURI);
378         }
379         return null;
380     }
381 
382     /**
383      * Method itemSubjectName
384      *
385      * @param i
386      * @return the X509SubjectName, null if not present
387      * @throws XMLSecurityException
388      */
389     public XMLX509SubjectName itemSubjectName(int i) throws XMLSecurityException {
390 
391         Element e =
392             XMLUtils.selectDsNode(
393                 this.constructionElement.getFirstChild(), Constants._TAG_X509SUBJECTNAME, i);
394 
395         if (e != null) {
396             return new XMLX509SubjectName(e, this.baseURI);
397         }
398         return null;
399     }
400 
401     /**
402      * Method itemCertificate
403      *
404      * @param i
405      * @return the X509Certifacte, null if not present
406      * @throws XMLSecurityException
407      */
408     public XMLX509Certificate itemCertificate(int i) throws XMLSecurityException {
409 
410         Element e =
411             XMLUtils.selectDsNode(
412                 this.constructionElement.getFirstChild(), Constants._TAG_X509CERTIFICATE, i);
413 
414         if (e != null) {
415             return new XMLX509Certificate(e, this.baseURI);
416         }
417         return null;
418     }
419 
420     /**
421      * Method itemCRL
422      *
423      * @param i
424      * @return the X509CRL, null if not present
425      * @throws XMLSecurityException
426      */
427     public XMLX509CRL itemCRL(int i) throws XMLSecurityException {
428 
429         Element e =
430             XMLUtils.selectDsNode(
431                 this.constructionElement.getFirstChild(), Constants._TAG_X509CRL, i);
432 
433         if (e != null) {
434             return new XMLX509CRL(e, this.baseURI);
435         }
436         return null;
437     }
438 
439     /**
440      * Method itemDigest
441      *
442      * @param i
443      * @return the X509Digest, null if not present
444      * @throws XMLSecurityException
445      */
446     public XMLX509Digest itemDigest(int i) throws XMLSecurityException {
447 
448         Element e =
449             XMLUtils.selectDs11Node(
450                 this.constructionElement.getFirstChild(), Constants._TAG_X509DIGEST, i);
451 
452         if (e != null) {
453             return new XMLX509Digest(e, this.baseURI);
454         }
455         return null;
456     }
457 
458     /**
459      * Method itemUnknownElement
460      *
461      * @param i
462      * @return the Unknown Element at i
463      * TODO implement
464      **/
465     public Element itemUnknownElement(int i) {
466         if (log.isLoggable(java.util.logging.Level.FINE)) {
467             log.log(java.util.logging.Level.FINE, "itemUnknownElement not implemented:" + i);
468         }
469         return null;
470     }
471 
472     /**
473      * Method containsIssuerSerial
474      *
475      * @return true if this X509Data contains a IssuerSerial
476      */
477     public boolean containsIssuerSerial() {
478         return this.lengthIssuerSerial() > 0;
479     }
480 
481     /**
482      * Method containsSKI
483      *
484      * @return true if this X509Data contains a SKI
485      */
486     public boolean containsSKI() {
487         return this.lengthSKI() > 0;
488     }
489 
490     /**
491      * Method containsSubjectName
492      *
493      * @return true if this X509Data contains a SubjectName
494      */
495     public boolean containsSubjectName() {
496         return this.lengthSubjectName() > 0;
497     }
498 
499     /**
500      * Method containsCertificate
501      *
502      * @return true if this X509Data contains a Certificate
503      */
504     public boolean containsCertificate() {
505         return this.lengthCertificate() > 0;
506     }
507 
508     /**
509      * Method containsDigest
510      *
511      * @return true if this X509Data contains an X509Digest
512      */
513     public boolean containsDigest() {
514         return this.lengthDigest() > 0;
515     }
516 
517     /**
518      * Method containsCRL
519      *
520      * @return true if this X509Data contains a CRL
521      */
522     public boolean containsCRL() {
523         return this.lengthCRL() > 0;
524     }
525 
526     /**
527      * Method containsUnknownElement
528      *
529      * @return true if this X509Data contains an UnknownElement
530      */
531     public boolean containsUnknownElement() {
532         return this.lengthUnknownElement() > 0;
533     }
534 
535     /** @inheritDoc */
536     public String getBaseLocalName() {
537         return Constants._TAG_X509DATA;
538     }
539 }