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);
}
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());
}
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");
}
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()));
}
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");
}
Aggregations