1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Collections;
29 using System.Net.Sockets;
30 using System.Security.Cryptography;
31 using System.Security.Cryptography.X509Certificates;
33 using Mono.Security.Protocol.Tls.Handshake;
35 namespace Mono.Security.Protocol.Tls
37 public class SslServerStream : SslStreamBase
39 #region Internal Events
41 internal event CertificateValidationCallback ClientCertValidation;
42 internal event PrivateKeySelectionCallback PrivateKeySelection;
48 public X509Certificate ClientCertificate
52 if (this.context.HandshakeState == HandshakeState.Finished)
54 return this.context.ClientSettings.ClientCertificate;
63 #region Callback Properties
65 public CertificateValidationCallback ClientCertValidationDelegate
67 get { return this.ClientCertValidation; }
68 set { this.ClientCertValidation = value; }
71 public PrivateKeySelectionCallback PrivateKeyCertSelectionDelegate
73 get { return this.PrivateKeySelection; }
74 set { this.PrivateKeySelection = value; }
81 public SslServerStream(
83 X509Certificate serverCertificate) : this(
88 SecurityProtocolType.Default)
92 public SslServerStream(
94 X509Certificate serverCertificate,
95 bool clientCertificateRequired,
96 bool ownsStream): this(
99 clientCertificateRequired,
101 SecurityProtocolType.Default)
105 public SslServerStream(
107 X509Certificate serverCertificate,
108 bool clientCertificateRequired,
110 SecurityProtocolType securityProtocolType)
111 : base(stream, ownsStream)
113 this.context = new ServerContext(
115 securityProtocolType,
117 clientCertificateRequired);
119 this.protocol = new ServerRecordProtocol(innerStream, (ServerContext)this.context);
133 #region IDisposable Methods
135 protected override void Dispose(bool disposing)
137 base.Dispose(disposing);
141 this.ClientCertValidation = null;
142 this.PrivateKeySelection = null;
148 #region Handsake Methods
153 ClientHello -------->
158 <-------- ServerHelloDone
166 Application Data <-------> Application Data
168 Fig. 1 - Message flow for a full handshake
171 internal override IAsyncResult OnBeginNegotiateHandshake(AsyncCallback callback, object state)
173 // Reset the context if needed
174 if (this.context.HandshakeState != HandshakeState.None)
176 this.context.Clear();
179 // Obtain supported cipher suites
180 this.context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(this.context.SecurityProtocol);
182 // Set handshake state
183 this.context.HandshakeState = HandshakeState.Started;
185 // Receive Client Hello message
186 return this.protocol.BeginReceiveRecord(this.innerStream, callback, state);
190 internal override void OnNegotiateHandshakeCallback(IAsyncResult asyncResult)
192 // Receive Client Hello message and ignore it
193 this.protocol.EndReceiveRecord(asyncResult);
195 // If received message is not an ClientHello send a
197 if (this.context.LastHandshakeMsg != HandshakeType.ClientHello)
199 this.protocol.SendAlert(AlertDescription.UnexpectedMessage);
202 // Send ServerHello message
203 this.protocol.SendRecord(HandshakeType.ServerHello);
205 // Send ServerCertificate message
206 this.protocol.SendRecord(HandshakeType.Certificate);
208 // If the negotiated cipher is a KeyEx cipher send ServerKeyExchange
209 if (this.context.Cipher.ExchangeAlgorithmType == ExchangeAlgorithmType.RsaKeyX)
211 this.protocol.SendRecord(HandshakeType.ServerKeyExchange);
214 bool certRequested = false;
216 // If the negotiated cipher is a KeyEx cipher or
217 // the client certificate is required send the CertificateRequest message
218 if (this.context.Cipher.ExchangeAlgorithmType == ExchangeAlgorithmType.RsaKeyX ||
219 ((ServerContext)this.context).ClientCertificateRequired)
221 this.protocol.SendRecord(HandshakeType.CertificateRequest);
222 certRequested = true;
225 // Send ServerHelloDone message
226 this.protocol.SendRecord(HandshakeType.ServerHelloDone);
228 // Receive client response, until the Client Finished message
229 // is received. IE can be interrupted at this stage and never
230 // complete the handshake
231 DateTime complete = DateTime.Now.AddSeconds (10);
232 while (this.context.LastHandshakeMsg != HandshakeType.Finished)
234 byte[] record = this.protocol.ReceiveRecord(this.innerStream);
235 if ((record == null) || (record.Length == 0))
237 throw new TlsException(
238 AlertDescription.HandshakeFailiure,
239 "The client stopped the handshake.");
243 if (certRequested && (this.context.ClientSettings.ClientCertificate == null))
245 // we asked for a certificate but didn't receive one
246 // e.g. wget for SSL3
247 if (!RaiseClientCertificateValidation(null, new int[0]))
249 throw new TlsException(
250 AlertDescription.BadCertificate,
251 "No certificate received from client.");
255 // Send ChangeCipherSpec and ServerFinished messages
256 this.protocol.SendChangeCipherSpec();
258 // The handshake is finished
259 this.context.HandshakeState = HandshakeState.Finished;
262 this.context.ClearKeyInfo();
267 #region Event Methods
269 internal override X509Certificate OnLocalCertificateSelection(X509CertificateCollection clientCertificates, X509Certificate serverCertificate, string targetHost, X509CertificateCollection serverRequestedCertificates)
271 throw new NotSupportedException();
274 internal override bool OnRemoteCertificateValidation(X509Certificate certificate, int[] errors)
276 if (this.ClientCertValidation != null)
278 return this.ClientCertValidation(certificate, errors);
281 return (errors != null && errors.Length == 0);
284 internal bool RaiseClientCertificateValidation(
285 X509Certificate certificate,
286 int[] certificateErrors)
288 return base.RaiseRemoteCertificateValidation(certificate, certificateErrors);
291 internal override AsymmetricAlgorithm OnLocalPrivateKeySelection(X509Certificate certificate, string targetHost)
293 if (this.PrivateKeySelection != null)
295 return this.PrivateKeySelection(certificate, targetHost);
301 internal AsymmetricAlgorithm RaisePrivateKeySelection(
302 X509Certificate certificate,
305 return base.RaiseLocalPrivateKeySelection(certificate, targetHost);