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
.Negotiating
.Cipher
.IsExportable
)
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
.Negotiating
.Cipher
.IsExportable
||
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 while (this.context
.LastHandshakeMsg
!= HandshakeType
.Finished
)
233 byte[] record
= this.protocol
.ReceiveRecord(this.innerStream
);
234 if ((record
== null) || (record
.Length
== 0))
236 throw new TlsException(
237 AlertDescription
.HandshakeFailiure
,
238 "The client stopped the handshake.");
242 if (certRequested
&& (this.context
.ClientSettings
.ClientCertificate
== null))
244 // we asked for a certificate but didn't receive one
245 // e.g. wget for SSL3
246 if (!RaiseClientCertificateValidation(null, new int[0]))
248 throw new TlsException(
249 AlertDescription
.BadCertificate
,
250 "No certificate received from client.");
254 // Send ChangeCipherSpec and ServerFinished messages
255 this.protocol
.SendChangeCipherSpec();
256 this.protocol
.SendRecord (HandshakeType
.Finished
);
258 // The handshake is finished
259 this.context
.HandshakeState
= HandshakeState
.Finished
;
261 // Reset Handshake messages information
262 this.context
.HandshakeMessages
.Reset ();
265 this.context
.ClearKeyInfo();
270 #region Event Methods
272 internal override X509Certificate
OnLocalCertificateSelection(X509CertificateCollection clientCertificates
, X509Certificate serverCertificate
, string targetHost
, X509CertificateCollection serverRequestedCertificates
)
274 throw new NotSupportedException();
277 internal override bool OnRemoteCertificateValidation(X509Certificate certificate
, int[] errors
)
279 if (this.ClientCertValidation
!= null)
281 return this.ClientCertValidation(certificate
, errors
);
284 return (errors
!= null && errors
.Length
== 0);
287 internal bool RaiseClientCertificateValidation(
288 X509Certificate certificate
,
289 int[] certificateErrors
)
291 return base.RaiseRemoteCertificateValidation(certificate
, certificateErrors
);
294 internal override AsymmetricAlgorithm
OnLocalPrivateKeySelection(X509Certificate certificate
, string targetHost
)
296 if (this.PrivateKeySelection
!= null)
298 return this.PrivateKeySelection(certificate
, targetHost
);
304 internal AsymmetricAlgorithm
RaisePrivateKeySelection(
305 X509Certificate certificate
,
308 return base.RaiseLocalPrivateKeySelection(certificate
, targetHost
);