1 2 18 package com.sun.org.apache.xml.internal.security.signature; 19 20 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.UnsupportedEncodingException ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.List ; 31 import java.util.Set ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 37 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 38 import com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315OmitComments; 39 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityRuntimeException; 40 import com.sun.org.apache.xml.internal.security.utils.JavaUtils; 41 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Node ; 44 import org.xml.sax.SAXException ; 45 46 47 53 public class XMLSignatureInput implements Cloneable { 54 static java.util.logging.Logger log = 55 java.util.logging.Logger.getLogger(XMLSignatureInput.class.getName()); 56 57 65 69 InputStream _inputOctetStreamProxy = null; 70 73 Set _inputNodeSet = null; 74 77 Node _subNode=null; 78 81 Node excludeNode=null; 82 85 boolean excludeComments=false; 86 87 boolean isNodeSet=false; 88 91 byte []bytes=null; 92 93 96 private String _MIMEType = null; 97 98 101 private String _SourceURI = null; 102 103 106 List nodeFilters=new ArrayList (); 107 108 boolean needsToBeExpanded=false; 109 113 public boolean isNeedsToBeExpanded() { 114 return needsToBeExpanded; 115 } 116 117 121 public void setNeedsToBeExpanded(boolean needsToBeExpanded) { 122 this.needsToBeExpanded = needsToBeExpanded; 123 } 124 OutputStream outputStream=null; 125 126 133 public XMLSignatureInput(byte[] inputOctets) { 134 135 137 this.bytes=inputOctets; 139 } 140 141 142 148 public XMLSignatureInput(InputStream inputOctetStream) { 149 this._inputOctetStreamProxy=inputOctetStream; 150 151 153 } 154 155 162 public XMLSignatureInput(String inputStr) { 163 this(inputStr.getBytes()); 164 } 165 166 176 public XMLSignatureInput(String inputStr, String encoding) 177 throws UnsupportedEncodingException { 178 this(inputStr.getBytes(encoding)); 179 } 180 181 187 public XMLSignatureInput(Node rootNode) 188 { 189 this._subNode = rootNode; 190 } 191 192 198 public XMLSignatureInput(Set inputNodeSet) { 199 this._inputNodeSet = inputNodeSet; 200 } 201 202 215 public Set getNodeSet() throws CanonicalizationException, ParserConfigurationException , IOException , SAXException { 216 return getNodeSet(false); 217 } 218 232 public Set getNodeSet(boolean circunvent) 233 throws ParserConfigurationException , IOException , SAXException , 234 CanonicalizationException { 235 if (this._inputNodeSet!=null) { 236 return this._inputNodeSet; 237 } 238 if (this.isElement()) { 239 240 if (circunvent) { 241 XMLUtils.circumventBug2650(XMLUtils.getOwnerDocument(_subNode)); 242 } 243 this._inputNodeSet = new HashSet (); 244 XMLUtils.getSet(_subNode,this._inputNodeSet, excludeNode, this.excludeComments); 245 246 return this._inputNodeSet; 247 } 248 else if (this.isOctetStream()) { 249 convertToNodes(); 250 HashSet result=new HashSet (); 251 XMLUtils.getSet(_subNode, result,null,false); 252 return result; 254 } 255 256 throw new RuntimeException ( 257 "getNodeSet() called but no input data present"); 258 } 259 260 266 public InputStream getOctetStream() 267 throws IOException { 268 269 return getResetableInputStream(); 270 271 } 272 275 public InputStream getOctetStreamReal () { 276 return this._inputOctetStreamProxy; 277 } 278 286 public byte[] getBytes() 287 throws IOException , CanonicalizationException { 288 if (bytes!=null) { 289 return bytes; 290 } 291 InputStream is = getResetableInputStream(); 292 if (is!=null) { 293 if (bytes==null) { 295 is.reset(); 296 bytes=JavaUtils.getBytesFromStream(is); 297 } 298 return bytes; 299 } 300 Canonicalizer20010315OmitComments c14nizer = 301 new Canonicalizer20010315OmitComments(); 302 bytes=c14nizer.engineCanonicalize(this); 303 return bytes; 304 } 305 306 307 312 public boolean isNodeSet() { 313 return (( (this._inputOctetStreamProxy == null) 314 && (this._inputNodeSet != null) ) || isNodeSet); 315 } 316 321 public boolean isElement() { 322 return ((this._inputOctetStreamProxy==null)&& (this._subNode!=null) 323 && (this._inputNodeSet==null) && !isNodeSet 324 ); 325 } 326 327 332 public boolean isOctetStream() { 333 return ( ((this._inputOctetStreamProxy != null) || bytes!=null) 334 && ((this._inputNodeSet == null) && _subNode ==null)); 335 } 336 337 342 public boolean isByteArray() { 343 return ( (bytes!=null) 344 && ((this._inputNodeSet == null) && _subNode ==null)); 345 } 346 347 352 public boolean isInitialized() { 353 return (this.isOctetStream() || this.isNodeSet()); 354 } 355 356 361 public String getMIMEType() { 362 return this._MIMEType; 363 } 364 365 370 public void setMIMEType(String MIMEType) { 371 this._MIMEType = MIMEType; 372 } 373 374 379 public String getSourceURI() { 380 return this._SourceURI; 381 } 382 383 388 public void setSourceURI(String SourceURI) { 389 this._SourceURI = SourceURI; 390 } 391 392 393 398 public String toString() { 399 400 if (this.isNodeSet()) { 401 return "XMLSignatureInput/NodeSet/" + this._inputNodeSet.size() 402 + " nodes/" + this.getSourceURI(); 403 } 404 if (this.isElement()) { 405 return "XMLSignatureInput/Element/" + this._subNode 406 + " exclude "+ this.excludeNode + " comments:" + 407 this.excludeComments 408 +"/" + this.getSourceURI(); 409 } 410 try { 411 return "XMLSignatureInput/OctetStream/" + this.getBytes().length 412 + " octets/" + this.getSourceURI(); 413 } catch (Exception ex) { 414 return "XMLSignatureInput/OctetStream//" + this.getSourceURI(); 415 } 416 417 } 418 419 425 public String getHTMLRepresentation() throws XMLSignatureException { 426 427 XMLSignatureInputDebugger db = new XMLSignatureInputDebugger(this); 428 429 return db.getHTMLRepresentation(); 430 } 431 432 439 public String getHTMLRepresentation(Set inclusiveNamespaces) 440 throws XMLSignatureException { 441 442 XMLSignatureInputDebugger db = new XMLSignatureInputDebugger( this, 443 inclusiveNamespaces); 444 445 return db.getHTMLRepresentation(); 446 } 447 448 452 public Node getExcludeNode() { 453 return excludeNode; 454 } 455 456 460 public void setExcludeNode(Node excludeNode) { 461 this.excludeNode = excludeNode; 462 } 463 464 468 public Node getSubNode() { 469 return _subNode; 470 } 471 474 public boolean isExcludeComments() { 475 return excludeComments; 476 } 477 480 public void setExcludeComments(boolean excludeComments) { 481 this.excludeComments = excludeComments; 482 } 483 484 489 public void updateOutputStream(OutputStream diOs) throws CanonicalizationException, IOException { 490 if (diOs==outputStream) { 491 return; 492 } 493 if (bytes!=null) { 494 diOs.write(bytes); 495 return; 496 }else if (_inputOctetStreamProxy==null) { 497 Canonicalizer20010315OmitComments c14nizer = 498 new Canonicalizer20010315OmitComments(); 499 c14nizer.setWriter(diOs); 500 c14nizer.engineCanonicalize(this); 501 return; 502 } else { 503 InputStream is = getResetableInputStream(); 504 if (bytes!=null) { 505 diOs.write(bytes,0,bytes.length); 507 return; 508 } 509 is.reset(); 510 int num; 511 byte[] bytesT = new byte[1024]; 512 while ((num=is.read(bytesT))>0) { 513 diOs.write(bytesT,0,num); 514 } 515 516 } 517 518 } 519 520 521 524 public void setOutputStream(OutputStream os) { 525 outputStream=os; 526 527 } 528 protected InputStream getResetableInputStream() throws IOException { 529 if ((_inputOctetStreamProxy instanceof ByteArrayInputStream ) ) { 530 if (!_inputOctetStreamProxy.markSupported()) { 531 throw new RuntimeException ("Accepted as Markable but not truly been"+_inputOctetStreamProxy); 532 } 533 return _inputOctetStreamProxy; 534 } 535 if (bytes!=null) { 536 _inputOctetStreamProxy=new ByteArrayInputStream (bytes); 537 return _inputOctetStreamProxy; 538 } 539 if (_inputOctetStreamProxy ==null) 540 return null; 541 if (_inputOctetStreamProxy.markSupported()) { 542 if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "Mark Suported but not used as reset"); 543 } 544 bytes=JavaUtils.getBytesFromStream(_inputOctetStreamProxy); 545 _inputOctetStreamProxy.close(); 546 _inputOctetStreamProxy=new ByteArrayInputStream (bytes); 547 return _inputOctetStreamProxy; 548 } 549 550 551 554 public void addNodeFilter(NodeFilter filter) { 555 if (isOctetStream()) { 556 try { 557 convertToNodes(); 558 } catch (Exception e) { 559 throw new XMLSecurityRuntimeException("signature.XMLSignatureInput.nodesetReference",e); 560 } 561 } 562 nodeFilters.add(filter); 563 564 } 565 566 569 public List getNodeFilters() { 570 return nodeFilters; 572 } 573 574 577 public void setNodeSet(boolean b) { 578 isNodeSet=b; 579 580 } 581 582 void convertToNodes() throws CanonicalizationException, ParserConfigurationException , IOException , SAXException { 583 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 584 dfactory.setValidating(false); 585 dfactory.setNamespaceAware(true); 586 DocumentBuilder db = dfactory.newDocumentBuilder(); 587 try { 589 db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils 590 .IgnoreAllErrorHandler()); 591 592 Document doc = db.parse(this.getOctetStream()); 593 594 XMLUtils.circumventBug2650(doc); 595 this._subNode=doc.getDocumentElement(); 596 } catch (SAXException ex) { 597 598 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 600 601 baos.write("<container>".getBytes()); 602 baos.write(this.getBytes()); 603 baos.write("</container>".getBytes()); 604 605 byte result[] = baos.toByteArray(); 606 Document document = db.parse(new ByteArrayInputStream (result)); 607 this._subNode=document.getDocumentElement().getFirstChild().getFirstChild(); 608 } 609 this._inputOctetStreamProxy=null; 610 this.bytes=null; 611 } 612 } 613 | Popular Tags |