use of org.apache.xml.security.signature.Reference in project xades4j by luisgoncalves.
the class DataGenCommitmentType method generatePropertyData.
@Override
public PropertyDataObject generatePropertyData(CommitmentTypeProperty prop, PropertiesDataGenerationContext ctx) {
CommitmentTypeData commTypeData = new CommitmentTypeData(prop.getUri(), prop.getDescription());
/* One ObjectReference element refers to one ds:Reference element of the
* ds:SignedInfo corresponding with one data object qualified by this
* property. If some but not all the signed data objects share the same
* commitment, one ObjectReference element MUST appear for each one of
* them. However, if all the signed data objects share the same commitment,
* the AllSignedDataObjects empty element MUST be present.
*/
Collection<DataObjectDesc> targets = prop.getTargetDataObjects();
Map<DataObjectDesc, Reference> referencesMappings = ctx.getReferencesMappings();
for (DataObjectDesc obj : targets) {
// The ObjectReference refers the Reference element. This assumes
// that the QualifyingProperties are in the signature's document.
commTypeData.addObjReferences('#' + referencesMappings.get(obj).getId());
}
commTypeData.setQualifiers(prop.getQualifiers());
return commTypeData;
}
use of org.apache.xml.security.signature.Reference in project xades4j by luisgoncalves.
the class DataGenIndivDataObjsTimeStamp method addPropSpecificTimeStampInput.
@Override
protected void addPropSpecificTimeStampInput(IndividualDataObjsTimeStampProperty prop, TimeStampDigestInput digestInput, PropertiesDataGenerationContext ctx) throws CannotAddDataToDigestInputException {
Collection<DataObjectDesc> targetDataObjs = prop.getTargetDataObjects();
Map<DataObjectDesc, Reference> refsMaps = ctx.getReferencesMappings();
for (DataObjectDesc dataObj : targetDataObjs) {
Reference r = refsMaps.get(dataObj);
digestInput.addReference(r);
}
}
use of org.apache.xml.security.signature.Reference in project xades4j by luisgoncalves.
the class DataGenArchiveTimeStamp method addPropSpecificTimeStampInput.
@Override
protected void addPropSpecificTimeStampInput(ArchiveTimeStampProperty prop, TimeStampDigestInput digestInput, PropertiesDataGenerationContext ctx) throws CannotAddDataToDigestInputException, PropertyDataGenerationException {
Element unsignedSigPropsElem = DOMHelper.getFirstDescendant(ctx.getTargetXmlSignature().getElement(), QualifyingProperty.XADES_XMLNS, QualifyingProperty.UNSIGNED_SIGNATURE_PROPS_TAG);
if (null == unsignedSigPropsElem)
throw new PropertyDataGenerationException(prop, "no unsigned signature properties to get inputs");
try {
// References, processed accordingly to XML-DSIG.
List<Reference> refs = ctx.getReferences();
for (Reference r : refs) {
digestInput.addReference(r);
}
// SignedInfo.
Element e = ctx.getTargetXmlSignature().getSignedInfo().getElement();
digestInput.addNode(e);
// SignatureValue.
e = DOMHelper.getFirstDescendant(ctx.getTargetXmlSignature().getElement(), Constants.SignatureSpecNS, Constants._TAG_SIGNATUREVALUE);
digestInput.addNode(e);
// KeyInfo, if present.
KeyInfo ki = ctx.getTargetXmlSignature().getKeyInfo();
if (ki != null)
digestInput.addNode(ki.getElement());
// Unsigned properties, in order of appearance.
Map<String, Integer> propsCnt = new HashMap<String, Integer>(5);
propsCnt.put(CertificateValuesProperty.PROP_NAME, 0);
propsCnt.put(RevocationValuesProperty.PROP_NAME, 0);
propsCnt.put(CompleteCertificateRefsProperty.PROP_NAME, 0);
propsCnt.put(CompleteRevocationRefsProperty.PROP_NAME, 0);
propsCnt.put(SignatureTimeStampProperty.PROP_NAME, 0);
e = DOMHelper.getFirstChildElement(unsignedSigPropsElem);
// UnsignedProperties shouldn't be empty!
do {
digestInput.addNode(e);
Integer pCnt = propsCnt.get(e.getLocalName());
if (pCnt != null)
propsCnt.put(e.getLocalName(), pCnt += 1);
} while ((e = DOMHelper.getNextSiblingElement(e)) != null);
for (Map.Entry<String, Integer> entry : propsCnt.entrySet()) {
if (entry.getValue() == 0)
throw new PropertyDataGenerationException(prop, String.format("no %s for input", entry.getKey()));
}
// Objects, except the one containing the qualifying properties.
for (int i = 0; i < ctx.getTargetXmlSignature().getObjectLength(); i++) {
ObjectContainer obj = ctx.getTargetXmlSignature().getObjectItem(i);
if (null == DOMHelper.getFirstDescendant(obj.getElement(), QualifyingProperty.XADES_XMLNS, "*"))
digestInput.addNode(obj.getElement());
}
} catch (CannotAddDataToDigestInputException ex) {
throw new PropertyDataGenerationException(prop, "cannot create time stamp input", ex);
}
}
use of org.apache.xml.security.signature.Reference 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 org.apache.xml.security.signature.Reference in project xades4j by luisgoncalves.
the class SignedDataObjectsProcessorTest method testAddNullReference.
@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"));
}
Aggregations