org.apache.xml.security.signature.XMLSignatureInput Java Examples

The following examples show how to use org.apache.xml.security.signature.XMLSignatureInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentResolver.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
   String id = context.attr.getNodeValue();
   if (id.startsWith("#")) {
      id = id.substring(1);
   }

   if (LOG.isDebugEnabled()) {
      try {
         LOG.debug("Selected document: " + ConnectorXmlUtils.flatten(ConnectorXmlUtils.toString((Source)(new DOMSource(this.doc)))));
      } catch (TechnicalConnectorException var5) {
         LOG.error(var5.getMessage());
      }
   }

   Node selectedElem = this.doc.getElementById(id);
   if (LOG.isDebugEnabled()) {
      LOG.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem);
   }

   this.processElement(context.attr, context.baseUri, selectedElem, id);
   XMLSignatureInput result = new XMLSignatureInput(selectedElem);
   result.setExcludeComments(true);
   result.setMIMEType("text/xml");
   result.setSourceURI(context.baseUri != null ? context.baseUri.concat(context.attr.getNodeValue()) : context.attr.getNodeValue());
   return result;
}
 
Example #2
Source File: TimeStampDigestInputImpl.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void addToDigestInput(XMLSignatureInput refData, Document doc) throws CannotAddDataToDigestInputException
{
    try
    {
        if (refData.isNodeSet() || refData.isElement())
        {
            Transform c14nTransform = TransformUtils.createTransform(this.c14n, this.parametersMarshallingProvider, doc);
            refData = c14nTransform.performTransform(refData);
            // Fall through to add the bytes resulting from the canonicalization.
        }

        if (refData.isByteArray())
        {
            digestInput.write(refData.getBytes());
        } else if (refData.isOctetStream())
        {
            StreamUtils.readWrite(refData.getOctetStream(), digestInput);
        }
    }
    catch (Exception ex)
    {
        throw new CannotAddDataToDigestInputException(ex);
    }
}
 
Example #3
Source File: TimeStampDigestInputImpl.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void addReference(Reference r) throws CannotAddDataToDigestInputException
{
    if (null == r)
    {
        throw new NullPointerException();
    }

    try
    {
        XMLSignatureInput refData = r.getContentsAfterTransformation();
        addToDigestInput(refData, r.getDocument());

    } catch (XMLSignatureException ex)
    {
        throw new CannotAddDataToDigestInputException(ex);
    }
}
 
Example #4
Source File: XadesTSpecification.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private byte[] generateTimestampDigest(Element baseElement, String c14nMethodValue) {
   try {
      Node signatureValue = DomUtils.getMatchingChilds(baseElement, "http://www.w3.org/2000/09/xmldsig#", "SignatureValue").item(0);
      Transform transform = new Transform(signatureValue.getOwnerDocument(), c14nMethodValue);
      XMLSignatureInput refData = transform.performTransform(new XMLSignatureInput(signatureValue));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (refData.isByteArray()) {
         baos.write(refData.getBytes());
      } else if (refData.isOctetStream()) {
         baos.write(ConnectorIOUtils.getBytes(refData.getOctetStream()));
      }

      return baos.toByteArray();
   } catch (Exception var7) {
      throw new IllegalArgumentException("Unable to calculateDigest", var7);
   }
}
 
Example #5
Source File: DocumentResolver.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
   String id = context.attr.getNodeValue();
   if (id.startsWith("#")) {
      id = id.substring(1);
   }

   if (LOG.isDebugEnabled()) {
      try {
         LOG.debug("Selected document: " + ConnectorXmlUtils.flatten(ConnectorXmlUtils.toString((Source)(new DOMSource(this.doc)))));
      } catch (TechnicalConnectorException var5) {
         LOG.error(var5.getMessage());
      }
   }

   Node selectedElem = this.doc.getElementById(id);
   if (LOG.isDebugEnabled()) {
      LOG.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem);
   }

   this.processElement(context.attr, context.baseUri, selectedElem, id);
   XMLSignatureInput result = new XMLSignatureInput(selectedElem);
   result.setExcludeComments(true);
   result.setMIMEType("text/xml");
   result.setSourceURI(context.baseUri != null ? context.baseUri.concat(context.attr.getNodeValue()) : context.attr.getNodeValue());
   return result;
}
 
Example #6
Source File: NippinOptionalDeflateTransformer.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private XMLSignatureInput processElement(XMLSignatureInput input, OutputStream os) throws TechnicalConnectorException, IOException {
   Node el = input.getSubNode();
   if (input.getSubNode().getNodeType() == 3) {
      el = el.getParentNode();
   }

   StringBuilder sb = new StringBuilder();
   this.traverseElement((Element)el, sb);
   if (os == null) {
      byte[] decodedBytes = ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8));
      return new XMLSignatureInput(decodedBytes);
   } else {
      os.write(ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8)));
      XMLSignatureInput output = new XMLSignatureInput((byte[])null);
      output.setOutputStream(os);
      return output;
   }
}
 
