Search in sources :

Example 11 with Reference

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;
}
Also used : Reference(org.apache.xml.security.signature.Reference) CommitmentTypeData(xades4j.properties.data.CommitmentTypeData) DataObjectDesc(xades4j.properties.DataObjectDesc)

Example 12 with Reference

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);
    }
}
Also used : Reference(org.apache.xml.security.signature.Reference) DataObjectDesc(xades4j.properties.DataObjectDesc)

Example 13 with Reference

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);
    }
}
Also used : CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) HashMap(java.util.HashMap) Reference(org.apache.xml.security.signature.Reference) Element(org.w3c.dom.Element) KeyInfo(org.apache.xml.security.keys.KeyInfo) HashMap(java.util.HashMap) Map(java.util.Map) ObjectContainer(org.apache.xml.security.signature.ObjectContainer)

Example 14 with Reference

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()));
}
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 15 with Reference

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

Aggregations

Reference (org.apache.xml.security.signature.Reference)16 Element (org.w3c.dom.Element)7 DataObjectDesc (xades4j.properties.DataObjectDesc)7 SignedInfo (org.apache.xml.security.signature.SignedInfo)6 XMLSignature (org.apache.xml.security.signature.XMLSignature)6 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)5 Document (org.w3c.dom.Document)5 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)4 ArrayList (java.util.ArrayList)3 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)3 Transforms (org.apache.xml.security.transforms.Transforms)3 XAdES4jXMLSigException (xades4j.XAdES4jXMLSigException)3 PublicKey (java.security.PublicKey)2 X509Certificate (java.security.cert.X509Certificate)2 KeyInfo (org.apache.xml.security.keys.KeyInfo)2 ReferenceData (org.apache.xml.security.signature.reference.ReferenceData)2 ReferenceNodeSetData (org.apache.xml.security.signature.reference.ReferenceNodeSetData)2 ResourceResolver (org.apache.xml.security.utils.resolver.ResourceResolver)2 ResolverAnonymous (org.apache.xml.security.utils.resolver.implementations.ResolverAnonymous)2 Test (org.junit.Test)2