Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | using System; |
| 21 | using System.Net.Security; |
| 22 | using System.Net.Sockets; |
| 23 | using System.Security.Authentication; |
| 24 | using System.Security.Cryptography.X509Certificates; |
| 25 | |
| 26 | namespace Thrift.Transport |
| 27 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 28 | /// <summary> |
| 29 | /// SSL Socket Wrapper class |
| 30 | /// </summary> |
| 31 | public class TTLSSocket : TStreamTransport |
| 32 | { |
| 33 | /// <summary> |
| 34 | /// Internal TCP Client |
| 35 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 36 | private TcpClient client; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 37 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 38 | /// <summary> |
| 39 | /// The host |
| 40 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 41 | private string host; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 42 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 43 | /// <summary> |
| 44 | /// The port |
| 45 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 46 | private int port; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 47 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 48 | /// <summary> |
| 49 | /// The timeout for the connection |
| 50 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 51 | private int timeout; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 52 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 53 | /// <summary> |
| 54 | /// Internal SSL Stream for IO |
| 55 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 56 | private SslStream secureStream; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 57 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 58 | /// <summary> |
| 59 | /// Defines wheter or not this socket is a server socket<br/> |
| 60 | /// This is used for the TLS-authentication |
| 61 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 62 | private bool isServer; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 63 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 64 | /// <summary> |
| 65 | /// The certificate |
| 66 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 67 | private X509Certificate certificate; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 68 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 69 | /// <summary> |
| 70 | /// User defined certificate validator. |
| 71 | /// </summary> |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 72 | private RemoteCertificateValidationCallback certValidator; |
Jens Geyer | 7b11fec | 2014-06-05 22:03:19 +0200 | [diff] [blame] | 73 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 74 | /// <summary> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 75 | /// The function to determine which certificate to use. |
| 76 | /// </summary> |
| 77 | private LocalCertificateSelectionCallback localCertificateSelectionCallback; |
| 78 | |
| 79 | /// <summary> |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 80 | /// Initializes a new instance of the <see cref="TTLSSocket"/> class. |
| 81 | /// </summary> |
| 82 | /// <param name="client">An already created TCP-client</param> |
| 83 | /// <param name="certificate">The certificate.</param> |
| 84 | /// <param name="isServer">if set to <c>true</c> [is server].</param> |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 85 | /// <param name="certValidator">User defined cert validator.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 86 | /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param> |
| 87 | public TTLSSocket( |
| 88 | TcpClient client, |
| 89 | X509Certificate certificate, |
| 90 | bool isServer = false, |
| 91 | RemoteCertificateValidationCallback certValidator = null, |
| 92 | LocalCertificateSelectionCallback localCertificateSelectionCallback = null) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 93 | { |
| 94 | this.client = client; |
| 95 | this.certificate = certificate; |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 96 | this.certValidator = certValidator; |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 97 | this.localCertificateSelectionCallback = localCertificateSelectionCallback; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 98 | this.isServer = isServer; |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 99 | if (isServer && certificate == null) |
| 100 | { |
| 101 | throw new ArgumentException("TTLSSocket needs certificate to be used for server", "certificate"); |
| 102 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 103 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 104 | if (IsOpen) |
| 105 | { |
| 106 | base.inputStream = client.GetStream(); |
| 107 | base.outputStream = client.GetStream(); |
| 108 | } |
| 109 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 110 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 111 | /// <summary> |
| 112 | /// Initializes a new instance of the <see cref="TTLSSocket"/> class. |
| 113 | /// </summary> |
| 114 | /// <param name="host">The host, where the socket should connect to.</param> |
| 115 | /// <param name="port">The port.</param> |
| 116 | /// <param name="certificatePath">The certificate path.</param> |
| 117 | /// <param name="certValidator">User defined cert validator.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 118 | /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param> |
| 119 | public TTLSSocket( |
| 120 | string host, |
| 121 | int port, |
| 122 | string certificatePath, |
| 123 | RemoteCertificateValidationCallback certValidator = null, |
| 124 | LocalCertificateSelectionCallback localCertificateSelectionCallback = null) |
| 125 | : this(host, port, 0, X509Certificate.CreateFromCertFile(certificatePath), certValidator, localCertificateSelectionCallback) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 126 | { |
| 127 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 128 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 129 | /// <summary> |
| 130 | /// Initializes a new instance of the <see cref="TTLSSocket"/> class. |
| 131 | /// </summary> |
| 132 | /// <param name="host">The host, where the socket should connect to.</param> |
| 133 | /// <param name="port">The port.</param> |
| 134 | /// <param name="certificate">The certificate.</param> |
| 135 | /// <param name="certValidator">User defined cert validator.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 136 | /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param> |
| 137 | public TTLSSocket( |
| 138 | string host, |
| 139 | int port, |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 140 | X509Certificate certificate = null, |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 141 | RemoteCertificateValidationCallback certValidator = null, |
| 142 | LocalCertificateSelectionCallback localCertificateSelectionCallback = null) |
| 143 | : this(host, port, 0, certificate, certValidator, localCertificateSelectionCallback) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 144 | { |
| 145 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 146 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 147 | /// <summary> |
| 148 | /// Initializes a new instance of the <see cref="TTLSSocket"/> class. |
| 149 | /// </summary> |
| 150 | /// <param name="host">The host, where the socket should connect to.</param> |
| 151 | /// <param name="port">The port.</param> |
| 152 | /// <param name="timeout">The timeout.</param> |
| 153 | /// <param name="certificate">The certificate.</param> |
| 154 | /// <param name="certValidator">User defined cert validator.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 155 | /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param> |
| 156 | public TTLSSocket( |
| 157 | string host, |
| 158 | int port, |
| 159 | int timeout, |
| 160 | X509Certificate certificate, |
| 161 | RemoteCertificateValidationCallback certValidator = null, |
| 162 | LocalCertificateSelectionCallback localCertificateSelectionCallback = null) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 163 | { |
| 164 | this.host = host; |
| 165 | this.port = port; |
| 166 | this.timeout = timeout; |
| 167 | this.certificate = certificate; |
| 168 | this.certValidator = certValidator; |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 169 | this.localCertificateSelectionCallback = localCertificateSelectionCallback; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 170 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 171 | InitSocket(); |
| 172 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 173 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 174 | /// <summary> |
| 175 | /// Creates the TcpClient and sets the timeouts |
| 176 | /// </summary> |
| 177 | private void InitSocket() |
| 178 | { |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame] | 179 | this.client = new TcpClient(); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 180 | client.ReceiveTimeout = client.SendTimeout = timeout; |
| 181 | client.Client.NoDelay = true; |
| 182 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 183 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 184 | /// <summary> |
| 185 | /// Sets Send / Recv Timeout for IO |
| 186 | /// </summary> |
| 187 | public int Timeout |
| 188 | { |
| 189 | set |
| 190 | { |
| 191 | this.client.ReceiveTimeout = this.client.SendTimeout = this.timeout = value; |
| 192 | } |
| 193 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 194 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 195 | /// <summary> |
| 196 | /// Gets the TCP client. |
| 197 | /// </summary> |
| 198 | public TcpClient TcpClient |
| 199 | { |
| 200 | get |
| 201 | { |
| 202 | return client; |
| 203 | } |
| 204 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 205 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 206 | /// <summary> |
| 207 | /// Gets the host. |
| 208 | /// </summary> |
| 209 | public string Host |
| 210 | { |
| 211 | get |
| 212 | { |
| 213 | return host; |
| 214 | } |
| 215 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 216 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 217 | /// <summary> |
| 218 | /// Gets the port. |
| 219 | /// </summary> |
| 220 | public int Port |
| 221 | { |
| 222 | get |
| 223 | { |
| 224 | return port; |
| 225 | } |
| 226 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 227 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 228 | /// <summary> |
| 229 | /// Gets a value indicating whether TCP Client is Cpen |
| 230 | /// </summary> |
| 231 | public override bool IsOpen |
| 232 | { |
| 233 | get |
| 234 | { |
| 235 | if (this.client == null) |
| 236 | { |
| 237 | return false; |
| 238 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 239 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 240 | return this.client.Connected; |
| 241 | } |
| 242 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 243 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 244 | /// <summary> |
| 245 | /// Validates the certificates!<br/> |
| 246 | /// </summary> |
| 247 | /// <param name="sender">The sender-object.</param> |
| 248 | /// <param name="certificate">The used certificate.</param> |
| 249 | /// <param name="chain">The certificate chain.</param> |
| 250 | /// <param name="sslPolicyErrors">An enum, which lists all the errors from the .NET certificate check.</param> |
| 251 | /// <returns></returns> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 252 | private bool DefaultCertificateValidator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslValidationErrors) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 253 | { |
| 254 | return (sslValidationErrors == SslPolicyErrors.None); |
| 255 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 256 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 257 | /// <summary> |
| 258 | /// Connects to the host and starts the routine, which sets up the TLS |
| 259 | /// </summary> |
| 260 | public override void Open() |
| 261 | { |
| 262 | if (IsOpen) |
| 263 | { |
| 264 | throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected"); |
| 265 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 266 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 267 | if (String.IsNullOrEmpty(host)) |
| 268 | { |
| 269 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host"); |
| 270 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 271 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 272 | if (port <= 0) |
| 273 | { |
| 274 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port"); |
| 275 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 276 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 277 | if (client == null) |
| 278 | { |
| 279 | InitSocket(); |
| 280 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 281 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 282 | client.Connect(host, port); |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 283 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 284 | setupTLS(); |
| 285 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 286 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 287 | /// <summary> |
| 288 | /// Creates a TLS-stream and lays it over the existing socket |
| 289 | /// </summary> |
| 290 | public void setupTLS() |
| 291 | { |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 292 | RemoteCertificateValidationCallback validator = this.certValidator ?? DefaultCertificateValidator; |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame] | 293 | |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 294 | if( this.localCertificateSelectionCallback != null) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 295 | { |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 296 | this.secureStream = new SslStream( |
| 297 | this.client.GetStream(), |
| 298 | false, |
| 299 | validator, |
| 300 | this.localCertificateSelectionCallback |
| 301 | ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 302 | } |
| 303 | else |
| 304 | { |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 305 | this.secureStream = new SslStream( |
| 306 | this.client.GetStream(), |
| 307 | false, |
| 308 | validator |
| 309 | ); |
| 310 | } |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame] | 311 | |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 312 | try |
| 313 | { |
| 314 | if (isServer) |
| 315 | { |
| 316 | // Server authentication |
| 317 | this.secureStream.AuthenticateAsServer(this.certificate, this.certValidator != null, SslProtocols.Tls, true); |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | // Client authentication |
Jens Geyer | 178b813 | 2015-09-30 23:16:45 +0200 | [diff] [blame] | 322 | X509CertificateCollection certs = certificate != null ? new X509CertificateCollection { certificate } : new X509CertificateCollection(); |
| 323 | this.secureStream.AuthenticateAsClient(host, certs, SslProtocols.Tls, true); |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | catch (Exception) |
| 327 | { |
| 328 | this.Close(); |
| 329 | throw; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 330 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 331 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 332 | inputStream = this.secureStream; |
| 333 | outputStream = this.secureStream; |
| 334 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 335 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 336 | /// <summary> |
| 337 | /// Closes the SSL Socket |
| 338 | /// </summary> |
| 339 | public override void Close() |
| 340 | { |
| 341 | base.Close(); |
| 342 | if (this.client != null) |
| 343 | { |
| 344 | this.client.Close(); |
| 345 | this.client = null; |
| 346 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 347 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 348 | if (this.secureStream != null) |
| 349 | { |
| 350 | this.secureStream.Close(); |
| 351 | this.secureStream = null; |
| 352 | } |
| 353 | } |
| 354 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 355 | } |