Example #7
Source File: XadesTSpecification.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private byte[] generateTimestampDigest(Element baseElement, String c14nMethodValue) {
   try {
      Node signatureValue = DomUtils.getMatchingChilds(baseElement, "http://www.w3.org/2000/09/xmldsig#", "SignatureValue").item(0);
      Transform transform = new Transform(signatureValue.getOwnerDocument(), c14nMethodValue);
      XMLSignatureInput refData = transform.performTransform(new XMLSignatureInput(signatureValue));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (refData.isByteArray()) {
         baos.write(refData.getBytes());
      } else if (refData.isOctetStream()) {
         baos.write(ConnectorIOUtils.getBytes(refData.getOctetStream()));
      }

      return baos.toByteArray();
   } catch (Exception var7) {
      throw new IllegalArgumentException("Unable to calculateDigest", var7);
   }
}
 
Example #8
Source File: DocumentResolver.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
   String id = context.attr.getNodeValue();
   if (id.startsWith("#")) {
      id = id.substring(1);
   }

   if (LOG.isDebugEnabled()) {
      try {
         LOG.debug("Selected document: " + ConnectorXmlUtils.flatten(ConnectorXmlUtils.toString((Source)(new DOMSource(this.doc)))));
      } catch (TechnicalConnectorException var5) {
         LOG.error(var5.getMessage());
      }
   }

   Node selectedElem = this.doc.getElementById(id);
   if (LOG.isDebugEnabled()) {
      LOG.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem);
   }

   this.processElement(context.attr, context.baseUri, selectedElem, id);
   XMLSignatureInput result = new XMLSignatureInput(selectedElem);
   result.setExcludeComments(true);
   result.setMIMEType("text/xml");
   result.setSourceURI(context.baseUri != null ? context.baseUri.concat(context.attr.getNodeValue()) : context.attr.getNodeValue());
   return result;
}
 
Example #9
Source File: NippinOptionalDeflateTransformer.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private XMLSignatureInput processElement(XMLSignatureInput input, OutputStream os) throws TechnicalConnectorException, IOException {
   Node el = input.getSubNode();
   if (input.getSubNode().getNodeType() == 3) {
      el = el.getParentNode();
   }

   StringBuilder sb = new StringBuilder();
   this.traverseElement((Element)el, sb);
   if (os == null) {
      byte[] decodedBytes = ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8));
      return new XMLSignatureInput(decodedBytes);
   } else {
      os.write(ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8)));
      XMLSignatureInput output = new XMLSignatureInput((byte[])null);
      output.setOutputStream(os);
      return output;
   }
}
 
Example #10
Source File: DocumentResolver.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
   String id = context.attr.getNodeValue();
   if (id.startsWith("#")) {
      id = id.substring(1);
   }

   if (LOG.isDebugEnabled()) {
      try {
         LOG.debug("Selected document: " + ConnectorXmlUtils.flatten(ConnectorXmlUtils.toString((Source)(new DOMSource(this.doc)))));
      } catch (TechnicalConnectorException var5) {
         LOG.error(var5.getMessage());
      }
   }

   Node selectedElem = this.doc.getElementById(id);
   if (LOG.isDebugEnabled()) {
      LOG.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem);
   }

   this.processElement(context.attr, context.baseUri, selectedElem, id);
   XMLSignatureInput result = new XMLSignatureInput(selectedElem);
   result.setExcludeComments(true);
   result.setMIMEType("text/xml");
   result.setSourceURI(context.baseUri != null ? context.baseUri.concat(context.attr.getNodeValue()) : context.attr.getNodeValue());
   return result;
}
 
Example #11
Source File: NippinOptionalDeflateTransformer.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private XMLSignatureInput processElement(XMLSignatureInput input, OutputStream os) throws TechnicalConnectorException, IOException {
   Node el = input.getSubNode();
   if (input.getSubNode().getNodeType() == 3) {
      el = el.getParentNode();
   }

   StringBuilder sb = new StringBuilder();
   this.traverseElement((Element)el, sb);
   if (os == null) {
      byte[] decodedBytes = ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8));
      return new XMLSignatureInput(decodedBytes);
   } else {
      os.write(ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8)));
      XMLSignatureInput output = new XMLSignatureInput((byte[])null);
      output.setOutputStream(os);
      return output;
   }
}
 
