【问题标题】:Java Implementation of time stamp protocol时间戳协议的Java实现
【发布时间】:2011-12-05 17:56:44
【问题描述】:

我们目前使用 openssl 进行时间戳。我想知道,是否有时间戳协议 (RFC 3161) 的 Java 实现。

【问题讨论】:

    标签: java trusted-timestamp rfc3161


    【解决方案1】:

    Bouncy Castle 和它的TSP support 呢?

    【讨论】:

    • 这个api有没有示例用法。
    • @ayengin:不知道,我只是在谷歌上搜索了一下。但是,如果您找不到任何东西,请随时提出一个新问题。
    • ,我应该打开一个问题“如何用谷歌搜索”:)) 我用谷歌搜索了一整天,但没有找到。谢谢...
    【解决方案2】:

    在下面的示例中,我们有一个使用 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();
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多