Search in sources :

Example 1 with EnvelopedSignatureTransform

use of xades4j.algorithms.EnvelopedSignatureTransform in project xades4j by luisgoncalves.

the class Enveloped method sign.

/**
 * Creates an enveloped signature over an element. The element must have an
 * Id or be the document root if it doesn't. In the last case an empty (URI="")
 * reference is used.
 *
 * @param elementToSign the element that will be signed and will be the signature's parent
 *
 * @throws XAdES4jException see {@link XadesSigner#sign(xades4j.production.SignedDataObjects, org.w3c.dom.Node)}
 * @throws IllegalArgumentException if {@code elementToSign} doesn't have an Id and isn't the document root
 */
public void sign(Element elementToSign) throws XAdES4jException {
    String refUri;
    if (elementToSign.hasAttribute("Id"))
        refUri = '#' + elementToSign.getAttribute("Id");
    else {
        if (elementToSign.getParentNode().getNodeType() != Node.DOCUMENT_NODE)
            throw new IllegalArgumentException("Element without Id must be the document root");
        refUri = "";
    }
    DataObjectDesc dataObjRef = new DataObjectReference(refUri).withTransform(new EnvelopedSignatureTransform());
    signer.sign(new SignedDataObjects(dataObjRef), elementToSign);
}
Also used : EnvelopedSignatureTransform(xades4j.algorithms.EnvelopedSignatureTransform) DataObjectDesc(xades4j.properties.DataObjectDesc)

Example 2 with EnvelopedSignatureTransform

use of xades4j.algorithms.EnvelopedSignatureTransform in project xades4j by luisgoncalves.

the class OtherSignerTests method testSignAndAppendAsFirstChild.

@Test
public void testSignAndAppendAsFirstChild() throws Exception {
    System.out.println("signAndAppendAsFirstChild");
    Document doc = getTestDocument();
    Element root = doc.getDocumentElement();
    XadesSigner signer = new XadesBesSigningProfile(keyingProviderMy).newSigner();
    DataObjectDesc obj1 = new DataObjectReference('#' + root.getAttribute("Id")).withTransform(new EnvelopedSignatureTransform());
    SignedDataObjects dataObjs = new SignedDataObjects(obj1);
    signer.sign(dataObjs, root, SignatureAppendingStrategies.AsFirstChild);
    Element firstChild = (Element) doc.getDocumentElement().getFirstChild();
    assertEquals(Constants._TAG_SIGNATURE, firstChild.getLocalName());
    assertEquals(Constants.SignatureSpecNS, firstChild.getNamespaceURI());
}
Also used : Element(org.w3c.dom.Element) EnvelopedSignatureTransform(xades4j.algorithms.EnvelopedSignatureTransform) Document(org.w3c.dom.Document) DataObjectDesc(xades4j.properties.DataObjectDesc) Test(org.junit.Test)

Example 3 with EnvelopedSignatureTransform

use of xades4j.algorithms.EnvelopedSignatureTransform in project xades4j by luisgoncalves.

the class SignerBESTest method testSignBESWithCounterSig.

@Test
public void testSignBESWithCounterSig() throws Exception {
    System.out.println("signBESWithCounterSig");
    Document doc = getTestDocument();
    Element elemToSign = doc.getDocumentElement();
    XadesBesSigningProfile profile = new XadesBesSigningProfile(keyingProviderMy);
    final XadesSigner counterSigner = profile.newSigner();
    profile.withSignaturePropertiesProvider(new SignaturePropertiesProvider() {

        @Override
        public void provideProperties(SignaturePropertiesCollector signedPropsCol) {
            signedPropsCol.addCounterSignature(new CounterSignatureProperty(counterSigner));
            signedPropsCol.setSignerRole(new SignerRoleProperty("CounterSignature maniac"));
        }
    });
    SignerBES signer = (SignerBES) profile.newSigner();
    DataObjectDesc obj1 = new DataObjectReference('#' + elemToSign.getAttribute("Id")).withTransform(new EnvelopedSignatureTransform());
    SignedDataObjects dataObjs = new SignedDataObjects().withSignedDataObject(obj1);
    signer.sign(dataObjs, elemToSign);
    outputDocument(doc, "document.signed.bes.cs.xml");
}
Also used : Element(org.w3c.dom.Element) SignerRoleProperty(xades4j.properties.SignerRoleProperty) SignaturePropertiesCollector(xades4j.providers.SignaturePropertiesCollector) EnvelopedSignatureTransform(xades4j.algorithms.EnvelopedSignatureTransform) Document(org.w3c.dom.Document) SignaturePropertiesProvider(xades4j.providers.SignaturePropertiesProvider) DataObjectDesc(xades4j.properties.DataObjectDesc) CounterSignatureProperty(xades4j.properties.CounterSignatureProperty) Test(org.junit.Test)

Example 4 with EnvelopedSignatureTransform