Example #12
Source File: XadesTSpecification.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private byte[] generateTimestampDigest(Element baseElement, String c14nMethodValue) {
   try {
      Node signatureValue = DomUtils.getMatchingChilds(baseElement, "http://www.w3.org/2000/09/xmldsig#", "SignatureValue").item(0);
      Transform transform = new Transform(signatureValue.getOwnerDocument(), c14nMethodValue);
      XMLSignatureInput refData = transform.performTransform(new XMLSignatureInput(signatureValue));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (refData.isByteArray()) {
         baos.write(refData.getBytes());
      } else if (refData.isOctetStream()) {
         baos.write(ConnectorIOUtils.getBytes(refData.getOctetStream()));
      }

      return baos.toByteArray();
   } catch (Exception var7) {
      throw new IllegalArgumentException("Unable to calculateDigest", var7);
   }
}
 
Example #13
Source File: NippinOptionalDeflateTransformer.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private XMLSignatureInput processElement(XMLSignatureInput input, OutputStream os) throws TechnicalConnectorException, IOException {
   Node el = input.getSubNode();
   if (input.getSubNode().getNodeType() == 3) {
      el = el.getParentNode();
   }

   StringBuilder sb = new StringBuilder();
   this.traverseElement((Element)el, sb);
   if (os == null) {
      byte[] decodedBytes = ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8));
      return new XMLSignatureInput(decodedBytes);
   } else {
      os.write(ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8)));
      XMLSignatureInput output = new XMLSignatureInput((byte[])null);
      output.setOutputStream(os);
      return output;
   }
}
 
Example #14
Source File: DocumentResolver.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
   String id = context.attr.getNodeValue();
   if (id.startsWith("#")) {
      id = id.substring(1);
   }

   if (LOG.isDebugEnabled()) {
      try {
         LOG.debug("Selected document: " + ConnectorXmlUtils.flatten(ConnectorXmlUtils.toString((Source)(new DOMSource(this.doc)))));
      } catch (TechnicalConnectorException var5) {
         LOG.error(var5.getMessage());
      }
   }

   Node selectedElem = this.doc.getElementById(id);
   if (LOG.isDebugEnabled()) {
      LOG.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem);
   }

   this.processElement(context.attr, context.baseUri, selectedElem, id);
   XMLSignatureInput result = new XMLSignatureInput(selectedElem);
   result.setExcludeComments(true);
   result.setMIMEType("text/xml");
   result.setSourceURI(context.baseUri != null ? context.baseUri.concat(context.attr.getNodeValue()) : context.attr.getNodeValue());
   return result;
}
 
Example #15
Source File: XadesTSpecification.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private byte[] generateTimestampDigest(Element baseElement, String c14nMethodValue) {
   try {
      Node signatureValue = DomUtils.getMatchingChilds(baseElement, "http://www.w3.org/2000/09/xmldsig#", "SignatureValue").item(0);
      Transform transform = new Transform(signatureValue.getOwnerDocument(), c14nMethodValue);
      XMLSignatureInput refData = transform.performTransform(new XMLSignatureInput(signatureValue));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (refData.isByteArray()) {
         baos.write(refData.getBytes());
      } else if (refData.isOctetStream()) {
         baos.write(ConnectorIOUtils.getBytes(refData.getOctetStream()));
      }

      return baos.toByteArray();
   } catch (Exception var7) {
      throw new IllegalArgumentException("Unable to calculateDigest", var7);
   }
}
 
Example #16
Source File: NippinOptionalDeflateTransformer.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private XMLSignatureInput processElement(XMLSignatureInput input, OutputStream os) throws TechnicalConnectorException, IOException {
   Node el = input.getSubNode();
   if (input.getSubNode().getNodeType() == 3) {
      el = el.getParentNode();
   }

   StringBuilder sb = new StringBuilder();
   this.traverseElement((Element)el, sb);
   if (os == null) {
      byte[] decodedBytes = ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8));
      return new XMLSignatureInput(decodedBytes);
   } else {
      os.write(ConnectorIOUtils.decompress(ConnectorIOUtils.toBytes(sb.toString(), Charset.UTF_8)));
      XMLSignatureInput output = new XMLSignatureInput((byte[])null);
      output.setOutputStream(os);
      return output;
   }
}
 
Example #17
Source File: XadesTSpecification.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private byte[] generateTimestampDigest(Element baseElement, String c14nMethodValue) {
   try {
      Node signatureValue = DomUtils.getMatchingChilds(baseElement, "http://www.w3.org/2000/09/xmldsig#", "SignatureValue").item(0);
      Transform transform = new Transform(signatureValue.getOwnerDocument(), c14nMethodValue);
      XMLSignatureInput refData = transform.performTransform(new XMLSignatureInput(signatureValue));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (refData.isByteArray()) {
         baos.write(refData.getBytes());
      } else if (refData.isOctetStream()) {
         baos.write(ConnectorIOUtils.getBytes(refData.getOctetStream()));
      }

      return baos.toByteArray();
   } catch (Exception var7) {
      throw new IllegalArgumentException("Unable to calculateDigest", var7);
   }
}
 
