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
42 class SslServerStream : SslStreamBase
44 #region Internal Events
46 internal event CertificateValidationCallback ClientCertValidation;
47 internal event PrivateKeySelectionCallback PrivateKeySelection;
53 public X509Certificate ClientCertificate
57 if (this.context.HandshakeState == HandshakeState.Finished)
59 return this.context.ClientSettings.ClientCertificate;
68 #region Callback Properties
70 public CertificateValidationCallback ClientCertValidationDelegate
72 get { return this.ClientCertValidation; }
73 set { this.ClientCertValidation = value; }
76 public PrivateKeySelectionCallback PrivateKeyCertSelectionDelegate
78 get { return this.PrivateKeySelection; }
79 set { this.PrivateKeySelection = value; }
84 public event CertificateValidationCallback2 ClientCertValidation2;
87 public SslServerStream(
89 X509Certificate serverCertificate) : this(
94 SecurityProtocolType.Default)
98 public SslServerStream(
100 X509Certificate serverCertificate,
101 bool clientCertificateRequired,
102 bool ownsStream): this(
105 clientCertificateRequired,
107 SecurityProtocolType.Default)
111 public SslServerStream(
113 X509Certificate serverCertificate,
114 bool clientCertificateRequired,
115 bool requestClientCertificate,
117 : this (stream, serverCertificate, clientCertificateRequired, requestClientCertificate, ownsStream, SecurityProtocolType.Default)
121 public SslServerStream(
123 X509Certificate serverCertificate,
124 bool clientCertificateRequired,
126 SecurityProtocolType securityProtocolType)
127 : this (stream, serverCertificate, clientCertificateRequired, false, ownsStream, securityProtocolType)
131 public SslServerStream(
133 X509Certificate serverCertificate,
134 bool clientCertificateRequired,
135 bool requestClientCertificate,
137 SecurityProtocolType securityProtocolType)
138 : base(stream, ownsStream)
140 this.context = new ServerContext(
142 securityProtocolType,
144 clientCertificateRequired,
145 requestClientCertificate);
147 this.protocol = new ServerRecordProtocol(innerStream, (ServerContext)this.context);
161 #region IDisposable Methods
163 protected override void Dispose(bool disposing)
165 base.Dispose(disposing);
169 this.ClientCertValidation = null;
170 this.PrivateKeySelection = null;
176 #region Handsake Methods
181 ClientHello -------->
186 <-------- ServerHelloDone
194 Application Data <-------> Application Data
196 Fig. 1 - Message flow for a full handshake
199 internal override IAsyncResult OnBeginNegotiateHandshake(AsyncCallback callback, object state)
201 // Reset the context if needed
202 if (this.context.HandshakeState != HandshakeState.None)
204 this.context.Clear();
207 // Obtain supported cipher suites
208 this.context.SupportedCiphers = CipherSuiteFactory.GetSupportedCiphers(this.context.SecurityProtocol);
210 // Set handshake state
211 this.context.HandshakeState = HandshakeState.Started;
213 // Receive Client Hello message
214 return this.protocol.BeginReceiveRecord(this.innerStream, callback, state);
218 internal override void OnNegotiateHandshakeCallback(IAsyncResult asyncResult)
220 // Receive Client Hello message and ignore it
221 this.protocol.EndReceiveRecord(asyncResult);
223 // If received message is not an ClientHello send a
225 if (this.context.LastHandshakeMsg != HandshakeType.ClientHello)
227 this.protocol.SendAlert(AlertDescription.UnexpectedMessage);
230 // Send ServerHello message
231 this.protocol.SendRecord(HandshakeType.ServerHello);
233 // Send ServerCertificate message
234 this.protocol.SendRecord(HandshakeType.Certificate);
236 // If the negotiated cipher is a KeyEx cipher send ServerKeyExchange
237 if (this.context.Negotiating.Cipher.IsExportable)
239 this.protocol.SendRecord(HandshakeType.ServerKeyExchange);
242 // If the negotiated cipher is a KeyEx cipher or
243 // the client certificate is required send the CertificateRequest message
244 if (this.context.Negotiating.Cipher.IsExportable ||
245 ((ServerContext)this.context).ClientCertificateRequired ||
246 ((ServerContext)this.context).RequestClientCertificate)
248 this.protocol.SendRecord(HandshakeType.CertificateRequest);
251 // Send ServerHelloDone message
252 this.protocol.SendRecord(HandshakeType.ServerHelloDone);
254 // Receive client response, until the Client Finished message
255 // is received. IE can be interrupted at this stage and never
256 // complete the handshake
257 while (this.context.LastHandshakeMsg != HandshakeType.Finished)
259 byte[] record = this.protocol.ReceiveRecord(this.innerStream);
260 if ((record == null) || (record.Length == 0))
262 throw new TlsException(
263 AlertDescription.HandshakeFailiure,
264 "The client stopped the handshake.");
268 // Send ChangeCipherSpec and ServerFinished messages
269 this.protocol.SendChangeCipherSpec();
270 this.protocol.SendRecord (HandshakeType.Finished);
272 // The handshake is finished
273 this.context.HandshakeState = HandshakeState.Finished;
275 // Reset Handshake messages information
276 this.context.HandshakeMessages.Reset ();
279 this.context.ClearKeyInfo();
284 #region Event Methods
286 internal override X509Certificate OnLocalCertificateSelection(X509CertificateCollection clientCertificates, X509Certificate serverCertificate, string targetHost, X509CertificateCollection serverRequestedCertificates)
288 throw new NotSupportedException();
291 internal override bool OnRemoteCertificateValidation(X509Certificate certificate, int[] errors)
293 if (this.ClientCertValidation != null)
295 return this.ClientCertValidation(certificate, errors);
298 return (errors != null && errors.Length == 0);
301 internal override bool HaveRemoteValidation2Callback {
302 get { return ClientCertValidation2 != null; }
305 internal override ValidationResult OnRemoteCertificateValidation2 (Mono.Security.X509.X509CertificateCollection collection)
307 CertificateValidationCallback2 cb = ClientCertValidation2;
309 return cb (collection);
313 internal bool RaiseClientCertificateValidation(
314 X509Certificate certificate,
315 int[] certificateErrors)
317 return base.RaiseRemoteCertificateValidation(certificate, certificateErrors);
320 internal override AsymmetricAlgorithm OnLocalPrivateKeySelection(X509Certificate certificate, string targetHost)
322 if (this.PrivateKeySelection != null)
324 return this.PrivateKeySelection(certificate, targetHost);
330 internal AsymmetricAlgorithm RaisePrivateKeySelection(
331 X509Certificate certificate,
334 return base.RaiseLocalPrivateKeySelection(certificate, targetHost);