Example usage for org.bouncycastle.asn1.tsp TimeStampResp getInstance

List of usage examples for org.bouncycastle.asn1.tsp TimeStampResp getInstance

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.tsp TimeStampResp getInstance.

Prototype

public static TimeStampResp getInstance(Object o) 

Source Link

Usage

From source file:com.guardtime.asn1.TimestampResponse.java

License:Apache License

/**
 * Class constructor.//from www .  jav a  2s .  co  m
 *
 * @param obj ASN.1 representation of timestamp response.
 */
TimestampResponse(ASN1Encodable obj) throws Asn1FormatException {
    try {
        response = TimeStampResp.getInstance(obj);

        statusInfo = new StatusInfo(response.getStatus());

        int statusCode = statusInfo.getStatusCode();
        org.bouncycastle.asn1.cms.ContentInfo ci = response.getTimeStampToken();
        // RFC 3161:
        //
        // When the status contains the value zero or one, a TimeStampToken
        // MUST be present.  When status contains a value other than zero
        // or one, a TimeStampToken MUST NOT be present.
        //
        // When the PKIStatus contains the value zero a TimeStampToken, as
        // requested, is present.
        //
        // When the PKIStatus contains the value one a TimeStampToken,
        // with modifications, is present.
        if (statusCode == 0 || statusCode == 1) {
            if (ci == null) {
                throw new Asn1FormatException("timestamp token is missing in timestamp response");
            }
            token = new ContentInfo(ci);
        } else if (statusCode >= 2 && statusCode <= 5) {
            if (ci != null) {
                throw new Asn1FormatException("unexpected timestamp token in timestamp response");
            }
            token = null;
        } else {
            throw new Asn1FormatException("invalid status code: " + statusCode);
        }
    } catch (Asn1FormatException e) {
        throw e;
    } catch (Exception e) {
        throw new Asn1FormatException("timestamp response has invalid format", e);
    }
}

From source file:ee.ria.xroad.proxy.messagelog.TimestamperUtil.java

License:Open Source License

static TimeStampResponse getTimestampResponse(InputStream in) throws Exception {
    TimeStampResp response = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());

    if (response == null) {
        throw new RuntimeException("Could not read time-stamp response");
    }//  w ww  .j a  v  a 2  s.c  o m

    BigInteger status = response.getStatus().getStatus();

    log.trace("getTimestampDer() - TimeStampResp.status: {}", status);

    if (!PKIStatus.granted.getValue().equals(status) && !PKIStatus.grantedWithMods.getValue().equals(status)) {
        PKIFreeText statusString = response.getStatus().getStatusString();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < statusString.size(); i++) {
            if (i > 0) {
                sb.append(", ");
            }

            sb.append("\"" + statusString.getStringAt(i) + "\"");
        }

        log.error("getTimestampDer() - TimeStampResp.status is not "
                + "\"granted\" neither \"grantedWithMods\": {}, {}", status, sb);

        throw new RuntimeException("TimeStampResp.status: " + status + ", .statusString: " + sb);
    }

    return new TimeStampResponse(response);
}

From source file:net.jsign.timestamp.RFC3161Timestamper.java

License:Apache License

protected CMSSignedData timestamp(DigestAlgorithm algo, byte[] encryptedDigest)
        throws IOException, TimestampingException {
    TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
    reqgen.setCertReq(true);// ww  w. j a v  a2 s. co m
    TimeStampRequest req = reqgen.generate(algo.oid, algo.getMessageDigest().digest(encryptedDigest));
    byte request[] = req.getEncoded();

    HttpURLConnection conn = (HttpURLConnection) tsaurl.openConnection();
    conn.setConnectTimeout(10000);
    conn.setReadTimeout(10000);
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-type", "application/timestamp-query");
    conn.setRequestProperty("Content-length", String.valueOf(request.length));
    conn.setRequestProperty("Accept", "application/timestamp-query");
    conn.setRequestProperty("User-Agent", "Transport");

    conn.getOutputStream().write(request);
    conn.getOutputStream().flush();

    if (conn.getResponseCode() >= 400) {
        throw new IOException("Unable to complete the timestamping due to HTTP error: " + conn.getResponseCode()
                + " - " + conn.getResponseMessage());
    }

    try {
        TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(conn.getInputStream()).readObject());
        TimeStampResponse response = new TimeStampResponse(resp);
        response.validate(req);
        if (response.getStatus() != 0) {
            throw new IOException("Unable to complete the timestamping due to an invalid response ("
                    + response.getStatusString() + ")");
        }

        return response.getTimeStampToken().toCMSSignedData();

    } catch (Exception e) {
        throw new TimestampingException("Unable to complete the timestamping", e);
    }
}