Skip to content

Commit

Permalink
Merge branch 'DEV-17' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pvandenbroucke committed Apr 8, 2015
2 parents b399304 + ba1c6a3 commit c406903
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import eu.europa.ec.markt.dss.DigestAlgorithm;
import eu.europa.ec.markt.dss.cookbook.example.Cookbook;
import eu.europa.ec.markt.dss.validation102853.tsp.OnlineTSPSource;
import eu.europa.ec.markt.dss.validation102853.tsp.TSPSource;

/**
* How to initialize online TSP source.
Expand All @@ -38,7 +37,7 @@ public class InitOnlineTSPSource extends Cookbook {
public static void main(String[] args) throws IOException {

final String tspServer = "http://services.globaltrustfinder.com/adss/tsa";
TSPSource tspSource = new OnlineTSPSource(tspServer);
OnlineTSPSource tspSource = new OnlineTSPSource(tspServer);
tspSource.setPolicyOid("1.2.3.4.5");

final DigestAlgorithm digestAlgorithm = DigestAlgorithm.SHA256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
package eu.europa.ec.markt.dss.validation102853.tsp;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.tsp.TSPException;
import org.bouncycastle.tsp.TimeStampRequest;
Expand All @@ -36,11 +32,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.europa.ec.markt.dss.DSSUtils;
import eu.europa.ec.markt.dss.DigestAlgorithm;
import eu.europa.ec.markt.dss.exception.DSSException;
import eu.europa.ec.markt.dss.validation102853.NonceSource;
import eu.europa.ec.markt.dss.validation102853.loader.DataLoader;
import eu.europa.ec.markt.dss.validation102853.loader.NativeHTTPDataLoader;

/**
* Class encompassing a RFC 3161 TSA, accessed through HTTP(S) to a given URI
Expand Down Expand Up @@ -100,22 +96,10 @@ public void setTspServer(final String tspServer) {
*
* @param policyOid
*/
@Override
public void setPolicyOid(final String policyOid) {
this.policyOid = new ASN1ObjectIdentifier(policyOid);
}

@Override
public String getUniqueId(final byte[] digestValue) {
byte[] digest;
if (nonceSource !=null){
digest = DSSUtils.digest(DigestAlgorithm.MD5, digestValue, nonceSource.getNonce().toByteArray());
} else {
digest = DSSUtils.digest(DigestAlgorithm.MD5, digestValue);
}
return Hex.encodeHexString(digest);
}

/**
* Set the DataLoader to use for querying the TSP server.
*
Expand Down Expand Up @@ -160,12 +144,10 @@ public TimeStampToken getTimeStampResponse(final DigestAlgorithm digestAlgorithm
final byte[] requestBytes = timeStampRequest.getEncoded();

// Call the communications layer
byte[] respBytes;
if (dataLoader != null) {
respBytes = dataLoader.post(tspServer, requestBytes);
} else {
respBytes = getTSAResponse(requestBytes);
if (dataLoader == null) {
dataLoader = new NativeHTTPDataLoader();
}
byte[] respBytes = dataLoader.post(tspServer, requestBytes);

// Handle the TSA response
final TimeStampResponse timeStampResponse = new TimeStampResponse(respBytes);
Expand All @@ -192,39 +174,4 @@ public TimeStampToken getTimeStampResponse(final DigestAlgorithm digestAlgorithm
}
}

/**
* Get timestamp token - communications layer
*
* @return - byte[] - TSA response, raw bytes (RFC 3161 encoded)
*/
protected byte[] getTSAResponse(final byte[] requestBytes) throws DSSException {

// Setup the TSA connection
final URLConnection tsaConnection = DSSUtils.openURLConnection(tspServer);

tsaConnection.setDoInput(true);
tsaConnection.setDoOutput(true);
tsaConnection.setUseCaches(false);
tsaConnection.setRequestProperty("Content-Type", "application/timestamp-query");
tsaConnection.setRequestProperty("Content-Transfer-Encoding", "binary");

DSSUtils.writeToURLConnection(tsaConnection, requestBytes);

// Get TSA response as a byte array
byte[] respBytes = getReadFromURLConnection(tsaConnection);
final String encoding = tsaConnection.getContentEncoding();
if ("base64".equalsIgnoreCase(encoding)) {
respBytes = Base64.decodeBase64(respBytes);
}
return respBytes;
}

private byte[] getReadFromURLConnection(final URLConnection tsaConnection) throws DSSException {
try {
final InputStream inputStream = tsaConnection.getInputStream();
return IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new DSSException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import eu.europa.ec.markt.dss.DSSUtils;
import eu.europa.ec.markt.dss.DigestAlgorithm;
import eu.europa.ec.markt.dss.validation102853.NonceSource;
import eu.europa.ec.markt.dss.validation102853.https.CommonsDataLoader;
import eu.europa.ec.markt.dss.validation102853.loader.NativeHTTPDataLoader;

public class OnlineTSPSourceTest {
Expand All @@ -23,6 +24,16 @@ public void testWithoutNonce() {
assertNotNull(timeStampResponse);
}

@Test
public void testWithCommonDataLoader() {
OnlineTSPSource tspSource = new OnlineTSPSource(TSA_URL);
tspSource.setDataLoader(new CommonsDataLoader());

byte[] digest = DSSUtils.digest(DigestAlgorithm.SHA1, "Hello world".getBytes());
TimeStampToken timeStampResponse = tspSource.getTimeStampResponse(DigestAlgorithm.SHA1, digest);
assertNotNull(timeStampResponse);
}

@Test
public void testWithNativeHTTPDataLoader() {
OnlineTSPSource tspSource = new OnlineTSPSource(TSA_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ public interface TSPSource extends Serializable {
* @return
* @throws DSSException
*/
public TimeStampToken getTimeStampResponse(final DigestAlgorithm digestAlgorithm, final byte[] digest) throws DSSException;
TimeStampToken getTimeStampResponse(final DigestAlgorithm digestAlgorithm, final byte[] digest) throws DSSException;

/**
* @param policyOid
*/
public void setPolicyOid(final String policyOid);

/**
* @return this method returns the unique id associated with the timestamp
* @param digestValue
*/
public String getUniqueId(byte[] digestValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Hashtable;
import java.util.Set;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cms.AttributeTable;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
Expand Down Expand Up @@ -89,8 +88,6 @@ public class MockTSPSource implements TSPSource {
/**
* The default constructor for MockTSPSource.
*/


public MockTSPSource(final DSSPrivateKeyEntry entry, final Date timestampDate) throws DSSException {
this.timestampDate = timestampDate;

Expand Down Expand Up @@ -170,19 +167,11 @@ public TimeStampToken getTimeStampResponse(final DigestAlgorithm digestAlgorithm
}
}

@Override
public void setPolicyOid(final String policyOid) {
this.policyOid = new ASN1ObjectIdentifier(policyOid);
}

@Override
public String getUniqueId(byte[] digestValue) {
final byte[] digest = DSSUtils.digest(DigestAlgorithm.MD5, digestValue, DSSUtils.toByteArray(timestampDate.getTime()));
return Hex.encodeHexString(digest);
}

private String getSignatureAlgorithm(DigestAlgorithm algorithm, byte[] digest) {

String signatureAlgorithm;
if (DigestAlgorithm.SHA1.equals(algorithm)) {
signatureAlgorithm = "SHA1withRSA";
Expand Down

0 comments on commit c406903

Please sign in to comment.