001/** 002 * Copyright 2014 Florent Daigniere 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package net.jsign.timestamp; 018 019import java.io.IOException; 020import java.net.HttpURLConnection; 021 022import org.bouncycastle.asn1.ASN1InputStream; 023import org.bouncycastle.asn1.DERSet; 024import org.bouncycastle.asn1.cms.Attribute; 025import org.bouncycastle.asn1.tsp.TimeStampResp; 026import org.bouncycastle.cms.CMSSignedData; 027import org.bouncycastle.tsp.TimeStampRequest; 028import org.bouncycastle.tsp.TimeStampRequestGenerator; 029import org.bouncycastle.tsp.TimeStampResponse; 030 031import net.jsign.DigestAlgorithm; 032import net.jsign.asn1.authenticode.AuthenticodeObjectIdentifiers; 033 034/** 035 * RFC 3161 timestamping. 036 * 037 * @author Florent Daigniere 038 * @see <a href="https://www.ietf.org/rfc/rfc3161.txt">Internet X.509 Public Key Infrastructure Time-Stamp Protocol (TSP)</a> 039 * @since 1.3 040 */ 041public class RFC3161Timestamper extends Timestamper { 042 043 public RFC3161Timestamper() { 044 setURL("http://timestamp.sectigo.com"); 045 } 046 047 protected CMSSignedData timestamp(DigestAlgorithm algo, byte[] encryptedDigest) throws IOException, TimestampingException { 048 TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator(); 049 reqgen.setCertReq(true); 050 TimeStampRequest req = reqgen.generate(algo.oid, algo.getMessageDigest().digest(encryptedDigest)); 051 byte[] request = req.getEncoded(); 052 053 HttpURLConnection conn = (HttpURLConnection) tsaurl.openConnection(); 054 conn.setConnectTimeout(10000); 055 conn.setReadTimeout(10000); 056 conn.setDoOutput(true); 057 conn.setDoInput(true); 058 conn.setUseCaches(false); 059 conn.setRequestMethod("POST"); 060 conn.setRequestProperty("Content-type", "application/timestamp-query"); 061 conn.setRequestProperty("Content-length", String.valueOf(request.length)); 062 conn.setRequestProperty("Accept", "application/timestamp-reply"); 063 conn.setRequestProperty("User-Agent", "Transport"); 064 065 conn.getOutputStream().write(request); 066 conn.getOutputStream().flush(); 067 068 if (conn.getResponseCode() >= 400) { 069 throw new IOException("Unable to complete the timestamping due to HTTP error: " + conn.getResponseCode() + " - " + conn.getResponseMessage()); 070 } 071 072 try { 073 TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(conn.getInputStream()).readObject()); 074 TimeStampResponse response = new TimeStampResponse(resp); 075 response.validate(req); 076 if (response.getStatus() != 0) { 077 throw new IOException("Unable to complete the timestamping due to an invalid response (" + response.getStatusString() + ")"); 078 } 079 080 return response.getTimeStampToken().toCMSSignedData(); 081 082 } catch (Exception e) { 083 throw new TimestampingException("Unable to complete the timestamping", e); 084 } 085 } 086 087 @Override 088 protected Attribute getCounterSignature(CMSSignedData token) { 089 return new Attribute(AuthenticodeObjectIdentifiers.SPC_RFC3161_OBJID, new DERSet(token.toASN1Structure())); 090 } 091}