Java Code Examples for org.apache.xml.security.signature.XMLSignature#setId()

The following examples show how to use org.apache.xml.security.signature.XMLSignature#setId() . 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: SignedDataObjectsProcessorTest.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testAddNullReference() throws Exception
{
    System.out.println("addNullReference");

    Document doc = SignatureServicesTestBase.getNewDocument();

    SignedDataObjects dataObjsDescs = new SignedDataObjects()
        .withSignedDataObject(new AnonymousDataObjectReference("data".getBytes()));

    XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
    xmlSignature.setId("sigId");

    SignedDataObjectsProcessor processor = new SignedDataObjectsProcessor(new TestAlgorithmsProvider(), new AllwaysNullAlgsParamsMarshaller());
    Map<DataObjectDesc, Reference> result = processor.process(dataObjsDescs, xmlSignature);

    assertEquals(1, result.size());
    assertEquals(0, xmlSignature.getObjectLength());
    assertEquals(1, xmlSignature.getSignedInfo().getLength());

    Reference r = xmlSignature.getSignedInfo().item(0);
    assertNull(r.getElement().getAttributeNodeNS(Constants.SignatureSpecNS, "URI"));
}
 
Example 2
Source File: SignedDataObjectsProcessorTest.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testAddMultipleNullReferencesFails() throws Exception
{
    System.out.println("addMultipleNullReferencesFails");

    Document doc = SignatureServicesTestBase.getNewDocument();

    SignedDataObjects dataObjsDescs = new SignedDataObjects()
        .withSignedDataObject(new AnonymousDataObjectReference("data1".getBytes()))
        .withSignedDataObject(new AnonymousDataObjectReference("data2".getBytes()));

    XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
    xmlSignature.setId("sigId");

    SignedDataObjectsProcessor processor = new SignedDataObjectsProcessor(new TestAlgorithmsProvider(), new AllwaysNullAlgsParamsMarshaller());
    processor.process(dataObjsDescs, xmlSignature);
}
 
Example 3
Source File: XmlSignatureBuilder.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] sign(Credential signatureCredential, byte[] byteArrayToSign, Map<String, Object> options) throws TechnicalConnectorException {
   Map<String, Object> optionMap = new HashMap();
   if (options != null) {
      optionMap.putAll(options);
   }

   this.validateInput(signatureCredential, byteArrayToSign);

   try {
      String xmldsigId = "xmldsig-" + IdGeneratorFactory.getIdGenerator("uuid").generateId();
      String baseURI = (String)SignatureUtils.getOption("baseURI", optionMap, "");
      String signatureMethodURI = (String)SignatureUtils.getOption("signatureMethodURI", optionMap, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
      String canonicalizationMethodURI = (String)SignatureUtils.getOption("canonicalizationMethodURI", optionMap, "http://www.w3.org/2001/10/xml-exc-c14n#");
      String digestURI = (String)SignatureUtils.getOption("digestURI", optionMap, "http://www.w3.org/2001/04/xmlenc#sha256");
      String encapsulateLocation = (String)SignatureUtils.getOption("encapsulate-xpath", optionMap, (Object)null);
      EncapsulationTransformer encapsulationTranformer = (EncapsulationTransformer)SignatureUtils.getOption("encapsulate-transformer", optionMap, new XmlSignatureBuilder.PassthroughEncapsulationTransformer());
      List<String> transformerList = getTransformerList(optionMap);
      Document doc = ConnectorXmlUtils.toDocument(byteArrayToSign);
      XMLSignature sig = new XMLSignature(doc, baseURI, signatureMethodURI, canonicalizationMethodURI);
      sig.addResourceResolver(new DocumentResolver(doc));
      sig.addDocument(ref(baseURI), transforms(transformerList, doc), digestURI);
      addKeyInfo(signatureCredential, sig);
      XadesHandler handler = new XadesHandler(sig, signatureCredential, options, this.specs);
      handler.before();
      sig.sign(signatureCredential.getPrivateKey());
      sig.setId(xmldsigId);
      handler.after();
      return transform(mustEncapsulate(transformerList), encapsulateLocation, encapsulationTranformer, doc, sig);
   } catch (Exception var16) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_GENERAL, var16, new Object[]{var16.getMessage()});
   }
}
 
