src/jdk.httpserver/share/classes/sun/net/httpserver/SSLStreams.java
author ssadetsky
Mon, 16 Oct 2017 08:06:56 -0700
changeset 47387 4d711a58bb3b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8180821: [macosx]com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java: The menu item should have been GC-ed Reviewed-by: prr, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.SSLEngineResult.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * given a non-blocking SocketChannel, it produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (blocking) streams which encrypt/decrypt the SSL content
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and handle the SSL handshaking automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class SSLStreams {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    SSLContext sslctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    TimeSource time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    SSLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    EngineWrapper wrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /* held by thread doing the hand-shake on this connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    Lock handshaking = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    SSLStreams (ServerImpl server, SSLContext sslctx, SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.time= (TimeSource)server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.sslctx= sslctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.chan= chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        InetSocketAddress addr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                (InetSocketAddress)chan.socket().getRemoteSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        engine = sslctx.createSSLEngine (addr.getHostName(), addr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        engine.setUseClientMode (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        HttpsConfigurator cfg = server.getHttpsConfigurator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        configureEngine (cfg, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        wrapper = new EngineWrapper (chan, engine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private void configureEngine(HttpsConfigurator cfg, InetSocketAddress addr){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (cfg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            Parameters params = new Parameters (cfg, addr);
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
    74
//BEGIN_TIGER_EXCLUDE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            cfg.configure (params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            SSLParameters sslParams = params.getSSLParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (sslParams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                engine.setSSLParameters (sslParams);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    79
            } else
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    80
//END_TIGER_EXCLUDE
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    81
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                /* tiger compatibility */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                if (params.getCipherSuites() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        engine.setEnabledCipherSuites (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                            params.getCipherSuites()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    } catch (IllegalArgumentException e) { /* LOG */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                engine.setNeedClientAuth (params.getNeedClientAuth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                engine.setWantClientAuth (params.getWantClientAuth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (params.getProtocols() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        engine.setEnabledProtocols (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                            params.getProtocols()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    } catch (IllegalArgumentException e) { /* LOG */}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    class Parameters extends HttpsParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        InetSocketAddress addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        HttpsConfigurator cfg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Parameters (HttpsConfigurator cfg, InetSocketAddress addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            this.cfg = cfg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public InetSocketAddress getClientAddress () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        public HttpsConfigurator getHttpsConfigurator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return cfg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   117
//BEGIN_TIGER_EXCLUDE
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   118
        SSLParameters params;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public void setSSLParameters (SSLParameters p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            params = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        SSLParameters getSSLParameters () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   125
//END_TIGER_EXCLUDE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * cleanup resources allocated inside this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        wrapper.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * return the SSL InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    InputStream getInputStream () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (is == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            is = new InputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * return the SSL OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    OutputStream getOutputStream () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            os = new OutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    SSLEngine getSSLEngine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * request the engine to repeat the handshake on this session
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the handshake must be driven by reads/writes on the streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Normally, not necessary to call this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    void beginHandshake() throws SSLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        engine.beginHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    class WrapperResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        SSLEngineResult result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /* if passed in buffer was not big enough then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * a reallocated buffer is returned here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    int app_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    int packet_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    enum BufType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        PACKET, APPLICATION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private ByteBuffer allocate (BufType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return allocate (type, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private ByteBuffer allocate (BufType type, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        assert engine != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (type == BufType.PACKET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (packet_buf_size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    SSLSession sess = engine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    packet_buf_size = sess.getPacketBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (len > packet_buf_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    packet_buf_size = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                size = packet_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (app_buf_size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    SSLSession sess = engine.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    app_buf_size = sess.getApplicationBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (len > app_buf_size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    app_buf_size = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                size = app_buf_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return ByteBuffer.allocate (size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /* reallocates the buffer by :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * 1. creating a new buffer double the size of the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * 2. putting the contents of the old buffer into the new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * 3. set xx_buf_size to the new size if it was smaller than new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * flip is set to true if the old buffer needs to be flipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * before it is copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private ByteBuffer realloc (ByteBuffer b, boolean flip, BufType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            int nsize = 2 * b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ByteBuffer n = allocate (type, nsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (flip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            n.put(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            b = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * This is a thin wrapper over SSLEngine and the SocketChannel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * which guarantees the ordering of wraps/unwraps with respect to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * channel read/writes. It handles the UNDER/OVERFLOW status codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * It does not handle the handshaking status codes, or the CLOSED status code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * though once the engine is closed, any attempt to read/write to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * will get an exception.  The overall result is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * It functions synchronously/blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    class EngineWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        SSLEngine engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Object wrapLock, unwrapLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        ByteBuffer unwrap_src, wrap_dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int u_remaining; // the number of bytes left in unwrap_src after an unwrap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        EngineWrapper (SocketChannel chan, SSLEngine engine) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            this.engine = engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            wrapLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            unwrapLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            unwrap_src = allocate(BufType.PACKET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            wrap_dst = allocate(BufType.PACKET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        /* try to wrap and send the data in src. Handles OVERFLOW.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * Might block if there is an outbound blockage or if another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         * thread is calling wrap(). Also, might not send any data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * if an unwrap is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        WrapperResult wrapAndSend(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return wrapAndSendX(src, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        WrapperResult wrapAndSendX(ByteBuffer src, boolean ignoreClose) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (closed && !ignoreClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                throw new IOException ("Engine is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            Status status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            WrapperResult r = new WrapperResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            synchronized (wrapLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                wrap_dst.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    r.result = engine.wrap (src, wrap_dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    if (status == Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        wrap_dst = realloc (wrap_dst, true, BufType.PACKET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                } while (status == Status.BUFFER_OVERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (status == Status.CLOSED && !ignoreClose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (r.result.bytesProduced() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    wrap_dst.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    int l = wrap_dst.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    assert l == r.result.bytesProduced();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    while (l>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        l -= chan.write (wrap_dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        /* block until a complete message is available and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * in dst, together with the Result. dst may have been re-allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * so caller should check the returned value in Result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * If handshaking is in progress then, possibly no data is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        WrapperResult recvAndUnwrap(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Status status = Status.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            WrapperResult r = new WrapperResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            r.buf = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                throw new IOException ("Engine is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            boolean needData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (u_remaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                unwrap_src.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                unwrap_src.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                needData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                unwrap_src.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                needData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            synchronized (unwrapLock) {
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   327
                int x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    if (needData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        x = chan.read (unwrap_src);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   332
                        } while (x == 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        if (x == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                            throw new IOException ("connection closed for reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        unwrap_src.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    r.result = engine.unwrap (unwrap_src, r.buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    if (status == Status.BUFFER_UNDERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        if (unwrap_src.limit() == unwrap_src.capacity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            /* buffer not big enough */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                            unwrap_src = realloc (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                unwrap_src, false, BufType.PACKET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                            /* Buffer not full, just need to read more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                             * data off the channel. Reset pointers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                             * for reading off SocketChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            unwrap_src.position (unwrap_src.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            unwrap_src.limit (unwrap_src.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        needData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    } else if (status == Status.BUFFER_OVERFLOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        r.buf = realloc (r.buf, true, BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        needData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    } else if (status == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        r.buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                } while (status != Status.OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            u_remaining = unwrap_src.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * send the data in the given ByteBuffer. If a handshake is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * then this is handled within this method. When this call returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * all of the given user data has been sent and any handshake has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * completed. Caller should check if engine has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public WrapperResult sendData (ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        WrapperResult r=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        while (src.remaining() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            r = wrapper.wrapAndSend(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            Status status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (status == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                doClosure ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            HandshakeStatus hs_status = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (hs_status != HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                hs_status != HandshakeStatus.NOT_HANDSHAKING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                doHandshake(hs_status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * read data thru the engine into the given ByteBuffer. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * given buffer was not large enough, a new one is allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * and returned. This call handles handshaking automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Caller should check if engine has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public WrapperResult recvData (ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        /* we wait until some user data arrives */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        WrapperResult r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        assert dst.position() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        while (dst.position() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            r = wrapper.recvAndUnwrap (dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            dst = (r.buf != dst) ? r.buf: dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            Status status = r.result.getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (status == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                doClosure ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            HandshakeStatus hs_status = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (hs_status != HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                hs_status != HandshakeStatus.NOT_HANDSHAKING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                doHandshake (hs_status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        dst.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /* we've received a close notify. Need to call wrap to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    void doClosure () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            handshaking.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            ByteBuffer tmp = allocate(BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            WrapperResult r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                tmp.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                tmp.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                r = wrapper.wrapAndSendX (tmp, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            } while (r.result.getStatus() != Status.CLOSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            handshaking.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /* do the (complete) handshake after acquiring the handshake lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * If two threads call this at the same time, then we depend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * on the wrapper methods being idempotent. eg. if wrapAndSend()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * is called with no data to send then there must be no problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   448
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    void doHandshake (HandshakeStatus hs_status) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            handshaking.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            ByteBuffer tmp = allocate(BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            while (hs_status != HandshakeStatus.FINISHED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                   hs_status != HandshakeStatus.NOT_HANDSHAKING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                WrapperResult r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                switch (hs_status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    case NEED_TASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        Runnable task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        while ((task = engine.getDelegatedTask()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            /* run in current thread, because we are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                             * running an external Executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            task.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        /* fall thru - call wrap again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    case NEED_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        tmp.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        tmp.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        r = wrapper.wrapAndSend(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    case NEED_UNWRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        tmp.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        r = wrapper.recvAndUnwrap (tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        if (r.buf != tmp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            tmp = r.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        assert tmp.position() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                hs_status = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            handshaking.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * represents an SSL input stream. Multiple https requests can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * be sent over one stream. closing this stream causes an SSL close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    class InputStream extends java.io.InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        ByteBuffer bbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        /* this stream eof */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        boolean eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        boolean needData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        InputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            bbuf = allocate (BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        public int read (byte[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                throw new IOException ("SSL stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            int available=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (!needData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                available = bbuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                needData = (available==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if (needData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                bbuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                WrapperResult r = recvData (bbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                bbuf = r.buf== bbuf? bbuf: r.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if ((available=bbuf.remaining()) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    needData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            /* copy as much as possible from buf into users buf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (len > available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                len = available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            bbuf.get (buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        public int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return bbuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        public boolean markSupported () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return false; /* not possible with SSLEngine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new IOException ("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        public long skip (long s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            int n = (int)s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                throw new IOException ("SSL stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            int ret = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            while (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (bbuf.remaining() >= n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    bbuf.position (bbuf.position()+n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    n -= bbuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    bbuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    WrapperResult r = recvData (bbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    bbuf = r.buf==bbuf? bbuf: r.buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return ret; /* not reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * close the SSL connection. All data must have been consumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * before this is called. Otherwise an exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * [Note. May need to revisit this. not quite the normal close() symantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            engine.closeInbound ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        public int read (byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return read (buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        byte single[] = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        public int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            int n = read (single, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                return single[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * represents an SSL output stream. plain text data written to this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * is encrypted by the stream. Multiple HTTPS responses can be sent on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * one stream. closing this stream initiates an SSL closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    class OutputStream extends java.io.OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        byte single[] = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        OutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            buf = allocate(BufType.APPLICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            single[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            write (single, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                throw new IOException ("output stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                int l = len > buf.capacity() ? buf.capacity() : len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                buf.put (b, off, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                len -= l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                off += l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                WrapperResult r = sendData (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                if (r.result.getStatus() == Status.CLOSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        throw new IOException ("output stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            /* no-op */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            WrapperResult r=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            engine.closeOutbound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            HandshakeStatus stat = HandshakeStatus.NEED_WRAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            while (stat == HandshakeStatus.NEED_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                r = wrapper.wrapAndSend (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                stat = r.result.getHandshakeStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            assert r.result.getStatus() == Status.CLOSED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
}