copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / ServerContext.cs
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3
4 //
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:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
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.
23 //
24
25 using System;
26 using System.Security.Cryptography;
27 using System.Security.Cryptography.X509Certificates;
28
29 using Mono.Security.Protocol.Tls.Handshake;
30 using MonoX509 = Mono.Security.X509;
31
32 namespace Mono.Security.Protocol.Tls
33 {
34         internal class ServerContext : Context
35         {
36                 #region Fields
37
38                 private SslServerStream sslStream;
39                 private bool                    clientCertificateRequired;
40
41                 #endregion
42
43                 #region Properties
44
45                 public SslServerStream SslStream
46                 {
47                         get { return this.sslStream; }
48                 }
49
50                 public bool     ClientCertificateRequired
51                 {
52                         get { return this.clientCertificateRequired; }
53                 }
54
55                 #endregion
56
57                 #region Constructors
58
59                 public ServerContext(
60                         SslServerStream                 stream,
61                         SecurityProtocolType    securityProtocolType,
62                         X509Certificate                 serverCertificate,
63                         bool                                    clientCertificateRequired)
64                         : base(securityProtocolType)
65                 {
66                         this.sslStream                                  = stream;
67                         this.clientCertificateRequired  = clientCertificateRequired;
68
69                         // Convert the System.Security cert to a Mono Cert
70                         MonoX509.X509Certificate cert = new MonoX509.X509Certificate(serverCertificate.GetRawCertData());
71
72                         // Add server certificate to the certificate collection
73                         this.ServerSettings.Certificates = new MonoX509.X509CertificateCollection();
74                         this.ServerSettings.Certificates.Add(cert);
75
76                         this.ServerSettings.UpdateCertificateRSA();
77
78                         // Add requested certificate types
79                         this.ServerSettings.CertificateTypes = new ClientCertificateType[1];
80                         this.ServerSettings.CertificateTypes[0] = ClientCertificateType.RSA;
81
82                         // Add certificate authorities
83                         this.ServerSettings.DistinguisedNames = new string[0];
84                 }
85
86                 #endregion
87         }
88 }