Search in sources :

Example 1 with CannotAddDataToDigestInputException

use of xades4j.utils.CannotAddDataToDigestInputException in project xades4j by luisgoncalves.

the class DataGenBaseTimeStamp method generatePropertyData.

@Override
public final PropertyDataObject generatePropertyData(TProp prop, PropertiesDataGenerationContext ctx) throws PropertyDataGenerationException {
    Algorithm c14n = this.algsProvider.getCanonicalizationAlgorithmForTimeStampProperties();
    try {
        TimeStampDigestInput digestInput = this.tsInputFactory.newTimeStampDigestInput(c14n);
        addPropSpecificTimeStampInput(prop, digestInput, ctx);
        TimeStampTokenRes tsTknRes = this.tsTokenProvider.getTimeStampToken(digestInput.getBytes(), this.algsProvider.getDigestAlgorithmForTimeStampProperties());
        return createPropDataObj(prop, c14n, tsTknRes, ctx);
    } catch (UnsupportedAlgorithmException ex) {
        throw new PropertyDataGenerationException(prop, ex.getMessage(), ex);
    } catch (CannotAddDataToDigestInputException ex) {
        throw new PropertyDataGenerationException(prop, "cannot create time stamp input", ex);
    } catch (TimeStampTokenGenerationException ex) {
        throw new PropertyDataGenerationException(prop, "cannot get a time-stamp", ex);
    }
}
Also used : CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) TimeStampDigestInput(xades4j.utils.TimeStampDigestInput) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) TimeStampTokenRes(xades4j.providers.TimeStampTokenProvider.TimeStampTokenRes) Algorithm(xades4j.algorithms.Algorithm) TimeStampTokenGenerationException(xades4j.providers.TimeStampTokenGenerationException)

Example 2 with CannotAddDataToDigestInputException

use of xades4j.utils.CannotAddDataToDigestInputException in project xades4j by luisgoncalves.

the class TimeStampVerifierBase method verify.

@Override
public final QualifyingProperty verify(TData propData, QualifyingPropertyVerificationContext ctx) throws InvalidPropertyException {
    try {
        TimeStampDigestInput digestInput = this.tsInputFactory.newTimeStampDigestInput(propData.getCanonicalizationAlgorithm());
        QualifyingProperty prop = addPropSpecificTimeStampInputAndCreateProperty(propData, digestInput, ctx);
        byte[] data = digestInput.getBytes();
        /**
         * Verify the time-stamp tokens on a time-stamp property data object. All
         * the tokens are verified, but the returned time-stamp is from the last token.
         */
        List<byte[]> tokens = propData.getTimeStampTokens();
        Date ts = null;
        for (byte[] tkn : tokens) {
            ts = this.tsVerifier.verifyToken(tkn, data);
        }
        // By convention all timestamp property types have a setTime(Date) method
        Method setTimeMethod = prop.getClass().getMethod("setTime", Date.class);
        setTimeMethod.invoke(prop, ts);
        return prop;
    } catch (UnsupportedAlgorithmException ex) {
        throw getEx(ex, this.propName);
    } catch (CannotAddDataToDigestInputException ex) {
        throw new TimeStampDigestInputException(this.propName, ex);
    } catch (TimeStampTokenVerificationException ex) {
        throw getEx(ex, this.propName);
    } catch (Exception ex) {
        // Exceptions related to setTimeMethod.invoke(...)
        throw getEx(ex, this.propName);
    }
}
Also used : CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) TimeStampDigestInput(xades4j.utils.TimeStampDigestInput) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) QualifyingProperty(xades4j.properties.QualifyingProperty) Method(java.lang.reflect.Method) TimeStampTokenVerificationException(xades4j.providers.TimeStampTokenVerificationException) Date(java.util.Date) CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) TimeStampTokenStructureException(xades4j.providers.TimeStampTokenStructureException) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) TimeStampTokenDigestException(xades4j.providers.TimeStampTokenDigestException) TimeStampTokenVerificationException(xades4j.providers.TimeStampTokenVerificationException) TimeStampTokenSignatureException(xades4j.providers.TimeStampTokenSignatureException)

Example 3 with CannotAddDataToDigestInputException

use of xades4j.utils.CannotAddDataToDigestInputException in project xades4j by luisgoncalves.

