|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Original Work: MIT License, Copyright (c) [2015-2018] all contributors |
| 17 | + * https://github.com/marianobarrios/tls-channel |
| 18 | + */ |
| 19 | + |
| 20 | +package com.mongodb.internal.connection.tlschannel; |
| 21 | + |
| 22 | +import com.mongodb.internal.connection.tlschannel.impl.BufferHolder; |
| 23 | +import com.mongodb.internal.connection.tlschannel.impl.ByteBufferSet; |
| 24 | +import com.mongodb.internal.connection.tlschannel.impl.TlsChannelImpl; |
| 25 | + |
| 26 | +import javax.net.ssl.SSLContext; |
| 27 | +import javax.net.ssl.SSLEngine; |
| 28 | +import javax.net.ssl.SSLSession; |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.ByteBuffer; |
| 31 | +import java.nio.channels.ByteChannel; |
| 32 | +import java.nio.channels.Channel; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.function.Consumer; |
| 35 | +import java.util.function.Supplier; |
| 36 | + |
| 37 | +/** |
| 38 | + * A client-side {@link TlsChannel}. |
| 39 | + */ |
| 40 | +public final class ClientTlsChannel implements TlsChannel { |
| 41 | + |
| 42 | + /** |
| 43 | + * Builder of {@link ClientTlsChannel} |
| 44 | + */ |
| 45 | + public static final class Builder extends TlsChannelBuilder<Builder> { |
| 46 | + |
| 47 | + private Supplier<SSLEngine> sslEngineFactory; |
| 48 | + |
| 49 | + private Builder(final ByteChannel underlying, final SSLEngine sslEngine) { |
| 50 | + super(underlying); |
| 51 | + this.sslEngineFactory = new Supplier<SSLEngine>() { |
| 52 | + @Override |
| 53 | + public SSLEngine get() { |
| 54 | + return sslEngine; |
| 55 | + } |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + private Builder(final ByteChannel underlying, final SSLContext sslContext) { |
| 60 | + super(underlying); |
| 61 | + this.sslEngineFactory = new Supplier<SSLEngine>() { |
| 62 | + @Override |
| 63 | + public SSLEngine get() { |
| 64 | + return defaultSSLEngineFactory(sslContext); |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + Builder getThis() { |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public ClientTlsChannel build() { |
| 75 | + return new ClientTlsChannel(underlying, sslEngineFactory.get(), sessionInitCallback, runTasks, |
| 76 | + plainBufferAllocator, encryptedBufferAllocator, releaseBuffers, waitForCloseConfirmation); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + private static SSLEngine defaultSSLEngineFactory(final SSLContext sslContext) { |
| 82 | + SSLEngine engine = sslContext.createSSLEngine(); |
| 83 | + engine.setUseClientMode(true); |
| 84 | + return engine; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a new {@link Builder}, configured with a underlying |
| 89 | + * {@link Channel} and a fixed {@link SSLEngine}. |
| 90 | + * |
| 91 | + * @param underlying a reference to the underlying {@link ByteChannel} |
| 92 | + * @param sslEngine the engine to use with this channel |
| 93 | + */ |
| 94 | + public static Builder newBuilder(final ByteChannel underlying, final SSLEngine sslEngine) { |
| 95 | + return new Builder(underlying, sslEngine); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Create a new {@link Builder}, configured with a underlying |
| 100 | + * {@link Channel} and a {@link SSLContext}. |
| 101 | + * |
| 102 | + * @param underlying a reference to the underlying {@link ByteChannel} |
| 103 | + * @param sslContext a context to use with this channel, it will be used to create a client {@link SSLEngine}. |
| 104 | + */ |
| 105 | + public static Builder newBuilder(final ByteChannel underlying, final SSLContext sslContext) { |
| 106 | + return new Builder(underlying, sslContext); |
| 107 | + } |
| 108 | + |
| 109 | + private final ByteChannel underlying; |
| 110 | + private final TlsChannelImpl impl; |
| 111 | + |
| 112 | + private ClientTlsChannel( |
| 113 | + final ByteChannel underlying, |
| 114 | + final SSLEngine engine, |
| 115 | + final Consumer<SSLSession> sessionInitCallback, |
| 116 | + final boolean runTasks, |
| 117 | + final BufferAllocator plainBufAllocator, |
| 118 | + final BufferAllocator encryptedBufAllocator, |
| 119 | + final boolean releaseBuffers, |
| 120 | + final boolean waitForCloseNotifyOnClose) { |
| 121 | + if (!engine.getUseClientMode()) { |
| 122 | + throw new IllegalArgumentException("SSLEngine must be in client mode"); |
| 123 | + } |
| 124 | + this.underlying = underlying; |
| 125 | + TrackingAllocator trackingPlainBufAllocator = new TrackingAllocator(plainBufAllocator); |
| 126 | + TrackingAllocator trackingEncryptedAllocator = new TrackingAllocator(encryptedBufAllocator); |
| 127 | + impl = new TlsChannelImpl(underlying, underlying, engine, Optional.<BufferHolder>empty(), sessionInitCallback, runTasks, |
| 128 | + trackingPlainBufAllocator, trackingEncryptedAllocator, releaseBuffers, waitForCloseNotifyOnClose); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public ByteChannel getUnderlying() { |
| 133 | + return underlying; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public SSLEngine getSslEngine() { |
| 138 | + return impl.engine(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Consumer<SSLSession> getSessionInitCallback() { |
| 143 | + return impl.getSessionInitCallback(); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public TrackingAllocator getPlainBufferAllocator() { |
| 148 | + return impl.getPlainBufferAllocator(); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public TrackingAllocator getEncryptedBufferAllocator() { |
| 153 | + return impl.getEncryptedBufferAllocator(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public boolean getRunTasks() { |
| 158 | + return impl.getRunTasks(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public long read(final ByteBuffer[] dstBuffers, final int offset, final int length) throws IOException { |
| 163 | + ByteBufferSet dest = new ByteBufferSet(dstBuffers, offset, length); |
| 164 | + TlsChannelImpl.checkReadBuffer(dest); |
| 165 | + return impl.read(dest); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public long read(final ByteBuffer[] dstBuffers) throws IOException { |
| 170 | + return read(dstBuffers, 0, dstBuffers.length); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public int read(final ByteBuffer dstBuffer) throws IOException { |
| 175 | + return (int) read(new ByteBuffer[]{dstBuffer}); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public long write(final ByteBuffer[] srcBuffers, final int offset, final int length) throws IOException { |
| 180 | + ByteBufferSet source = new ByteBufferSet(srcBuffers, offset, length); |
| 181 | + return impl.write(source); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public long write(final ByteBuffer[] outs) throws IOException { |
| 186 | + return write(outs, 0, outs.length); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public int write(final ByteBuffer srcBuffer) throws IOException { |
| 191 | + return (int) write(new ByteBuffer[]{srcBuffer}); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void renegotiate() throws IOException { |
| 196 | + impl.renegotiate(); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void handshake() throws IOException { |
| 201 | + impl.handshake(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void close() throws IOException { |
| 206 | + impl.close(); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public boolean isOpen() { |
| 211 | + return impl.isOpen(); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public boolean shutdown() throws IOException { |
| 216 | + return impl.shutdown(); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public boolean shutdownReceived() { |
| 221 | + return impl.shutdownReceived(); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public boolean shutdownSent() { |
| 226 | + return impl.shutdownSent(); |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments