use of xades4j.providers.TimeStampTokenGenerationException in project xades4j by luisgoncalves.
the class DataGenBaseTimeStamp method generatePropertyData.
@Override
public final PropertyDataObject generatePropertyData(TProp prop, PropertiesDataGenerationContext ctx) throws PropertyDataGenerationException {
Algorithm c14n = this.algsProvider.getCanonicalizationAlgorithmForTimeStampProperties();
try {
TimeStampDigestInput digestInput = this.tsInputFactory.newTimeStampDigestInput(c14n);
addPropSpecificTimeStampInput(prop, digestInput, ctx);
TimeStampTokenRes tsTknRes = this.tsTokenProvider.getTimeStampToken(digestInput.getBytes(), this.algsProvider.getDigestAlgorithmForTimeStampProperties());
return createPropDataObj(prop, c14n, tsTknRes, ctx);
} catch (UnsupportedAlgorithmException ex) {
throw new PropertyDataGenerationException(prop, ex.getMessage(), ex);
} catch (CannotAddDataToDigestInputException ex) {
throw new PropertyDataGenerationException(prop, "cannot create time stamp input", ex);
} catch (TimeStampTokenGenerationException ex) {
throw new PropertyDataGenerationException(prop, "cannot get a time-stamp", ex);
}
}
use of xades4j.providers.TimeStampTokenGenerationException in project xades4j by luisgoncalves.
the class HttpTimeStampTokenProvider method getResponse.
protected byte[] getResponse(byte[] encodedRequest) throws TimeStampTokenGenerationException {
HttpURLConnection connection = null;
try {
connection = createHttpConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "application/timestamp-query");
connection.setRequestProperty("Content-length", String.valueOf(encodedRequest.length));
OutputStream out = connection.getOutputStream();
out.write(encodedRequest);
out.flush();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new TimeStampTokenGenerationException(String.format("TSA returned HTTP %d %s", connection.getResponseCode(), connection.getResponseMessage()));
}
BufferedInputStream input = null;
try {
input = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
return baos.toByteArray();
} finally {
if (input != null)
input.close();
}
} catch (IOException ex) {
throw new TimeStampTokenGenerationException("Error when connecting to the TSA", ex);
} finally {
if (connection != null)
connection.disconnect();
}
}
use of xades4j.providers.TimeStampTokenGenerationException in project xades4j by luisgoncalves.
the class AbstractTimeStampTokenProvider method getTimeStampToken.
@Override
public final TimeStampTokenRes getTimeStampToken(byte[] tsDigestInput, String digestAlgUri) throws TimeStampTokenGenerationException {
byte[] digest;
try {
MessageDigest md = messageDigestProvider.getEngine(digestAlgUri);
digest = md.digest(tsDigestInput);
} catch (UnsupportedAlgorithmException ex) {
throw new TimeStampTokenGenerationException("Digest algorithm not supported", ex);
}
TimeStampRequest tsRequest = this.tsRequestGenerator.generate(identifierForDigest(digestAlgUri), digest, BigInteger.valueOf(System.currentTimeMillis()));
TimeStampResponse tsResponse = getTimeStampResponse(tsRequest);
if (tsResponse.getStatus() != PKIStatus.GRANTED && tsResponse.getStatus() != PKIStatus.GRANTED_WITH_MODS) {
throw new TimeStampTokenGenerationException("Time stamp token not granted. " + tsResponse.getStatusString());
}
try {
tsResponse.validate(tsRequest);
} catch (TSPException ex) {
throw new TimeStampTokenGenerationException("Invalid time stamp response", ex);
}
TimeStampToken tsToken = tsResponse.getTimeStampToken();
TimeStampTokenRes tsTokenRes;
try {
tsTokenRes = new TimeStampTokenRes(tsToken.getEncoded(), tsToken.getTimeStampInfo().getGenTime());
} catch (IOException ex) {
throw new TimeStampTokenGenerationException("Encoding error", ex);
}
return tsTokenRes;
}
use of xades4j.providers.TimeStampTokenGenerationException in project xades4j by luisgoncalves.
the class AbstractTimeStampTokenProvider method getTimeStampResponse.
private TimeStampResponse getTimeStampResponse(TimeStampRequest tsRequest) throws TimeStampTokenGenerationException {
TimeStampResponse tsResponse;
try {
byte[] responseStream = getResponse(tsRequest.getEncoded());
tsResponse = new TimeStampResponse(responseStream);
} catch (TSPException ex) {
throw new TimeStampTokenGenerationException("Invalid time stamp response", ex);
} catch (IOException ex) {
throw new TimeStampTokenGenerationException("Encoding error", ex);
}
return tsResponse;
}
Aggregations