blob: 99f21ca0c8f5deee3210d64af20180b0f2128ea2 [file] [log] [blame]
Jens Geyerc1d79432014-04-22 22:52:43 +02001/**
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
20using System;
Jens Geyeraf577242015-03-30 23:44:51 +020021using System.Net.Security;
Jens Geyerc1d79432014-04-22 22:52:43 +020022using System.Net.Sockets;
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +090023using System.Security.Authentication;
Jens Geyerc1d79432014-04-22 22:52:43 +020024using System.Security.Cryptography.X509Certificates;
25
26namespace Thrift.Transport
27{
Jens Geyerd5436f52014-10-03 19:50:38 +020028 /// <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 Geyerc1d79432014-04-22 22:52:43 +020037
Jens Geyerd5436f52014-10-03 19:50:38 +020038 /// <summary>
39 /// The port where the socket listen
40 /// </summary>
41 private int port = 0;
Jens Geyerc1d79432014-04-22 22:52:43 +020042
Jens Geyerd5436f52014-10-03 19:50:38 +020043 /// <summary>
44 /// Timeout for the created server socket
45 /// </summary>
46 private int clientTimeout = 0;
Jens Geyerc1d79432014-04-22 22:52:43 +020047
Jens Geyerd5436f52014-10-03 19:50:38 +020048 /// <summary>
49 /// Whether or not to wrap new TSocket connections in buffers
50 /// </summary>
51 private bool useBufferedSockets = false;
Jens Geyerc1d79432014-04-22 22:52:43 +020052
Jens Geyerd5436f52014-10-03 19:50:38 +020053 /// <summary>
54 /// The servercertificate with the private- and public-key
55 /// </summary>
56 private X509Certificate serverCertificate;
Jens Geyerc1d79432014-04-22 22:52:43 +020057
Jens Geyeraf577242015-03-30 23:44:51 +020058 /// <summary>
59 /// The function to validate the client certificate.
60 /// </summary>
61 private RemoteCertificateValidationCallback clientCertValidator;
Jens Geyerc1d79432014-04-22 22:52:43 +020062
Jens Geyerd5436f52014-10-03 19:50:38 +020063 /// <summary>
Jens Geyer1dc26532015-04-05 19:13:29 +020064 /// The function to determine which certificate to use.
65 /// </summary>
66 private LocalCertificateSelectionCallback localCertificateSelectionCallback;
67
68 /// <summary>
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +090069 /// The SslProtocols value that represents the protocol used for authentication.
70 /// </summary>
71 private readonly SslProtocols sslProtocols;
72
73 /// <summary>
Jens Geyerd5436f52014-10-03 19:50:38 +020074 /// 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 Geyerc1d79432014-04-22 22:52:43 +020082
Jens Geyerd5436f52014-10-03 19:50:38 +020083 /// <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 Geyerc1d79432014-04-22 22:52:43 +020093
Jens Geyerd5436f52014-10-03 19:50:38 +020094 /// <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 Geyeraf577242015-03-30 23:44:51 +0200101 /// <param name="clientCertValidator">The certificate validator.</param>
Jens Geyer1dc26532015-04-05 19:13:29 +0200102 /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param>
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900103 /// <param name="sslProtocols">The SslProtocols value that represents the protocol used for authentication.</param>
Jens Geyer1dc26532015-04-05 19:13:29 +0200104 public TTLSServerSocket(
105 int port,
106 int clientTimeout,
107 bool useBufferedSockets,
108 X509Certificate2 certificate,
109 RemoteCertificateValidationCallback clientCertValidator = null,
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900110 LocalCertificateSelectionCallback localCertificateSelectionCallback = null,
111 // TODO: Enable Tls1 and Tls2 (TLS 1.1 and 1.2) by default once we start using .NET 4.5+.
112 SslProtocols sslProtocols = SslProtocols.Tls)
Jens Geyerd5436f52014-10-03 19:50:38 +0200113 {
114 if (!certificate.HasPrivateKey)
115 {
116 throw new TTransportException(TTransportException.ExceptionType.Unknown, "Your server-certificate needs to have a private key");
117 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200118
Jens Geyerd5436f52014-10-03 19:50:38 +0200119 this.port = port;
120 this.serverCertificate = certificate;
121 this.useBufferedSockets = useBufferedSockets;
Jens Geyeraf577242015-03-30 23:44:51 +0200122 this.clientCertValidator = clientCertValidator;
Jens Geyer1dc26532015-04-05 19:13:29 +0200123 this.localCertificateSelectionCallback = localCertificateSelectionCallback;
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900124 this.sslProtocols = sslProtocols;
Jens Geyerd5436f52014-10-03 19:50:38 +0200125 try
126 {
127 // Create server socket
Jens Geyer95717c92015-04-23 22:48:13 +0200128 server = new TcpListener(System.Net.IPAddress.Any, this.port);
129 server.Server.NoDelay = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200130 }
131 catch (Exception)
132 {
133 server = null;
134 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
135 }
136 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200137
Jens Geyerd5436f52014-10-03 19:50:38 +0200138 /// <summary>
139 /// Starts the server.
140 /// </summary>
141 public override void Listen()
142 {
143 // Make sure accept is not blocking
144 if (this.server != null)
145 {
146 try
147 {
148 this.server.Start();
149 }
150 catch (SocketException sx)
151 {
152 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
153 }
154 }
155 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200156
Jens Geyerd5436f52014-10-03 19:50:38 +0200157 /// <summary>
158 /// Callback for Accept Implementation
159 /// </summary>
160 /// <returns>
161 /// TTransport-object.
162 /// </returns>
163 protected override TTransport AcceptImpl()
164 {
165 if (this.server == null)
166 {
167 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
168 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200169
Jens Geyerd5436f52014-10-03 19:50:38 +0200170 try
171 {
172 TcpClient client = this.server.AcceptTcpClient();
173 client.SendTimeout = client.ReceiveTimeout = this.clientTimeout;
Jens Geyerc1d79432014-04-22 22:52:43 +0200174
Jens Geyerd5436f52014-10-03 19:50:38 +0200175 //wrap the client in an SSL Socket passing in the SSL cert
Jens Geyer1dc26532015-04-05 19:13:29 +0200176 TTLSSocket socket = new TTLSSocket(
177 client,
178 this.serverCertificate,
179 true,
180 this.clientCertValidator,
Nobuaki Sukegawa474ddbd2016-02-17 23:44:27 +0900181 this.localCertificateSelectionCallback,
182 this.sslProtocols);
Jens Geyerc1d79432014-04-22 22:52:43 +0200183
Jens Geyerd5436f52014-10-03 19:50:38 +0200184 socket.setupTLS();
Jens Geyerc1d79432014-04-22 22:52:43 +0200185
Jens Geyerd5436f52014-10-03 19:50:38 +0200186 if (useBufferedSockets)
187 {
188 TBufferedTransport trans = new TBufferedTransport(socket);
189 return trans;
190 }
191 else
192 {
193 return socket;
194 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200195
Jens Geyerd5436f52014-10-03 19:50:38 +0200196 }
197 catch (Exception ex)
198 {
199 throw new TTransportException(ex.ToString());
200 }
201 }
202
203 /// <summary>
204 /// Stops the Server
205 /// </summary>
206 public override void Close()
207 {
208 if (this.server != null)
209 {
210 try
211 {
212 this.server.Stop();
213 }
214 catch (Exception ex)
215 {
216 throw new TTransportException("WARNING: Could not close server socket: " + ex);
217 }
218 this.server = null;
219 }
220 }
221 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200222}