blob: 631a593a9800d31dde3f49d8c83fca6d48b63a5c [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;
23using System.Security.Cryptography.X509Certificates;
24
25namespace Thrift.Transport
26{
Jens Geyerd5436f52014-10-03 19:50:38 +020027 /// <summary>
28 /// SSL Server Socket Wrapper Class
29 /// </summary>
30 public class TTLSServerSocket : TServerTransport
31 {
32 /// <summary>
33 /// Underlying tcp server
34 /// </summary>
35 private TcpListener server = null;
Jens Geyerc1d79432014-04-22 22:52:43 +020036
Jens Geyerd5436f52014-10-03 19:50:38 +020037 /// <summary>
38 /// The port where the socket listen
39 /// </summary>
40 private int port = 0;
Jens Geyerc1d79432014-04-22 22:52:43 +020041
Jens Geyerd5436f52014-10-03 19:50:38 +020042 /// <summary>
43 /// Timeout for the created server socket
44 /// </summary>
45 private int clientTimeout = 0;
Jens Geyerc1d79432014-04-22 22:52:43 +020046
Jens Geyerd5436f52014-10-03 19:50:38 +020047 /// <summary>
48 /// Whether or not to wrap new TSocket connections in buffers
49 /// </summary>
50 private bool useBufferedSockets = false;
Jens Geyerc1d79432014-04-22 22:52:43 +020051
Jens Geyerd5436f52014-10-03 19:50:38 +020052 /// <summary>
53 /// The servercertificate with the private- and public-key
54 /// </summary>
55 private X509Certificate serverCertificate;
Jens Geyerc1d79432014-04-22 22:52:43 +020056
Jens Geyeraf577242015-03-30 23:44:51 +020057 /// <summary>
58 /// The function to validate the client certificate.
59 /// </summary>
60 private RemoteCertificateValidationCallback clientCertValidator;
Jens Geyerc1d79432014-04-22 22:52:43 +020061
Jens Geyerd5436f52014-10-03 19:50:38 +020062 /// <summary>
Jens Geyer1dc26532015-04-05 19:13:29 +020063 /// The function to determine which certificate to use.
64 /// </summary>
65 private LocalCertificateSelectionCallback localCertificateSelectionCallback;
66
67 /// <summary>
Jens Geyerd5436f52014-10-03 19:50:38 +020068 /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class.
69 /// </summary>
70 /// <param name="port">The port where the server runs.</param>
71 /// <param name="certificate">The certificate object.</param>
72 public TTLSServerSocket(int port, X509Certificate2 certificate)
73 : this(port, 0, certificate)
74 {
75 }
Jens Geyerc1d79432014-04-22 22:52:43 +020076
Jens Geyerd5436f52014-10-03 19:50:38 +020077 /// <summary>
78 /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class.
79 /// </summary>
80 /// <param name="port">The port where the server runs.</param>
81 /// <param name="clientTimeout">Send/receive timeout.</param>
82 /// <param name="certificate">The certificate object.</param>
83 public TTLSServerSocket(int port, int clientTimeout, X509Certificate2 certificate)
84 : this(port, clientTimeout, false, certificate)
85 {
86 }
Jens Geyerc1d79432014-04-22 22:52:43 +020087
Jens Geyerd5436f52014-10-03 19:50:38 +020088 /// <summary>
89 /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class.
90 /// </summary>
91 /// <param name="port">The port where the server runs.</param>
92 /// <param name="clientTimeout">Send/receive timeout.</param>
93 /// <param name="useBufferedSockets">If set to <c>true</c> [use buffered sockets].</param>
94 /// <param name="certificate">The certificate object.</param>
Jens Geyeraf577242015-03-30 23:44:51 +020095 /// <param name="clientCertValidator">The certificate validator.</param>
Jens Geyer1dc26532015-04-05 19:13:29 +020096 /// <param name="localCertificateSelectionCallback">The callback to select which certificate to use.</param>
97 public TTLSServerSocket(
98 int port,
99 int clientTimeout,
100 bool useBufferedSockets,
101 X509Certificate2 certificate,
102 RemoteCertificateValidationCallback clientCertValidator = null,
103 LocalCertificateSelectionCallback localCertificateSelectionCallback = null)
Jens Geyerd5436f52014-10-03 19:50:38 +0200104 {
105 if (!certificate.HasPrivateKey)
106 {
107 throw new TTransportException(TTransportException.ExceptionType.Unknown, "Your server-certificate needs to have a private key");
108 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200109
Jens Geyerd5436f52014-10-03 19:50:38 +0200110 this.port = port;
111 this.serverCertificate = certificate;
112 this.useBufferedSockets = useBufferedSockets;
Jens Geyeraf577242015-03-30 23:44:51 +0200113 this.clientCertValidator = clientCertValidator;
Jens Geyer1dc26532015-04-05 19:13:29 +0200114 this.localCertificateSelectionCallback = localCertificateSelectionCallback;
Jens Geyerd5436f52014-10-03 19:50:38 +0200115 try
116 {
117 // Create server socket
Jens Geyer95717c92015-04-23 22:48:13 +0200118 server = new TcpListener(System.Net.IPAddress.Any, this.port);
119 server.Server.NoDelay = true;
Jens Geyerd5436f52014-10-03 19:50:38 +0200120 }
121 catch (Exception)
122 {
123 server = null;
124 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
125 }
126 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200127
Jens Geyerd5436f52014-10-03 19:50:38 +0200128 /// <summary>
129 /// Starts the server.
130 /// </summary>
131 public override void Listen()
132 {
133 // Make sure accept is not blocking
134 if (this.server != null)
135 {
136 try
137 {
138 this.server.Start();
139 }
140 catch (SocketException sx)
141 {
142 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
143 }
144 }
145 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200146
Jens Geyerd5436f52014-10-03 19:50:38 +0200147 /// <summary>
148 /// Callback for Accept Implementation
149 /// </summary>
150 /// <returns>
151 /// TTransport-object.
152 /// </returns>
153 protected override TTransport AcceptImpl()
154 {
155 if (this.server == null)
156 {
157 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
158 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200159
Jens Geyerd5436f52014-10-03 19:50:38 +0200160 try
161 {
162 TcpClient client = this.server.AcceptTcpClient();
163 client.SendTimeout = client.ReceiveTimeout = this.clientTimeout;
Jens Geyerc1d79432014-04-22 22:52:43 +0200164
Jens Geyerd5436f52014-10-03 19:50:38 +0200165 //wrap the client in an SSL Socket passing in the SSL cert
Jens Geyer1dc26532015-04-05 19:13:29 +0200166 TTLSSocket socket = new TTLSSocket(
167 client,
168 this.serverCertificate,
169 true,
170 this.clientCertValidator,
171 this.localCertificateSelectionCallback
172 );
Jens Geyerc1d79432014-04-22 22:52:43 +0200173
Jens Geyerd5436f52014-10-03 19:50:38 +0200174 socket.setupTLS();
Jens Geyerc1d79432014-04-22 22:52:43 +0200175
Jens Geyerd5436f52014-10-03 19:50:38 +0200176 if (useBufferedSockets)
177 {
178 TBufferedTransport trans = new TBufferedTransport(socket);
179 return trans;
180 }
181 else
182 {
183 return socket;
184 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200185
Jens Geyerd5436f52014-10-03 19:50:38 +0200186 }
187 catch (Exception ex)
188 {
189 throw new TTransportException(ex.ToString());
190 }
191 }
192
193 /// <summary>
194 /// Stops the Server
195 /// </summary>
196 public override void Close()
197 {
198 if (this.server != null)
199 {
200 try
201 {
202 this.server.Stop();
203 }
204 catch (Exception ex)
205 {
206 throw new TTransportException("WARNING: Could not close server socket: " + ex);
207 }
208 this.server = null;
209 }
210 }
211 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200212}