KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > content > X509Data


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.keys.content;
18
19
20
21 import java.math.BigInteger JavaDoc;
22 import java.security.cert.X509Certificate JavaDoc;
23
24 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
25 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509CRL;
26 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate;
27 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509IssuerSerial;
28 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI;
29 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SubjectName;
30 import com.sun.org.apache.xml.internal.security.utils.Constants;
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 JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37
38 /**
39  *
40  * @author $Author: raul $
41  */

42 public class X509Data extends SignatureElementProxy implements KeyInfoContent {
43
44    /** {@link java.util.logging} logging facility */
45     static java.util.logging.Logger JavaDoc log =
46         java.util.logging.Logger.getLogger(X509Data.class.getName());
47
48    /**
49     * Constructor X509Data
50     *
51     * @param doc
52     */

53    public X509Data(Document JavaDoc doc) {
54
55       super(doc);
56
57       XMLUtils.addReturnToElement(this._constructionElement);
58    }
59
60    /**
61     * Constructor X509Data
62     *
63     * @param element
64     * @param BaseURI
65     * @throws XMLSecurityException
66     */

67    public X509Data(Element JavaDoc element, String JavaDoc BaseURI)
68            throws XMLSecurityException {
69
70       super(element, BaseURI);
71       
72       boolean noElements=true;
73       Node JavaDoc sibling=this._constructionElement.getFirstChild();
74       while (sibling!=null) {
75          if (sibling.getNodeType()!=Node.ELEMENT_NODE) {
76             sibling=sibling.getNextSibling();
77             continue;
78          }
79         noElements=false;
80          Element JavaDoc currentElem = (Element JavaDoc) sibling;
81          sibling=sibling.getNextSibling();
82          String JavaDoc localname = currentElem.getLocalName();
83
84          if (currentElem.getNamespaceURI().equals(Constants.SignatureSpecNS)) {
85             if (localname.equals(Constants._TAG_X509ISSUERSERIAL)) {
86                XMLX509IssuerSerial is = new XMLX509IssuerSerial(currentElem,
87                                            BaseURI);
88
89                this.add(is);
90             } else if (localname.equals(Constants._TAG_X509SKI)) {
91                XMLX509SKI ski = new XMLX509SKI(currentElem, BaseURI);
92
93                this.add(ski);
94             } else if (localname.equals(Constants._TAG_X509SUBJECTNAME)) {
95                XMLX509SubjectName sn = new XMLX509SubjectName(currentElem,
96                                           BaseURI);
97
98                this.add(sn);
99             } else if (localname.equals(Constants._TAG_X509CERTIFICATE)) {
100                XMLX509Certificate cert = new XMLX509Certificate(currentElem,
101                                             BaseURI);
102
103                this.add(cert);
104             } else if (localname.equals(Constants._TAG_X509CRL)) {
105                XMLX509CRL crl = new XMLX509CRL(currentElem, BaseURI);
106
107                this.add(crl);
108             } else {
109                log.log(java.util.logging.Level.WARNING, "Found a " + currentElem.getTagName() + " element in "
110                         + Constants._TAG_X509DATA);
111                this.addUnknownElement(currentElem);
112             }
113          } else {
114             log.log(java.util.logging.Level.WARNING, "Found a " + currentElem.getTagName() + " element in "
115                      + Constants._TAG_X509DATA);
116             this.addUnknownElement(currentElem);
117          }
118       }
119       if (noElements) {
120         Object JavaDoc exArgs[] = { "Elements", Constants._TAG_X509DATA };
121
122         throw new XMLSecurityException("xml.WrongContent", exArgs);
123      }
124
125    }
126
127    /**
128     * Method addIssuerSerial
129     *
130     * @param X509IssuerName
131     * @param X509SerialNumber
132     */

133    public void addIssuerSerial(String JavaDoc X509IssuerName,
134                                BigInteger JavaDoc X509SerialNumber) {
135       this.add(new XMLX509IssuerSerial(this._doc, X509IssuerName,
136                                        X509SerialNumber));
137    }
138
139    /**
140     * Method addIssuerSerial
141     *
142     * @param X509IssuerName
143     * @param X509SerialNumber
144     */

145    public void addIssuerSerial(String JavaDoc X509IssuerName, String JavaDoc X509SerialNumber) {
146       this.add(new XMLX509IssuerSerial(this._doc, X509IssuerName,
147                                        X509SerialNumber));
148    }
149
150    /**
151     * Method addIssuerSerial
152     *
153     * @param X509IssuerName
154     * @param X509SerialNumber
155     */

156    public void addIssuerSerial(String JavaDoc X509IssuerName, int X509SerialNumber) {
157       this.add(new XMLX509IssuerSerial(this._doc, X509IssuerName,
158                                        X509SerialNumber));
159    }
160
161    /**
162     * Method add
163     *
164     * @param xmlX509IssuerSerial
165     */

166    public void add(XMLX509IssuerSerial xmlX509IssuerSerial) {
167
168       if (this._state == MODE_SIGN) {
169          this._constructionElement
170             .appendChild(xmlX509IssuerSerial.getElement());
171          XMLUtils.addReturnToElement(this._constructionElement);
172       }
173    }
174
175    /**
176     * Method addSKI
177     *
178     * @param skiBytes
179     */

180    public void addSKI(byte[] skiBytes) {
181       this.add(new XMLX509SKI(this._doc, skiBytes));
182    }
183
184    /**
185     * Method addSKI
186     *
187     * @param x509certificate
188     * @throws XMLSecurityException
189     */

190    public void addSKI(X509Certificate JavaDoc x509certificate)
191            throws XMLSecurityException {
192       this.add(new XMLX509SKI(this._doc, x509certificate));
193    }
194
195    /**
196     * Method add
197     *
198     * @param xmlX509SKI
199     */

200    public void add(XMLX509SKI xmlX509SKI) {
201
202       if (this._state == MODE_SIGN) {
203          this._constructionElement.appendChild(xmlX509SKI.getElement());
204          XMLUtils.addReturnToElement(this._constructionElement);
205       }
206    }
207
208    /**
209     * Method addSubjectName
210     *
211     * @param subjectName
212     */

213    public void addSubjectName(String JavaDoc subjectName) {
214       this.add(new XMLX509SubjectName(this._doc, subjectName));
215    }
216
217    /**
218     * Method addSubjectName
219     *
220     * @param x509certificate
221     */

222    public void addSubjectName(X509Certificate JavaDoc x509certificate) {
223       this.add(new XMLX509SubjectName(this._doc, x509certificate));
224    }
225
226    /**
227     * Method add
228     *
229     * @param xmlX509SubjectName
230     */

231    public void add(XMLX509SubjectName xmlX509SubjectName) {
232
233       if (this._state == MODE_SIGN) {
234          this._constructionElement.appendChild(xmlX509SubjectName.getElement());
235          XMLUtils.addReturnToElement(this._constructionElement);
236       }
237    }
238
239    /**
240     * Method addCertificate
241     *
242     * @param x509certificate
243     * @throws XMLSecurityException
244     */

245    public void addCertificate(X509Certificate JavaDoc x509certificate)
246            throws XMLSecurityException {
247       this.add(new XMLX509Certificate(this._doc, x509certificate));
248    }
249
250    /**
251     * Method addCertificate
252     *
253     * @param x509certificateBytes
254     */

255    public void addCertificate(byte[] x509certificateBytes) {
256       this.add(new XMLX509Certificate(this._doc, x509certificateBytes));
257    }
258
259    /**
260     * Method add
261     *
262     * @param xmlX509Certificate
263     */

264    public void add(XMLX509Certificate xmlX509Certificate) {
265
266       if (this._state == MODE_SIGN) {
267          this._constructionElement.appendChild(xmlX509Certificate.getElement());
268          XMLUtils.addReturnToElement(this._constructionElement);
269       }
270    }
271
272    /**
273     * Method addCRL
274     *
275     * @param crlBytes
276     */

277    public void addCRL(byte[] crlBytes) {
278       this.add(new XMLX509CRL(this._doc, crlBytes));
279    }
280
281    /**
282     * Method add
283     *
284     * @param xmlX509CRL
285     */

286    public void add(XMLX509CRL xmlX509CRL) {
287
288       if (this._state == MODE_SIGN) {
289          this._constructionElement.appendChild(xmlX509CRL.getElement());
290          XMLUtils.addReturnToElement(this._constructionElement);
291       }
292    }
293
294    /**
295     * Method addUnknownElement
296     *
297     * @param element
298     */

299    public void addUnknownElement(Element JavaDoc element) {
300
301       if (this._state == MODE_SIGN) {
302          this._constructionElement.appendChild(element);
303          XMLUtils.addReturnToElement(this._constructionElement);
304       }
305    }
306
307    /**
308     * Method lengthIssuerSerial
309     *
310     * @return the number of IssuerSerial elements in this X509Data
311     */

312    public int lengthIssuerSerial() {
313       return this.length(Constants.SignatureSpecNS,
314                          Constants._TAG_X509ISSUERSERIAL);
315    }
316
317    /**
318     * Method lengthSKI
319     *
320     * @return the number of SKI elements in this X509Data
321     */

322    public int lengthSKI() {
323       return this.length(Constants.SignatureSpecNS, Constants._TAG_X509SKI);
324    }
325
326    /**
327     * Method lengthSubjectName
328     *
329     * @return the number of SubjectName elements in this X509Data
330     */

331    public int lengthSubjectName() {
332       return this.length(Constants.SignatureSpecNS,
333                          Constants._TAG_X509SUBJECTNAME);
334    }
335
336    /**
337     * Method lengthCertificate
338     *
339     * @return the number of Certificate elements in this X509Data
340     */

341    public int lengthCertificate() {
342       return this.length(Constants.SignatureSpecNS,
343                          Constants._TAG_X509CERTIFICATE);
344    }
345
346    /**
347     * Method lengthCRL
348     *
349     * @return the number of CRL elements in this X509Data
350     */

351    public int lengthCRL() {
352       return this.length(Constants.SignatureSpecNS, Constants._TAG_X509CRL);
353    }
354
355    /**
356     * Method lengthUnknownElement
357     *
358     * @return the number of UnknownElement elements in this X509Data
359     */

360    public int lengthUnknownElement() {
361       
362       int result = 0;
363       Node JavaDoc n=this._constructionElement.getFirstChild();
364       while (n!=null){
365
366          if ((n.getNodeType() == Node.ELEMENT_NODE)
367                  &&!n.getNamespaceURI().equals(Constants.SignatureSpecNS)) {
368             result += 1;
369          }
370          n=n.getNextSibling();
371       }
372       
373       return result;
374    }
375
376    /**
377     * Method itemIssuerSerial
378     *
379     * @param i
380     * @return the X509IssuerSerial, null if not present
381     * @throws XMLSecurityException
382     */

383    public XMLX509IssuerSerial itemIssuerSerial(int i)
384            throws XMLSecurityException {
385
386       Element JavaDoc e =
387         XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
388                                        Constants._TAG_X509ISSUERSERIAL,i);
389
390       if (e != null) {
391          return new XMLX509IssuerSerial(e, this._baseURI);
392       }
393       return null;
394    }
395
396    /**
397     * Method itemSKI
398     *
399     * @param i
400     * @return the X509SKI, null if not present
401     * @throws XMLSecurityException
402     */

