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