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 BeginNegotiateHandshake(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 (true, 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 EndNegotiateHandshake(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 client certificate is required send the CertificateRequest message
237 if (((ServerContext)this.context).ClientCertificateRequired ||
238 ((ServerContext)this.context).RequestClientCertificate)
240 this.protocol.SendRecord(HandshakeType.CertificateRequest);
243 // Send ServerHelloDone message
244 this.protocol.SendRecord(HandshakeType.ServerHelloDone);
246 // Receive client response, until the Client Finished message
247 // is received. IE can be interrupted at this stage and never
248 // complete the handshake
249 while (this.context.LastHandshakeMsg != HandshakeType.Finished)
251 byte[] record = this.protocol.ReceiveRecord(this.innerStream);
252 if ((record == null) || (record.Length == 0))
254 throw new TlsException(
255 AlertDescription.HandshakeFailiure,
256 "The client stopped the handshake.");
260 // Send ChangeCipherSpec and ServerFinished messages
261 this.protocol.SendChangeCipherSpec();
262 this.protocol.SendRecord (HandshakeType.Finished);
264 // The handshake is finished
265 this.context.HandshakeState = HandshakeState.Finished;
267 // Reset Handshake messages information
268 this.context.HandshakeMessages.Reset ();
271 this.context.ClearKeyInfo();
276 #region Event Methods
278 internal override X509Certificate OnLocalCertificateSelection(X509CertificateCollection clientCertificates, X509Certificate serverCertificate, string targetHost, X509CertificateCollection serverRequestedCertificates)
280 throw new NotSupportedException();
283 internal override bool OnRemoteCertificateValidation(X509Certificate certificate, int[] errors)
285 if (this.ClientCertValidation != null)
287 return this.ClientCertValidation(certificate, errors);
290 return (errors != null && errors.Length == 0);
293 internal override bool HaveRemoteValidation2Callback {
294 get { return ClientCertValidation2 != null; }
297 internal override ValidationResult OnRemoteCertificateValidation2 (Mono.Security.X509.X509CertificateCollection collection)
299 CertificateValidationCallback2 cb = ClientCertValidation2;
301 return cb (collection);
305 internal bool RaiseClientCertificateValidation(
306 X509Certificate certificate,
307 int[] certificateErrors)
309 return base.RaiseRemoteCertificateValidation(certificate, certificateErrors);
312 internal override AsymmetricAlgorithm OnLocalPrivateKeySelection(X509Certificate certificate, string targetHost)
314 if (this.PrivateKeySelection != null)
316 return this.PrivateKeySelection(certificate, targetHost);
322 internal AsymmetricAlgorithm RaisePrivateKeySelection(
323 X509Certificate certificate,
326 return base.RaiseLocalPrivateKeySelection(certificate, targetHost);