org.apache.xml.security.transforms.Transform Java Examples

The following examples show how to use org.apache.xml.security.transforms.Transform. 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: 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 #2
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 #3
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 #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: 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 #6
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 #7
Source File: TransformUtils.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a Transform element for a given algorithm.
 * @param algorithm algorithm
 * @param parametersMarshallingProvider algorithm parameters marshaller
 * @param document the target XML document
 * @return the Transform
 * @throws UnsupportedAlgorithmException if the algorithm is not supported
 */
public static Transform createTransform(Algorithm algorithm, AlgorithmsParametersMarshallingProvider parametersMarshallingProvider, Document document) throws UnsupportedAlgorithmException
{
    List<Node> params = parametersMarshallingProvider.marshalParameters(algorithm, document);
    try
    {
        if (null == params)
        {
            return new Transform(document, algorithm.getUri());
        }
        else
        {
            return new Transform(document, algorithm.getUri(), DOMHelper.nodeList(params));
        }
    }
    catch (InvalidTransformException ex)
    {
        throw new UnsupportedAlgorithmException("C14N algorithm not supported in the XML Signature provider", algorithm.getUri(), ex);
    }
}
 
Example #8
Source File: ConfigurationModuleRegisterTransformers.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void init(Configuration config) throws TechnicalConnectorException {
   if (!loaded) {
      try {
         Transform.register("urn:nippin:xml:sig:transform:optional-deflate", NippinOptionalDeflateTransformer.class);
         loaded = true;
      } catch (AlgorithmAlreadyRegisteredException var3) {
         LOG.error("Algorihm [{}] already loaded.", "urn:nippin:xml:sig:transform:optional-deflate", var3);
      }
   }

}
 
Example #9
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 #10
Source File: ManifestValidator.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Transform> getTransforms(List<String> transformNames) throws InvalidTransformException {
	List<Transform> transforms = new ArrayList<>();
	for (String algorithm : transformNames) {
		transforms.add(new Transform(signatureElement.getOwnerDocument(), algorithm));
	}
	return transforms;
}
 
Example #11
Source File: ManifestValidator.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean isIntact(final ReferenceValidation referenceValidation, Node node) {
	Digest digest = referenceValidation.getDigest();
	if (digest == null) {
		return false;
	}
	try {
		List<Transform> transforms = getTransforms(referenceValidation.getTransformationNames());
		byte[] bytesAfterTransforms = getBytesAfterTransforms(node, transforms);
		return Arrays.equals(digest.getValue(), DSSUtils.digest(digest.getAlgorithm(), bytesAfterTransforms));
	} catch (Exception e) {
		LOG.warn("Unable to verify integrity for element '{}' : {}", node.getLocalName(), e.getMessage());
		return false;
	}
}
 
Example #12
Source File: SAMLObjectContentReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Populate the inclusive namspace prefixes on the specified Apache (exclusive) transform object.
 * 
 * @param signature the Apache XMLSignature object
 * @param transform the Apache Transform object representing an exclusive transform
 */
private void processExclusiveTransform(XMLSignature signature, Transform transform) {
    // Namespaces that aren't visibly used, such as those used in QName attribute values, would
    // be stripped out by exclusive canonicalization. Need to make sure they aren't by explicitly
    // telling the transformer about them.
    log.debug("Adding list of inclusive namespaces for signature exclusive canonicalization transform");
    LazySet<String> inclusiveNamespacePrefixes = new LazySet<String>();
    populateNamespacePrefixes(inclusiveNamespacePrefixes, signableObject);
    
    if (inclusiveNamespacePrefixes != null && inclusiveNamespacePrefixes.size() > 0) {
        InclusiveNamespaces inclusiveNamespaces = new InclusiveNamespaces(signature.getDocument(),
                inclusiveNamespacePrefixes);
        transform.getElement().appendChild(inclusiveNamespaces.getElement());
    }
}
 
Example #13
Source File: ConfigurationModuleRegisterTransformers.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void init(Configuration config) throws TechnicalConnectorException {
   if (!loaded) {
      try {
         Transform.register("urn:nippin:xml:sig:transform:optional-deflate", NippinOptionalDeflateTransformer.class);
         loaded = true;
      } catch (AlgorithmAlreadyRegisteredException var3) {
         LOG.error("Algorihm [{}] already loaded.", "urn:nippin:xml:sig:transform:optional-deflate", var3);
      }
   }

}
 
Example #14
Source File: ConfigurationModuleRegisterTransformers.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void init(Configuration config) throws TechnicalConnectorException {
   if (!loaded) {
      try {
         Transform.register("urn:nippin:xml:sig:transform:optional-deflate", NippinOptionalDeflateTransformer.class);
         loaded = true;
      } catch (AlgorithmAlreadyRegisteredException var3) {
         LOG.error("Algorihm [{}] already loaded.", "urn:nippin:xml:sig:transform:optional-deflate", var3);
      }
   }

}
 
Example #15
Source File: ConfigurationModuleRegisterTransformers.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void init(Configuration config) throws TechnicalConnectorException {
   if (!loaded) {
      try {
         Transform.register("urn:nippin:xml:sig:transform:optional-deflate", NippinOptionalDeflateTransformer.class);
         loaded = true;
      } catch (AlgorithmAlreadyRegisteredException var3) {
         LOG.error("Algorihm [{}] already loaded.", "urn:nippin:xml:sig:transform:optional-deflate", var3);
      }
   }

}
 
Example #16
Source File: ConfigurationModuleRegisterTransformers.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void init(Configuration config) throws TechnicalConnectorException {
   if (!loaded) {
      try {
         Transform.register("urn:nippin:xml:sig:transform:optional-deflate", NippinOptionalDeflateTransformer.class);
         loaded = true;
      } catch (AlgorithmAlreadyRegisteredException var3) {
         LOG.error("Algorihm [{}] already loaded.", "urn:nippin:xml:sig:transform:optional-deflate", var3);
      }
   }

}
 
Example #17
Source File: SantuarioInitializer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Dynamically initialise the library by registering the default
 * algorithms/implementations
 */
private static void dynamicInit() {
	//
	// Load the Resource Bundle - the default is the English resource bundle.
	// To load another resource bundle, call I18n.init(...) before calling this
	// method.
	//
	I18n.init("en", "US");

	if (LOG.isDebugEnabled()) {
		LOG.debug("Registering default algorithms");
	}
	try {
		//
		// Bind the default prefixes
		//
		ElementProxy.registerDefaultPrefixes();
	} catch (XMLSecurityException ex) {
		LOG.error(ex.getMessage(), ex);
	}

	//
	// Set the default Transforms
	//
	Transform.registerDefaultAlgorithms();

	//
	// Set the default signature algorithms
	//
	SignatureAlgorithm.registerDefaultAlgorithms();

	//
	// Set the default JCE algorithms
	//
	JCEMapper.registerDefaultAlgorithms();

	//
	// Set the default c14n algorithms
	//
	Canonicalizer.registerDefaultAlgorithms();

	//
	// Register the default resolvers (custom)
	//
	registerDefaultResolvers();

	//
	// Register the default key resolvers
	//
	KeyResolver.registerDefaultResolvers();
}