cloudstack-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jburwell <...@git.apache.org>
Subject [GitHub] cloudstack pull request: utils: Use non-blocking SSL handshake in ...
Date Wed, 13 Apr 2016 21:53:14 GMT
Github user jburwell commented on a diff in the pull request:

    https://github.com/apache/cloudstack/pull/1493#discussion_r59632745
  
    --- Diff: utils/src/main/java/com/cloud/utils/nio/Link.java ---
    @@ -453,115 +449,150 @@ public static SSLContext initSSLContext(boolean isClient) throws
GeneralSecurity
             return sslContext;
         }
     
    -    public static void doHandshake(SocketChannel ch, SSLEngine sslEngine, boolean isClient)
throws IOException {
    -        if (s_logger.isTraceEnabled()) {
    -            s_logger.trace("SSL: begin Handshake, isClient: " + isClient);
    +    public static ByteBuffer enlargeBuffer(ByteBuffer buffer, final int sessionProposedCapacity)
{
    +        if (sessionProposedCapacity > buffer.capacity()) {
    +            buffer = ByteBuffer.allocate(sessionProposedCapacity);
    +        } else {
    +            buffer = ByteBuffer.allocate(buffer.capacity() * 2);
             }
    +        return buffer;
    +    }
     
    -        SSLEngineResult engResult;
    -        SSLSession sslSession = sslEngine.getSession();
    -        HandshakeStatus hsStatus;
    -        ByteBuffer in_pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize() +
40);
    -        ByteBuffer in_appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize()
+ 40);
    -        ByteBuffer out_pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize()
+ 40);
    -        ByteBuffer out_appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize()
