1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net.http;
  27 
  28 import java.io.IOException;
  29 import java.io.UncheckedIOException;
  30 import java.net.InetSocketAddress;
  31 import java.nio.ByteBuffer;
  32 import java.nio.channels.SocketChannel;
  33 import java.security.AccessControlContext;
  34 import java.util.concurrent.CompletableFuture;
  35 import javax.net.ssl.SSLEngineResult.Status;
  36 import javax.net.ssl.SSLParameters;
  37 import java.net.http.SSLDelegate.BufType;
  38 import java.net.http.SSLDelegate.WrapperResult;
  39 
  40 /**
  41  * An SSL tunnel built on a Plain (CONNECT) TCP tunnel.
  42  */
  43 class SSLTunnelConnection extends HttpConnection {
  44 
  45     final PlainTunnelingConnection delegate;
  46     protected SSLDelegate sslDelegate;
  47     private volatile boolean connected;
  48 
  49     @Override
  50     public void connect() throws IOException, InterruptedException {
  51         delegate.connect();
  52         this.sslDelegate = new SSLDelegate(delegate.channel(), client, null);
  53         connected = true;
  54     }
  55 
  56     @Override
  57     boolean connected() {
  58         return connected && delegate.connected();
  59     }
  60 
  61     @Override
  62     public CompletableFuture<Void> connectAsync() {
  63         return delegate.connectAsync()
  64             .thenAccept((Void v) -> {
  65                 try {
  66                     // can this block?
  67                     this.sslDelegate = new SSLDelegate(delegate.channel(),
  68                                                        client,
  69                                                        null);
  70                     connected = true;
  71                 } catch (IOException e) {
  72                     throw new UncheckedIOException(e);
  73                 }
  74             });
  75     }
  76 
  77     SSLTunnelConnection(InetSocketAddress addr,
  78                         HttpClientImpl client,
  79                         InetSocketAddress proxy,
  80                         AccessControlContext acc) {
  81         super(addr, client);
  82         delegate = new PlainTunnelingConnection(addr, proxy, client, acc);
  83     }
  84 
  85     @Override
  86     SSLParameters sslParameters() {
  87         return sslDelegate.getSSLParameters();
  88     }
  89 
  90     @Override
  91     public String toString() {
  92         return "SSLTunnelConnection: " + super.toString();
  93     }
  94 
  95     private static long countBytes(ByteBuffer[] buffers, int start, int number) {
  96         long c = 0;
  97         for (int i=0; i<number; i++) {
  98             c+= buffers[start+i].remaining();
  99         }
 100         return c;
 101     }
 102 
 103     @Override
 104     ConnectionPool.CacheKey cacheKey() {
 105         return ConnectionPool.cacheKey(address, delegate.proxyAddr);
 106     }
 107 
 108     @Override
 109     long write(ByteBuffer[] buffers, int start, int number) throws IOException {
 110         //debugPrint("Send", buffers, start, number);
 111         long l = countBytes(buffers, start, number);
 112         WrapperResult r = sslDelegate.sendData(buffers, start, number);
 113         if (r.result.getStatus() == Status.CLOSED) {
 114             if (l > 0) {
 115                 throw new IOException("SSLHttpConnection closed");
 116             }
 117         }
 118         return l;
 119     }
 120 
 121     @Override
 122     long write(ByteBuffer buffer) throws IOException {
 123         //debugPrint("Send", buffer);
 124         long l = buffer.remaining();
 125         WrapperResult r = sslDelegate.sendData(buffer);
 126         if (r.result.getStatus() == Status.CLOSED) {
 127             if (l > 0) {
 128                 throw new IOException("SSLHttpConnection closed");
 129             }
 130         }
 131         return l;
 132     }
 133 
 134     @Override
 135     public void close() {
 136         Utils.close(delegate.channel());
 137     }
 138 
 139     @Override
 140     protected ByteBuffer readImpl(int length) throws IOException {
 141         ByteBuffer buf = sslDelegate.allocate(BufType.PACKET, length);
 142         WrapperResult r = sslDelegate.recvData(buf);
 143         // TODO: check for closure
 144         String s = "Receive) ";
 145         //debugPrint(s, r.buf);
 146         return r.buf;
 147     }
 148 
 149     @Override
 150     protected int readImpl(ByteBuffer buf) throws IOException {
 151         WrapperResult r = sslDelegate.recvData(buf);
 152         // TODO: check for closure
 153         String s = "Receive) ";
 154         //debugPrint(s, r.buf);
 155         if (r.result.bytesProduced() > 0) {
 156             assert buf == r.buf;
 157         }
 158 
 159         return r.result.bytesProduced();
 160     }
 161 
 162     @Override
 163     SocketChannel channel() {
 164         return delegate.channel();
 165     }
 166 
 167     @Override
 168     CompletableFuture<Void> whenReceivingResponse() {
 169         return delegate.whenReceivingResponse();
 170     }
 171 
 172     @Override
 173     boolean isSecure() {
 174         return true;
 175     }
 176 
 177     @Override
 178     boolean isProxied() {
 179         return true;
 180     }
 181 }