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; |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 21 | using System.Net.Security; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 22 | using System.Net.Sockets; |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 23 | using System.Security.Authentication; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 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 Server Socket Wrapper Class |
| 30 | /// </summary> |
| 31 | public class TTLSServerSocket : TServerTransport |
| 32 | { |
| 33 | /// <summary> |
| 34 | /// Underlying tcp server |
| 35 | /// </summary> |
| 36 | private TcpListener server = null; |
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 port where the socket listen |
| 40 | /// </summary> |
| 41 | private int port = 0; |
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 | /// Timeout for the created server socket |
| 45 | /// </summary> |
Nobuaki Sukegawa | 900569c | 2016-09-04 17:46:53 +0900 | [diff] [blame] | 46 | private readonly int clientTimeout; |
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 | /// Whether or not to wrap new TSocket connections in buffers |
| 50 | /// </summary> |
| 51 | private bool useBufferedSockets = false; |
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 | /// The servercertificate with the private- and public-key |
| 55 | /// </summary> |
| 56 | private X509Certificate serverCertificate; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 57 | |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 58 | /// <summary> |
| 59 | /// The function to validate the client certificate. |
| 60 | /// </summary> |
| 61 | private RemoteCertificateValidationCallback clientCertValidator; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 62 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 63 | /// <summary> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 64 | /// The function to determine which certificate to use. |
| 65 | /// </summary> |
| 66 | private LocalCertificateSelectionCallback localCertificateSelectionCallback; |
| 67 | |
| 68 | /// <summary> |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 69 | /// The SslProtocols value that represents the protocol used for authentication. |
| 70 | /// </summary> |
| 71 | private readonly SslProtocols sslProtocols; |
| 72 | |
| 73 | /// <summary> |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 74 | /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class. |
| 75 | /// </summary> |
| 76 | /// <param name="port">The port where the server runs.</param> |
| 77 | /// <param name="certificate">The certificate object.</param> |
| 78 | public TTLSServerSocket(int port, X509Certificate2 certificate) |
| 79 | : this(port, 0, certificate) |
| 80 | { |
| 81 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 82 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 83 | /// <summary> |
| 84 | /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class. |
| 85 | /// </summary> |
| 86 | /// <param name="port">The port where the server runs.</param> |
| 87 | /// <param name="clientTimeout">Send/receive timeout.</param> |
| 88 | /// <param name="certificate">The certificate object.</param> |
| 89 | public TTLSServerSocket(int port, int clientTimeout, X509Certificate2 certificate) |
| 90 | : this(port, clientTimeout, false, certificate) |
| 91 | { |
| 92 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 93 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 94 | /// <summary> |
| 95 | /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class. |
| 96 | /// </summary> |
| 97 | /// <param name="port">The port where the server runs.</param> |
| 98 | /// <param name="clientTimeout">Send/receive timeout.</param> |
| 99 | /// <param name="useBufferedSockets">If set to <c>true</c> [use buffered sockets].</param> |
| 100 | /// <param name="certificate">The certificate object.</param> |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 101 | /// <param name="clientCertValidator">The certificate validator.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 102 | /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param> |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 103 | /// <param name="sslProtocols">The SslProtocols value that represents the protocol used for authentication.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 104 | public TTLSServerSocket( |
| 105 | int port, |
| 106 | int clientTimeout, |
| 107 | bool useBufferedSockets, |
| 108 | X509Certificate2 certificate, |
| 109 | RemoteCertificateValidationCallback clientCertValidator = null, |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 110 | LocalCertificateSelectionCallback localCertificateSelectionCallback = null, |
James E. King, III | 0619087 | 2017-02-20 08:52:11 -0500 | [diff] [blame^] | 111 | // TODO: Enable Tls11 and Tls12 (TLS 1.1 and 1.2) by default once we start using .NET 4.5+. |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 112 | SslProtocols sslProtocols = SslProtocols.Tls) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 113 | { |
| 114 | if (!certificate.HasPrivateKey) |
| 115 | { |
| 116 | throw new TTransportException(TTransportException.ExceptionType.Unknown, "Your server-certificate needs to have a private key"); |
| 117 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 118 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 119 | this.port = port; |
Nobuaki Sukegawa | 900569c | 2016-09-04 17:46:53 +0900 | [diff] [blame] | 120 | this.clientTimeout = clientTimeout; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 121 | this.serverCertificate = certificate; |
| 122 | this.useBufferedSockets = useBufferedSockets; |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 123 | this.clientCertValidator = clientCertValidator; |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 124 | this.localCertificateSelectionCallback = localCertificateSelectionCallback; |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 125 | this.sslProtocols = sslProtocols; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 126 | try |
| 127 | { |
| 128 | // Create server socket |
James E. King, III | 0619087 | 2017-02-20 08:52:11 -0500 | [diff] [blame^] | 129 | this.server = TSocketVersionizer.CreateTcpListener(port); |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame] | 130 | this.server.Server.NoDelay = true; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 131 | } |
| 132 | catch (Exception) |
| 133 | { |
| 134 | server = null; |
| 135 | throw new TTransportException("Could not create ServerSocket on port " + port + "."); |
| 136 | } |
| 137 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 138 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 139 | /// <summary> |
| 140 | /// Starts the server. |
| 141 | /// </summary> |
| 142 | public override void Listen() |
| 143 | { |
| 144 | // Make sure accept is not blocking |
| 145 | if (this.server != null) |
| 146 | { |
| 147 | try |
| 148 | { |
| 149 | this.server.Start(); |
| 150 | } |
| 151 | catch (SocketException sx) |
| 152 | { |
| 153 | throw new TTransportException("Could not accept on listening socket: " + sx.Message); |
| 154 | } |
| 155 | } |
| 156 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 157 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 158 | /// <summary> |
| 159 | /// Callback for Accept Implementation |
| 160 | /// </summary> |
| 161 | /// <returns> |
| 162 | /// TTransport-object. |
| 163 | /// </returns> |
| 164 | protected override TTransport AcceptImpl() |
| 165 | { |
| 166 | if (this.server == null) |
| 167 | { |
| 168 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket."); |
| 169 | } |
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 | try |
| 172 | { |
| 173 | TcpClient client = this.server.AcceptTcpClient(); |
| 174 | client.SendTimeout = client.ReceiveTimeout = this.clientTimeout; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 175 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 176 | //wrap the client in an SSL Socket passing in the SSL cert |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 177 | TTLSSocket socket = new TTLSSocket( |
| 178 | client, |
| 179 | this.serverCertificate, |
| 180 | true, |
| 181 | this.clientCertValidator, |
Nobuaki Sukegawa | 474ddbd | 2016-02-17 23:44:27 +0900 | [diff] [blame] | 182 | this.localCertificateSelectionCallback, |
| 183 | this.sslProtocols); |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 184 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 185 | socket.setupTLS(); |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 186 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 187 | if (useBufferedSockets) |
| 188 | { |
| 189 | TBufferedTransport trans = new TBufferedTransport(socket); |
| 190 | return trans; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | return socket; |
| 195 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 196 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 197 | } |
| 198 | catch (Exception ex) |
| 199 | { |
| 200 | throw new TTransportException(ex.ToString()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// <summary> |
| 205 | /// Stops the Server |
| 206 | /// </summary> |
| 207 | public override void Close() |
| 208 | { |
| 209 | if (this.server != null) |
| 210 | { |
| 211 | try |
| 212 | { |
| 213 | this.server.Stop(); |
| 214 | } |
| 215 | catch (Exception ex) |
| 216 | { |
| 217 | throw new TTransportException("WARNING: Could not close server socket: " + ex); |
| 218 | } |
| 219 | this.server = null; |
| 220 | } |
| 221 | } |
| 222 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 223 | } |