use of xades4j.algorithms.EnvelopedSignatureTransform in project xades4j by luisgoncalves.

the class SignedDataObjectsProcessorTest method testProcess.

@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()));
}
Also used : Reference(org.apache.xml.security.signature.Reference) EnvelopedSignatureTransform(xades4j.algorithms.EnvelopedSignatureTransform) Document(org.w3c.dom.Document) DataObjectDesc(xades4j.properties.DataObjectDesc) XMLSignature(org.apache.xml.security.signature.XMLSignature) ObjectContainer(org.apache.xml.security.signature.ObjectContainer) Test(org.junit.Test)

Example 5 with EnvelopedSignatureTransform

use of xades4j.algorithms.EnvelopedSignatureTransform in project xades4j by luisgoncalves.

the class SignerBESTest method testSignBES.

@Test
public void testSignBES() throws Exception {
    System.out.println("signBES");
    Document doc1 = getTestDocument();
    Document doc2 = getDocument("content.xml");
    Node objectContent = doc1.importNode(doc2.getDocumentElement(), true);
    Element elemToSign = doc1.getDocumentElement();
    SignerBES signer = (SignerBES) new XadesBesSigningProfile(keyingProviderMy).newSigner();
    IndividualDataObjsTimeStampProperty dataObjsTimeStamp = new IndividualDataObjsTimeStampProperty();
    AllDataObjsCommitmentTypeProperty globalCommitment = AllDataObjsCommitmentTypeProperty.proofOfApproval();
    CommitmentTypeProperty commitment = (CommitmentTypeProperty) CommitmentTypeProperty.proofOfCreation().withQualifier("MyQualifier");
    DataObjectDesc obj1 = new DataObjectReference('#' + elemToSign.getAttribute("Id")).withTransform(new EnvelopedSignatureTransform()).withDataObjectFormat(new DataObjectFormatProperty("text/xml", "MyEncoding").withDescription("Isto é uma descrição do elemento raiz").withDocumentationUri("http://doc1.txt").withDocumentationUri("http://doc2.txt").withIdentifier("http://elem.root")).withCommitmentType(commitment).withDataObjectTimeStamp(dataObjsTimeStamp);
    DataObjectDesc obj2 = new EnvelopedXmlObject(objectContent, "text/xml", null).withDataObjectFormat(new DataObjectFormatProperty("text/xml", "MyEncoding").withDescription("Isto é uma descrição do elemento dentro do object").withDocumentationUri("http://doc3.txt").withDocumentationUri("http://doc4.txt").withIdentifier("http://elem.in.object")).withCommitmentType(commitment).withDataObjectTimeStamp(dataObjsTimeStamp);
    SignedDataObjects dataObjs = new SignedDataObjects(obj1, obj2).withCommitmentType(globalCommitment).withDataObjectsTimeStamp();
    signer.sign(dataObjs, elemToSign);
    outputDocument(doc1, "document.signed.bes.xml");
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DataObjectFormatProperty(xades4j.properties.DataObjectFormatProperty) EnvelopedSignatureTransform(xades4j.algorithms.EnvelopedSignatureTransform) Document(org.w3c.dom.Document) DataObjectDesc(xades4j.properties.DataObjectDesc) AllDataObjsCommitmentTypeProperty(xades4j.properties.AllDataObjsCommitmentTypeProperty) AllDataObjsCommitmentTypeProperty(xades4j.properties.AllDataObjsCommitmentTypeProperty) CommitmentTypeProperty(xades4j.properties.CommitmentTypeProperty) IndividualDataObjsTimeStampProperty(xades4j.properties.IndividualDataObjsTimeStampProperty) Test(org.junit.Test)

Aggregations

EnvelopedSignatureTransform (xades4j.algorithms.EnvelopedSignatureTransform)5 DataObjectDesc (xades4j.properties.DataObjectDesc)5 Test (org.junit.Test)4 Document (org.w3c.dom.Document)4 Element (org.w3c.dom.Element)3 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)1 Reference (org.apache.xml.security.signature.Reference)1 XMLSignature (org.apache.xml.security.signature.XMLSignature)1 Node (org.w3c.dom.Node)1 AllDataObjsCommitmentTypeProperty (xades4j.properties.AllDataObjsCommitmentTypeProperty)1 CommitmentTypeProperty (xades4j.properties.CommitmentTypeProperty)1 CounterSignatureProperty (xades4j.properties.CounterSignatureProperty)1 DataObjectFormatProperty (xades4j.properties.DataObjectFormatProperty)1 IndividualDataObjsTimeStampProperty (xades4j.properties.IndividualDataObjsTimeStampProperty)1 SignerRoleProperty (xades4j.properties.SignerRoleProperty)1 SignaturePropertiesCollector (xades4j.providers.SignaturePropertiesCollector)1 SignaturePropertiesProvider (xades4j.providers.SignaturePropertiesProvider)1