|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package com.sun.org.apache.xml.internal.security.signature; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
import java.io.IOException; |
|
import java.io.OutputStream; |
|
import java.security.Provider; |
|
import java.security.spec.AlgorithmParameterSpec; |
|
import javax.crypto.SecretKey; |
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm; |
|
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; |
|
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; |
|
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; |
|
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; |
|
import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces; |
|
import com.sun.org.apache.xml.internal.security.utils.Constants; |
|
import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
|
|
|
import org.w3c.dom.Document; |
|
import org.w3c.dom.Element; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class SignedInfo extends Manifest { |
|
|
|
|
|
private final SignatureAlgorithm signatureAlgorithm; |
|
|
|
|
|
private byte[] c14nizedBytes; |
|
|
|
private Element c14nMethod; |
|
private Element signatureMethod; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo(Document doc) throws XMLSecurityException { |
|
this(doc, XMLSignature.ALGO_ID_SIGNATURE_DSA, |
|
Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Document doc, String signatureMethodURI, String canonicalizationMethodURI |
|
) throws XMLSecurityException { |
|
this(doc, signatureMethodURI, 0, canonicalizationMethodURI, null, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Document doc, String signatureMethodURI, String canonicalizationMethodURI, Provider provider |
|
) throws XMLSecurityException { |
|
this(doc, signatureMethodURI, 0, canonicalizationMethodURI, provider, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Document doc, String signatureMethodURI, |
|
int hMACOutputLength, String canonicalizationMethodURI |
|
) throws XMLSecurityException { |
|
this(doc, signatureMethodURI, hMACOutputLength, canonicalizationMethodURI, null, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Document doc, String signatureMethodURI, |
|
int hMACOutputLength, String canonicalizationMethodURI, Provider provider, AlgorithmParameterSpec spec |
|
) throws XMLSecurityException { |
|
super(doc); |
|
|
|
c14nMethod = |
|
XMLUtils.createElementInSignatureSpace(getDocument(), Constants._TAG_CANONICALIZATIONMETHOD); |
|
|
|
c14nMethod.setAttributeNS(null, Constants._ATT_ALGORITHM, canonicalizationMethodURI); |
|
appendSelf(c14nMethod); |
|
addReturnToSelf(); |
|
|
|
if (hMACOutputLength > 0) { |
|
this.signatureAlgorithm = |
|
new SignatureAlgorithm(getDocument(), signatureMethodURI, hMACOutputLength, provider); |
|
} else { |
|
this.signatureAlgorithm = new SignatureAlgorithm(getDocument(), signatureMethodURI, provider, spec); |
|
} |
|
|
|
signatureMethod = this.signatureAlgorithm.getElement(); |
|
appendSelf(signatureMethod); |
|
addReturnToSelf(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Document doc, Element signatureMethodElem, Element canonicalizationMethodElem |
|
) throws XMLSecurityException { |
|
this(doc, signatureMethodElem, canonicalizationMethodElem, null); |
|
} |
|
|
|
public SignedInfo( |
|
Document doc, Element signatureMethodElem, Element canonicalizationMethodElem, Provider provider |
|
) throws XMLSecurityException { |
|
super(doc); |
|
|
|
this.c14nMethod = canonicalizationMethodElem; |
|
appendSelf(c14nMethod); |
|
addReturnToSelf(); |
|
|
|
this.signatureAlgorithm = |
|
new SignatureAlgorithm(signatureMethodElem, null, provider); |
|
|
|
signatureMethod = this.signatureAlgorithm.getElement(); |
|
appendSelf(signatureMethod); |
|
|
|
addReturnToSelf(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo(Element element, String baseURI) throws XMLSecurityException { |
|
this(element, baseURI, true, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Element element, String baseURI, boolean secureValidation |
|
) throws XMLSecurityException { |
|
this(element, baseURI, secureValidation, null); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SignedInfo( |
|
Element element, String baseURI, boolean secureValidation, Provider provider |
|
) throws XMLSecurityException { |
|
super(element, baseURI, secureValidation); |
|
|
|
c14nMethod = XMLUtils.getNextElement(element.getFirstChild()); |
|
if (c14nMethod == null || |
|
!(Constants.SignatureSpecNS.equals(c14nMethod.getNamespaceURI()) |
|
&& Constants._TAG_CANONICALIZATIONMETHOD.equals(c14nMethod.getLocalName()))) { |
|
Object[] exArgs = { Constants._TAG_CANONICALIZATIONMETHOD, Constants._TAG_SIGNEDINFO }; |
|
throw new XMLSignatureException("xml.WrongContent", exArgs); |
|
} |
|
|
|
signatureMethod = XMLUtils.getNextElement(c14nMethod.getNextSibling()); |
|
if (signatureMethod == null || |
|
!(Constants.SignatureSpecNS.equals(signatureMethod.getNamespaceURI()) |
|
&& Constants._TAG_SIGNATUREMETHOD.equals(signatureMethod.getLocalName()))) { |
|
Object[] exArgs = { Constants._TAG_SIGNATUREMETHOD, Constants._TAG_SIGNEDINFO }; |
|
throw new XMLSignatureException("xml.WrongContent", exArgs); |
|
} |
|
|
|
this.signatureAlgorithm = |
|
new SignatureAlgorithm(signatureMethod, this.getBaseURI(), secureValidation, provider); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean verify() |
|
throws MissingResourceFailureException, XMLSecurityException { |
|
return super.verifyReferences(false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean verify(boolean followManifests) |
|
throws MissingResourceFailureException, XMLSecurityException { |
|
return super.verifyReferences(followManifests); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public byte[] getCanonicalizedOctetStream() |
|
throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException, IOException { |
|
if (this.c14nizedBytes == null) { |
|
Canonicalizer c14nizer = |
|
Canonicalizer.getInstance(this.getCanonicalizationMethodURI()); |
|
|
|
String inclusiveNamespaces = this.getInclusiveNamespaces(); |
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
|
if (inclusiveNamespaces == null) { |
|
c14nizer.canonicalizeSubtree(getElement(), baos); |
|
} else { |
|
c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces, baos); |
|
} |
|
this.c14nizedBytes = baos.toByteArray(); |
|
} |
|
} |
|
|
|
|
|
return this.c14nizedBytes.clone(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void signInOctetStream(OutputStream os) |
|
throws CanonicalizationException, InvalidCanonicalizerException, XMLSecurityException { |
|
if (this.c14nizedBytes == null) { |
|
Canonicalizer c14nizer = |
|
Canonicalizer.getInstance(this.getCanonicalizationMethodURI()); |
|
String inclusiveNamespaces = this.getInclusiveNamespaces(); |
|
|
|
if (inclusiveNamespaces == null) { |
|
c14nizer.canonicalizeSubtree(getElement(), os); |
|
} else { |
|
c14nizer.canonicalizeSubtree(getElement(), inclusiveNamespaces, os); |
|
} |
|
} else { |
|
try { |
|
os.write(this.c14nizedBytes); |
|
} catch (IOException e) { |
|
throw new RuntimeException(e); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getCanonicalizationMethodURI() { |
|
return c14nMethod.getAttributeNS(null, Constants._ATT_ALGORITHM); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getSignatureMethodURI() { |
|
Element signatureElement = this.getSignatureMethodElement(); |
|
|
|
if (signatureElement != null) { |
|
return signatureElement.getAttributeNS(null, Constants._ATT_ALGORITHM); |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Element getSignatureMethodElement() { |
|
return signatureMethod; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SecretKey createSecretKey(byte[] secretKeyBytes) { |
|
return new SecretKeySpec(secretKeyBytes, this.signatureAlgorithm.getJCEAlgorithmString()); |
|
} |
|
|
|
public SignatureAlgorithm getSignatureAlgorithm() { |
|
return signatureAlgorithm; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getBaseLocalName() { |
|
return Constants._TAG_SIGNEDINFO; |
|
} |
|
|
|
public String getInclusiveNamespaces() { |
|
String c14nMethodURI = getCanonicalizationMethodURI(); |
|
if (!("http://www.w3.org/2001/10/xml-exc-c14n#".equals(c14nMethodURI) || |
|
"http://www.w3.org/2001/10/xml-exc-c14n#WithComments".equals(c14nMethodURI))) { |
|
return null; |
|
} |
|
|
|
Element inclusiveElement = XMLUtils.getNextElement(c14nMethod.getFirstChild()); |
|
|
|
if (inclusiveElement != null) { |
|
try { |
|
String inclusiveNamespaces = |
|
new InclusiveNamespaces( |
|
inclusiveElement, |
|
InclusiveNamespaces.ExclusiveCanonicalizationNamespace |
|
).getInclusiveNamespaces(); |
|
return inclusiveNamespaces; |
|
} catch (XMLSecurityException e) { |
|
return null; |
|
} |
|
} |
|
return null; |
|
} |
|
} |