use of org.bouncycastle.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 org.bouncycastle.asn1.tsp.MessageImprint in project pdfbox by apache.
the class TSAClient method getTimeStampToken.
/**
* @param messageImprint imprint of message contents
* @return the encoded time stamp token
* @throws IOException if there was an error with the connection or data from the TSA server,
* or if the time stamp response could not be validated
*/
public byte[] getTimeStampToken(byte[] messageImprint) throws IOException {
digest.reset();
byte[] hash = digest.digest(messageImprint);
// 32-bit cryptographic nonce
SecureRandom random = new SecureRandom();
int nonce = random.nextInt();
// generate TSA request
TimeStampRequestGenerator tsaGenerator = new TimeStampRequestGenerator();
tsaGenerator.setCertReq(true);
ASN1ObjectIdentifier oid = getHashObjectIdentifier(digest.getAlgorithm());
TimeStampRequest request = tsaGenerator.generate(oid, hash, BigInteger.valueOf(nonce));
// get TSA response
byte[] tsaResponse = getTSAResponse(request.getEncoded());
TimeStampResponse response;
try {
response = new TimeStampResponse(tsaResponse);
response.validate(request);
} catch (TSPException e) {
throw new IOException(e);
}
TimeStampToken token = response.getTimeStampToken();
if (token == null) {
throw new IOException("Response does not have a time stamp token");
}
return token.getEncoded();
}
use of org.bouncycastle.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 org.bouncycastle.asn1.tsp.MessageImprint in project Insights by CognizantOneDevOps.
the class TimeStampingAuthorityClient method getTimeStampToken.
/**
* @param messageImprint imprint of message contents
* @return the encoded time stamp token
* @throws IOException if there was an error with the connection or data from the TSA server,
* or if the time stamp response could not be validated
*/
public byte[] getTimeStampToken(byte[] messageImprint) throws IOException {
digest.reset();
SecureRandom saltRandom = new SecureRandom();
byte[] salt = new byte[16];
saltRandom.nextBytes(salt);
LOG.info("salt updated to digest");
digest.update(salt);
byte[] hash = digest.digest(messageImprint);
// 32-bit cryptographic nonce
SecureRandom random = new SecureRandom();
int nonce = random.nextInt();
// generate TSA request
TimeStampRequestGenerator tsaGenerator = new TimeStampRequestGenerator();
tsaGenerator.setCertReq(true);
ASN1ObjectIdentifier oid = getHashObjectIdentifier(digest.getAlgorithm());
TimeStampRequest request = tsaGenerator.generate(oid, hash, BigInteger.valueOf(nonce));
// get TSA response
byte[] tsaResponse = getTSAResponse(request.getEncoded());
TimeStampResponse response;
try {
response = new TimeStampResponse(tsaResponse);
response.validate(request);
} catch (TSPException e) {
throw new IOException(e);
}
TimeStampToken token = response.getTimeStampToken();
if (token == null) {
throw new IOException("Response does not have a time stamp token");
}
return token.getEncoded();
}
use of org.bouncycastle.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);
}
Aggregations