the class DataGenSigAndRefsTimeStamp method addPropSpecificTimeStampInput.

@Override
protected void addPropSpecificTimeStampInput(SigAndRefsTimeStampProperty 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");
    /**
     * This property contains a time-stamp token that covers the following data
     * objects: {@code ds:SignatureValue} element, all present {@code SignatureTimeStamp}
     * elements, {@code CompleteCertificateRefs}, {@code CompleteRevocationRefs}, and
     * when present, {@code AttributeCertificateRefs} and {@code AttributeRevocationRefs}.
     *
     * "Those (...) that appear before SigAndRefsTimeStamp, in their order of
     * appearance within the UnsignedSignatureProperties element."
     */
    Map<String, Integer> elegiblePropsCnt = new HashMap<String, Integer>(5);
    elegiblePropsCnt.put(CompleteCertificateRefsProperty.PROP_NAME, 0);
    elegiblePropsCnt.put(CompleteRevocationRefsProperty.PROP_NAME, 0);
    elegiblePropsCnt.put(SignatureTimeStampProperty.PROP_NAME, 0);
    elegiblePropsCnt.put("AttributeCertificateRefs", 0);
    elegiblePropsCnt.put("AttributeRevocationRefs", 0);
    try {
        // SignatureValue.
        Element e = DOMHelper.getFirstDescendant(ctx.getTargetXmlSignature().getElement(), Constants.SignatureSpecNS, Constants._TAG_SIGNATUREVALUE);
        digestInput.addNode(e);
        e = DOMHelper.getFirstChildElement(unsignedSigPropsElem);
        // UnsignedProperties shouldn't be empty!
        do {
            Integer pCnt = elegiblePropsCnt.get(e.getLocalName());
            if (pCnt != null) {
                elegiblePropsCnt.put(e.getLocalName(), pCnt += 1);
                digestInput.addNode(e);
            }
        } while ((e = DOMHelper.getNextSiblingElement(e)) != null);
        // SignatureTimeStamp has to be present.
        if (elegiblePropsCnt.get(SignatureTimeStampProperty.PROP_NAME) == 0)
            throw new PropertyDataGenerationException(prop, "no signature time-stamps for input");
        // CompleteCertificateRefs has to be present.
        if (elegiblePropsCnt.get(CompleteCertificateRefsProperty.PROP_NAME) != 1)
            throw new PropertyDataGenerationException(prop, "no CompleteCertificateRefs for input");
        // CompleteRevocationRefs has to be present.
        if (elegiblePropsCnt.get(CompleteRevocationRefsProperty.PROP_NAME) != 1)
            throw new PropertyDataGenerationException(prop, "no CompleteRevocationRefs for input");
    } catch (CannotAddDataToDigestInputException ex) {
        throw new PropertyDataGenerationException(prop, "cannot create timestamp input", ex);
    }
}
Also used : CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) HashMap(java.util.HashMap) Element(org.w3c.dom.Element)

Example 4 with CannotAddDataToDigestInputException

use of xades4j.utils.CannotAddDataToDigestInputException 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)

Aggregations

CannotAddDataToDigestInputException (xades4j.utils.CannotAddDataToDigestInputException)4 HashMap (java.util.HashMap)2 Element (org.w3c.dom.Element)2 UnsupportedAlgorithmException (xades4j.UnsupportedAlgorithmException)2 TimeStampDigestInput (xades4j.utils.TimeStampDigestInput)2 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 Map (java.util.Map)1 KeyInfo (org.apache.xml.security.keys.KeyInfo)1 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)1 Reference (org.apache.xml.security.signature.Reference)1 Algorithm (xades4j.algorithms.Algorithm)1 QualifyingProperty (xades4j.properties.QualifyingProperty)1 TimeStampTokenDigestException (xades4j.providers.TimeStampTokenDigestException)1 TimeStampTokenGenerationException (xades4j.providers.TimeStampTokenGenerationException)1 TimeStampTokenRes (xades4j.providers.TimeStampTokenProvider.TimeStampTokenRes)1 TimeStampTokenSignatureException (xades4j.providers.TimeStampTokenSignatureException)1 TimeStampTokenStructureException (xades4j.providers.TimeStampTokenStructureException)1 TimeStampTokenVerificationException (xades4j.providers.TimeStampTokenVerificationException)1