Java Реализация протокола отметки времени - PullRequest
3 голосов
/ 05 декабря 2011

В настоящее время мы используем openssl для отметки времени.Интересно, есть ли в Java реализация протокола отметок времени (RFC 3161).

Ответы [ 2 ]

5 голосов
/ 06 сентября 2013

В приведенном ниже примере у нас есть отметка времени запроса с использованием библиотек BouncyCastle 1.49. Надеюсь, это поможет.

package br.gov.frameworkdemoiselle;

import java.io.*;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.tsp.TimeStampResp;
import org.bouncycastle.tsp.TSPAlgorithms;
import org.bouncycastle.tsp.TimeStampRequest;
import org.bouncycastle.tsp.TimeStampRequestGenerator;
import org.bouncycastle.tsp.TimeStampResponse;

public class Example {

private final static Logger logger = Logger.getLogger(Example.class.getName());

public static void main(String[] args) {
    new Example().stamp();
}

private void stamp() {

    String ocspUrl = "http://timestamping.edelweb.fr/service/tsp";
    OutputStream out = null;
    HttpURLConnection con = null;

    try {

        TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
        timeStampRequestGenerator.setReqPolicy(new ASN1ObjectIdentifier("1.3.6.1.4.1.13762.3"));
        TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, new byte[20], BigInteger.valueOf(100));
        byte request[] = timeStampRequest.getEncoded();

        URL url = new URL(ocspUrl);
        con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type", "application/timestamp-query");
        con.setRequestProperty("Content-length", String.valueOf(request.length));
        out = con.getOutputStream();
        out.write(request);
        out.flush();

        if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
        } else {
            logger.log(Level.INFO, "Response Code: ".concat(Integer.toString(con.getResponseCode())));
        }
        InputStream in = con.getInputStream();
        TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
        TimeStampResponse response = new TimeStampResponse(resp);
        response.validate(timeStampRequest);

        logger.log(Level.INFO, "Status = {0}", response.getStatusString());

        if (response.getFailInfo()
                != null) {

            switch (response.getFailInfo().intValue()) {
                case 0: {
                    logger.log(Level.INFO, "unrecognized or unsupported Algorithm Identifier");
                    return;
                }

                case 2: {
                    logger.log(Level.INFO, "transaction not permitted or supported");
                    return;
                }

                case 5: {
                    logger.log(Level.INFO, "the data submitted has the wrong format");
                    return;
                }

                case 14: {
                    logger.log(Level.INFO, "the TSA’s time source is not available");
                    return;
                }

                case 15: {
                    logger.log(Level.INFO, "the requested TSA policy is not supported by the TSA");
                    return;
                }
                case 16: {
                    logger.log(Level.INFO, "the requested extension is not supported by the TSA");
                    return;
                }

                case 17: {
                    logger.log(Level.INFO, "the additional information requested could not be understood or is not available");
                    return;
                }

                case 25: {
                    logger.log(Level.INFO, "the request cannot be handled due to system failure");
                    return;
                }
            }
        }

        logger.log(Level.INFO, "Timestamp: {0}", response.getTimeStampToken().getTimeStampInfo().getGenTime());
        logger.log(Level.INFO, "TSA: {0}", response.getTimeStampToken().getTimeStampInfo().getTsa());
        logger.log(Level.INFO, "Serial number: {0}", response.getTimeStampToken().getTimeStampInfo().getSerialNumber());
        logger.log(Level.INFO, "Policy: {0}", response.getTimeStampToken().getTimeStampInfo().getPolicy());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
4 голосов
/ 05 декабря 2011
...