Example #1
0
  /**
   * sends TS Request and receives an answer in following definition
   *
   * <p>The TimeStampResp ASN.1 type definition:
   *
   * <pre>
   *
   *     TimeStampResp ::= SEQUENCE {
   *         status            PKIStatusInfo,
   *         timeStampToken    TimeStampToken OPTIONAL ]
   *
   *     PKIStatusInfo ::= SEQUENCE {
   *         status        PKIStatus,
   *         statusString  PKIFreeText OPTIONAL,
   *         failInfo      PKIFailureInfo OPTIONAL }
   *
   *     PKIStatus ::= INTEGER {
   *         granted                (0),
   *           -- when the PKIStatus contains the value zero a TimeStampToken, as
   *           -- requested, is present.
   *         grantedWithMods        (1),
   *           -- when the PKIStatus contains the value one a TimeStampToken,
   *           -- with modifications, is present.
   *         rejection              (2),
   *         waiting                (3),
   *         revocationWarning      (4),
   *           -- this message contains a warning that a revocation is
   *           -- imminent
   *         revocationNotification (5)
   *           -- notification that a revocation has occurred }
   *
   *     PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
   *           -- text encoded as UTF-8 String (note:  each UTF8String SHOULD
   *           -- include an RFC 1766 language tag to indicate the language
   *           -- of the contained text)
   *
   *     PKIFailureInfo ::= BIT STRING {
   *         badAlg              (0),
   *           -- unrecognized or unsupported Algorithm Identifier
   *         badRequest          (2),
   *           -- transaction not permitted or supported
   *         badDataFormat       (5),
   *           -- the data submitted has the wrong format
   *         timeNotAvailable    (14),
   *           -- the TSA's time source is not available
   *         unacceptedPolicy    (15),
   *           -- the requested TSA policy is not supported by the TSA
   *         unacceptedExtension (16),
   *           -- the requested extension is not supported by the TSA
   *         addInfoNotAvailable (17)
   *           -- the additional information requested could not be understood
   *           -- or is not available
   *         systemFailure       (25)
   *           -- the request cannot be handled due to system failure }
   *
   *     TimeStampToken ::= ContentInfo
   *         -- contentType is id-signedData
   *         -- content is SignedData
   *         -- eContentType within SignedData is id-ct-TSTInfo
   *         -- eContent within SignedData is TSTInfo
   *
   * </pre>
   *
   * @param tsq TimeStamp Request to be sent to TSA
   * @param server complete URL of the TSA server
   * @return TimeStamp Response created from TSA's response
   */
  private TimeStampResponse getTSResponse(TimeStampRequest tsq, String server)
      throws IOException, TSPException {
    logger.trace("entering getTSResponse() method");
    logger.entry(tsq, server);
    final byte[] request = tsq.getEncoded();
    // open valid connection
    HttpURLConnection con;
    try {
      URL url = new URL(server);
      con = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
      logger.error("TSA server couldn't be contacted");
      throw e;
    }
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestProperty("Content-type", "application/timestamp-query");
    // con.setRequestProperty("Content-length", String.valueOf(request.length));
    logger.info("TSA server was successfully contacted");

    // send request
    OutputStream out;
    try {
      out = con.getOutputStream();
      out.write(request);
      out.flush();
    } catch (IOException e) {
      logger.error("Failed to send the TS request.");
      throw e;
    }
    logger.debug("TS request sent");

    // receive response
    InputStream in;
    TimeStampResp resp;
    TimeStampResponse response;
    try {
      // verify connection status
      if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new IOException(
            "Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
      } else {
        logger.debug("Response Code: {}", con.getResponseCode());
      }
      // accept the answer
      in = con.getInputStream();
      resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
      response = new TimeStampResponse(resp);
      // verify the answer
      response.validate(tsq);
    } catch (TSPException | IOException e) {
      logger.error("Cannot interpret incoming data.");
      throw e;
    }

    logger.debug("Status: {}", response.getStatusString()); // null means OK

    return logger.exit(response);
  }