git-svn-id: http://locode01.ad.dom/svn/WEBMIP/trunk@50874 248e525c-4dfb-0310-94bc-949c084e9493

This commit is contained in:
andrew.gilmore
2012-03-19 11:57:19 +00:00
parent 2a0f4900c3
commit 0e9ca75d77
1587 changed files with 500863 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
/*
* TimeStamp.java
*
* Created on 2007. March 26
*
* Adapted from code supplied by MAV Informatika by Advantica Ltd.
*/
package tsdemo;
import org.bouncycastle.tsp.TimeStampRequestGenerator;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.contrib.ssl.AuthSSLProtocolSocketFactory;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.bouncycastle.tsp.*;
import java.security.MessageDigest;
import java.net.URL;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
/**
*
* @author root
*/
public class TimeStamp {
private String KeystoreFile;
private String KeystorePassword;
private String URL;
private String time;
private boolean l_return;
//
public static byte[] TSRespByte;
//
private final TimeStampRequestGenerator TSReqGen;
/** Creates a new instance of TimeStamp */
public TimeStamp() {
TSReqGen = new TimeStampRequestGenerator();
TSReqGen.setCertReq(true);
}
public void setKeystoreFile(String KeystoreFile) {
this.KeystoreFile = KeystoreFile;
//System.out.println("genTS KeystoreFile " + this.KeystoreFile);
}
public void setKeystorePassword(String KeystorePassword) {
this.KeystorePassword = KeystorePassword;
//System.out.println("genTS KeystorePassword " + this.KeystorePassword);
}
public void setURL(String URL) {
this.URL = URL;
//System.out.println("genTS URL " + this.URL);
}
//
public static byte[] getResp() {
//
return TSRespByte;
}
//
public String generateTimestamp(byte[] hash,int portNumber) throws TSException { // Timestamp creation
//
// Code to produce a String java timestamp from a TSA source.
//
System.out.println("Entered generateTimestamp with: " + hash + " port: " + portNumber);
try {
TimeStampRequest TSReq = TSReqGen.generate("1.3.14.3.2.26", hash); // Setting hash algorithm to SHA-1, creating the TimeStamp Rrequest
//
//System.out.println("Got here 1");
ProtocolSocketFactory authfact = new AuthSSLProtocolSocketFactory( // This Factory helps httpclien to handle https with client side certificate authentication
new URL("file:" + KeystoreFile), KeystorePassword, // keystore that stores the client certificate
new URL("file:" + KeystoreFile), KeystorePassword); // keystore that stores the Root certificate of the server
//
//System.out.println("Got here 2");
Protocol authhttps = new Protocol("https", authfact, 443); // registering SSL/TLS handler for https protocol, default port 443
Protocol.registerProtocol("https", authhttps);
//
//System.out.println("Got here 3");
PostMethod post = new PostMethod(URL); // HTTP POST initialization, configuration
//
post.setRequestHeader("Content-type", "application/timestamp-query"); // We send a timestamp request
post.setRequestHeader("Accept", "application/timestamp-reply"); // and want a timestamp reply in exchange
//
//System.out.println("Got here 4");
post.setRequestEntity(new ByteArrayRequestEntity(TSReq.getEncoded())); // Loading the Timestamp request into the POST method
post.getParams().setParameter("http.socket.timeout", new Integer(60000)); // in milliseconds (1 min)
//
new HttpClient().executeMethod(post); // executing the HTTP POST operation, actual network traffic happens here
//System.out.println("Got here 5");
//
if (post.getStatusCode() != HttpStatus.SC_OK) // If we dont get a normal response, throw an exception
throw new TSException("unexpected status code: " + post.getStatusCode());
//
InputStream in = post.getResponseBodyAsStream(); // transfer the HTTP reply into a managable byte[]
//System.out.println("Got here 6");
//
// New code from Verify routine to produce the timestamp from the InoutStream.
//
TimeStampResponse TSResp = new TimeStampResponse(in); // Generate a TimeStampResponse object from the raw data
//System.out.println("Got here 7");
TimeStampTokenInfo tsinfo = TSResp.getTimeStampToken().getTimeStampInfo(); // Get the valuable data from the timestamp
//System.out.println("Got here 8");
time = tsinfo.getGenTime().toString(); // Translate Timestamp date into a readable form
//
// Get the byte[] response from the TSResp object so we can return it to amTimestamp.
//
TSRespByte = TSResp.getEncoded();
in.close();
return time; // return the timestamp response if no exception occured
} catch (Exception e) {
e.printStackTrace();
throw new TSException(e.getMessage()); // we got an exception, throw it up
}
}
public boolean verifyTimeStamp(byte[] hash, String Algorithm, InputStream tsdata) throws TSException { // Basic TimeStamp verification
try {
System.out.println("Verify hash is : " + hash + " Algorithm " + Algorithm + " Data: " + tsdata.toString());
TimeStampResponse TSResp = new TimeStampResponse(tsdata); // Generate a TimeStampResponse object from the raw data
TimeStampTokenInfo tsinfo = TSResp.getTimeStampToken().getTimeStampInfo(); // Get the valuable data from the timestamp
System.out.println("Verify 2");
time = tsinfo.getGenTime().toString(); // Translate Timestamp date into a readable form
// Optimistic!
System.out.println("Time: " + time);
//
MessageDigest d = MessageDigest.getInstance(Algorithm); // Get an SHA-1 handler
return d.isEqual(hash, tsinfo.getMessageImprintDigest()); // Return if the provided hash and the hash in the Timestamp is identical
} catch (Exception e) {
e.printStackTrace();
throw new TSException(e.getMessage()); // some expection occured, throw up
}
}
public String getTime() {
return time;
}
}