use of com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint in project itext2 by albfernandez.
the class PdfPKCS7 method verifyTimestampImprint.
/**
* Checks if the timestamp refers to this document.
* @throws java.security.NoSuchAlgorithmException on error
* @return true if it checks false otherwise
* @since 2.1.6
*/
public boolean verifyTimestampImprint() throws NoSuchAlgorithmException {
if (timeStampToken == null)
return false;
MessageImprint imprint = timeStampToken.getTimeStampInfo().toASN1Structure().getMessageImprint();
TimeStampTokenInfo info = timeStampToken.getTimeStampInfo();
String algOID = info.getMessageImprintAlgOID().getId();
byte[] md = MessageDigest.getInstance(getStandardJavaName(getDigest(algOID))).digest(digest);
byte[] imphashed = imprint.getHashedMessage();
boolean res = Arrays.equals(md, imphashed);
return res;
}
use of com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint in project LinLong-Java by zhenwei1108.
the class TSPUtil method getSignatureTimestamps.
/**
* Fetches the signature time-stamp attributes from a SignerInformation object. Checks that the
* MessageImprint for each time-stamp matches the signature field. (see RFC 3161 Appendix A).
*
* @param signerInfo a SignerInformation to search for time-stamps
* @param digCalcProvider provider for digest calculators
* @return a collection of TimeStampToken objects
* @throws TSPValidationException
*/
public static Collection getSignatureTimestamps(SignerInformation signerInfo, DigestCalculatorProvider digCalcProvider) throws TSPValidationException {
List timestamps = new ArrayList();
AttributeTable unsignedAttrs = signerInfo.getUnsignedAttributes();
if (unsignedAttrs != null) {
ASN1EncodableVector allTSAttrs = unsignedAttrs.getAll(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
for (int i = 0; i < allTSAttrs.size(); ++i) {
Attribute tsAttr = (Attribute) allTSAttrs.get(i);
ASN1Set tsAttrValues = tsAttr.getAttrValues();
for (int j = 0; j < tsAttrValues.size(); ++j) {
try {
ContentInfo contentInfo = ContentInfo.getInstance(tsAttrValues.getObjectAt(j));
TimeStampToken timeStampToken = new TimeStampToken(contentInfo);
TimeStampTokenInfo tstInfo = timeStampToken.getTimeStampInfo();
DigestCalculator digCalc = digCalcProvider.get(tstInfo.getHashAlgorithm());
OutputStream dOut = digCalc.getOutputStream();
dOut.write(signerInfo.getSignature());
dOut.close();
byte[] expectedDigest = digCalc.getDigest();
if (!Arrays.constantTimeAreEqual(expectedDigest, tstInfo.getMessageImprintDigest())) {
throw new TSPValidationException("Incorrect digest in message imprint");
}
timestamps.add(timeStampToken);
} catch (OperatorCreationException e) {
throw new TSPValidationException("Unknown hash algorithm specified in timestamp");
} catch (Exception e) {
throw new TSPValidationException("Timestamp could not be parsed");
}
}
}
}
return timestamps;
}
use of com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint in project LinLong-Java by zhenwei1108.
the class TimeStampRequestGenerator method generate.
/**
* @deprecated use method taking ANS1ObjectIdentifier
*/
public TimeStampRequest generate(String digestAlgorithmOID, byte[] digest, BigInteger nonce) {
if (digestAlgorithmOID == null) {
throw new IllegalArgumentException("No digest algorithm specified");
}
ASN1ObjectIdentifier digestAlgOID = new ASN1ObjectIdentifier(digestAlgorithmOID);
AlgorithmIdentifier algID = dgstAlgFinder.find(digestAlgOID);
MessageImprint messageImprint = new MessageImprint(algID, digest);
Extensions ext = null;
if (!extGenerator.isEmpty()) {
ext = extGenerator.generate();
}
if (nonce != null) {
return new TimeStampRequest(new TimeStampReq(messageImprint, reqPolicy, new ASN1Integer(nonce), certReq, ext));
} else {
return new TimeStampRequest(new TimeStampReq(messageImprint, reqPolicy, null, certReq, ext));
}
}
use of com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint in project OpenPDF by LibrePDF.
the class PdfPKCS7 method verifyTimestampImprint.
/**
* Checks if the timestamp refers to this document.
*
* @return true if it checks false otherwise
* @throws java.security.NoSuchAlgorithmException on error
* @since 2.1.6
*/
public boolean verifyTimestampImprint() throws NoSuchAlgorithmException {
if (timeStampToken == null)
return false;
MessageImprint imprint = timeStampToken.getTimeStampInfo().toASN1Structure().getMessageImprint();
TimeStampTokenInfo info = timeStampToken.getTimeStampInfo();
String algOID = info.getMessageImprintAlgOID().getId();
byte[] md = MessageDigest.getInstance(getStandardJavaName(getDigest(algOID))).digest(digest);
byte[] imphashed = imprint.getHashedMessage();
return Arrays.equals(md, imphashed);
}
use of com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint in project LinLong-Java by zhenwei1108.
the class TimeStampRequestGenerator method generate.
public TimeStampRequest generate(AlgorithmIdentifier digestAlgorithmID, byte[] digest, BigInteger nonce) {
if (digestAlgorithmID == null) {
throw new IllegalArgumentException("digest algorithm not specified");
}
MessageImprint messageImprint = new MessageImprint(digestAlgorithmID, digest);
Extensions ext = null;
if (!extGenerator.isEmpty()) {
ext = extGenerator.generate();
}
if (nonce != null) {
return new TimeStampRequest(new TimeStampReq(messageImprint, reqPolicy, new ASN1Integer(nonce), certReq, ext));
} else {
return new TimeStampRequest(new TimeStampReq(messageImprint, reqPolicy, null, certReq, ext));
}
}
Aggregations