+ 40);
    -        int count;
    -        ch.socket().setSoTimeout(60 * 1000);
    -        InputStream inStream = ch.socket().getInputStream();
    -        // Use readCh to make sure the timeout on reading is working
    -        ReadableByteChannel readCh = Channels.newChannel(inStream);
    -
    -        if (isClient) {
    -            hsStatus = SSLEngineResult.HandshakeStatus.NEED_WRAP;
    -        } else {
    -            hsStatus = SSLEngineResult.HandshakeStatus.NEED_UNWRAP;
    +    public static ByteBuffer handleBufferUnderflow(final SSLEngine engine, ByteBuffer
buffer) {
    +        if (buffer.position() < buffer.limit()) {
    +            return buffer;
             }
    +        ByteBuffer replaceBuffer = enlargeBuffer(buffer, engine.getSession().getPacketBufferSize());
    +        buffer.flip();
    +        replaceBuffer.put(buffer);
    +        return replaceBuffer;
    +    }
     
    -        while (hsStatus != SSLEngineResult.HandshakeStatus.FINISHED) {
    -            if (s_logger.isTraceEnabled()) {
    -                s_logger.trace("SSL: Handshake status " + hsStatus);
    -            }
    -            engResult = null;
    -            if (hsStatus == SSLEngineResult.HandshakeStatus.NEED_WRAP) {
    -                out_pkgBuf.clear();
    -                out_appBuf.clear();
    -                out_appBuf.put("Hello".getBytes());
    -                engResult = sslEngine.wrap(out_appBuf, out_pkgBuf);
    -                out_pkgBuf.flip();
    -                int remain = out_pkgBuf.limit();
    -                while (remain != 0) {
    -                    remain -= ch.write(out_pkgBuf);
    -                    if (remain < 0) {
    -                        throw new IOException("Too much bytes sent?");
    -                    }
    -                }
    -            } else if (hsStatus == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
    -                in_appBuf.clear();
    -                // One packet may contained multiply operation
    -                if (in_pkgBuf.position() == 0 || !in_pkgBuf.hasRemaining()) {
    -                    in_pkgBuf.clear();
    -                    count = 0;
    -                    try {
    -                        count = readCh.read(in_pkgBuf);
    -                    } catch (SocketTimeoutException ex) {
    -                        if (s_logger.isTraceEnabled()) {
    -                            s_logger.trace("Handshake reading time out! Cut the connection");
    +    public static boolean doHandshake(SocketChannel socketChannel, SSLEngine sslEngine)
throws IOException {
    +        final int appBufferSize = sslEngine.getSession().getApplicationBufferSize();
    +        final int netBufferSize = sslEngine.getSession().getPacketBufferSize();
    +        ByteBuffer myAppData = ByteBuffer.allocate(appBufferSize);
    +        ByteBuffer peerAppData = ByteBuffer.allocate(appBufferSize);
    +        ByteBuffer myNetData = ByteBuffer.allocate(netBufferSize);
    +        ByteBuffer peerNetData = ByteBuffer.allocate(netBufferSize);
    +
    +        SSLEngineResult result;
    +        HandshakeStatus handshakeStatus = sslEngine.getHandshakeStatus();
    +        while (handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED
    +                && handshakeStatus != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING)
{
    +            switch (handshakeStatus) {
    +                case NEED_UNWRAP:
    +                    if (socketChannel.read(peerNetData) < 0) {
    +                        if (sslEngine.isInboundDone() && sslEngine.isOutboundDone())
{
    +                            return false;
                             }
    -                        count = -1;
    +                        try {
    +                            sslEngine.closeInbound();
    +                        } catch (SSLException e) {
    +                            s_logger.error("This SSL engine was forced to close inbound
due to end of stream.");
    +                        }
    +                        sslEngine.closeOutbound();
    +                        // After closeOutbound the engine will be set to WRAP state,
    +                        // in order to try to send a close message to the client.
    +                        handshakeStatus = sslEngine.getHandshakeStatus();
    +                        break;
                         }
    -                    if (count == -1) {
    -                        throw new IOException("Connection closed with -1 on reading size.");
    +                    peerNetData.flip();
    +                    try {
    +                        result = sslEngine.unwrap(peerNetData, peerAppData);
    +                        peerNetData.compact();
    +                        handshakeStatus = result.getHandshakeStatus();
    +                    } catch (SSLException sslException) {
    +                        s_logger.error("SSL error occurred while processing unwrap data:
" + sslException.getMessage());
    +                        sslEngine.closeOutbound();
    +                        handshakeStatus = sslEngine.getHandshakeStatus();
    +                        break;
                         }
    -                    in_pkgBuf.flip();
    -                }
    -                engResult = sslEngine.unwrap(in_pkgBuf, in_appBuf);
    -                ByteBuffer tmp_pkgBuf = ByteBuffer.allocate(sslSession.getPacketBufferSize()
+ 40);
    -                int loop_count = 0;
    -                while (engResult.getStatus() == SSLEngineResult.Status.BUFFER_UNDERFLOW)
{
    -                    // The client is too slow? Cut it and let it reconnect
    -                    if (loop_count > 10) {
    -                        throw new IOException("Too many times in SSL BUFFER_UNDERFLOW,
disconnect guest.");
    +                    switch (result.getStatus()) {
    +                        case OK:
    +                            break;
    +                        case BUFFER_OVERFLOW:
    +                            // Will occur when peerAppData's capacity is smaller than
the data derived from peerNetData's unwrap.
    +                            peerAppData = enlargeBuffer(peerAppData, appBufferSize);
    +                            break;
    +                        case BUFFER_UNDERFLOW:
    +                            // Will occur either when no data was read from the peer
or when the peerNetData buffer
    +                            // was too small to hold all peer's data.
    +                            peerNetData = handleBufferUnderflow(sslEngine, peerNetData);
    +                            break;
    +                        case CLOSED:
    +                            if (sslEngine.isOutboundDone()) {
    +                                return false;
    +                            } else {
    +                                sslEngine.closeOutbound();
    +                                handshakeStatus = sslEngine.getHandshakeStatus();
    +                                break;
    +                            }
    +                        default:
    +                            throw new IllegalStateException("Invalid SSL status: " +
result.getStatus());
    --- End diff --
    
    This case statement is quite long.  Consider extracting to private method to increase
readability/maintainability.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message