1 17 package com.sun.org.apache.xml.internal.security.signature; 18 19 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import javax.crypto.SecretKey; 26 import javax.crypto.spec.SecretKeySpec; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm; 30 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 31 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; 32 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; 33 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 34 import com.sun.org.apache.xml.internal.security.utils.Constants; 35 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 36 import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.xml.sax.SAXException ; 41 42 43 49 public class SignedInfo extends Manifest { 50 51 52 private SignatureAlgorithm _signatureAlgorithm = null; 53 54 55 private byte[] _c14nizedBytes = null; 56 57 63 public SignedInfo(Document doc) throws XMLSecurityException { 64 this(doc, XMLSignature.ALGO_ID_SIGNATURE_DSA, Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS); 65 } 66 67 75 public SignedInfo( 76 Document doc, String SignatureMethodURI, String CanonicalizationMethodURI) 77 throws XMLSecurityException { 78 this(doc, SignatureMethodURI, 0, CanonicalizationMethodURI); 79 } 80 81 90 public SignedInfo( 91 Document doc, String SignatureMethodURI, int HMACOutputLength, String CanonicalizationMethodURI) 92 throws XMLSecurityException { 93 94 super(doc); 95 96 { 98 Element canonElem = XMLUtils.createElementInSignatureSpace(this._doc, 99 Constants._TAG_CANONICALIZATIONMETHOD); 100 101 canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM, 102 CanonicalizationMethodURI); 103 this._constructionElement.appendChild(canonElem); 104 XMLUtils.addReturnToElement(this._constructionElement); 105 } 106 { 107 if (HMACOutputLength > 0) { 108 this._signatureAlgorithm = new SignatureAlgorithm(this._doc, 109 SignatureMethodURI, HMACOutputLength); 110 } else { 111 this._signatureAlgorithm = new SignatureAlgorithm(this._doc, 112 SignatureMethodURI); 113 } 114 115 this._constructionElement 116 .appendChild(this._signatureAlgorithm.getElement()); 117 XMLUtils.addReturnToElement(this._constructionElement); 118 } 119 } 120 121 127 public SignedInfo( 128 Document doc, Element SignatureMethodElem, Element CanonicalizationMethodElem) 129 throws XMLSecurityException { 130 131 super(doc); 132 133 this._constructionElement.appendChild(CanonicalizationMethodElem); 134 XMLUtils.addReturnToElement(this._constructionElement); 135 136 this._signatureAlgorithm = new SignatureAlgorithm(SignatureMethodElem, null); 137 138 this._constructionElement 139 .appendChild(this._signatureAlgorithm.getElement()); 140 XMLUtils.addReturnToElement(this._constructionElement); 141 } 142 143 152 public SignedInfo(Element element, String BaseURI) 153 throws XMLSecurityException { 154 155 super(element, BaseURI); 157 158 162 String c14nMethodURI=this.getCanonicalizationMethodURI(); 163 if (!(c14nMethodURI.equals("http://www.w3.org/TR/2001/REC-xml-c14n-20010315") || 164 c14nMethodURI.equals("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments") || 165 c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#") || 166 c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#WithComments"))) { 167 try { 169 Canonicalizer c14nizer = 170 Canonicalizer.getInstance(this.getCanonicalizationMethodURI()); 171 172 this._c14nizedBytes = 173 c14nizer.canonicalizeSubtree(this._constructionElement); 174 javax.xml.parsers.DocumentBuilderFactory dbf = 175 javax.xml.parsers.DocumentBuilderFactory.newInstance(); 176 177 dbf.setNamespaceAware(true); 178 179 javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); 180 org.w3c.dom.Document newdoc = 181 db.parse(new ByteArrayInputStream (this._c14nizedBytes)); 182 Node imported = this._doc.importNode(newdoc.getDocumentElement(), 183 true); 184 185 this._constructionElement.getParentNode().replaceChild(imported, 186 this._constructionElement); 187 188 this._constructionElement = (Element ) imported; 189 } catch (ParserConfigurationException ex) { 190 throw new XMLSecurityException("empty", ex); 191 } catch (IOException ex) { 192 throw new XMLSecurityException("empty", ex); 193 } catch (SAXException ex) { 194 throw new XMLSecurityException("empty", ex); 195 } 196 } 197 this._signatureAlgorithm = 198 new SignatureAlgorithm(this.getSignatureMethodElement(), 199 this.getBaseURI()); 200 } 201 202 209 public boolean verify() 210 throws MissingResourceFailureException, XMLSecurityException { 211 return super.verifyReferences(false); 212 } 213 214 222 public boolean verify(boolean followManifests) 223 throws MissingResourceFailureException, XMLSecurityException { 224 return super.verifyReferences(followManifests); 225 } 226 227 235 public byte[] getCanonicalizedOctetStream() 236 throws CanonicalizationException, InvalidCanonicalizerException, 237 XMLSecurityException { 238 239 if ((this._c14nizedBytes == null) 240 ) { 241 Canonicalizer c14nizer = 242 Canonicalizer.getInstance(this.getCanonicalizationMethodURI()); 243 244 this._c14nizedBytes = 245 c14nizer.canonicalizeSubtree(this._constructionElement); 246 } 247 248 byte[] output = new byte[this._c14nizedBytes.length]; 250 251 System.arraycopy(this._c14nizedBytes, 0, output, 0, output.length); 252 253 return output; 254 } 255 256 263 public void signInOctectStream(OutputStream os) 264 throws CanonicalizationException, InvalidCanonicalizerException, 265 XMLSecurityException { 266 267 if ((this._c14nizedBytes == null)) { 268 Canonicalizer c14nizer = 269 Canonicalizer.getInstance(this.getCanonicalizationMethodURI()); 270 c14nizer.setWriter(os); 271 String inclusiveNamespaces = this.getInclusiveNamespaces(); 272 273 if(inclusiveNamespaces == null) 274 c14nizer.canonicalizeSubtree(this._constructionElement); 275 else 276 c14nizer.canonicalizeSubtree(this._constructionElement, inclusiveNamespaces); 277 } else { 278 try { 279 os.write(this._c14nizedBytes); 280 } catch (IOException e) { 281 throw new RuntimeException (""+e); 282 } 283 } 284 } 285 286 291 public String getCanonicalizationMethodURI() { 292 293 Element el= XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), 294 Constants._TAG_CANONICALIZATIONMETHOD,0); 295 if (el==null) { 296 return null; 297 } 298 return el.getAttributeNS(null, Constants._ATT_ALGORITHM); 299 } 300 301 306 public String getSignatureMethodURI() { 307 308 Element signatureElement = this.getSignatureMethodElement(); 309 310 if (signatureElement != null) { 311 return signatureElement.getAttributeNS(null, Constants._ATT_ALGORITHM); 312 } 313 314 return null; 315 } 316 317 322 public Element getSignatureMethodElement() { 323 return XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), 324 Constants._TAG_SIGNATUREMETHOD,0); 325 } 326 327 334 public SecretKey createSecretKey(byte[] secretKeyBytes) 335 { 336 337 return new SecretKeySpec(secretKeyBytes, 338 this._signatureAlgorithm 339 .getJCEAlgorithmString()); 340 } 341 342 347 public String getBaseLocalName() { 348 return Constants._TAG_SIGNEDINFO; 349 } 350 351 public String getInclusiveNamespaces() { 352 353 Element el= XMLUtils.selectDsNode(this._constructionElement.getFirstChild(), 354 Constants._TAG_CANONICALIZATIONMETHOD,0); 355 if (el==null) { 356 return null; 357 } 358 359 String c14nMethodURI = el.getAttributeNS(null, Constants._ATT_ALGORITHM); 360 if(!(c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#") || 361 c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#WithComments"))) { 362 return null; 363 } 364 365 Element inclusiveElement = XMLUtils.selectNode( 366 el.getFirstChild(),InclusiveNamespaces.ExclusiveCanonicalizationNamespace, 367 InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,0); 368 369 if(inclusiveElement != null) 370 { 371 try 372 { 373 String inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement, 374 InclusiveNamespaces.ExclusiveCanonicalizationNamespace).getInclusiveNamespaces(); 375 return inclusiveNamespaces; 376 } 377 catch (XMLSecurityException e) 378 { 379 return null; 380 } 381 } 382 return null; 383 } 384 } 385 | Popular Tags |