Example 4
Source File: XmlSignatureBuilder.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] sign(Credential signatureCredential, byte[] byteArrayToSign, Map<String, Object> options) throws TechnicalConnectorException {
   Map<String, Object> optionMap = new HashMap();
   if (options != null) {
      optionMap.putAll(options);
   }

   this.validateInput(signatureCredential, byteArrayToSign);

   try {
      String xmldsigId = "xmldsig-" + IdGeneratorFactory.getIdGenerator("uuid").generateId();
      String baseURI = (String)SignatureUtils.getOption("baseURI", optionMap, "");
      String signatureMethodURI = (String)SignatureUtils.getOption("signatureMethodURI", optionMap, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
      String canonicalizationMethodURI = (String)SignatureUtils.getOption("canonicalizationMethodURI", optionMap, "http://www.w3.org/2001/10/xml-exc-c14n#");
      String digestURI = (String)SignatureUtils.getOption("digestURI", optionMap, "http://www.w3.org/2001/04/xmlenc#sha256");
      String encapsulateLocation = (String)SignatureUtils.getOption("encapsulate-xpath", optionMap, (Object)null);
      EncapsulationTransformer encapsulationTranformer = (EncapsulationTransformer)SignatureUtils.getOption("encapsulate-transformer", optionMap, new XmlSignatureBuilder.PassthroughEncapsulationTransformer());
      List<String> transformerList = getTransformerList(optionMap);
      Document doc = ConnectorXmlUtils.toDocument(byteArrayToSign);
      XMLSignature sig = new XMLSignature(doc, baseURI, signatureMethodURI, canonicalizationMethodURI);
      sig.addResourceResolver(new DocumentResolver(doc));
      sig.addDocument(ref(baseURI), transforms(transformerList, doc), digestURI);
      addKeyInfo(signatureCredential, sig);
      XadesHandler handler = new XadesHandler(sig, signatureCredential, options, this.specs);
      handler.before();
      sig.sign(signatureCredential.getPrivateKey());
      sig.setId(xmldsigId);
      handler.after();
      return transform(mustEncapsulate(transformerList), encapsulateLocation, encapsulationTranformer, doc, sig);
   } catch (Exception var16) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_GENERAL, var16, new Object[]{var16.getMessage()});
   }
}
 
Example 5
Source File: XmlSignatureBuilder.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] sign(Credential signatureCredential, byte[] byteArrayToSign, Map<String, Object> options) throws TechnicalConnectorException {
   Map<String, Object> optionMap = new HashMap();
   if (options != null) {
      optionMap.putAll(options);
   }

   this.validateInput(signatureCredential, byteArrayToSign);

   try {
      String xmldsigId = "xmldsig-" + IdGeneratorFactory.getIdGenerator("uuid").generateId();
      String baseURI = (String)SignatureUtils.getOption("baseURI", optionMap, "");
      String signatureMethodURI = (String)SignatureUtils.getOption("signatureMethodURI", optionMap, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
      String canonicalizationMethodURI = (String)SignatureUtils.getOption("canonicalizationMethodURI", optionMap, "http://www.w3.org/2001/10/xml-exc-c14n#");
      String digestURI = (String)SignatureUtils.getOption("digestURI", optionMap, "http://www.w3.org/2001/04/xmlenc#sha256");
      String encapsulateLocation = (String)SignatureUtils.getOption("encapsulate-xpath", optionMap, (Object)null);
      EncapsulationTransformer encapsulationTranformer = (EncapsulationTransformer)SignatureUtils.getOption("encapsulate-transformer", optionMap, new XmlSignatureBuilder.PassthroughEncapsulationTransformer());
      List<String> transformerList = getTransformerList(optionMap);
      Document doc = ConnectorXmlUtils.toDocument(byteArrayToSign);
      XMLSignature sig = new XMLSignature(doc, baseURI, signatureMethodURI, canonicalizationMethodURI);
      sig.addResourceResolver(new DocumentResolver(doc));
      sig.addDocument(ref(baseURI), transforms(transformerList, doc), digestURI);
      addKeyInfo(signatureCredential, sig);
      XadesHandler handler = new XadesHandler(sig, signatureCredential, options, this.specs);
      handler.before();
      sig.sign(signatureCredential.getPrivateKey());
      sig.setId(xmldsigId);
      handler.after();
      return transform(mustEncapsulate(transformerList), encapsulateLocation, encapsulationTranformer, doc, sig);
   } catch (Exception var16) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_GENERAL, var16, new Object[]{var16.getMessage()});
   }
}
 
Example 6
Source File: XmlSignatureBuilder.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] sign(Credential signatureCredential, byte[] byteArrayToSign, Map<String, Object> options) throws TechnicalConnectorException {
   Map<String, Object> optionMap = new HashMap();
   if (options != null) {
      optionMap.putAll(options);
   }

   this.validateInput(signatureCredential, byteArrayToSign);

   try {
      String xmldsigId = "xmldsig-" + IdGeneratorFactory.getIdGenerator("uuid").generateId();
      String baseURI = (String)SignatureUtils.getOption("baseURI", optionMap, "");
      String signatureMethodURI = (String)SignatureUtils.getOption("signatureMethodURI", optionMap, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
      String canonicalizationMethodURI = (String)SignatureUtils.getOption("canonicalizationMethodURI", optionMap, "http://www.w3.org/2001/10/xml-exc-c14n#");
      String digestURI = (String)SignatureUtils.getOption("digestURI", optionMap, "http://www.w3.org/2001/04/xmlenc#sha256");
      String encapsulateLocation = (String)SignatureUtils.getOption("encapsulate-xpath", optionMap, (Object)null);
      EncapsulationTransformer encapsulationTranformer = (EncapsulationTransformer)SignatureUtils.getOption("encapsulate-transformer", optionMap, new XmlSignatureBuilder.PassthroughEncapsulationTransformer());
      List<String> transformerList = getTransformerList(optionMap);
      Document doc = ConnectorXmlUtils.toDocument(byteArrayToSign);
      XMLSignature sig = new XMLSignature(doc, baseURI, signatureMethodURI, canonicalizationMethodURI);
      sig.addResourceResolver(new DocumentResolver(doc));
      sig.addDocument(ref(baseURI), transforms(transformerList, doc), digestURI);
      addKeyInfo(signatureCredential, sig);
      XadesHandler handler = new XadesHandler(sig, signatureCredential, options, this.specs);
      handler.before();
      sig.sign(signatureCredential.getPrivateKey());
      sig.setId(xmldsigId);
      handler.after();
      return transform(mustEncapsulate(transformerList), encapsulateLocation, encapsulationTranformer, doc, sig);
   } catch (Exception var16) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_GENERAL, var16, new Object[]{var16.getMessage()});
   }
}
 