403    public XMLX509SKI itemSKI(int i) throws XMLSecurityException {
404
405       Element JavaDoc e = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
406                                                 Constants._TAG_X509SKI,i);
407
408       if (e != null) {
409          return new XMLX509SKI(e, this._baseURI);
410       }
411       return null;
412    }
413
414    /**
415     * Method itemSubjectName
416     *
417     * @param i
418     * @return the X509SubjectName, null if not present
419     * @throws XMLSecurityException
420     */

421    public XMLX509SubjectName itemSubjectName(int i)
422            throws XMLSecurityException {
423
424       Element JavaDoc e = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
425                                                 Constants._TAG_X509SUBJECTNAME,i);
426
427       if (e != null) {
428          return new XMLX509SubjectName(e, this._baseURI);
429       }
430        return null;
431    }
432
433    /**
434     * Method itemCertificate
435     *
436     * @param i
437     * @return the X509Certifacte, null if not present
438     * @throws XMLSecurityException
439     */

440    public XMLX509Certificate itemCertificate(int i)
441            throws XMLSecurityException {
442
443       Element JavaDoc e = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
444                                                 Constants._TAG_X509CERTIFICATE,i);
445
446       if (e != null) {
447          return new XMLX509Certificate(e, this._baseURI);
448       }
449        return null;
450    }
451
452    /**
453     * Method itemCRL
454     *
455     * @param i
456     * @return the X509CRL, null if not present
457     * @throws XMLSecurityException
458     */

