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