blob: 2e2d299f8d47aab4d258d931b3b01b7edc188350 [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>
63 /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class.
64 /// </summary>
65 /// <param name="port">The port where the server runs.</param>
66 /// <param name="certificate">The certificate object.</param>
67 public TTLSServerSocket(int port, X509Certificate2 certificate)
68 : this(port, 0, certificate)
69 {
70 }
Jens Geyerc1d79432014-04-22 22:52:43 +020071
Jens Geyerd5436f52014-10-03 19:50:38 +020072 /// <summary>
73 /// Initializes a new instance of the <see cref="TTLSServerSocket" /> class.
74 /// </summary>
75 /// <param name="port">The port where the server runs.</param>
76 /// <param name="clientTimeout">Send/receive timeout.</param>
77 /// <param name="certificate">The certificate object.</param>
78 public TTLSServerSocket(int port, int clientTimeout, X509Certificate2 certificate)
79 : this(port, clientTimeout, false, 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="useBufferedSockets">If set to <c>true</c> [use buffered sockets].</param>
89 /// <param name="certificate">The certificate object.</param>
Jens Geyeraf577242015-03-30 23:44:51 +020090 /// <param name="clientCertValidator">The certificate validator.</param>
91 public TTLSServerSocket(int port, int clientTimeout, bool useBufferedSockets, X509Certificate2 certificate, RemoteCertificateValidationCallback clientCertValidator = null)
Jens Geyerd5436f52014-10-03 19:50:38 +020092 {
93 if (!certificate.HasPrivateKey)
94 {
95 throw new TTransportException(TTransportException.ExceptionType.Unknown, "Your server-certificate needs to have a private key");
96 }
Jens Geyerc1d79432014-04-22 22:52:43 +020097
Jens Geyerd5436f52014-10-03 19:50:38 +020098 this.port = port;
99 this.serverCertificate = certificate;
100 this.useBufferedSockets = useBufferedSockets;
Jens Geyeraf577242015-03-30 23:44:51 +0200101 this.clientCertValidator = clientCertValidator;
Jens Geyerd5436f52014-10-03 19:50:38 +0200102 try
103 {
104 // Create server socket
105 server = new TcpListener(System.Net.IPAddress.Any, this.port);
106 server.Server.NoDelay = true;
107 }
108 catch (Exception)
109 {
110 server = null;
111 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
112 }
113 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200114
Jens Geyerd5436f52014-10-03 19:50:38 +0200115 /// <summary>
116 /// Starts the server.
117 /// </summary>
118 public override void Listen()
119 {
120 // Make sure accept is not blocking
121 if (this.server != null)
122 {
123 try
124 {
125 this.server.Start();
126 }
127 catch (SocketException sx)
128 {
129 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
130 }
131 }
132 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200133
Jens Geyerd5436f52014-10-03 19:50:38 +0200134 /// <summary>
135 /// Callback for Accept Implementation
136 /// </summary>
137 /// <returns>
138 /// TTransport-object.
139 /// </returns>
140 protected override TTransport AcceptImpl()
141 {
142 if (this.server == null)
143 {
144 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
145 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200146
Jens Geyerd5436f52014-10-03 19:50:38 +0200147 try
148 {
149 TcpClient client = this.server.AcceptTcpClient();
150 client.SendTimeout = client.ReceiveTimeout = this.clientTimeout;
Jens Geyerc1d79432014-04-22 22:52:43 +0200151
Jens Geyerd5436f52014-10-03 19:50:38 +0200152 //wrap the client in an SSL Socket passing in the SSL cert
Jens Geyeraf577242015-03-30 23:44:51 +0200153 TTLSSocket socket = new TTLSSocket(client, this.serverCertificate, true, this.clientCertValidator);
Jens Geyerc1d79432014-04-22 22:52:43 +0200154
Jens Geyerd5436f52014-10-03 19:50:38 +0200155 socket.setupTLS();
Jens Geyerc1d79432014-04-22 22:52:43 +0200156
Jens Geyerd5436f52014-10-03 19:50:38 +0200157 if (useBufferedSockets)
158 {
159 TBufferedTransport trans = new TBufferedTransport(socket);
160 return trans;
161 }
162 else
163 {
164 return socket;
165 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200166
Jens Geyerd5436f52014-10-03 19:50:38 +0200167 }
168 catch (Exception ex)
169 {
170 throw new TTransportException(ex.ToString());
171 }
172 }
173
174 /// <summary>
175 /// Stops the Server
176 /// </summary>
177 public override void Close()
178 {
179 if (this.server != null)
180 {
181 try
182 {
183 this.server.Stop();
184 }
185 catch (Exception ex)
186 {
187 throw new TTransportException("WARNING: Could not close server socket: " + ex);
188 }
189 this.server = null;
190 }
191 }
192 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200193}