459    public XMLX509CRL itemCRL(int i) throws XMLSecurityException {
460
461       Element JavaDoc e = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(),
462                                                 Constants._TAG_X509CRL,i);
463
464       if (e != null) {
465          return new XMLX509CRL(e, this._baseURI);
466       }
467        return null;
468    }
469
470    /**
471     * Method itemUnknownElement
472     *
473     * @param i
474     * @return the Unknown Element at i
475     * TODO implement
476     **/

477    public Element JavaDoc itemUnknownElement(int i) {
478       if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "itemUnknownElement not implemented:"+i);
479       return null;
480    }
481
482    /**
483     * Method containsIssuerSerial
484     *
485     * @return true if this X509Data contains a IssuerSerial
486     */

487    public boolean containsIssuerSerial() {
488       return this.lengthIssuerSerial() > 0;
489    }
490
491    /**
492     * Method containsSKI
493     *
494     * @return true if this X509Data contains a SKI
495     */

496    public boolean containsSKI() {
497       return this.lengthSKI() > 0;
498    }
499
500    /**
501     * Method containsSubjectName
502     *
503     * @return true if this X509Data contains a SubjectName
504     */

505    public boolean containsSubjectName() {
506       return this.lengthSubjectName() > 0;
507    }
508
509    /**
510     * Method containsCertificate
511     *
512     * @return true if this X509Data contains a Certificate
513     */

514    public boolean containsCertificate() {
515       return this.lengthCertificate() > 0;
516    }
517
518    /**
519     * Method containsCRL
520     *
521     * @return true if this X509Data contains a CRL
522     */

523    public boolean containsCRL() {
524       return this.lengthCRL() > 0;
525    }
526
527    /**
528     * Method containsUnknownElement
529     *
530     * @return true if this X509Data contains an UnknownElement
531     */

532    public boolean containsUnknownElement() {
533       return this.lengthUnknownElement() > 0;
534    }
535
536    /** @inheritDoc */
537    public String JavaDoc getBaseLocalName() {
538       return Constants._TAG_X509DATA;
539    }
540 }
541
Popular Tags