1 17 package com.sun.org.apache.xml.internal.security.signature; 18 19 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 import javax.xml.parsers.ParserConfigurationException ; 29 30 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 31 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; 32 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 33 import com.sun.org.apache.xml.internal.security.transforms.Transforms; 34 import com.sun.org.apache.xml.internal.security.utils.Constants; 35 import com.sun.org.apache.xml.internal.security.utils.I18n; 36 import com.sun.org.apache.xml.internal.security.utils.IdResolver; 37 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy; 38 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 39 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver; 40 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi; 41 import org.w3c.dom.DOMException ; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.xml.sax.SAXException ; 46 47 48 49 54 public class Manifest extends SignatureElementProxy { 55 56 57 static java.util.logging.Logger log = 58 java.util.logging.Logger.getLogger(Manifest.class.getName()); 59 60 61 List _references; 62 Element [] _referencesEl; 63 64 65 private boolean verificationResults[] = null; 66 67 68 List _signedContents = new ArrayList (); 69 70 71 HashMap _resolverProperties = new HashMap (10); 72 73 74 List _perManifestResolvers = new ArrayList (); 75 76 81 public Manifest(Document doc) { 82 83 super(doc); 84 85 XMLUtils.addReturnToElement(this._constructionElement); 86 87 this._references = new ArrayList (); 88 } 89 90 97 public Manifest(Element element, String BaseURI) 98 throws XMLSecurityException { 99 100 super(element, BaseURI); 101 102 this._referencesEl = XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(), 104 Constants._TAG_REFERENCE); 105 int le = this._referencesEl.length; 106 { 107 if (le == 0) { 108 109 Object exArgs[] = { Constants._TAG_REFERENCE, 111 Constants._TAG_MANIFEST }; 112 113 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, 114 I18n.translate("xml.WrongContent", exArgs)); 115 } 116 } 117 118 this._references = new ArrayList (le); 120 121 for (int i = 0; i < le; i++) { 122 this._references.add(null); 123 } 124 } 125 126 139 public void addDocument( 140 String BaseURI, String referenceURI, Transforms transforms, String digestURI, String ReferenceId, String ReferenceType) 141 throws XMLSignatureException { 142 143 if (this._state == MODE_SIGN) { 144 145 Reference ref = new Reference(this._doc, BaseURI, referenceURI, this, 147 transforms, digestURI); 148 149 if (ReferenceId != null) { 150 ref.setId(ReferenceId); 151 } 152 153 if (ReferenceType != null) { 154 ref.setType(ReferenceType); 155 } 156 157 this._references.add(ref); 159 160 this._constructionElement.appendChild(ref.getElement()); 162 XMLUtils.addReturnToElement(this._constructionElement); 163 } 164 } 165 166 174 public void generateDigestValues() 175 throws XMLSignatureException, ReferenceNotInitializedException { 176 177 if (this._state == MODE_SIGN) { 178 for (int i = 0; i < this.getLength(); i++) { 179 180 Reference currentRef = (Reference) this._references.get(i); 182 183 currentRef.generateDigestValue(); 184 } 185 } 186 } 187 188 193 public int getLength() { 194 return this._references.size(); 195 } 196 197 205 public Reference item(int i) throws XMLSecurityException { 206 207 if (this._state == MODE_SIGN) { 208 209 return (Reference) this._references.get(i); 211 } 212 if (this._references.get(i) == null) { 213 214 Reference ref = new Reference(_referencesEl[i], this._baseURI, this); 216 217 this._references.set(i, ref); 218 } 219 220 return (Reference) this._references.get(i); 221 222 } 223 224 229 public void setId(String Id) { 230 231 if ((this._state == MODE_SIGN) && (Id != null)) { 232 this._constructionElement.setAttributeNS(null, Constants._ATT_ID, Id); 233 IdResolver.registerElementById(this._constructionElement, Id); 234 } 235 } 236 237 242 public String getId() { 243 return this._constructionElement.getAttributeNS(null, Constants._ATT_ID); 244 } 245 246 263 public boolean verifyReferences() 264 throws MissingResourceFailureException, XMLSecurityException { 265 return this.verifyReferences(false); 266 } 267 268 286 public boolean verifyReferences(boolean followManifests) 287 throws MissingResourceFailureException, XMLSecurityException { 288 if (_referencesEl==null) { 289 this._referencesEl = 290 XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(), 291 Constants._TAG_REFERENCE); 292 } 293 if (true) { 294 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "verify " +_referencesEl.length + " References"); 295 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I am " + (followManifests 296 ? "" 297 : "not") + " requested to follow nested Manifests"); 298 } 299 boolean verify = true; 300 301 if (_referencesEl.length==0) { 302 throw new XMLSecurityException("empty"); 303 } 304 305 this.verificationResults = 306 new boolean[_referencesEl.length]; 307 308 for (int i = 309 0; i < this._referencesEl.length; i++) { 310 Reference currentRef = 311 new Reference(_referencesEl[i], this._baseURI, this); 312 313 this._references.set(i, currentRef); 314 315 316 try { 317 boolean currentRefVerified = currentRef.verify(); 318 319 this.setVerificationResult(i, currentRefVerified); 320 321 if (!currentRefVerified) { 322 verify = false; 323 } 324 if (true) 325 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "The Reference has Type " + currentRef.getType()); 326 327 if (verify && followManifests 329 && currentRef.typeIsReferenceToManifest()) { 330 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "We have to follow a nested Manifest"); 331 332 try { 333 XMLSignatureInput signedManifestNodes = 334 currentRef.dereferenceURIandPerformTransforms(null); 335 Set nl = signedManifestNodes.getNodeSet(); 336 Manifest referencedManifest = null; 337 Iterator nlIterator = nl.iterator(); 338 339 findManifest: while (nlIterator.hasNext()) { 340 Node n = (Node ) nlIterator.next(); 341 342 if ((n.getNodeType() == Node.ELEMENT_NODE) && ((Element ) n) 343 .getNamespaceURI() 344 .equals(Constants.SignatureSpecNS) && ((Element ) n) 345 .getLocalName().equals(Constants._TAG_MANIFEST)) { 346 try { 347 referencedManifest = 348 new Manifest((Element ) n, 349 signedManifestNodes.getSourceURI()); 350 351 break findManifest; 352 } catch (XMLSecurityException ex) { 353 354 } 356 } 357 } 358 359 if (referencedManifest == null) { 360 361 throw new MissingResourceFailureException("empty", 364 currentRef); 365 } 366 367 referencedManifest._perManifestResolvers = 368 this._perManifestResolvers; 369 referencedManifest._resolverProperties = 370 this._resolverProperties; 371 372 boolean referencedManifestValid = 373 referencedManifest.verifyReferences(followManifests); 374 375 if (!referencedManifestValid) { 376 verify = false; 377 378 log.log(java.util.logging.Level.WARNING, "The nested Manifest was invalid (bad)"); 379 } else { 380 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "The nested Manifest was valid (good)"); 381 } 382 } catch (IOException ex) { 383 throw new ReferenceNotInitializedException("empty", ex); 384 } catch (ParserConfigurationException ex) { 385 throw new ReferenceNotInitializedException("empty", ex); 386 } catch (SAXException ex) { 387 throw new ReferenceNotInitializedException("empty", ex); 388 } 389 } 390 } catch (ReferenceNotInitializedException ex) { 391 Object exArgs[] = { currentRef.getURI() }; 392 393 throw new MissingResourceFailureException( 394 "signature.Verification.Reference.NoInput", exArgs, ex, 395 currentRef); 396 } 397 } 398 399 return verify; 400 } 401 402 408 private void setVerificationResult(int index, boolean verify) 409 { 410 411 if (this.verificationResults == null) { 412 this.verificationResults = new boolean[this.getLength()]; 413 } 414 415 this.verificationResults[index] = verify; 416 } 417 418 427 public boolean getVerificationResult(int index) throws XMLSecurityException { 428 429 if ((index < 0) || (index > this.getLength() - 1)) { 430 Object exArgs[] = { Integer.toString(index), 431 Integer.toString(this.getLength()) }; 432 Exception e = 433 new IndexOutOfBoundsException (I18n 434 .translate("signature.Verification.IndexOutOfBounds", exArgs)); 435 436 throw new XMLSecurityException("generic.EmptyMessage", e); 437 } 438 439 if (this.verificationResults == null) { 440 try { 441 this.verifyReferences(); 442 } catch (Exception ex) { 443 throw new XMLSecurityException("generic.EmptyMessage", ex); 444 } 445 } 446 447 return this.verificationResults[index]; 448 } 449 450 455 public void addResourceResolver(ResourceResolver resolver) { 456 457 if (resolver != null) { 458 this._perManifestResolvers.add(resolver); 459 } 460 } 461 462 467 public void addResourceResolver(ResourceResolverSpi resolverSpi) { 468 469 if (resolverSpi != null) { 470 this._perManifestResolvers.add(new ResourceResolver(resolverSpi)); 471 } 472 } 473 474 481 public void setResolverProperty(String key, String value) { 482 this._resolverProperties.put(key, value); 483 } 484 485 491 public String getResolverProperty(String key) { 492 return (String ) this._resolverProperties.get(key); 493 } 494 495 503 public byte[] getSignedContentItem(int i) throws XMLSignatureException { 504 505 try { 506 return this.getReferencedContentAfterTransformsItem(i).getBytes(); 507 } catch (IOException ex) { 508 throw new XMLSignatureException("empty", ex); 509 } catch (CanonicalizationException ex) { 510 throw new XMLSignatureException("empty", ex); 511 } catch (InvalidCanonicalizerException ex) { 512 throw new XMLSignatureException("empty", ex); 513 } catch (XMLSecurityException ex) { 514 throw new XMLSignatureException("empty", ex); 515 } 516 } 517 518 525 public XMLSignatureInput getReferencedContentBeforeTransformsItem(int i) 526 throws XMLSecurityException { 527 return this.item(i).getContentsBeforeTransformation(); 528 } 529 530 537 public XMLSignatureInput getReferencedContentAfterTransformsItem(int i) 538 throws XMLSecurityException { 539 return this.item(i).getContentsAfterTransformation(); 540 } 541 542 547 public int getSignedContentLength() { 548 return this.getLength(); 549 } 550 551 556 public String getBaseLocalName() { 557 return Constants._TAG_MANIFEST; 558 } 559 } 560 | Popular Tags |