1 17 package com.sun.org.apache.xml.internal.security.keys.content; 18 19 20 21 import java.math.BigInteger ; 22 import java.security.cert.X509Certificate ; 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 ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 37 38 42 public class X509Data extends SignatureElementProxy implements KeyInfoContent { 43 44 45 static java.util.logging.Logger log = 46 java.util.logging.Logger.getLogger(X509Data.class.getName()); 47 48 53 public X509Data(Document doc) { 54 55 super(doc); 56 57 XMLUtils.addReturnToElement(this._constructionElement); 58 } 59 60 67 public X509Data(Element element, String BaseURI) 68 throws XMLSecurityException { 69 70 super(element, BaseURI); 71 72 boolean noElements=true; 73 Node 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 currentElem = (Element ) sibling; 81 sibling=sibling.getNextSibling(); 82 String 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 exArgs[] = { "Elements", Constants._TAG_X509DATA }; 121 122 throw new XMLSecurityException("xml.WrongContent", exArgs); 123 } 124 125 } 126 127 133 public void addIssuerSerial(String X509IssuerName, 134 BigInteger X509SerialNumber) { 135 this.add(new XMLX509IssuerSerial(this._doc, X509IssuerName, 136 X509SerialNumber)); 137 } 138 139 145 public void addIssuerSerial(String X509IssuerName, String X509SerialNumber) { 146 this.add(new XMLX509IssuerSerial(this._doc, X509IssuerName, 147 X509SerialNumber)); 148 } 149 150 156 public void addIssuerSerial(String X509IssuerName, int X509SerialNumber) { 157 this.add(new XMLX509IssuerSerial(this._doc, X509IssuerName, 158 X509SerialNumber)); 159 } 160 161 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 180 public void addSKI(byte[] skiBytes) { 181 this.add(new XMLX509SKI(this._doc, skiBytes)); 182 } 183 184 190 public void addSKI(X509Certificate x509certificate) 191 throws XMLSecurityException { 192 this.add(new XMLX509SKI(this._doc, x509certificate)); 193 } 194 195 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 213 public void addSubjectName(String subjectName) { 214 this.add(new XMLX509SubjectName(this._doc, subjectName)); 215 } 216 217 222 public void addSubjectName(X509Certificate x509certificate) { 223 this.add(new XMLX509SubjectName(this._doc, x509certificate)); 224 } 225 226 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 245 public void addCertificate(X509Certificate x509certificate) 246 throws XMLSecurityException { 247 this.add(new XMLX509Certificate(this._doc, x509certificate)); 248 } 249 250 255 public void addCertificate(byte[] x509certificateBytes) { 256 this.add(new XMLX509Certificate(this._doc, x509certificateBytes)); 257 } 258 259 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 277 public void addCRL(byte[] crlBytes) { 278 this.add(new XMLX509CRL(this._doc, crlBytes)); 279 } 280 281 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 299 public void addUnknownElement(Element element) { 300 301 if (this._state == MODE_SIGN) { 302 this._constructionElement.appendChild(element); 303 XMLUtils.addReturnToElement(this._constructionElement); 304 } 305 } 306 307 312 public int lengthIssuerSerial() { 313 return this.length(Constants.SignatureSpecNS, 314 Constants._TAG_X509ISSUERSERIAL); 315 } 316 317 322 public int lengthSKI() { 323 return this.length(Constants.SignatureSpecNS, Constants._TAG_X509SKI); 324 } 325 326 331 public int lengthSubjectName() { 332 return this.length(Constants.SignatureSpecNS, 333 Constants._TAG_X509SUBJECTNAME); 334 } 335 336 341 public int lengthCertificate() { 342 return this.length(Constants.SignatureSpecNS, 343 Constants._TAG_X509CERTIFICATE); 344 } 345 346 351 public int lengthCRL() { 352 return this.length(Constants.SignatureSpecNS, Constants._TAG_X509CRL); 353 } 354 355 360 public int lengthUnknownElement() { 361 362 int result = 0; 363 Node 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 383 public XMLX509IssuerSerial itemIssuerSerial(int i) 384 throws XMLSecurityException { 385 386 Element 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 403 public XMLX509SKI itemSKI(int i) throws XMLSecurityException { 404 405 Element 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 421 public XMLX509SubjectName itemSubjectName(int i) 422 throws XMLSecurityException { 423 424 Element 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 440 public XMLX509Certificate itemCertificate(int i) 441 throws XMLSecurityException { 442 443 Element 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 459 public XMLX509CRL itemCRL(int i) throws XMLSecurityException { 460 461 Element 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 477 public Element 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 487 public boolean containsIssuerSerial() { 488 return this.lengthIssuerSerial() > 0; 489 } 490 491 496 public boolean containsSKI() { 497 return this.lengthSKI() > 0; 498 } 499 500 505 public boolean containsSubjectName() { 506 return this.lengthSubjectName() > 0; 507 } 508 509 514 public boolean containsCertificate() { 515 return this.lengthCertificate() > 0; 516 } 517 518 523 public boolean containsCRL() { 524 return this.lengthCRL() > 0; 525 } 526 527 532 public boolean containsUnknownElement() { 533 return this.lengthUnknownElement() > 0; 534 } 535 536 537 public String getBaseLocalName() { 538 return Constants._TAG_X509DATA; 539 } 540 } 541 | Popular Tags |