Search in sources :

Example 1 with MessageImprint

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;
}
Also used : MessageImprint(org.bouncycastle.asn1.tsp.MessageImprint) TimeStampTokenInfo(org.bouncycastle.tsp.TimeStampTokenInfo) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1String(org.bouncycastle.asn1.ASN1String)

Example 2 with MessageImprint

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();
}
Also used : TimeStampResponse(org.bouncycastle.tsp.TimeStampResponse) SecureRandom(java.security.SecureRandom) TimeStampRequestGenerator(org.bouncycastle.tsp.TimeStampRequestGenerator) TSPException(org.bouncycastle.tsp.TSPException) IOException(java.io.IOException) TimeStampToken(org.bouncycastle.tsp.TimeStampToken) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) TimeStampRequest(org.bouncycastle.tsp.TimeStampRequest)

Example 3 with MessageImprint

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));
    }
}
Also used : TimeStampReq(com.github.zhenwei.pkix.util.asn1.tsp.TimeStampReq) MessageImprint(com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) Extensions(com.github.zhenwei.core.asn1.x509.Extensions) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 4 with MessageImprint

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();
}
Also used : TimeStampResponse(org.bouncycastle.tsp.TimeStampResponse) SecureRandom(java.security.SecureRandom) TimeStampRequestGenerator(org.bouncycastle.tsp.TimeStampRequestGenerator) TSPException(org.bouncycastle.tsp.TSPException) IOException(java.io.IOException) TimeStampToken(org.bouncycastle.tsp.TimeStampToken) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) TimeStampRequest(org.bouncycastle.tsp.TimeStampRequest)

Example 5 with MessageImprint

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);
}
Also used : MessageImprint(org.bouncycastle.asn1.tsp.MessageImprint) TimeStampTokenInfo(org.bouncycastle.tsp.TimeStampTokenInfo) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1String(org.bouncycastle.asn1.ASN1String)

Aggregations

IOException (java.io.IOException)4 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)3 Extensions (com.github.zhenwei.core.asn1.x509.Extensions)3 MessageImprint (com.github.zhenwei.pkix.util.asn1.tsp.MessageImprint)3 SecureRandom (java.security.SecureRandom)3 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)3 TimeStampRequest (org.bouncycastle.tsp.TimeStampRequest)3 TimeStampRequestGenerator (org.bouncycastle.tsp.TimeStampRequestGenerator)3 TimeStampResponse (org.bouncycastle.tsp.TimeStampResponse)3 TimeStampToken (org.bouncycastle.tsp.TimeStampToken)3 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 TimeStampReq (com.github.zhenwei.pkix.util.asn1.tsp.TimeStampReq)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2 ASN1String (org.bouncycastle.asn1.ASN1String)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 MessageImprint (org.bouncycastle.asn1.tsp.MessageImprint)2 TSPException (org.bouncycastle.tsp.TSPException)2 TimeStampTokenInfo (org.bouncycastle.tsp.TimeStampTokenInfo)2 ASN1Boolean (com.github.zhenwei.core.asn1.ASN1Boolean)1