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> |
| 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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 71 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 72 | /// <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 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="useBufferedSockets">If set to <c>true</c> [use buffered sockets].</param> |
| 89 | /// <param name="certificate">The certificate object.</param> |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame^] | 90 | /// <param name="clientCertValidator">The certificate validator.</param> |
| 91 | public TTLSServerSocket(int port, int clientTimeout, bool useBufferedSockets, X509Certificate2 certificate, RemoteCertificateValidationCallback clientCertValidator = null) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 92 | { |
| 93 | if (!certificate.HasPrivateKey) |
| 94 | { |
| 95 | throw new TTransportException(TTransportException.ExceptionType.Unknown, "Your server-certificate needs to have a private key"); |
| 96 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 97 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 98 | this.port = port; |
| 99 | this.serverCertificate = certificate; |
| 100 | this.useBufferedSockets = useBufferedSockets; |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame^] | 101 | this.clientCertValidator = clientCertValidator; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 102 | 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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 114 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 115 | /// <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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 133 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 134 | /// <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 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 | try |
| 148 | { |
| 149 | TcpClient client = this.server.AcceptTcpClient(); |
| 150 | client.SendTimeout = client.ReceiveTimeout = this.clientTimeout; |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 151 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 152 | //wrap the client in an SSL Socket passing in the SSL cert |
Jens Geyer | af57724 | 2015-03-30 23:44:51 +0200 | [diff] [blame^] | 153 | TTLSSocket socket = new TTLSSocket(client, this.serverCertificate, true, this.clientCertValidator); |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 154 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 155 | socket.setupTLS(); |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 156 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 157 | if (useBufferedSockets) |
| 158 | { |
| 159 | TBufferedTransport trans = new TBufferedTransport(socket); |
| 160 | return trans; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | return socket; |
| 165 | } |
Jens Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 166 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 167 | } |
| 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 Geyer | c1d7943 | 2014-04-22 22:52:43 +0200 | [diff] [blame] | 193 | } |