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

The following examples show how to use org.apache.xml.security.signature.XMLSignature#addDocument() . 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: XmlSigOutInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private XMLSignature prepareEnvelopingSignature(Document doc,
                                                String id,
                                                String referenceId,
                                                String sigAlgo,
                                                String digestAlgo) throws Exception {
    Element docEl = doc.getDocumentElement();
    Document newDoc = DOMUtils.createDocument();
    doc.removeChild(docEl);
    newDoc.adoptNode(docEl);
    Element object = newDoc.createElementNS(Constants.SignatureSpecNS, "ds:Object");
    object.appendChild(docEl);
    docEl.setAttributeNS(null, "Id", id);
    docEl.setIdAttributeNS(null, "Id", true);

    XMLSignature sig = new XMLSignature(newDoc, "", sigAlgo);
    newDoc.appendChild(sig.getElement());
    sig.getElement().appendChild(object);

    Transforms transforms = new Transforms(newDoc);
    transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

    sig.addDocument(referenceId, transforms, digestAlgo);
    return sig;
}
 
Example 2
Source File: XmlSigOutInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private XMLSignature prepareDetachedSignature(Document doc,
        String id,
        String referenceId,
        String sigAlgo,
        String digestAlgo) throws Exception {
    Element docEl = doc.getDocumentElement();
    Document newDoc = DOMUtils.createDocument();
    doc.removeChild(docEl);
    newDoc.adoptNode(docEl);
    docEl.setAttributeNS(null, "Id", id);
    docEl.setIdAttributeNS(null, "Id", true);

    Element root = newDoc.createElementNS(envelopeQName.getNamespaceURI(),
            envelopeQName.getPrefix() + ":" + envelopeQName.getLocalPart());
    root.appendChild(docEl);
    newDoc.appendChild(root);

    XMLSignature sig = new XMLSignature(newDoc, "", sigAlgo);
    root.appendChild(sig.getElement());

    Transforms transforms = new Transforms(newDoc);
    transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

    sig.addDocument(referenceId, transforms, digestAlgo);
    return sig;
}
 
Example 3
Source File: XmlSigOutInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private XMLSignature prepareEnvelopedSignature(Document doc,
        String id,
        String referenceURI,
        String sigAlgo,
        String digestAlgo) throws Exception {
    doc.getDocumentElement().setAttributeNS(null, "Id", id);
    doc.getDocumentElement().setIdAttributeNS(null, "Id", true);

    XMLSignature sig = new XMLSignature(doc, "", sigAlgo);
    doc.getDocumentElement().appendChild(sig.getElement());
    Transforms transforms = new Transforms(doc);
    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);

    sig.addDocument(referenceURI, transforms, digestAlgo);
    return sig;
}
 
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: 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 9
Source File: URIContentReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void createReference(XMLSignature signature) {
    try {
        Transforms dsigTransforms = new Transforms(signature.getDocument());
        for (String transform : transforms) {
            dsigTransforms.addTransform(transform);
        }

        signature.addDocument(referenceID, dsigTransforms, digestAlgorithm);
    } catch (Exception e) {
        log.error("Error while adding content reference", e);
    }
}