Example #18
Source File: TimeStampDigestInputImpl.java    From xades4j with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void addNode(Node n) throws CannotAddDataToDigestInputException
{
    if (null == n)
    {
        throw new NullPointerException();
    }

    addToDigestInput(new XMLSignatureInput(n), n.getOwnerDocument());
}
 
Example #19
Source File: QualifyingPropertyVerificationContext.java    From xades4j with GNU Lesser General Public License v3.0 5 votes vote down vote up
private <TObj> TObj getReferencedElement(
        String objReference,
        Map<Element, TObj> mapper)
{
    if (!objReference.startsWith("#"))
        throw new IllegalArgumentException();

    if (mapper.isEmpty())
        return null;

    // A little workaround to be able to use the ResourceResolver.
    Attr refAttr = this.signatureDoc.createAttributeNS(null, "ref");
    refAttr.setNodeValue(objReference);
    this.signatureDoc.createElementNS(null, "dummy").setAttributeNodeNS(refAttr);

    try
    {
        XMLSignatureInput refData = ResourceResolver.getInstance(refAttr, "", true).resolve(refAttr, "", true);
        // This has to be a NodeSet data because it is a same-document reference.
        Node refNode = refData.getSubNode();
        if (refNode.getNodeType() != Node.ELEMENT_NODE)
            return null;
        // May return null.
        return mapper.get((Element)refNode);
    } catch (ResourceResolverException ex)
    {
        // Maybe an exception should be thrown...
        return null;
    }
}
 
Example #20
Source File: OtherSignerTests.java    From xades4j with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException
{
    XMLSignatureInput input = new XMLSignatureInput(context.attr.getValue().getBytes());
    resolveCount++;
    return input;
}
 
Example #21
Source File: DetachedSignatureResolver.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public XMLSignatureInput engineResolveURI(ResourceResolverContext context) throws ResourceResolverException {
	DSSDocument document = getCurrentDocument(context);
	if (document instanceof DigestDocument) {
		DigestDocument digestDoc = (DigestDocument) document;
		return new XMLSignatureInput(digestDoc.getDigest(digestAlgorithm));
	} else {
		return createFromCommonDocument(document);
	}
}
 
Example #22
Source File: DetachedSignatureResolver.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private XMLSignatureInput createFromCommonDocument(DSSDocument document) {
	// Full binaries are required
	final XMLSignatureInput result = new XMLSignatureInput(DSSUtils.toByteArray(document));
	final MimeType mimeType = document.getMimeType();
	if (mimeType != null) {
		result.setMIMEType(mimeType.getMimeTypeString());
	}
	return result;
}
 
Example #23
Source File: ManifestValidator.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private byte[] getBytesAfterTransforms(Node nodeToTransform, final List<Transform> transforms) throws DSSException {
	try {
		XMLSignatureInput xmlSignatureInput = new XMLSignatureInput(nodeToTransform);
		for (Transform transform : transforms) {
			xmlSignatureInput = transform.performTransform(xmlSignatureInput);
		}
		return xmlSignatureInput.getBytes();
	} catch (Exception e) {
		throw new DSSException("An error occurred during applying of transformations. Transforms cannot be performed!", e);
	}
}
 
Example #24
Source File: ComplexTransform.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] getBytesAfterTranformation(Node node, String uri) {
	if (transformObject == null) {
		buildTransformObject();
	}
	try {
		final XMLSignatureInput xmlSignatureInput = getXMLSignatureInput(node, uri);
		final XMLSignatureInput xmlSignatureInputOut = transformObject.performTransform(xmlSignatureInput);
		return xmlSignatureInputOut.getBytes();
	} catch (IOException | XMLSecurityException e) {
		throw new DSSException(String.format("Cannot process transformation [%s] on the given DOM object. Reason : [%s]", 
				algorithm, e.getMessage()), e);
	}
}
 
Example #25
Source File: ComplexTransform.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected XMLSignatureInput getXMLSignatureInput(Node node, String uri) {
	return new XMLSignatureInput(node);
}
 
Example #26
Source File: CanonicalizationTransform.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
protected XMLSignatureInput getXMLSignatureInput(Node node, String uri) {
	XMLSignatureInput xmlSignatureInput = super.getXMLSignatureInput(node, uri);
	xmlSignatureInput.setExcludeComments(isExcludeComments(uri));
	return xmlSignatureInput;
}