Example 7
Source File: XmlSignatureBuilder.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] sign(Credential signatureCredential, byte[] byteArrayToSign, Map<String, Object> options) throws TechnicalConnectorException {
   Map<String, Object> optionMap = new HashMap();
   if (options != null) {
      optionMap.putAll(options);
   }

   this.validateInput(signatureCredential, byteArrayToSign);

   try {
      String xmldsigId = "xmldsig-" + IdGeneratorFactory.getIdGenerator("uuid").generateId();
      String baseURI = (String)SignatureUtils.getOption("baseURI", optionMap, "");
      String signatureMethodURI = (String)SignatureUtils.getOption("signatureMethodURI", optionMap, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
      String canonicalizationMethodURI = (String)SignatureUtils.getOption("canonicalizationMethodURI", optionMap, "http://www.w3.org/2001/10/xml-exc-c14n#");
      String digestURI = (String)SignatureUtils.getOption("digestURI", optionMap, "http://www.w3.org/2001/04/xmlenc#sha256");
      String encapsulateLocation = (String)SignatureUtils.getOption("encapsulate-xpath", optionMap, (Object)null);
      EncapsulationTransformer encapsulationTranformer = (EncapsulationTransformer)SignatureUtils.getOption("encapsulate-transformer", optionMap, new XmlSignatureBuilder.PassthroughEncapsulationTransformer());
      List<String> transformerList = getTransformerList(optionMap);
      Document doc = ConnectorXmlUtils.toDocument(byteArrayToSign);
      XMLSignature sig = new XMLSignature(doc, baseURI, signatureMethodURI, canonicalizationMethodURI);
      sig.addResourceResolver(new DocumentResolver(doc));
      sig.addDocument(ref(baseURI), transforms(transformerList, doc), digestURI);
      addKeyInfo(signatureCredential, sig);
      XadesHandler handler = new XadesHandler(sig, signatureCredential, options, this.specs);
      handler.before();
      sig.sign(signatureCredential.getPrivateKey());
      sig.setId(xmldsigId);
      handler.after();
      return transform(mustEncapsulate(transformerList), encapsulateLocation, encapsulationTranformer, doc, sig);
   } catch (Exception var16) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_GENERAL, var16, new Object[]{var16.getMessage()});
   }
}
 
Example 8
Source File: SignedDataObjectsProcessorTest.java    From xades4j with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testProcess() throws Exception
{
    System.out.println("process");

    Document doc = getNewDocument();

    SignedDataObjects dataObjsDescs = new SignedDataObjects()
        .withSignedDataObject(new DataObjectReference("uri").withTransform(new EnvelopedSignatureTransform()))
        .withSignedDataObject(new EnvelopedXmlObject(doc.createElement("test1")))
        .withSignedDataObject(new EnvelopedXmlObject(doc.createElement("test2"), "text/xml", null));

    XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
    xmlSignature.setId("sigId");

    AllwaysNullAlgsParamsMarshaller algsParamsMarshaller = new AllwaysNullAlgsParamsMarshaller();

    SignedDataObjectsProcessor processor = new SignedDataObjectsProcessor(new TestAlgorithmsProvider(), algsParamsMarshaller);
    Map<DataObjectDesc, Reference> result = processor.process(dataObjsDescs, xmlSignature);

    assertEquals(dataObjsDescs.getDataObjectsDescs().size(), result.size());
    assertEquals(2, xmlSignature.getObjectLength());
    assertEquals(xmlSignature.getSignedInfo().getLength(), dataObjsDescs.getDataObjectsDescs().size());

    assertEquals(1, algsParamsMarshaller.getInvokeCount());
    Reference ref = xmlSignature.getSignedInfo().item(0);
    assertEquals(1, ref.getTransforms().getLength());

    ObjectContainer obj = xmlSignature.getObjectItem(1);
    assertEquals("text/xml", obj.getMimeType());
    assertTrue(StringUtils.isNullOrEmptyString(obj.getEncoding()));

}
 
Example 9
Source File: KeyInfoBuilderTest.java    From xades4j with GNU Lesser General Public License v3.0 5 votes vote down vote up
private XMLSignature getTestSignature() throws Exception
{
    Document doc = getNewDocument();
    XMLSignature xmlSignature = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
    xmlSignature.setId("sigId");
    doc.appendChild(xmlSignature.getElement());
    return xmlSignature;
}