Esempio n. 1
0
  /**
   * Creates new instance of {@code SSLIOSession} class.
   *
   * @param session I/O session to be decorated with the TLS/SSL capabilities.
   * @param sslMode SSL mode (client or server)
   * @param host original host (applicable in client mode only)
   * @param sslContext SSL context to use for this I/O session.
   * @param handler optional SSL setup handler. May be {@code null}.
   * @param bufferManagementStrategy buffer management strategy
   */
  public SSLIOSession(
      final IOSession session,
      final SSLMode sslMode,
      final HttpHost host,
      final SSLContext sslContext,
      final SSLSetupHandler handler,
      final SSLBufferManagementStrategy bufferManagementStrategy) {
    super();
    Args.notNull(session, "IO session");
    Args.notNull(sslContext, "SSL context");
    Args.notNull(bufferManagementStrategy, "Buffer management strategy");
    this.session = session;
    this.sslMode = sslMode;
    this.appEventMask = session.getEventMask();
    this.channel = new InternalByteChannel();
    this.handler = handler;

    // Override the status buffer interface
    this.session.setBufferStatus(this);

    if (this.sslMode == SSLMode.CLIENT && host != null) {
      this.sslEngine = sslContext.createSSLEngine(host.getHostName(), host.getPort());
    } else {
      this.sslEngine = sslContext.createSSLEngine();
    }

    // Allocate buffers for network (encrypted) data
    final int netBuffersize = this.sslEngine.getSession().getPacketBufferSize();
    this.inEncrypted = bufferManagementStrategy.constructBuffer(netBuffersize);
    this.outEncrypted = bufferManagementStrategy.constructBuffer(netBuffersize);

    // Allocate buffers for application (unencrypted) data
    final int appBuffersize = this.sslEngine.getSession().getApplicationBufferSize();
    this.inPlain = bufferManagementStrategy.constructBuffer(appBuffersize);
    this.outPlain = bufferManagementStrategy.constructBuffer(appBuffersize);
  }