2

署名にタイムスタンプを付ける際に使用されるプロセスと Java API を理解するのを手伝ってくれませんか。

ファイルに署名し、Java API を使用して TSA URL " http://timestamp.globalsign.com/scripts/timstamp.dll " を使用してタイムスタンプを付ける必要があります。

java.security API を使用してファイルに署名することはできますが、タイムスタンプを付けることができません。

4

1 に答える 1

7

あなたの質問は少し広いです...正しい方向に向けられることを願って、いくつかの情報を提供します。

問題は、タイムスタンプ サービスを使用して、そこにあるサービスを使用してタイムスタンプ署名を実行することですhttp://timestamp.globalsign.com/scripts/timstamp.dll

まず、このサービスはコンパイル済みです。ここで RFC の定義Time-Stamp Protocol (TSP) RFC3161を見て、これがどのように機能するかを明確に理解してください。

いずれにせよ、あなたは Java コードの例を探していると思うので、RFC3161 のタイムスタンプ サーバーを使用してタイムスタンプ署名を実行するサンプル コードを以下に示します。

基本的に、このサンプルの手順は次のとおりです。

  1. 最初にタイムスタンプ リクエストを作成し、次にリクエストをサービスに送信して、最後にレスポンスを読み取ります。

    タイムスタンプ リクエストには、次の定義があります。

    TimeStampReq ::= SEQUENCE  {
       version                      INTEGER  { v1(1) },
       messageImprint               MessageImprint,
       --a hash algorithm OID and the hash value of the data to be time-stamped
       reqPolicy             TSAPolicyId              OPTIONAL,
       nonce                 INTEGER                  OPTIONAL,
       certReq               BOOLEAN                  DEFAULT FALSE,
       extensions            [0] IMPLICIT Extensions  OPTIONAL  }
    

    必須のものだけを見ることができるmessageImprintので、残りはオプションであり、tsp サービスが提供するオプションに依存します。

  2. 2 番目のステップは、 http-header: POSTとして指定するメソッドを使用して、このタイムスタンプ リクエストを送信することです。Content-typeapplication/timestamp-query

  3. 最後の部分は、応答を解析してタイムスタンプ トークンを取得することです。

コードは次のとおりです。

すべて一緒に:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;
import java.util.Random;

import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1StreamParser;
import org.bouncycastle.asn1.DERBoolean;
import org.bouncycastle.asn1.DERInteger;
import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.asn1.tsp.MessageImprint;
import org.bouncycastle.asn1.tsp.TimeStampReq;
import org.bouncycastle.asn1.tsp.TimeStampResp;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.tsp.TimeStampResponse;
import org.bouncycastle.tsp.TimeStampToken;

public class TimeStampGenerationSample {

    public static void main(String args[]) throws Exception{

        // for this sample we will use SHA1 to perform the hashes
        // however feel free to use another algorithm since sha1 is weakness
        String sha1Oid = "1.3.14.3.2.26";
        // data to be timestamped
        byte[] data = "some sample data... or your signature...".getBytes();

        // perform the hash of your data
        byte[] digestData = MessageDigest.getInstance(sha1Oid, new BouncyCastleProvider()).digest(data);
        // generate random data to perform your ts, it's optional depends on your ts service
        Random rand = new Random(new Date().getTime()); 
        String nonce = BigInteger.valueOf(rand.nextLong()).toString();          
        // require cert optional (default false... so use false)
        boolean requireCert = false;
        // timestampPolicy it's an oid to identify a policy, if it's required
        // must be provided by your ts service... it's optional so we put null
        String timestampPolicy = null;      

        TimeStampReq ts_req = createTimeStampRequest(digestData, nonce, requireCert, sha1Oid, timestampPolicy);

        // the data to be send to the service
        byte[] dataToSend = ts_req.getEncoded();

        // simply send your data using POST method
        // don't forget to specify http-header content-type as "application/timestamp-query"
        byte[] response = // send the request as you want
        // parse the response 
        ASN1StreamParser asn1Sp = new ASN1StreamParser(response);
        TimeStampResp tspResp = new TimeStampResp((ASN1Sequence)asn1Sp.readObject());
        TimeStampResponse tsr = new TimeStampResponse(tspResp);
        // and get the timestamp token :)
        TimeStampToken token = tsr.getTimeStampToken();
    }

    /**
     * Create the timestamp request
     * @param hashedData
     * @param nonce
     * @param requireCert
     * @param digestAlgorithm
     * @param timestampPolicy
     * @return
     * @throws TimeStampGenerationException
     */
    public static TimeStampReq createTimeStampRequest(byte[] hashedData, String nonce, boolean requireCert, String digestAlgorithm, String timestampPolicy) throws TimeStampGenerationException {

        MessageImprint imprint = new MessageImprint(new AlgorithmIdentifier(digestAlgorithm), hashedData);

        TimeStampReq request = new TimeStampReq(
                imprint, 
                timestampPolicy!=null?new DERObjectIdentifier(timestampPolicy):null, 
                nonce!=null?new DERInteger(nonce.getBytes()):null, 
                new DERBoolean(requireCert), 
                null
        );      

        return request;
    }
}

bouncycastle APIサンプルで使用していることに注意してください。

お役に立てれば、

于 2015-01-23T14:07:14.213 に答える