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; |
| 23 | using System.Security.Cryptography.X509Certificates; |
| 24 | |
| 25 | namespace Thrift.Transport |
| 26 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 27 | /// <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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 36 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 37 | /// <summary> |
| 38 | /// The port where the socket listen |
| 39 | /// </summary> |
| 40 | private int port = 0; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 41 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 42 | /// <summary> |
| 43 | /// Timeout for the created server socket |
| 44 | /// </summary> |
| 45 | private int clientTimeout = 0; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 46 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 47 | /// <summary> |
| 48 | /// Whether or not to wrap new TSocket connections in buffers |
| 49 | /// </summary> |
| 50 | private bool useBufferedSockets = false; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 51 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 52 | /// <summary> |
| 53 | /// The servercertificate with the private- and public-key |
| 54 | /// </summary> |
| 55 | private X509Certificate serverCertificate; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 56 | |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 57 | /// <summary> |
| 58 | /// The function to validate the client certificate. |
| 59 | /// </summary> |
| 60 | private RemoteCertificateValidationCallback clientCertValidator; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 61 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 62 | /// <summary> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 63 | /// The function to determine which certificate to use. |
| 64 | /// </summary> |
| 65 | private LocalCertificateSelectionCallback localCertificateSelectionCallback; |
| 66 | |
| 67 | /// <summary> |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 68 | /// 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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 76 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 77 | /// <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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 87 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 88 | /// <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 Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 95 | /// <param name="clientCertValidator">The certificate validator.</param> |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 96 | /// <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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 104 | { |
| 105 | if (!certificate.HasPrivateKey) |
| 106 | { |
| 107 | throw new TTransportException(TTransportException.ExceptionType.Unknown, "Your server-certificate needs to have a private key"); |
| 108 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 109 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 110 | this.port = port; |
| 111 | this.serverCertificate = certificate; |
| 112 | this.useBufferedSockets = useBufferedSockets; |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame] | 113 | this.clientCertValidator = clientCertValidator; |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 114 | this.localCertificateSelectionCallback = localCertificateSelectionCallback; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 115 | try |
| 116 | { |
| 117 | // Create server socket |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame] | 118 | server = new TcpListener(System.Net.IPAddress.Any, this.port); |
| 119 | server.Server.NoDelay = true; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 120 | } |
| 121 | catch (Exception) |
| 122 | { |
| 123 | server = null; |
| 124 | throw new TTransportException("Could not create ServerSocket on port " + port + "."); |
| 125 | } |
| 126 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 127 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 128 | /// <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 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 | /// 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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 159 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 160 | try |
| 161 | { |
| 162 | TcpClient client = this.server.AcceptTcpClient(); |
| 163 | client.SendTimeout = client.ReceiveTimeout = this.clientTimeout; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 164 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 165 | //wrap the client in an SSL Socket passing in the SSL cert |
Jens Geyer | 1dc2653 | 2015-04-05 19:13:29 +0200 | [diff] [blame] | 166 | TTLSSocket socket = new TTLSSocket( |
| 167 | client, |
| 168 | this.serverCertificate, |
| 169 | true, |
| 170 | this.clientCertValidator, |
| 171 | this.localCertificateSelectionCallback |
| 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 | socket.setupTLS(); |
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 | if (useBufferedSockets) |
| 177 | { |
| 178 | TBufferedTransport trans = new TBufferedTransport(socket); |
| 179 | return trans; |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | return socket; |
| 184 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 185 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 186 | } |
| 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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 212 | } |