Esempio n. 1
0
 private static void log(String str, SSLEngineResult result) {
   if (!logging) {
     return;
   }
   if (resultOnce) {
     resultOnce = false;
     System.out.println(
         "The format of the SSLEngineResult is: \n"
             + "\t\"getStatus() / getHandshakeStatus()\" +\n"
             + "\t\"bytesConsumed() / bytesProduced()\"\n");
   }
   HandshakeStatus hsStatus = result.getHandshakeStatus();
   log(
       str
           + result.getStatus()
           + "/"
           + hsStatus
           + ", "
           + result.bytesConsumed()
           + "/"
           + result.bytesProduced()
           + " bytes");
   if (hsStatus == HandshakeStatus.FINISHED) {
     log("\t...ready for application data");
   }
 }
Esempio n. 2
0
  private void checkResult(
      SSLEngine engine,
      SSLEngineResult result,
      HandshakeStatus rqdHsStatus,
      boolean consumed,
      boolean produced)
      throws Exception {

    HandshakeStatus hsStatus = result.getHandshakeStatus();

    if (hsStatus == HandshakeStatus.NEED_TASK) {
      Runnable runnable;
      while ((runnable = engine.getDelegatedTask()) != null) {
        runnable.run();
      }
      hsStatus = engine.getHandshakeStatus();
    }

    if (hsStatus != rqdHsStatus) {
      throw new Exception("Required " + rqdHsStatus + ", got " + hsStatus);
    }

    int bc = result.bytesConsumed();
    int bp = result.bytesProduced();

    if (consumed) {
      if (bc <= 0) {
        throw new Exception("Should have consumed bytes");
      }
    } else {
      if (bc > 0) {
        throw new Exception("Should not have consumed bytes");
      }
    }

    if (produced) {
      if (bp <= 0) {
        throw new Exception("Should have produced bytes");
      }
    } else {
      if (bp > 0) {
        throw new Exception("Should not have produced bytes");
      }
    }
  }
Esempio n. 3
0
  /*
   * Try to flush out any existing outbound data, then try to wrap
   * anything new contained in the src buffer.
   * <P>
   * Return the number of bytes actually consumed from the buffer,
   * but the data may actually be still sitting in the output buffer,
   * waiting to be flushed.
   */
  private int doWrite(ByteBuffer src) throws IOException {
    int retValue = 0;

    if (outNetBB.hasRemaining() && !tryFlush(outNetBB)) {
      return retValue;
    }

    /*
     * The data buffer is empty, we can reuse the entire buffer.
     */
    outNetBB.clear();

    SSLEngineResult result = sslEngine.wrap(src, outNetBB);
    retValue = result.bytesConsumed();

    outNetBB.flip();

    switch (result.getStatus()) {
      case OK:
        if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
          doTasks();
        }
        break;

      default:
        throw new IOException("sslEngine error during data write: " + result.getStatus());
    }

    /*
     * Try to flush the data, regardless of whether or not
     * it's been selected.  Odds of a write buffer being full
     * is less than a read buffer being empty.
     */
    if (outNetBB.hasRemaining()) {
      tryFlush(outNetBB);
    }

    return retValue;
  }