1 2 18 package com.sun.org.apache.xml.internal.security.signature; 19 20 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 import com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm; 28 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 29 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; 30 import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; 31 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 32 import com.sun.org.apache.xml.internal.security.transforms.InvalidTransformException; 33 import com.sun.org.apache.xml.internal.security.transforms.Transform; 34 import com.sun.org.apache.xml.internal.security.transforms.TransformationException; 35 import com.sun.org.apache.xml.internal.security.transforms.Transforms; 36 import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces; 37 import com.sun.org.apache.xml.internal.security.utils.Base64; 38 import com.sun.org.apache.xml.internal.security.utils.Constants; 39 import com.sun.org.apache.xml.internal.security.utils.DigesterOutputStream; 40 import com.sun.org.apache.xml.internal.security.utils.IdResolver; 41 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy; 42 import com.sun.org.apache.xml.internal.security.utils.UnsyncBufferedOutputStream; 43 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 44 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolver; 45 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException; 46 import org.w3c.dom.Attr ; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.Text ; 51 52 53 99 public class Reference extends SignatureElementProxy { 100 101 102 static java.util.logging.Logger log = 103 java.util.logging.Logger.getLogger(Reference.class.getName()); 104 105 106 public static final String OBJECT_URI = Constants.SignatureSpecNS 107 + Constants._TAG_OBJECT; 108 109 110 public static final String MANIFEST_URI = Constants.SignatureSpecNS 111 + Constants._TAG_MANIFEST; 112 Manifest _manifest = null; 114 XMLSignatureInput _transformsOutput; 115 117 129 protected Reference(Document doc, String BaseURI, String ReferenceURI, Manifest manifest, Transforms transforms, String messageDigestAlgorithm) 130 throws XMLSignatureException { 131 132 super(doc); 133 134 XMLUtils.addReturnToElement(this._constructionElement); 135 136 this._baseURI = BaseURI; 137 this._manifest = manifest; 138 139 this.setURI(ReferenceURI); 140 141 146 if (transforms != null) { 147 this._constructionElement.appendChild(transforms.getElement()); 148 XMLUtils.addReturnToElement(this._constructionElement); 149 } 150 { 151 MessageDigestAlgorithm mda = 152 MessageDigestAlgorithm.getInstance(this._doc, 153 messageDigestAlgorithm); 154 155 this._constructionElement.appendChild(mda.getElement()); 156 XMLUtils.addReturnToElement(this._constructionElement); 157 } 158 { 159 Element digestValueElement = 160 XMLUtils.createElementInSignatureSpace(this._doc, 161 Constants._TAG_DIGESTVALUE); 162 163 this._constructionElement.appendChild(digestValueElement); 164 XMLUtils.addReturnToElement(this._constructionElement); 165 } 166 } 167 168 169 177 protected Reference(Element element, String BaseURI, Manifest manifest) 178 throws XMLSecurityException { 179 180 super(element, BaseURI); 181 182 this._manifest = manifest; 183 } 184 185 193 public MessageDigestAlgorithm getMessageDigestAlgorithm() 194 throws XMLSignatureException { 195 196 Element digestMethodElem = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), 197 Constants._TAG_DIGESTMETHOD,0); 198 199 if (digestMethodElem == null) { 200 return null; 201 } 202 203 String uri = digestMethodElem.getAttributeNS(null, 204 Constants._ATT_ALGORITHM); 205 206 if (uri == null) { 207 return null; 208 } 209 210 return MessageDigestAlgorithm.getInstance(this._doc, uri); 211 } 212 213 218 public void setURI(String URI) { 219 220 if ((this._state == MODE_SIGN) && (URI != null)) { 221 this._constructionElement.setAttributeNS(null, Constants._ATT_URI, 222 URI); 223 } 224 } 225 226 231 public String getURI() { 232 return this._constructionElement.getAttributeNS(null, Constants._ATT_URI); 233 } 234 235 240 public void setId(String Id) { 241 242 if ((this._state == MODE_SIGN) && (Id != null)) { 243 this._constructionElement.setAttributeNS(null, Constants._ATT_ID, Id); 244 IdResolver.registerElementById(this._constructionElement, Id); 245 } 246 } 247 248 253 public String getId() { 254 return this._constructionElement.getAttributeNS(null, Constants._ATT_ID); 255 } 256 257 262 public void setType(String Type) { 263 264 if ((this._state == MODE_SIGN) && (Type != null)) { 265 this._constructionElement.setAttributeNS(null, Constants._ATT_TYPE, 266 Type); 267 } 268 } 269 270 275 public String getType() { 276 return this._constructionElement.getAttributeNS(null, 277 Constants._ATT_TYPE); 278 } 279 280 288 public boolean typeIsReferenceToObject() { 289 290 if ((this.getType() != null) 291 && this.getType().equals(Reference.OBJECT_URI)) { 292 return true; 293 } 294 295 return false; 296 } 297 298 306 public boolean typeIsReferenceToManifest() { 307 308 if ((this.getType() != null) 309 && this.getType().equals(Reference.MANIFEST_URI)) { 310 return true; 311 } 312 313 return false; 314 } 315 316 321 private void setDigestValueElement(byte[] digestValue) 322 { 323 324 if (this._state == MODE_SIGN) { 325 Element digestValueElement =XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), 326 Constants._TAG_DIGESTVALUE,0); 327 Node n=digestValueElement.getFirstChild(); 328 while (n!=null) { 329 digestValueElement.removeChild(n); 330 n = n.getNextSibling(); 331 } 332 333 String base64codedValue = Base64.encode(digestValue); 334 Text t = this._doc.createTextNode(base64codedValue); 335 336 digestValueElement.appendChild(t); 337 } 338 } 339 340 346 public void generateDigestValue() 347 throws XMLSignatureException, ReferenceNotInitializedException { 348 349 if (this._state == MODE_SIGN) { 350 351 this.setDigestValueElement(this.calculateDigest()); 352 } 353 } 354 355 361 public XMLSignatureInput getContentsBeforeTransformation() 362 throws ReferenceNotInitializedException { 363 364 try { 365 Attr URIAttr = this._constructionElement.getAttributeNodeNS(null, 366 Constants._ATT_URI); 367 String URI; 368 369 if (URIAttr == null) { 370 URI = null; 371 } else { 372 URI = URIAttr.getNodeValue(); 373 } 374 375 ResourceResolver resolver = ResourceResolver.getInstance(URIAttr, 376 this._baseURI, this._manifest._perManifestResolvers); 377 378 if (resolver == null) { 379 Object exArgs[] = { URI }; 380 381 throw new ReferenceNotInitializedException( 382 "signature.Verification.Reference.NoInput", exArgs); 383 } 384 385 resolver.addProperties(this._manifest._resolverProperties); 386 387 XMLSignatureInput input = resolver.resolve(URIAttr, this._baseURI); 388 389 390 return input; 391 } catch (ResourceResolverException ex) { 392 throw new ReferenceNotInitializedException("empty", ex); 393 } catch (XMLSecurityException ex) { 394 throw new ReferenceNotInitializedException("empty", ex); 395 } 396 } 397 398 406 public XMLSignatureInput getTransformsInput() throws ReferenceNotInitializedException 407 { 408 XMLSignatureInput input=getContentsBeforeTransformation(); 409 XMLSignatureInput result; 410 try { 411 result = new XMLSignatureInput(input.getBytes()); 412 } catch (CanonicalizationException ex) { 413 throw new ReferenceNotInitializedException("empty", ex); 414 } catch (IOException ex) { 415 throw new ReferenceNotInitializedException("empty", ex); 416 } 417 result.setSourceURI(input.getSourceURI()); 418 return result; 419 420 } 421 422 private XMLSignatureInput getContentsAfterTransformation(XMLSignatureInput input, OutputStream os) 423 throws XMLSignatureException { 424 425 try { 426 Transforms transforms = this.getTransforms(); 427 XMLSignatureInput output = null; 428 429 if (transforms != null) { 430 output = transforms.performTransforms(input,os); 431 this._transformsOutput = output; 433 } else { 435 output = input; 436 } 437 438 return output; 439 } catch (ResourceResolverException ex) { 440 throw new XMLSignatureException("empty", ex); 441 } catch (CanonicalizationException ex) { 442 throw new XMLSignatureException("empty", ex); 443 } catch (InvalidCanonicalizerException ex) { 444 throw new XMLSignatureException("empty", ex); 445 } catch (TransformationException ex) { 446 throw new XMLSignatureException("empty", ex); 447 } catch (XMLSecurityException ex) { 448 throw new XMLSignatureException("empty", ex); 449 } 450 } 451 452 457 public XMLSignatureInput getContentsAfterTransformation() 458 throws XMLSignatureException { 459 460 XMLSignatureInput input = this.getContentsBeforeTransformation(); 461 462 return this.getContentsAfterTransformation(input, null); 463 } 464 465 472 public XMLSignatureInput getNodesetBeforeFirstCanonicalization() 473 throws XMLSignatureException { 474 475 try { 476 XMLSignatureInput input = this.getContentsBeforeTransformation(); 477 XMLSignatureInput output = input; 478 Transforms transforms = this.getTransforms(); 479 480 if (transforms != null) { 481 doTransforms: for (int i = 0; i < transforms.getLength(); i++) { 482 Transform t = transforms.item(i); 483 String URI = t.getURI(); 484 485 if (URI.equals(Transforms 486 .TRANSFORM_C14N_EXCL_OMIT_COMMENTS) || URI 487 .equals(Transforms 488 .TRANSFORM_C14N_EXCL_WITH_COMMENTS) || URI 489 .equals(Transforms 490 .TRANSFORM_C14N_OMIT_COMMENTS) || URI 491 .equals(Transforms 492 .TRANSFORM_C14N_WITH_COMMENTS)) { 493 494 break doTransforms; 495 } 496 497 output = t.performTransform(output, null); 498 } 499 500 output.setSourceURI(input.getSourceURI()); 501 } 502 return output; 503 } catch (IOException ex) { 504 throw new XMLSignatureException("empty", ex); 505 } catch (ResourceResolverException ex) { 506 throw new XMLSignatureException("empty", ex); 507 } catch (CanonicalizationException ex) { 508 throw new XMLSignatureException("empty", ex); 509 } catch (InvalidCanonicalizerException ex) { 510 throw new XMLSignatureException("empty", ex); 511 } catch (TransformationException ex) { 512 throw new XMLSignatureException("empty", ex); 513 } catch (XMLSecurityException ex) { 514 throw new XMLSignatureException("empty", ex); 515 } 516 } 517 518 523 public String getHTMLRepresentation() throws XMLSignatureException { 524 525 try { 526 XMLSignatureInput nodes = this.getNodesetBeforeFirstCanonicalization(); 527 Set inclusiveNamespaces = new HashSet (); 528 529 { 530 Transforms transforms = this.getTransforms(); 531 Transform c14nTransform = null; 532 533 if (transforms != null) { 534 doTransforms: for (int i = 0; i < transforms.getLength(); i++) { 535 Transform t = transforms.item(i); 536 String URI = t.getURI(); 537 538 if (URI.equals(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS) 539 || URI.equals( 540 Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS)) { 541 c14nTransform = t; 542 543 break doTransforms; 544 } 545 } 546 } 547 548 if (c14nTransform != null) { 549 550 if (c14nTransform 551 .length(InclusiveNamespaces 552 .ExclusiveCanonicalizationNamespace, InclusiveNamespaces 553 ._TAG_EC_INCLUSIVENAMESPACES) == 1) { 554 555 InclusiveNamespaces in = new InclusiveNamespaces( 557 XMLUtils.selectNode( 558 c14nTransform.getElement().getFirstChild(), 559 InclusiveNamespaces.ExclusiveCanonicalizationNamespace, 560 InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,0), this.getBaseURI()); 561 562 inclusiveNamespaces = InclusiveNamespaces.prefixStr2Set( 563 in.getInclusiveNamespaces()); 564 } 565 } 566 } 567 568 return nodes.getHTMLRepresentation(inclusiveNamespaces); 569 } catch (TransformationException ex) { 570 throw new XMLSignatureException("empty", ex); 571 } catch (InvalidTransformException ex) { 572 throw new XMLSignatureException("empty", ex); 573 } catch (XMLSecurityException ex) { 574 throw new XMLSignatureException("empty", ex); 575 } 576 } 577 578 582 public XMLSignatureInput getTransformsOutput() { 583 return this._transformsOutput; 584 } 585 586 595 protected XMLSignatureInput dereferenceURIandPerformTransforms(OutputStream os) 596 throws XMLSignatureException { 597 598 try { 599 XMLSignatureInput input = this.getContentsBeforeTransformation(); 600 XMLSignatureInput output = this.getContentsAfterTransformation(input, os); 601 602 607 608 this._transformsOutput = output; 609 610 return output; 611 } catch (XMLSecurityException ex) { 612 throw new ReferenceNotInitializedException("empty", ex); 613 } 614 } 615 616 625 public Transforms getTransforms() 626 throws XMLSignatureException, InvalidTransformException, 627 TransformationException, XMLSecurityException { 628 629 Element transformsElement = XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), 630 Constants._TAG_TRANSFORMS,0); 631 632 if (transformsElement != null) { 633 Transforms transforms = new Transforms(transformsElement, 634 this._baseURI); 635 636 return transforms; 637 } 638 return null; 639 } 640 641 648 public byte[] getReferencedBytes() 649 throws ReferenceNotInitializedException, XMLSignatureException { 650 try { 651 XMLSignatureInput output=this.dereferenceURIandPerformTransforms(null); 652 653 byte[] signedBytes = output.getBytes(); 654 655 return signedBytes; 656 } catch (IOException ex) { 657 throw new ReferenceNotInitializedException("empty", ex); 658 } catch (CanonicalizationException ex) { 659 throw new ReferenceNotInitializedException("empty", ex); 660 } 661 662 } 663 664 665 672 private byte[] calculateDigest() 673 throws ReferenceNotInitializedException, XMLSignatureException { 674 675 try { 676 677 MessageDigestAlgorithm mda = this.getMessageDigestAlgorithm(); 678 679 mda.reset(); 680 DigesterOutputStream diOs=new DigesterOutputStream(mda); 681 OutputStream os=new UnsyncBufferedOutputStream(diOs); 682 XMLSignatureInput output=this.dereferenceURIandPerformTransforms(os); 683 output.updateOutputStream(os); 684 os.flush(); 685 688 return diOs.getDigestValue(); 689 } catch (XMLSecurityException ex) { 690 throw new ReferenceNotInitializedException("empty", ex); 691 } catch (IOException ex) { 692 throw new ReferenceNotInitializedException("empty", ex); 693 } 694 } 695 696 703 public byte[] getDigestValue() throws Base64DecodingException, XMLSecurityException { 704 Element digestValueElem = XMLUtils.selectDsNode(this._constructionElement.getFirstChild() 705 ,Constants._TAG_DIGESTVALUE,0); 706 if (digestValueElem == null) { 707 Object [] exArgs ={ Constants._TAG_DIGESTVALUE, 709 Constants.SignatureSpecNS }; 710 throw new XMLSecurityException( 711 "signature.Verification.NoSignatureElement", 712 exArgs); 713 } 714 byte[] elemDig = Base64.decode(digestValueElem); 715 return elemDig; 716 } 717 718 719 726 public boolean verify() 727 throws ReferenceNotInitializedException, XMLSecurityException { 728 729 byte[] elemDig = this.getDigestValue(); 730 byte[] calcDig = this.calculateDigest(); 731 boolean equal = MessageDigestAlgorithm.isEqual(elemDig, calcDig); 732 733 if (!equal) { 734 log.log(java.util.logging.Level.WARNING, "Verification failed for URI \"" + this.getURI() + "\""); 735 } else { 736 if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "Verification successful for URI \"" + this.getURI() + "\""); 737 } 738 739 return equal; 740 } 741 742 747 public String getBaseLocalName() { 748 return Constants._TAG_REFERENCE; 749 } 750 } 751 | Popular Tags |