Search in sources :

Example 1 with TimeStampTokenGenerationException

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);
    }
}
Also used : CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) TimeStampDigestInput(xades4j.utils.TimeStampDigestInput) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) TimeStampTokenRes(xades4j.providers.TimeStampTokenProvider.TimeStampTokenRes) Algorithm(xades4j.algorithms.Algorithm) TimeStampTokenGenerationException(xades4j.providers.TimeStampTokenGenerationException)

Example 2 with TimeStampTokenGenerationException

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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TimeStampTokenGenerationException(xades4j.providers.TimeStampTokenGenerationException)

Example 3 with TimeStampTokenGenerationException

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;
}
Also used : UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) TimeStampTokenGenerationException(xades4j.providers.TimeStampTokenGenerationException)

Example 4 with TimeStampTokenGenerationException

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;
}
Also used : IOException(java.io.IOException) TimeStampTokenGenerationException(xades4j.providers.TimeStampTokenGenerationException)

Aggregations

TimeStampTokenGenerationException (xades4j.providers.TimeStampTokenGenerationException)4 IOException (java.io.IOException)3 UnsupportedAlgorithmException (xades4j.UnsupportedAlgorithmException)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MessageDigest (java.security.MessageDigest)1 Algorithm (xades4j.algorithms.Algorithm)1 TimeStampTokenRes (xades4j.providers.TimeStampTokenProvider.TimeStampTokenRes)1 CannotAddDataToDigestInputException (xades4j.utils.CannotAddDataToDigestInputException)1 TimeStampDigestInput (xades4j.utils.TimeStampDigestInput)1